Skip to content

Commit 36f2cc8

Browse files
chore(webpack): optimize webpack files (#2007)
* Optimize webpack files * upgrade webpack config files to ECMA script * Delete webpack-common.js * fix incorrect extraction of docs.css for stacks docs --------- Co-authored-by: Giamir Buoncristiani <[email protected]>
1 parent f127fd6 commit 36f2cc8

File tree

4 files changed

+146
-146
lines changed

4 files changed

+146
-146
lines changed

packages/stacks-classic/webpack.config.js

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import path from "path"
2+
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
3+
import cssnano from 'cssnano'
4+
import { fileURLToPath } from 'node:url';
5+
import { dirname } from 'node:path';
6+
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = dirname(__filename);
9+
10+
const tsRule = (configFile) => ({
11+
test: /\.tsx?$/,
12+
exclude: /node_modules/,
13+
use: [
14+
{
15+
loader: "ts-loader",
16+
options: configFile ? { configFile } : {},
17+
},
18+
],
19+
});
20+
21+
const lessRule = (minify = false) => ({
22+
test: /\.less$/,
23+
use: [
24+
MiniCssExtractPlugin.loader,
25+
{
26+
loader: "css-loader",
27+
options: {
28+
importLoaders: 1,
29+
url: false,
30+
},
31+
},
32+
{
33+
loader: "postcss-loader",
34+
options: {
35+
postcssOptions: {
36+
plugins: minify ? [cssnano] : [],
37+
},
38+
},
39+
},
40+
"less-loader",
41+
],
42+
});
43+
44+
const commonResolve = {
45+
extensions: [".tsx", ".ts", ".js"],
46+
};
47+
48+
const baseConfig = (isProd, minify) => ({
49+
name: "stacks" + (minify ? ".min" : ""),
50+
// run the minified bundle first, then the unminified bundle
51+
dependencies: minify ? [] : ["stacks.min"],
52+
mode: isProd ? "production" : "development",
53+
devtool: isProd ? false : "inline-source-map",
54+
entry: {
55+
// add .min to the file names of minified bundles
56+
[minify ? "stacks.min" : "stacks"]: path.resolve(
57+
__dirname,
58+
"lib/index.ts"
59+
),
60+
},
61+
output: {
62+
filename: `js/[name].js`,
63+
path: path.resolve(__dirname, "dist"),
64+
// don't empty out the dist folder when running the second build
65+
clean: minify,
66+
compareBeforeEmit: true,
67+
library: "Stacks",
68+
libraryTarget: "umd",
69+
},
70+
module: {
71+
rules: [tsRule("tsconfig.build.json"), lessRule(minify)],
72+
},
73+
optimization: {
74+
minimize: minify,
75+
},
76+
plugins: [
77+
new MiniCssExtractPlugin({
78+
filename: `css/[name].css`,
79+
}),
80+
],
81+
resolve: commonResolve,
82+
});
83+
84+
export { tsRule, lessRule, commonResolve };
85+
86+
// build the bundle twice - once minified and once not
87+
export default [
88+
(_, argv) => baseConfig(argv.mode === "production", true),
89+
(_, argv) => baseConfig(argv.mode === "production", false),
90+
];

packages/stacks-docs/webpack.config.js

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import path from "path";
2+
import fs from "fs";
3+
import {
4+
tsRule,
5+
lessRule,
6+
commonResolve,
7+
} from "../stacks-classic/webpack.config.mjs";
8+
import MiniCssExtractPlugin from "mini-css-extract-plugin";
9+
10+
import { fileURLToPath } from "node:url";
11+
import { dirname } from "node:path";
12+
13+
const __filename = fileURLToPath(import.meta.url);
14+
const __dirname = dirname(__filename);
15+
16+
export default (_, argv) => {
17+
const isProd = argv.mode === "production";
18+
19+
// load each entry.*.js file in assets/js as its own bundle
20+
const entries = fs
21+
.readdirSync(path.resolve(__dirname, "assets/js/"))
22+
.filter((f) => f.startsWith("entry."))
23+
.reduce((p, n) => {
24+
// { "entry.file": "path/to/entry.file.js" }
25+
p[n.slice(0, -3)] = path.resolve(__dirname, "assets/js/", n);
26+
return p;
27+
}, {});
28+
29+
return {
30+
mode: isProd ? "production" : "development",
31+
devtool: isProd ? false : "inline-source-map",
32+
entry: {
33+
docs: path.resolve(__dirname, "assets/js/index.ts"),
34+
...entries,
35+
},
36+
output: {
37+
filename: "[name].js",
38+
path: path.resolve(__dirname, "assets/dist"),
39+
clean: true,
40+
},
41+
module: {
42+
rules: [
43+
tsRule(), // no special config file here
44+
lessRule(isProd),
45+
],
46+
},
47+
plugins: [new MiniCssExtractPlugin()],
48+
resolve: commonResolve,
49+
devServer: {
50+
webSocketURL: {
51+
// 11ty/browsersync steal the default port (8080), so set it to something else
52+
port: 8081,
53+
},
54+
},
55+
};
56+
};

0 commit comments

Comments
 (0)