Skip to content

Commit ea39eda

Browse files
committed
export a function from webpack.config.js
This lets us pass the `environment` variable when running webpack and differentiate development builds from production builds
1 parent 59704e4 commit ea39eda

File tree

2 files changed

+72
-61
lines changed

2 files changed

+72
-61
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"scripts": {
3-
"dev": "webpack --config webpack.config.js"
3+
"build": "webpack --env environment=production --config webpack.config.js",
4+
"dev": "webpack --env environment=development --config webpack.config.js"
45
},
56
"devDependencies": {
67
"@babel/cli": "^7.17.6",

webpack.config.js

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,84 @@ const path = require("path");
55
const PATH_SOURCE = path.join(__dirname, "./src");
66
const PATH_DIST = path.join(__dirname, "./dist");
77

8-
// Export a configuration object
9-
module.exports = {
10-
// Tell Webpack to do some optimizations for our environment (development
11-
// or production). Webpack will enable certain plugins and set
12-
// `process.env.NODE_ENV` according to the environment we specify.
13-
// https://webpack.js.org/configuration/mode
14-
mode: "development",
8+
// If we export a function, it will be passed two parameters, the first
9+
// of which is the webpack command line environment option `--env`.
10+
// `webpack --env.a = b` sets env.a = 'b'
11+
// `webpack --env.production` sets env.production = true
12+
// https://webpack.js.org/configuration/configuration-types/#exporting-a-function
13+
module.exports = (env) => {
14+
const environment = env.environment;
15+
const isProduction = environment === "production";
16+
const isDevelopment = environment === "development";
1517

16-
// The point or points to enter the application. This is where Webpack will
17-
// start. We generally have one entry point per HTML page. For single-page
18-
// applications, this means one entry point. For traditional multi-page apps,
19-
// we may have multiple entry points.
20-
// https://webpack.js.org/concepts#entry
21-
entry: [path.join(PATH_SOURCE, "./index.js")],
18+
return {
19+
// Tell Webpack to do some optimizations for our environment (development
20+
// or production). Webpack will enable certain plugins and set
21+
// `process.env.NODE_ENV` according to the environment we specify.
22+
// https://webpack.js.org/configuration/mode
23+
mode: environment,
2224

23-
// Tell Webpack where to emit the bundles it creates and how to name them.
24-
// https://webpack.js.org/concepts#output
25-
// https://webpack.js.org/configuration/output
26-
// https://webpack.js.org/configuration/output#outputFilename
27-
output: {
28-
path: PATH_DIST,
29-
filename: "js/[name].[contenthash].js",
25+
// The point or points to enter the application. This is where Webpack will
26+
// start. We generally have one entry point per HTML page. For single-page
27+
// applications, this means one entry point. For traditional multi-page apps,
28+
// we may have multiple entry points.
29+
// https://webpack.js.org/concepts#entry
30+
entry: [path.join(PATH_SOURCE, "./index.js")],
3031

31-
// The public URL of the output dir when referenced in a browser.
32-
// This value is prefixed to every URL created by the runtime or loaders.
33-
// It's empty by default, which creates URLs like 'bundle.js' and results
34-
// in 404s if they're requested from a nested URL like /articles/1
35-
// https://webpack.js.org/configuration/output/#outputpublicpath
36-
publicPath: "/",
37-
},
32+
// Tell Webpack where to emit the bundles it creates and how to name them.
33+
// https://webpack.js.org/concepts#output
34+
// https://webpack.js.org/configuration/output
35+
// https://webpack.js.org/configuration/output#outputFilename
36+
output: {
37+
path: PATH_DIST,
38+
filename: "js/[name].[contenthash].js",
3839

39-
// Determine how the different types of modules will be treated.
40-
// https://webpack.js.org/configuration/module
41-
// https://webpack.js.org/concepts#loaders
42-
module: {
43-
rules: [
44-
{
45-
test: /\.js$/, // Apply this rule to files ending in .js
46-
exclude: /node_modules/, // Don't apply to files residing in node_modules
47-
use: {
48-
// Use the following loader and options
49-
loader: "babel-loader",
50-
// We can pass options to both babel-loader and Babel. This option object
51-
// will replace babel.config.js
52-
options: {
53-
presets: [
54-
[
55-
"@babel/preset-env",
56-
{
57-
debug: true, // Output the targets/plugins used when compiling
40+
// The public URL of the output dir when referenced in a browser.
41+
// This value is prefixed to every URL created by the runtime or loaders.
42+
// It's empty by default, which creates URLs like 'bundle.js' and results
43+
// in 404s if they're requested from a nested URL like /articles/1
44+
// https://webpack.js.org/configuration/output/#outputpublicpath
45+
publicPath: "/",
46+
},
5847

59-
// Configure how @babel/preset-env handles polyfills from core-js.
60-
// https://babeljs.io/docs/en/babel-preset-env
61-
useBuiltIns: "usage",
48+
// Determine how the different types of modules will be treated.
49+
// https://webpack.js.org/configuration/module
50+
// https://webpack.js.org/concepts#loaders
51+
module: {
52+
rules: [
53+
{
54+
test: /\.js$/, // Apply this rule to files ending in .js
55+
exclude: /node_modules/, // Don't apply to files residing in node_modules
56+
use: {
57+
// Use the following loader and options
58+
loader: "babel-loader",
59+
// We can pass options to both babel-loader and Babel. This option object
60+
// will replace babel.config.js
61+
options: {
62+
presets: [
63+
[
64+
"@babel/preset-env",
65+
{
66+
debug: true, // Output the targets/plugins used when compiling
6267

63-
// Specify the core-js version. Must match the version in package.json
64-
corejs: 3,
68+
// Configure how @babel/preset-env handles polyfills from core-js.
69+
// https://babeljs.io/docs/en/babel-preset-env
70+
useBuiltIns: "usage",
6571

66-
// Specify which environments we support/target for our project.
67-
// (We have chosen to specify targets in .browserslistrc, so there
68-
// is no need to do it here.)
69-
// targets: "",
70-
},
72+
// Specify the core-js version. Must match the version in package.json
73+
corejs: 3,
74+
75+
// Specify which environments we support/target for our project.
76+
// (We have chosen to specify targets in .browserslistrc, so there
77+
// is no need to do it here.)
78+
// targets: "",
79+
},
80+
],
7181
],
72-
],
82+
},
7383
},
7484
},
75-
},
76-
],
77-
},
85+
],
86+
},
87+
};
7888
};

0 commit comments

Comments
 (0)