-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply more tweaks to the way webpack config package is structured
- Loading branch information
Showing
8 changed files
with
231 additions
and
154 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' ); | ||
const LiveReloadPlugin = require( 'webpack-livereload-plugin' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { camelCaseDash } = require( './utils' ); | ||
|
||
/** | ||
* Converts @wordpress require into window reference. | ||
* | ||
* Note this isn't the same as camel case because of the | ||
* way that numbers don't trigger the capitalized next letter. | ||
* | ||
* @example | ||
* wordpressRequire( '@wordpress/api-fetch' ) = 'wp.apiFetch' | ||
* wordpressRequire( '@wordpress/i18n' ) = 'wp.i18n' | ||
* | ||
* @param {string} request import name | ||
* @return {string} global variable reference for import | ||
*/ | ||
const wordpressRequire = ( request ) => { | ||
// @wordpress/components -> [ @wordpress, components ] | ||
const [ , name ] = request.split( '/' ); | ||
|
||
// components -> wp.components | ||
return `wp.${ camelCaseDash( name ) }`; | ||
}; | ||
|
||
const wordpressExternals = ( context, request, callback ) => | ||
/^@wordpress\//.test( request ) ? | ||
callback( null, `root ${ wordpressRequire( request ) }` ) : | ||
callback(); | ||
|
||
const externals = [ | ||
{ | ||
react: 'React', | ||
'react-dom': 'ReactDOM', | ||
tinymce: 'tinymce', | ||
moment: 'moment', | ||
jquery: 'jQuery', | ||
lodash: 'lodash', | ||
'lodash-es': 'lodash', | ||
}, | ||
wordpressExternals, | ||
]; | ||
|
||
const isProduction = process.env.NODE_ENV === 'production'; | ||
const mode = isProduction ? 'production' : 'development'; | ||
|
||
const config = { | ||
mode, | ||
externals, | ||
resolve: { | ||
modules: [ | ||
process.cwd(), | ||
'node_modules', | ||
], | ||
alias: { | ||
'lodash-es': 'lodash', | ||
}, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
use: [ 'source-map-loader' ], | ||
enforce: 'pre', | ||
}, | ||
{ | ||
test: /\.js$/, | ||
exclude: [ | ||
/block-serialization-spec-parser/, | ||
/is-shallow-equal/, | ||
/node_modules/, | ||
], | ||
use: 'babel-loader', | ||
}, | ||
], | ||
}, | ||
plugins: [ | ||
// GUTENBERG_BUNDLE_ANALYZER global variable enables utility that represents bundle content | ||
// as convenient interactive zoomable treemap. | ||
process.env.GUTENBERG_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(), | ||
// GUTENBERG_LIVE_RELOAD_PORT global variable changes port on which live reload works | ||
// when running watch mode. | ||
! isProduction && new LiveReloadPlugin( { port: process.env.GUTENBERG_LIVE_RELOAD_PORT || 35729 } ), | ||
].filter( Boolean ), | ||
stats: { | ||
children: false, | ||
}, | ||
}; | ||
|
||
if ( ! isProduction ) { | ||
// GUTENBERG_DEVTOOL global variable controls how source maps are generated. | ||
// See: https://webpack.js.org/configuration/devtool/#devtool. | ||
config.devtool = process.env.GUTENBERG_DEVTOOL || 'source-map'; | ||
} | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,10 @@ | ||
/** | ||
* External dependencies | ||
* Internal dependencies | ||
*/ | ||
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' ); | ||
const LiveReloadPlugin = require( 'webpack-livereload-plugin' ); | ||
const { camelCaseDash } = require( './utils' ); | ||
const config = require( './config' ); | ||
|
||
/** | ||
* Converts @wordpress require into window reference | ||
* | ||
* Note this isn't the same as camel case because of the | ||
* way that numbers don't trigger the capitalized next letter | ||
* | ||
* @example | ||
* wordpressRequire( '@wordpress/api-fetch' ) = 'wp.apiFetch' | ||
* wordpressRequire( '@wordpress/i18n' ) = 'wp.i18n' | ||
* | ||
* @param {string} request import name | ||
* @return {string} global variable reference for import | ||
*/ | ||
const wordpressRequire = ( request ) => { | ||
// @wordpress/components -> [ @wordpress, components ] | ||
const [ , name ] = request.split( '/' ); | ||
|
||
// components -> wp.components | ||
return `wp.${ name.replace( /-([a-z])/g, ( match, letter ) => letter.toUpperCase() ) }`; | ||
}; | ||
|
||
const wordpressExternals = ( context, request, callback ) => | ||
/^@wordpress\//.test( request ) ? | ||
callback( null, `root ${ wordpressRequire( request ) }` ) : | ||
callback(); | ||
|
||
const externals = [ | ||
{ | ||
react: 'React', | ||
'react-dom': 'ReactDOM', | ||
tinymce: 'tinymce', | ||
moment: 'moment', | ||
jquery: 'jQuery', | ||
lodash: 'lodash', | ||
'lodash-es': 'lodash', | ||
}, | ||
wordpressExternals, | ||
]; | ||
|
||
const isProduction = process.env.NODE_ENV === 'production'; | ||
const mode = isProduction ? 'production' : 'development'; | ||
|
||
const config = { | ||
mode, | ||
output: { | ||
filename: './build/[basename]/index.js', | ||
path: process.cwd(), | ||
library: [ 'wp', '[name]' ], | ||
libraryTarget: 'this', | ||
}, | ||
externals, | ||
resolve: { | ||
modules: [ | ||
process.cwd(), | ||
'node_modules', | ||
], | ||
alias: { | ||
'lodash-es': 'lodash', | ||
}, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
use: [ 'source-map-loader' ], | ||
enforce: 'pre', | ||
}, | ||
{ | ||
test: /\.js$/, | ||
exclude: [ | ||
/block-serialization-spec-parser/, | ||
/is-shallow-equal/, | ||
/node_modules/, | ||
], | ||
use: 'babel-loader', | ||
}, | ||
], | ||
}, | ||
plugins: [ | ||
// GUTENBERG_BUNDLE_ANALYZER global variable enables utility that represents bundle content | ||
// as convenient interactive zoomable treemap. | ||
process.env.GUTENBERG_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(), | ||
// GUTENBERG_LIVE_RELOAD_PORT global variable changes port on which live reload works | ||
// when running watch mode. | ||
! isProduction && new LiveReloadPlugin( { port: process.env.GUTENBERG_LIVE_RELOAD_PORT || 35729 } ), | ||
].filter( Boolean ), | ||
stats: { | ||
children: false, | ||
}, | ||
module.exports = { | ||
camelCaseDash, | ||
config, | ||
}; | ||
|
||
if ( ! isProduction ) { | ||
// GUTENBERG_DEVTOOL global variable controls how source maps are generated. | ||
// See: https://webpack.js.org/configuration/devtool/#devtool. | ||
config.devtool = process.env.GUTENBERG_DEVTOOL || 'source-map'; | ||
} | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.