UNPKG

7.89 kBJavaScriptView Raw
1import { cwd } from 'process';
2import { join } from 'path';
3import webpack from 'webpack';
4import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
5import UglifyJSPlugin from 'uglifyjs-webpack-plugin';
6import { existsSync } from 'fs';
7
8const makePath = filePath => join(cwd(), filePath);
9
10// Opinionated life lol
11const src = makePath('src');
12const entry = makePath('src/assets/js/index.js');
13const styles = makePath('src/assets/cssModules/css.json');
14const output = makePath('dist/assets/js');
15
16const userConfig = makePath('scripts.config.js');
17
18const banner = `
19▄█ █▄ ▄████████ ▄████████ ███▄▄▄▄ ▄█ ███▄▄▄▄ ▄██████▄
20███ ███ ███ ███ ███ ███ ███▀▀▀██▄ ███ ███▀▀▀██▄ ███ ███
21███ ███ ███ ███ ███ ███ ███ ███ ███▌ ███ ███ ███ █▀
22███ ███ ███ ███ ▄███▄▄▄▄██▀ ███ ███ ███▌ ███ ███ ▄███
23███ ███ ▀███████████ ▀▀███▀▀▀▀▀ ███ ███ ███▌ ███ ███ ▀▀███ ████▄
24███ ███ ███ ███ ▀███████████ ███ ███ ███ ███ ███ ███ ███
25███ ▄█▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
26▀███▀███▀ ███ █▀ ███ ███ ▀█ █▀ █▀ ▀█ █▀ ████████▀
27 ███ ███
28
29Don't edit this file directly. Edit with Webpack.
30Then after you're done run npm run build
31
32Scaffolded with @pixel2html/scripts-frontend
33`;
34
35const common = [
36 // Do NOT import the BLOAT from moment.js
37 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
38 // People please don't edit the dist file pl0xx
39 new webpack.BannerPlugin(banner)
40];
41
42const debugPlugins = [
43 new BundleAnalyzerPlugin()
44];
45
46const developmentPlugins = [
47];
48
49const productionPlugins = [
50 new UglifyJSPlugin({sourceMap: true}),
51];
52
53const isProd = () => process.env.NODE_ENV === 'production';
54const isDebug = () => process.env.DEBUG_MODE === 'true';
55
56const checkDebug = () =>
57 isProd() || isDebug();
58
59const 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
71const 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 // Parse OUR js
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 // Parse VENDOR js
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
126const 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 // Check corresponding file for more info xoxoxo
140 module: modules,
141 plugins: getPlugins(),
142 devtool: shouldBeDebugMode
143 ? 'hidden-source-map'
144 : 'eval-source-map',
145 // Some libraries import Node modules but don't use them in the browser.
146 // Tell Webpack to provide empty mocks for them so importing them works.
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
169const setEnv = mode => {
170 process.env.NODE_ENV = mode;
171 process.env.BABEL_ENV = mode;
172};
173
174const setDebug = mode => {
175 process.env.DEBUG_MODE = mode;
176};
177
178const 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 // Let's do it the Next.js way
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
208const commonPlugins = [
209 // Add module names to factory functions so they appear in browser profiler.
210 new webpack.NamedModulesPlugin(),
211 // Allow everyone to use jQuery like it was global
212 new webpack.ProvidePlugin({
213 $: 'jquery',
214 jQuery: 'jquery',
215 'window.jQuery': 'jquery'
216 }),
217 // Do NOT import the BLOAT from moment.js
218 // thanks create-react-app
219 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
220 // Warning banner
221 new webpack.BannerPlugin(banner)
222];
223
224const productionPlugins$1 = [
225 new webpack.DefinePlugin({
226 'process.env': {
227 NODE_ENV: JSON.stringify('production')
228 }
229 }),
230 // Concatenate modules for smaller builds
231 new webpack.optimize.ModuleConcatenationPlugin(),
232 new UglifyJSPlugin({sourceMap: true}),
233 new webpack.optimize.ModuleConcatenationPlugin(),
234 new webpack.NoEmitOnErrorsPlugin()
235];
236
237const 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
248const makePath$1 = filePath => join(cwd(), filePath);
249
250const createShopifyConfig = config => {
251 config.entry.main = makePath$1('src/scripts');
252
253 config.output = {
254 // Only output js when debugging
255 // otherwise we want .js.liquid for external sourcemapa support
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 // Opt out of default to play nice with liquid
264 config.mode = 'none';
265
266 // Host jQuery outside of the bundle
267 // so other plugins can use it as well
268 config.externals = {
269 jquery: 'jQuery'
270 };
271
272 return config
273};
274
275export { getPlugins$1 as getShopifyPlugins, createShopifyConfig, compileJS as compiler };