generated from baileyherbert/svelte-webpack-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
300 lines (266 loc) · 8.1 KB
/
webpack.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Babel will compile modern JavaScript down to a format compatible with older browsers, but it will also increase your
* final bundle size and build speed. Edit the `browserslist` property in the package.json file to define which
* browsers Babel should target.
*
* Browserslist documentation: https://github.com/browserslist/browserslist#browserslist-
*/
const useBabel = true;
/**
* This option controls whether or not development builds should be compiled with Babel. Change this to `true` if you
* intend to test with older browsers during development, but it could significantly impact your build speed.
*/
const useBabelInDevelopment = false;
/**
* Define paths to any stylesheets you wish to include at the top of the CSS bundle. Any styles compiled from svelte
* will be added to the bundle after these. In other words, these are global styles for your svelte app. You can also
* specify paths to SCSS or SASS files, and they will be compiled automatically.
*/
const stylesheets = [
'./src/styles/index.scss'
];
/**
* Change this to `true` to generate source maps alongside your production bundle. This is useful for debugging, but
* will increase total bundle size and expose your source code.
*/
const sourceMapsInProduction = false;
/*********************************************************************************************************************/
/********** Webpack **********/
/*********************************************************************************************************************/
import Webpack from 'webpack';
import WebpackDev from 'webpack-dev-server';
import SveltePreprocess from 'svelte-preprocess';
import Autoprefixer from 'autoprefixer';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CSSMinimizerPlugin from 'css-minimizer-webpack-plugin';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import fs from 'fs';
import path from 'path';
const mode = process.env.NODE_ENV ?? 'development';
const isProduction = mode === 'production';
const isDevelopment = !isProduction;
const config: Configuration = {
mode: isProduction ? 'production' : 'development',
entry: {
bundle: [
...stylesheets,
'./src/main.ts'
]
},
resolve: {
alias: {
// Note: Later in this config file, we'll automatically add paths from `tsconfig.compilerOptions.paths`
svelte: path.resolve('node_modules', 'svelte')
},
extensions: ['.mjs', '.js', '.ts', '.svelte'],
mainFields: ['svelte', 'browser', 'module', 'main']
},
output: {
path: path.resolve(__dirname, 'public/build'),
publicPath: '/build/',
filename: '[name].js',
chunkFilename: '[name].[id].js'
},
module: {
rules: [
// Rule: Svelte
{
test: /\.svelte$/,
// excluding node_modules breaks @roxi/routify
//exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
compilerOptions: {
// Dev mode must be enabled for HMR to work!
dev: isDevelopment
},
emitCss: isProduction,
hotReload: isDevelopment,
hotOptions: {
// List of options and defaults: https://www.npmjs.com/package/svelte-loader-hot#usage
noPreserveState: false,
optimistic: true,
},
preprocess: SveltePreprocess({
scss: true,
sass: true,
postcss: {
plugins: [
Autoprefixer
]
}
})
}
}
},
// Required to prevent errors from Svelte on Webpack 5+, omit on Webpack 4
// See: https://github.com/sveltejs/svelte-loader#usage
{
test: /node_modules\/svelte\/.*\.mjs$/,
resolve: {
fullySpecified: false
}
},
// Rule: SASS
{
test: /\.(scss|sass)$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
Autoprefixer
]
}
}
},
'sass-loader'
]
},
// Rule: CSS
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader',
]
},
// Rule: TypeScript
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
devServer: {
hot: true,
stats: 'errors-only',
contentBase: 'public',
watchContentBase: true
},
target: isDevelopment ? 'web' : 'browserslist',
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css'
})
],
devtool: isProduction && !sourceMapsInProduction ? false : 'source-map',
stats: {
chunks: false,
chunkModules: false,
modules: false,
assets: true,
entrypoints: false
}
};
/**
* This interface combines configuration from `webpack` and `webpack-dev-server`. You can add or override properties
* in this interface to change the config object type used above.
*/
export interface Configuration extends Webpack.Configuration, WebpackDev.Configuration {
}
/*********************************************************************************************************************/
/********** Advanced **********/
/*********************************************************************************************************************/
// Configuration for production bundles
if (isProduction) {
// Clean the build directory for production builds
config.plugins?.push(new CleanWebpackPlugin());
// Minify CSS files
config.optimization?.minimizer?.push(
new CSSMinimizerPlugin({
parallel: true,
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: !sourceMapsInProduction },
},
],
},
})
);
// Minify and treeshake JS
if (config.optimization === undefined) {
config.optimization = {};
}
config.optimization.minimize = true;
}
// Parse as JSON5 to add support for comments in tsconfig.json parsing.
require('require-json5').replace();
// Load path aliases from the tsconfig.json file
const tsconfigPath = path.resolve(__dirname, 'tsconfig.json');
const tsconfig = fs.existsSync(tsconfigPath) ? require(tsconfigPath) : {};
if ('compilerOptions' in tsconfig && 'paths' in tsconfig.compilerOptions) {
const aliases = tsconfig.compilerOptions.paths;
for (const alias in aliases) {
const paths = aliases[alias].map((p: string) => path.resolve(__dirname, p));
// Our tsconfig uses glob path formats, whereas webpack just wants directories
// We'll need to transform the glob format into a format acceptable to webpack
const wpAlias = alias.replace(/(\\|\/)\*$/, '');
const wpPaths = paths.map((p: string) => p.replace(/(\\|\/)\*$/, ''));
if (config.resolve && config.resolve.alias) {
if (!(wpAlias in config.resolve.alias) && wpPaths.length) {
config.resolve.alias[wpAlias] = wpPaths.length > 1 ? wpPaths : wpPaths[0];
}
}
}
}
// Babel
if (useBabel && (isProduction || useBabelInDevelopment)) {
const loader = {
loader: 'babel-loader',
options: {
sourceType: 'unambiguous',
presets: [
[
// Docs: https://babeljs.io/docs/en/babel-preset-env
'@babel/preset-env',
{
debug: false,
corejs: { version: 3 },
useBuiltIns: 'usage'
}
]
],
plugins: ['@babel/plugin-transform-runtime']
}
};
config.module?.rules.unshift({
test: /\.(?:m?js|ts)$/,
include: [
path.resolve(__dirname, 'src'),
path.resolve('node_modules', 'svelte')
],
exclude: [
/node_modules[/\\](css-loader|core-js|webpack|regenerator-runtime)/
],
use: loader,
});
const svelte = config.module?.rules.find(rule => {
if (typeof rule !== 'object') return false;
else if (Array.isArray(rule.use))
return rule.use.includes((e: any) => typeof e.loader === 'string' && e.loader.startsWith('svelte-loader'));
else if (typeof rule.use === 'object')
return rule.use.loader?.startsWith('svelte-loader') ?? false;
return false;
}) as Webpack.RuleSetRule;
if (!svelte) {
console.error('ERR: Could not find svelte-loader for babel injection!');
process.exit(1);
}
if (!Array.isArray(svelte.use)) {
svelte.use = [svelte.use as any];
}
svelte.use.unshift(loader);
}
export default config;