|
1 |
| -const path = require("path") |
2 |
| -const TerserPlugin = require("terser-webpack-plugin") |
3 |
| -const MiniCssExtractPlugin = require("mini-css-extract-plugin") |
4 |
| -let isProduction = process.env.NODE_ENV === "production" |
5 |
| -const { CleanWebpackPlugin } = require("clean-webpack-plugin") |
| 1 | +const path = require("path"); |
| 2 | +const MiniCssExtractPlugin = require("mini-css-extract-plugin"); |
| 3 | +const { EsbuildPlugin } = require("esbuild-loader"); |
| 4 | +const { FileUploader } = require("@cocreate/webpack"); |
6 | 5 |
|
7 |
| -module.exports = { |
8 |
| - entry: { |
9 |
| - "CoCreate-file": "./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", "file"], |
17 |
| - globalObject: "this", |
18 |
| - }, |
19 |
| - |
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 |
| - }, |
41 |
| - }, |
42 |
| - }, |
43 |
| - { |
44 |
| - test: /.css$/i, |
45 |
| - use: [ |
46 |
| - { loader: "style-loader", options: { injectType: "linkTag" } }, |
47 |
| - "file-loader", |
48 |
| - ], |
49 |
| - }, |
50 |
| - ], |
51 |
| - }, |
52 |
| - |
53 |
| - // add source map |
54 |
| - ...(isProduction ? {} : { devtool: "eval-source-map" }), |
55 |
| - |
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, |
78 |
| - |
79 |
| - cacheGroups: { |
80 |
| - defaultVendors: false, |
81 |
| - }, |
82 |
| - }, |
83 |
| - }, |
84 |
| -} |
| 6 | +module.exports = async (env, argv) => { |
| 7 | + const isProduction = argv && argv.mode === "production"; |
| 8 | + const config = { |
| 9 | + entry: { |
| 10 | + "CoCreate-file": "./src/index.js" |
| 11 | + }, |
| 12 | + output: { |
| 13 | + path: path.resolve(__dirname, "dist"), |
| 14 | + filename: isProduction ? "[name].min.js" : "[name].js", |
| 15 | + libraryExport: "default", |
| 16 | + library: ["CoCreate", "file"], |
| 17 | + clean: true |
| 18 | + }, |
| 19 | + plugins: [ |
| 20 | + new MiniCssExtractPlugin({ |
| 21 | + filename: isProduction ? "[name].min.css" : "[name].css" |
| 22 | + }), |
| 23 | + new FileUploader(env, argv) |
| 24 | + ], |
| 25 | + mode: isProduction ? "production" : "development", |
| 26 | + devtool: isProduction ? "source-map" : "eval-source-map", |
| 27 | + module: { |
| 28 | + rules: [ |
| 29 | + { |
| 30 | + test: /.js$/, |
| 31 | + exclude: /node_modules/, |
| 32 | + use: { |
| 33 | + loader: "esbuild-loader", |
| 34 | + options: { |
| 35 | + loader: "js", |
| 36 | + target: "es2017" |
| 37 | + } |
| 38 | + } |
| 39 | + }, |
| 40 | + { |
| 41 | + test: /.css$/i, |
| 42 | + use: [MiniCssExtractPlugin.loader, "css-loader"] |
| 43 | + } |
| 44 | + ] |
| 45 | + }, |
| 46 | + optimization: { |
| 47 | + minimize: isProduction, |
| 48 | + minimizer: [ |
| 49 | + new EsbuildPlugin({ |
| 50 | + target: "es2017", |
| 51 | + css: true |
| 52 | + }) |
| 53 | + ], |
| 54 | + splitChunks: { |
| 55 | + cacheGroups: { |
| 56 | + defaultVendors: false |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + performance: { |
| 61 | + hints: isProduction ? "warning" : false |
| 62 | + } |
| 63 | + }; |
| 64 | + return config; |
| 65 | +}; |
0 commit comments