UNPKG

5.4 kBJavaScriptView Raw
1/**
2 * Common webpack configuration
3 */
4const MiniHtmlWebpackPlugin = require('mini-html-webpack-plugin');
5const ProgressBarPlugin = require('progress-bar-webpack-plugin');
6const MiniCssExtractPlugin = require('mini-css-extract-plugin');
7const webpack = require('webpack');
8const { fromCwd } = require('quickenv');
9
10const {
11 BUNDLE_NAME,
12 IS_POS,
13 IS_BROWSER,
14 IS_DEV,
15 IS_PROD,
16 NODE_ENV,
17 APP_ENV,
18 ADD_MAMBA_SIMULATOR,
19} = require('./helpers/consts.js');
20const {
21 isOfModuleType,
22 transpileIgnoreBaseCondition,
23} = require('./helpers/depTranspiling.js');
24const htmlTemplate = require('./helpers/htmlTemplate.js');
25const loaders = require('./helpers/loaders.js');
26const MambaFixesPlugin = require('./plugins/MambaFixesPlugin.js');
27
28/** App entry point */
29const entry = {
30 app: [
31 /** Mamba style resetter/normalizer */
32 '@mamba/styles/dist/pos.css',
33 /** Mamba simulator entry point */
34 ADD_MAMBA_SIMULATOR && './simulator.js',
35 /** App entry point */
36 './index.js',
37 ].filter(Boolean),
38};
39
40module.exports = {
41 mode: IS_PROD ? 'production' : 'development',
42 cache: true,
43 target: 'web',
44 node: false,
45 context: fromCwd('src'),
46 entry,
47 output: {
48 path: fromCwd('dist', BUNDLE_NAME),
49 publicPath: './',
50 filename: '[name].[hash:5].js',
51 chunkFilename: '[name].[hash:5].js',
52 },
53 resolve: {
54 /** Do not resolve symlinks */
55 symlinks: false,
56 enforceExtension: false,
57 mainFields: ['svelte', 'esnext', 'jsnext:main', 'module', 'main'],
58 extensions: ['.js', '.json', '.pcss', '.css', '.html', '.htmlx', '.svelte'],
59 /** Make webpack also resolve modules from './src' */
60 modules: [fromCwd('src'), 'node_modules'],
61 alias: {
62 '@mamba': fromCwd('node_modules', '@mamba'),
63 svelte: fromCwd('node_modules', 'svelte'),
64 page: fromCwd('node_modules', 'page'),
65 },
66 },
67 module: {
68 rules: [
69 /**
70 * ! App modules
71 * */
72 {
73 test: /\.(htmlx?|svelte)$/,
74 include: [fromCwd('src')],
75 use: [loaders.babelEsNext, loaders.svelte, loaders.eslint],
76 },
77 {
78 test: /\.js$/,
79 include: [fromCwd('src')],
80 use: [loaders.babelEsNext, loaders.eslint],
81 },
82 /**
83 * ! Dependency modules
84 * */
85 /** On dependencies svelte files, run svelte compiler and babel */
86 {
87 test: /\.(htmlx?|svelte)$/,
88 include: [/node_modules/],
89 /** When developing, parse linked packages svelte dependencies */
90 use: [loaders.babelEsNext, loaders.svelte],
91 },
92 /** Transpile .mjs dependencies as well */
93 {
94 test: /\.mjs$/,
95 include: [/node_modules/],
96 exclude: [/core-js/],
97 use: [loaders.babelEsNext],
98 },
99 /**
100 * * Run app COMMONJS dependencies through babel with module: 'commonjs'.
101 * @babel/preset-env inserts es6 import if we don't pass "module: 'commonjs'",
102 * resulting in mixed es6 and commonjs code.
103 * */
104 {
105 test: {
106 ...transpileIgnoreBaseCondition,
107 and: [isOfModuleType('cjs')],
108 },
109 use: [loaders.babelCJS],
110 },
111 /** Run app ES6 dependencies through babel with { modules: false } */
112 {
113 test: {
114 ...transpileIgnoreBaseCondition,
115 and: [isOfModuleType('es')],
116 },
117 use: [loaders.babelEsNext],
118 },
119 /**
120 * ! Generic files
121 */
122 {
123 test: /\.(css|pcss)$/,
124 /** When importing from a style file, let's
125 * use package.json's 'style' field before
126 * the actual 'main' one
127 * */
128 resolve: { mainFields: ['style', 'main'] },
129 use: [
130 loaders.extractCss,
131 loaders.css,
132 loaders.postcss,
133 loaders.resolveUrl,
134 ],
135 },
136 /** Handle font imports */
137 { test: /\.(eot|woff2?|otf|ttf)$/, use: [loaders.fonts] },
138 /** Handle image imports */
139 { test: /\.(gif|jpe?g|png|ico|svg)$/, use: [loaders.images] },
140 ],
141 },
142 plugins: [
143 /** Prepend the Function.prototype.bind() polyfill webpack's runtime code */
144 new MambaFixesPlugin(),
145 new ProgressBarPlugin(),
146 new MiniCssExtractPlugin({
147 filename: 'style.css',
148 chunkFilename: '[name].[hash:5].css',
149 }),
150 new MiniHtmlWebpackPlugin({
151 context: { title: 'Mamba Application' },
152 template: htmlTemplate,
153 }),
154 new webpack.DefinePlugin({
155 __NODE_ENV__: JSON.stringify(NODE_ENV),
156 __APP_ENV__: JSON.stringify(APP_ENV),
157 __PROD__: IS_PROD,
158 __TEST__: NODE_ENV === 'test',
159 __DEV__: IS_DEV,
160 __POS__: IS_POS,
161 __SIMULATOR__: ADD_MAMBA_SIMULATOR,
162 __BROWSER__: IS_BROWSER,
163 }),
164 ],
165 /** Minimal useful output log */
166 stats: {
167 modules: false,
168 chunks: false,
169 colors: true,
170 children: false,
171 },
172 optimization: {
173 namedChunks: true,
174 namedModules: true,
175 /** Create a separate chunk for webpack runtime */
176 runtimeChunk: { name: 'runtime' },
177 splitChunks: {
178 chunks: 'all',
179 minSize: 0,
180 minChunks: 1,
181 cacheGroups: {
182 vendors: false,
183 libs: {
184 test: /[\\/]node_modules[\\/]/,
185 priority: -10,
186 },
187 /** Chunk that contains used polyfills */
188 polyfills: {
189 test: /core-js/,
190 name: 'polyfills',
191 priority: 10,
192 },
193 },
194 },
195 },
196};