-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathwebpack.config.js
161 lines (151 loc) · 5.14 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
**** WARNING: No ES6 modules here. Not transpiled! ****
*/
/* eslint-disable import/no-nodejs-modules */
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const process = require( 'process' );
const webpack = require( 'webpack' );
const DuplicatePackageCheckerPlugin = require( 'duplicate-package-checker-webpack-plugin' );
const FileConfig = require( './webpack/file-loader' );
const Minify = require( './webpack/minify' );
const SassConfig = require( './webpack/sass' );
const TranspileConfig = require( './webpack/transpile' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
/**
* Internal dependencies
*/
const { cssNameFromFilename, shouldTranspileDependency } = require( './webpack/util' );
// const { workerCount } = require( './webpack.common' ); // todo: shard...
/**
* Internal variables
*/
const isDevelopment = process.env.NODE_ENV !== 'production';
const cachePath = path.resolve( '.cache' );
/**
* Return a webpack config object
*
* Arguments to this function replicate webpack's so this config can be used on the command line,
* with individual options overridden by command line args.
*
* @see {@link https://webpack.js.org/configuration/configuration-types/#exporting-a-function}
* @see {@link https://webpack.js.org/api/cli/}
*
* @param {object} env environment options
* @param {object} argv options map
* @param {object} argv.entry Entry point(s)
* @param {string} argv.'output-chunk-filename' Output chunk filename
* @param {string} argv.'output-path' Output path
* @param {string} argv.'output-filename' Output filename pattern
* @param {string} argv.'output-library-target' Output library target
* @param {string} argv.'output-jsonp-function' Output jsonp function
* @returns {object} webpack config
*/
function getWebpackConfig(
env = {},
{
entry,
'output-chunk-filename': outputChunkFilename,
'output-path': outputPath = path.join( process.cwd(), 'dist' ),
'output-filename': outputFilename = '[name].js',
'output-library-target': outputLibraryTarget = 'window',
'output-jsonp-function': outputJsonpFunction = 'webpackJsonp',
}
) {
const workerCount = 1;
const cssFilename = cssNameFromFilename( outputFilename );
const cssChunkFilename = cssNameFromFilename( outputChunkFilename );
let babelConfig = path.join( process.cwd(), 'babel.config.js' );
let presets = [];
if ( ! fs.existsSync( babelConfig ) ) {
// Default to this package's Babel presets
presets = [
path.join( __dirname, 'babel', 'default' ),
env.WP && path.join( __dirname, 'babel', 'wordpress-element' ),
].filter( Boolean );
babelConfig = undefined;
}
let postCssConfigPath = process.cwd();
if ( ! fs.existsSync( path.join( postCssConfigPath, 'postcss.config.js' ) ) ) {
// Default to this package's PostCSS config
postCssConfigPath = __dirname;
}
const webpackConfig = {
bail: ! isDevelopment,
entry,
mode: isDevelopment ? 'development' : 'production',
devtool: process.env.SOURCEMAP || ( isDevelopment ? '#eval' : false ),
output: {
chunkFilename: outputChunkFilename,
path: outputPath,
filename: outputFilename,
libraryTarget: outputLibraryTarget,
jsonpFunction: outputJsonpFunction,
},
optimization: {
minimize: ! isDevelopment,
minimizer: Minify( {
cache: path.resolve( cachePath, 'terser' ),
parallel: workerCount,
sourceMap: Boolean( process.env.SOURCEMAP ),
extractComments: false,
terserOptions: {
ecma: 5,
safari10: true,
mangle: { reserved: [ '__', '_n', '_nx', '_x' ] },
},
} ),
},
module: {
strictExportPresence: true,
rules: [
TranspileConfig.loader( {
cacheDirectory: path.resolve( cachePath, 'babel' ),
configFile: babelConfig,
exclude: /node_modules\//,
presets,
workerCount,
} ),
TranspileConfig.loader( {
cacheDirectory: path.resolve( cachePath, 'babel' ),
include: shouldTranspileDependency,
presets: [ path.join( __dirname, 'babel', 'dependencies' ) ],
workerCount,
} ),
SassConfig.loader( {
postCssConfig: { path: postCssConfigPath },
cacheDirectory: path.resolve( cachePath, 'css-loader' ),
} ),
FileConfig.loader(),
],
},
resolve: {
extensions: [ '.json', '.js', '.jsx', '.ts', '.tsx' ],
mainFields: [ 'browser', 'calypso:src', 'module', 'main' ],
modules: [ 'node_modules' ],
},
node: false,
plugins: [
new webpack.DefinePlugin( {
'process.env.NODE_ENV': JSON.stringify( process.env.NODE_ENV ),
'process.env.FORCE_REDUCED_MOTION': JSON.stringify(
!! process.env.FORCE_REDUCED_MOTION || false
),
global: 'window',
} ),
new webpack.IgnorePlugin( /^\.\/locale$/, /moment$/ ),
...SassConfig.plugins( {
chunkFilename: cssChunkFilename,
filename: cssFilename,
minify: ! isDevelopment,
} ),
new DuplicatePackageCheckerPlugin(),
...( env.WP ? [ new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ) ] : [] ),
],
};
return webpackConfig;
}
module.exports = getWebpackConfig;