1 | import { cwd } from 'process';
|
2 | import { join } from 'path';
|
3 | import webpack from 'webpack';
|
4 | import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
|
5 | import UglifyJSPlugin from 'uglifyjs-webpack-plugin';
|
6 | import { existsSync } from 'fs';
|
7 |
|
8 | const makePath = filePath => join(cwd(), filePath);
|
9 |
|
10 |
|
11 | const src = makePath('src');
|
12 | const entry = makePath('src/assets/js/index.js');
|
13 | const styles = makePath('src/assets/cssModules/css.json');
|
14 | const output = makePath('dist/assets/js');
|
15 |
|
16 | const userConfig = makePath('scripts.config.js');
|
17 |
|
18 | const banner = `
|
19 | ▄█ █▄ ▄████████ ▄████████ ███▄▄▄▄ ▄█ ███▄▄▄▄ ▄██████▄
|
20 | ███ ███ ███ ███ ███ ███ ███▀▀▀██▄ ███ ███▀▀▀██▄ ███ ███
|
21 | ███ ███ ███ ███ ███ ███ ███ ███ ███▌ ███ ███ ███ █▀
|
22 | ███ ███ ███ ███ ▄███▄▄▄▄██▀ ███ ███ ███▌ ███ ███ ▄███
|
23 | ███ ███ ▀███████████ ▀▀███▀▀▀▀▀ ███ ███ ███▌ ███ ███ ▀▀███ ████▄
|
24 | ███ ███ ███ ███ ▀███████████ ███ ███ ███ ███ ███ ███ ███
|
25 | ███ ▄█▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
|
26 | ▀███▀███▀ ███ █▀ ███ ███ ▀█ █▀ █▀ ▀█ █▀ ████████▀
|
27 | ███ ███
|
28 |
|
29 | Don't edit this file directly. Edit with Webpack.
|
30 | Then after you're done run npm run build
|
31 |
|
32 | Scaffolded with @pixel2html/scripts-frontend
|
33 | `;
|
34 |
|
35 | const common = [
|
36 |
|
37 | new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
38 |
|
39 | new webpack.BannerPlugin(banner)
|
40 | ];
|
41 |
|
42 | const debugPlugins = [
|
43 | new BundleAnalyzerPlugin()
|
44 | ];
|
45 |
|
46 | const developmentPlugins = [
|
47 | ];
|
48 |
|
49 | const productionPlugins = [
|
50 | new UglifyJSPlugin({sourceMap: true}),
|
51 | ];
|
52 |
|
53 | const isProd = () => process.env.NODE_ENV === 'production';
|
54 | const isDebug = () => process.env.DEBUG_MODE === 'true';
|
55 |
|
56 | const checkDebug = () =>
|
57 | isProd() || isDebug();
|
58 |
|
59 | const getPlugins = () => {
|
60 | const shouldBeDebugMode = checkDebug();
|
61 |
|
62 | let plugins = [ ...common ];
|
63 |
|
64 | if (!shouldBeDebugMode) plugins = [...plugins, ...developmentPlugins];
|
65 | if (shouldBeDebugMode) plugins = [...plugins, ...productionPlugins];
|
66 | if (isDebug()) plugins = [...plugins, ...debugPlugins];
|
67 |
|
68 | return plugins
|
69 | };
|
70 |
|
71 | const modules = {
|
72 | rules: [
|
73 | {
|
74 | test: /\.(js|jsx|mjs)$/,
|
75 | enforce: 'pre',
|
76 | use: [
|
77 | {
|
78 | options: {
|
79 | eslintPath: require.resolve('eslint'),
|
80 | baseConfig: {
|
81 | extends: [require.resolve('@pixel2html/eslint-config')]
|
82 | },
|
83 | ignore: false,
|
84 | useEslintrc: false
|
85 | },
|
86 | loader: require.resolve('eslint-loader')
|
87 | }
|
88 | ],
|
89 | include: src
|
90 | },
|
91 | {
|
92 | oneOf: [
|
93 |
|
94 | {
|
95 | test: /\.(js|jsx|mjs)$/,
|
96 | include: src,
|
97 | exclude: [/[/\\\\]node_modules[/\\\\]/],
|
98 | use: {
|
99 | loader: 'babel-loader',
|
100 | options: {
|
101 | babelrc: false,
|
102 | presets: [require.resolve('@pixel2html/babel-preset')],
|
103 | compact: true,
|
104 | highlightCode: true,
|
105 | }
|
106 | }
|
107 | },
|
108 |
|
109 | {
|
110 | test: /\.js$/,
|
111 | use: {
|
112 | loader: 'babel-loader',
|
113 | options: {
|
114 | babelrc: false,
|
115 | compact: false,
|
116 | presets: [require.resolve('@pixel2html/babel-preset/dependencies')],
|
117 | cacheDirectory: true,
|
118 | highlightCode: true,
|
119 | }
|
120 | }
|
121 | }
|
122 | ]
|
123 | }
|
124 | ]};
|
125 |
|
126 | const getConfig = () => {
|
127 | const shouldBeDebugMode = checkDebug();
|
128 |
|
129 | const config = {
|
130 | entry: {
|
131 | main: entry
|
132 | },
|
133 | output: {
|
134 | filename: '[name].js',
|
135 | chunkFilename: '[name].chunk.js',
|
136 | path: output,
|
137 | publicPath: '/'
|
138 | },
|
139 |
|
140 | module: modules,
|
141 | plugins: getPlugins(),
|
142 | devtool: shouldBeDebugMode
|
143 | ? 'hidden-source-map'
|
144 | : 'eval-source-map',
|
145 |
|
146 |
|
147 | node: {
|
148 | dgram: 'empty',
|
149 | fs: 'empty',
|
150 | net: 'empty',
|
151 | tls: 'empty',
|
152 | child_process: 'empty'
|
153 | },
|
154 | resolve: {
|
155 | alias: {
|
156 | styles: styles
|
157 | }
|
158 | },
|
159 | bail: shouldBeDebugMode,
|
160 | mode: shouldBeDebugMode ? 'production' : 'development',
|
161 | performance: {
|
162 | hints: shouldBeDebugMode ? 'warning' : false
|
163 | },
|
164 | };
|
165 |
|
166 | return config
|
167 | };
|
168 |
|
169 | const setEnv = mode => {
|
170 | process.env.NODE_ENV = mode;
|
171 | process.env.BABEL_ENV = mode;
|
172 | };
|
173 |
|
174 | const setDebug = mode => {
|
175 | process.env.DEBUG_MODE = mode;
|
176 | };
|
177 |
|
178 | const compileJS = mode => {
|
179 | const isDebug = mode === 'debug';
|
180 | setDebug(isDebug);
|
181 |
|
182 | if (mode === 'production' || isDebug) {
|
183 | setEnv('production');
|
184 | } else {
|
185 | setEnv('development');
|
186 | }
|
187 |
|
188 | const defaultConfig = getConfig();
|
189 |
|
190 | const customConfig = existsSync(userConfig);
|
191 |
|
192 | const config = customConfig
|
193 |
|
194 | ? require(userConfig)(defaultConfig, webpack)
|
195 | : defaultConfig;
|
196 | return new Promise(resolve =>
|
197 | webpack(config, (err, stats) => {
|
198 | if (err) console.log('Webpack', err);
|
199 | console.log(stats.toString({
|
200 | chunks: false,
|
201 | colors: true
|
202 | }));
|
203 | resolve();
|
204 | })
|
205 | )
|
206 | };
|
207 |
|
208 | const commonPlugins = [
|
209 |
|
210 | new webpack.NamedModulesPlugin(),
|
211 |
|
212 | new webpack.ProvidePlugin({
|
213 | $: 'jquery',
|
214 | jQuery: 'jquery',
|
215 | 'window.jQuery': 'jquery'
|
216 | }),
|
217 |
|
218 |
|
219 | new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
220 |
|
221 | new webpack.BannerPlugin(banner)
|
222 | ];
|
223 |
|
224 | const productionPlugins$1 = [
|
225 | new webpack.DefinePlugin({
|
226 | 'process.env': {
|
227 | NODE_ENV: JSON.stringify('production')
|
228 | }
|
229 | }),
|
230 |
|
231 | new webpack.optimize.ModuleConcatenationPlugin(),
|
232 | new UglifyJSPlugin({sourceMap: true}),
|
233 | new webpack.optimize.ModuleConcatenationPlugin(),
|
234 | new webpack.NoEmitOnErrorsPlugin()
|
235 | ];
|
236 |
|
237 | const getPlugins$1 = () => {
|
238 | const shouldBeDebugMode = checkDebug();
|
239 |
|
240 | let plugins = [ ...commonPlugins ];
|
241 |
|
242 | if (shouldBeDebugMode) plugins = [...plugins, ...productionPlugins$1];
|
243 | if (isDebug()) plugins = [...plugins, ...debugPlugins];
|
244 |
|
245 | return plugins
|
246 | };
|
247 |
|
248 | const makePath$1 = filePath => join(cwd(), filePath);
|
249 |
|
250 | const createShopifyConfig = config => {
|
251 | config.entry.main = makePath$1('src/scripts');
|
252 |
|
253 | config.output = {
|
254 |
|
255 |
|
256 | filename: '[name].js',
|
257 | chunkFilename: '[name].chunk.[chunkhash:5].js',
|
258 | path: makePath$1('.deploy/assets')
|
259 | };
|
260 |
|
261 | config.plugins = getPlugins$1();
|
262 |
|
263 |
|
264 | config.mode = 'none';
|
265 |
|
266 |
|
267 |
|
268 | config.externals = {
|
269 | jquery: 'jQuery'
|
270 | };
|
271 |
|
272 | return config
|
273 | };
|
274 |
|
275 | export { getPlugins$1 as getShopifyPlugins, createShopifyConfig, compileJS as compiler };
|