Skip to content

Commit c558a3f

Browse files
committed
fix: webpack.config and package.json make use of mode=production instead of process.env
1 parent 28839a7 commit c558a3f

File tree

2 files changed

+80
-74
lines changed

2 files changed

+80
-74
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"scripts": {
2727
"start": "npx webpack --config webpack.config.js",
28-
"build": "NODE_ENV=production npx webpack --config webpack.config.js",
28+
"build": "npx webpack --mode=production --config webpack.config.js",
2929
"dev": "npx webpack --config webpack.config.js --watch",
3030
"postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\""
3131
},

webpack.config.js

+79-73
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,90 @@
11
const path = require("path")
22
const TerserPlugin = require("terser-webpack-plugin")
33
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
4-
let isProduction = process.env.NODE_ENV === "production"
54
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
65

7-
module.exports = {
8-
entry: {
9-
"CoCreate-conditional-logic": "./src/index.js",
10-
},
11-
output: {
12-
path: path.resolve(__dirname, "dist"),
13-
filename: isProduction ? "[name].min.js" : "[name].js",
14-
libraryTarget: "umd",
15-
libraryExport: "default",
16-
library: ["CoCreate", "conditionalLogic"],
17-
globalObject: "this",
18-
},
6+
module.exports = (env, argv) => {
7+
let isProduction = false
8+
if (argv.mode === 'production')
9+
isProduction = true
1910

20-
plugins: [
21-
new CleanWebpackPlugin(),
22-
new MiniCssExtractPlugin({
23-
filename: "[name].css",
24-
}),
25-
],
26-
// Default mode for Webpack is production.
27-
// Depending on mode Webpack will apply different things
28-
// on final bundle. For now we don't need production's JavaScript
29-
// minifying and other thing so let's set mode to development
30-
mode: isProduction ? "production" : "development",
31-
module: {
32-
rules: [
33-
{
34-
test: /.js$/,
35-
exclude: /(node_modules)/,
36-
use: {
37-
loader: "babel-loader",
38-
options: {
39-
plugins: ["@babel/plugin-transform-modules-commonjs"],
40-
},
11+
const config = {
12+
entry: {
13+
"CoCreate-conditional-logic": "./src/index.js",
4114
},
42-
},
43-
{
44-
test: /.css$/i,
45-
use: [
46-
{ loader: "style-loader", options: { injectType: "linkTag" } },
47-
"file-loader",
15+
output: {
16+
path: path.resolve(__dirname, "dist"),
17+
filename: isProduction ? "[name].min.js" : "[name].js",
18+
libraryTarget: "umd",
19+
libraryExport: "default",
20+
library: ["CoCreate", "conditionalLogic"],
21+
globalObject: "this",
22+
},
23+
24+
plugins: [
25+
new CleanWebpackPlugin(),
26+
new MiniCssExtractPlugin({
27+
filename: "[name].css",
28+
}),
4829
],
49-
},
50-
],
51-
},
30+
// Default mode for Webpack is production.
31+
// Depending on mode Webpack will apply different things
32+
// on final bundle. For now we don't need production's JavaScript
33+
// minifying and other thing so let's set mode to development
34+
mode: isProduction ? "production" : "development",
35+
module: {
36+
rules: [
37+
{
38+
test: /.js$/,
39+
exclude: /(node_modules)/,
40+
use: {
41+
loader: "babel-loader",
42+
options: {
43+
plugins: ["@babel/plugin-transform-modules-commonjs"],
44+
},
45+
},
46+
},
47+
{
48+
test: /.css$/i,
49+
use: [
50+
{ loader: "style-loader", options: { injectType: "linkTag" } },
51+
"file-loader",
52+
],
53+
},
54+
],
55+
},
5256

53-
// add source map
54-
...(isProduction ? {} : { devtool: "eval-source-map" }),
57+
// add source map
58+
...(isProduction ? {} : { devtool: "eval-source-map" }),
5559

56-
optimization: {
57-
minimize: true,
58-
minimizer: [
59-
new TerserPlugin({
60-
extractComments: true,
61-
// cache: true,
62-
parallel: true,
63-
// sourceMap: true, // Must be set to true if using source-maps in production
64-
terserOptions: {
65-
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
66-
// extractComments: 'all',
67-
compress: {
68-
drop_console: true,
69-
},
70-
},
71-
}),
72-
],
73-
splitChunks: {
74-
chunks: "all",
75-
minSize: 200,
76-
// maxSize: 99999,
77-
//minChunks: 1,
60+
optimization: {
61+
minimize: true,
62+
minimizer: [
63+
new TerserPlugin({
64+
extractComments: true,
65+
// cache: true,
66+
parallel: true,
67+
// sourceMap: true, // Must be set to true if using source-maps in production
68+
terserOptions: {
69+
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
70+
// extractComments: 'all',
71+
compress: {
72+
drop_console: true,
73+
},
74+
},
75+
}),
76+
],
77+
splitChunks: {
78+
chunks: "all",
79+
minSize: 200,
80+
// maxSize: 99999,
81+
//minChunks: 1,
7882

79-
cacheGroups: {
80-
defaultVendors: false,
81-
},
82-
},
83-
},
84-
}
83+
cacheGroups: {
84+
defaultVendors: false,
85+
},
86+
},
87+
},
88+
}
89+
return config
90+
}

0 commit comments

Comments
 (0)