forked from stylescape/unit.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
382 lines (331 loc) · 14.5 KB
/
webpack.common.js
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// webpack.common.js
// ============================================================================
// Imports
// ============================================================================
import path from "path"
import paths from "./webpack.paths.js";
import params from "./webpack.params.js";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import { fileURLToPath } from "url";
// ============================================================================
// Constants
// ============================================================================
// Convert the current file's URL to a file path
const __filename = fileURLToPath(import.meta.url);
// Derive the directory name of the current module
const __dirname = path.dirname(__filename);
const isDevelopment = process.env.NODE_ENV !== "production";
/**
* Common Webpack Configuration
*
* This configuration file is the base for both development and production
* environments. It includes configurations that are common across both.
*/
const configCommon = {
// Configuration | Context
// ========================================================================
// Set the base directory for resolving entry points and loaders
// context: path.join(__dirname, "your-app"),
// context: path.resolve(__dirname, "../"), // Adjust the path as per your project structure
// Configuration | Target Environment
// ========================================================================
target: ["web", "es5"],
// Configuration | Entry Points
// ========================================================================
// Entry points for the application
// Where webpack looks to start building the bundle
entry: {
index: `${paths.src}/ts/index.ts`,
index_scss: `${paths.src}/scss/index.scss`,
// index: paths.src + "/ts/index.ts"
// index_scss: "./src/scss/index.scss",
},
// Configuration | Output
// ========================================================================
output: {
library: paths.name,
libraryTarget: "umd",
libraryExport: "default",
// Output directory
path: path.resolve(__dirname, "dist"),
// path: paths.build,
// Filename pattern
filename: "js/[name].bundle.js",
// Public URL of the output directory when referenced in a browser
// Adjust if your app is served from a specific path
publicPath: "/",
// Clean the output directory before emit
clean: true,
},
// Configuration | Module Rules
// ========================================================================
// Module rules for handling different file types.
// Determine how modules within the project are treated.
module: {
// entry: "./js/index.js",
// ...
// target: ["web", "es5"],
rules: [
// TypeScript Rules
// ----------------------------------------------------------------
// Compiles TypeScript (.ts) files to JavaScript and enables
// additional features for development
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
// Babel Loader
// Transpiles TypeScript to JavaScript with Babel.
// This allows for the use of the latest JavaScript
// features and ensures compatibility with older browsers.
{
loader: "babel-loader",
options: {
presets: [
// Preset for compiling modern JavaScript to
// a more compatible version
["@babel/preset-env", {
// Define target environments
targets: "> 0.25%, not dead",
// Only include polyfills and transforms
// needed for target environments
useBuiltIns: "usage",
// Specify the core-js version for polyfills
corejs: 3,
// Preserve ECMAScript modules for tree
// shaking in Webpack
// modules: true
modules: false
}],
// Preset for handling TypeScript
"@babel/preset-typescript"
],
caller: {
supportsStaticESM: true
},
plugins: [
// Add any necessary Babel plugins here
// Enables dynamic import syntax in JavaScript
// (important for code splitting in ESM)
"@babel/plugin-syntax-dynamic-import",
// Other plugins that your project might need
],
},
},
// TypeScript Loader
// Handles the TypeScript compilation
{
loader: "ts-loader",
options: {
// Enable transpileOnly to speed up compilation in
// development mode
// Note: This disables type checking during Webpack
// compilation.
// Type checking can be done separately (e.g., via
// a script or in the IDE).
transpileOnly: isDevelopment,
// Configure additional ts-loader options as needed
// For example, specify a custom tsconfig.json or
// enable/disable certain compiler options
},
},
],
},
// JavaScript Rules
// ----------------------------------------------------------------
// Use Babel Loader to transpile JavaScript file
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
// CSS and SCSS Rules
// ----------------------------------------------------------------
// Handles importing CSS and SCSS files. Use MiniCssExtractPlugin
// in production and style-loader in development
{
test: /\.(scss|css)$/,
exclude: /node_modules/,
use: [
isDevelopment ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
// Enable source maps.
sourceMap: true,
// Enable CSS Modules if needed.
// modules: true,
},
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
// Automatically add vendor prefixes.
"autoprefixer"
]
}
}
},
// "sass-to-string",
{
loader: "sass-loader",
options: {
// Enable source maps.
sourceMap: true,
// modules: false,
sassOptions: {
// More readable output in development
// outputStyle: "expanded",
outputStyle: "compressed"
}
},
},
]
},
// Nunjucks Rules
// ----------------------------------------------------------------
// Processes Nunjucks templates
{
test: /\.(njk|nunjucks|jinja)$/,
use: [
{
loader: "nunjucks-loader",
options: {
// Specify the path to the Nunjucks configuration file
// Adjust the path as per your project structure
// config: path.join(__dirname, "src/nunjucks.config.js"),
// Set to true if you are using Jinja compatibility features
jinjaCompat: true,
// Define the root directory for Nunjucks templates
// This is where Nunjucks will look for template files
// root: path.resolve(__dirname, "path/to/templates"),
// Optional: Suppress the "Cannot configure nunjucks
// environment before precompile" warning
quiet: true,
// Other Nunjucks loader specific options can be added here
}
}
],
},
// Font Rules
// ----------------------------------------------------------------
{
// Handles font files (woff, woff2, eot, ttf, otf)
test: /\.(woff(2)?|eot|ttf|otf)$/,
type: "asset/resource",
generator: {
// Output directory and naming format for fonts
filename: "fonts/[name][ext][query]",
// Optionally, customize the public path for the fonts
// publicPath: "../",
},
},
// SVG Rules
// ----------------------------------------------------------------
// Handles SVG files
{
test: /\.svg$/,
type: "asset/resource",
// Handles SVGs with different strategies based on file size
// type: "asset",
// parser: {
// dataUrlCondition: {
// // Inline SVGs smaller than 8kb as data URIs
// maxSize: 8192,
// },
// },
generator: {
// Output directory and naming format for SVGs
filename: "images/[name][ext][query]",
// Optionally, customize the public path for the SVGs
// publicPath: "../",
},
},
// Image Rules
// ----------------------------------------------------------------
// Manages image files and optimizes them for web
{
test: /\.(ico|gif|png|jpg|jpeg)$/i,
type: "asset/resource",
// type: "asset",
// parser: {
// dataUrlCondition: {
// // Inline images smaller than 8kb as data URIs
// maxSize: 8192,
// },
// },
generator: {
// Output directory and naming format for images
filename: "images/[name][ext][query]",
// Optionally, customize the public path for the images
// publicPath: "../",
},
},
],
},
// Configuration | Resolve
// ========================================================================
// Simplifies module resolution
resolve: {
// Modules directories
modules: [
paths.src,
"node_modules"
],
// Extensions that are used
extensions: [
".ts", ".tsx",
".js", ".jsx",
".json",
".scss"
],
// Alias for directories (to simplify imports)
alias: {
// Alias for source and assets paths
"@": paths.src,
assets: paths.public,
},
},
// Plugins
// ========================================================================
// Add any common plugins for both development and production here
// Customize the webpack build process
plugins: [
// Generates an HTML file from a template
// new HtmlWebpackPlugin(
// {
// // Title for the generated HTML document
// title: params.name,
// // Template for generating the main HTML file
// template: paths.src + "/html/index.html",
// filename: "index.html",
// // favicon: paths.public + "/favicon.ico" // Optional: Specify a favicon
// }
// ),
],
// Configuration | Performance
// ========================================================================
// Performance settings to control webpack"s hints
// In the common configuration, you might not set specific performance
// settings, as these are typically more relevant for production.
// However, you can set a baseline that can be overridden in
// environment-specific configurations.
// Basic performance hints can be set here
performance: {
hints: false
},
// Configuration | Devtool
// ========================================================================
// Control how source maps are generated.
// In the common configuration, you don"t typically specify devtool.
// Instead, defer this setting to the environment-specific configurations.
};
// ============================================================================
// Exports
// ============================================================================
export default configCommon