|
1 | | -const path = require('path'); |
2 | | -const webpack = require('webpack'); |
3 | | -const WebpackDashDynamicImport = require('@plotly/webpack-dash-dynamic-import'); |
4 | | -const packagejson = require('./package.json'); |
| 1 | +const path = require("path"); |
| 2 | +const webpack = require("webpack"); |
| 3 | +const WebpackDashDynamicImport = require("@plotly/webpack-dash-dynamic-import"); |
| 4 | +const packagejson = require("./package.json"); |
5 | 5 |
|
6 | | -const dashLibraryName = packagejson.name.replace(/-/g, '_'); |
| 6 | +const dashLibraryName = packagejson.name.replace(/-/g, "_"); |
7 | 7 |
|
8 | 8 | module.exports = (env, argv) => { |
| 9 | + let mode; |
9 | 10 |
|
10 | | - let mode; |
| 11 | + const overrides = module.exports || {}; |
11 | 12 |
|
12 | | - const overrides = module.exports || {}; |
| 13 | + // if user specified mode flag take that value |
| 14 | + if (argv && argv.mode) { |
| 15 | + mode = argv.mode; |
| 16 | + } |
13 | 17 |
|
14 | | - // if user specified mode flag take that value |
15 | | - if (argv && argv.mode) { |
16 | | - mode = argv.mode; |
17 | | - } |
| 18 | + // else if configuration object is already set (module.exports) use that value |
| 19 | + else if (overrides.mode) { |
| 20 | + mode = overrides.mode; |
| 21 | + } |
18 | 22 |
|
19 | | - // else if configuration object is already set (module.exports) use that value |
20 | | - else if (overrides.mode) { |
21 | | - mode = overrides.mode; |
22 | | - } |
| 23 | + // else take webpack default (production) |
| 24 | + else { |
| 25 | + mode = "production"; |
| 26 | + } |
23 | 27 |
|
24 | | - // else take webpack default (production) |
25 | | - else { |
26 | | - mode = 'production'; |
27 | | - } |
| 28 | + let filename = (overrides.output || {}).filename; |
| 29 | + if (!filename) { |
| 30 | + const modeSuffix = mode === "development" ? "dev" : "min"; |
| 31 | + filename = `${dashLibraryName}.${modeSuffix}.js`; |
| 32 | + } |
28 | 33 |
|
29 | | - let filename = (overrides.output || {}).filename; |
30 | | - if(!filename) { |
31 | | - const modeSuffix = mode === 'development' ? 'dev' : 'min'; |
32 | | - filename = `${dashLibraryName}.${modeSuffix}.js`; |
33 | | - } |
| 34 | + const entry = overrides.entry || {main: "./src/lib/index.js"}; |
34 | 35 |
|
35 | | - const entry = overrides.entry || {main: './src/lib/index.js'}; |
| 36 | + const devtool = overrides.devtool || "source-map"; |
36 | 37 |
|
37 | | - const devtool = overrides.devtool || 'source-map'; |
| 38 | + const externals = |
| 39 | + "externals" in overrides |
| 40 | + ? overrides.externals |
| 41 | + : { |
| 42 | + react: "React", |
| 43 | + "react-dom": "ReactDOM", |
| 44 | + "plotly.js": "Plotly", |
| 45 | + "prop-types": "PropTypes", |
| 46 | + }; |
38 | 47 |
|
39 | | - const externals = ('externals' in overrides) ? overrides.externals : ({ |
40 | | - react: 'React', |
41 | | - 'react-dom': 'ReactDOM', |
42 | | - 'plotly.js': 'Plotly', |
43 | | - 'prop-types': 'PropTypes', |
44 | | - }); |
45 | | - |
46 | | - return { |
47 | | - mode, |
48 | | - entry, |
49 | | - output: { |
50 | | - path: path.resolve(__dirname, dashLibraryName), |
51 | | - chunkFilename: '[name].js', |
52 | | - filename, |
53 | | - library: dashLibraryName, |
54 | | - libraryTarget: 'window', |
55 | | - }, |
56 | | - devtool, |
57 | | - devServer: { |
58 | | - static: { |
59 | | - directory: path.join(__dirname, '/') |
60 | | - } |
| 48 | + return { |
| 49 | + mode, |
| 50 | + entry, |
| 51 | + output: { |
| 52 | + path: path.resolve(__dirname, dashLibraryName), |
| 53 | + chunkFilename: "[name].js", |
| 54 | + filename, |
| 55 | + library: dashLibraryName, |
| 56 | + libraryTarget: "window", |
| 57 | + }, |
| 58 | + devtool, |
| 59 | + devServer: { |
| 60 | + static: { |
| 61 | + directory: path.join(__dirname, "/"), |
| 62 | + }, |
| 63 | + }, |
| 64 | + externals, |
| 65 | + module: { |
| 66 | + rules: [ |
| 67 | + { |
| 68 | + test: /\.jsx?$/, |
| 69 | + exclude: /node_modules/, |
| 70 | + use: { |
| 71 | + loader: "babel-loader", |
| 72 | + }, |
61 | 73 | }, |
62 | | - externals, |
63 | | - module: { |
64 | | - rules: [ |
65 | | - { |
66 | | - test: /\.jsx?$/, |
67 | | - exclude: /node_modules/, |
68 | | - use: { |
69 | | - loader: 'babel-loader', |
70 | | - }, |
| 74 | + { |
| 75 | + test: /\.css$/, |
| 76 | + use: [ |
| 77 | + { |
| 78 | + loader: "style-loader", |
| 79 | + }, |
| 80 | + { |
| 81 | + loader: "css-loader", |
| 82 | + options: { |
| 83 | + modules: { |
| 84 | + localIdentHashSalt: "dash-json-grid", |
| 85 | + localIdentName: "djg-[local]__[hash:base64:8]", |
71 | 86 | }, |
72 | | - { |
73 | | - test: /\.css$/, |
74 | | - use: [ |
75 | | - { |
76 | | - loader: 'style-loader', |
77 | | - }, |
78 | | - { |
79 | | - loader: 'css-loader', |
80 | | - }, |
81 | | - ], |
| 87 | + }, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + { |
| 92 | + test: /\.s[ac]ss$/i, |
| 93 | + use: [ |
| 94 | + { |
| 95 | + loader: "style-loader", |
| 96 | + }, |
| 97 | + { |
| 98 | + loader: "css-loader", |
| 99 | + options: { |
| 100 | + modules: { |
| 101 | + localIdentHashSalt: "dash-json-grid", |
| 102 | + localIdentName: "djg-[local]__[hash:base64:8]", |
82 | 103 | }, |
83 | | - ], |
| 104 | + }, |
| 105 | + }, |
| 106 | + // Compiles Sass to CSS |
| 107 | + { |
| 108 | + loader: "sass-loader", |
| 109 | + }, |
| 110 | + ], |
84 | 111 | }, |
85 | | - optimization: { |
86 | | - splitChunks: { |
87 | | - name: '[name].js', |
88 | | - cacheGroups: { |
89 | | - async: { |
90 | | - chunks: 'async', |
91 | | - minSize: 0, |
92 | | - name(module, chunks, cacheGroupKey) { |
93 | | - return `${cacheGroupKey}-${chunks[0].name}`; |
94 | | - } |
95 | | - }, |
96 | | - shared: { |
97 | | - chunks: 'all', |
98 | | - minSize: 0, |
99 | | - minChunks: 2, |
100 | | - name: 'dash_json_grid-shared' |
101 | | - } |
102 | | - } |
103 | | - } |
| 112 | + ], |
| 113 | + }, |
| 114 | + optimization: { |
| 115 | + splitChunks: { |
| 116 | + name: "[name].js", |
| 117 | + cacheGroups: { |
| 118 | + async: { |
| 119 | + chunks: "async", |
| 120 | + minSize: 0, |
| 121 | + name(module, chunks, cacheGroupKey) { |
| 122 | + return `${cacheGroupKey}-${chunks[0].name}`; |
| 123 | + }, |
| 124 | + }, |
| 125 | + shared: { |
| 126 | + chunks: "all", |
| 127 | + minSize: 0, |
| 128 | + minChunks: 2, |
| 129 | + name: "dash_json_grid-shared", |
| 130 | + }, |
104 | 131 | }, |
105 | | - plugins: [ |
106 | | - new WebpackDashDynamicImport(), |
107 | | - new webpack.SourceMapDevToolPlugin({ |
108 | | - filename: '[file].map', |
109 | | - exclude: ['async-plotlyjs'] |
110 | | - }) |
111 | | - ] |
112 | | - } |
| 132 | + }, |
| 133 | + }, |
| 134 | + plugins: [ |
| 135 | + new WebpackDashDynamicImport(), |
| 136 | + new webpack.SourceMapDevToolPlugin({ |
| 137 | + filename: "[file].map", |
| 138 | + exclude: ["async-plotlyjs"], |
| 139 | + }), |
| 140 | + ], |
| 141 | + }; |
113 | 142 | }; |
0 commit comments