Skip to content

Commit 44e3ce3

Browse files
authored
[code-infra] Build size snapshots from installed packages (#43452)
1 parent 46698de commit 44e3ce3

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

pnpm-lock.yaml

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ packages:
66
- 'docs'
77
- 'test'
88
- 'apps/*'
9+
- 'scripts/sizeSnapshot'

scripts/sizeSnapshot/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "size-snapshot",
3+
"version": "1.0.0",
4+
"description": "Size snapshot of MUI packages",
5+
"dependencies": {
6+
"@mui/lab": "workspace:^",
7+
"@mui/base": "workspace:^",
8+
"@mui/joy": "workspace:^",
9+
"@mui/material": "workspace:^",
10+
"@mui/styles": "workspace:^",
11+
"@mui/private-theming": "workspace:^",
12+
"@mui/utils": "workspace:^",
13+
"@mui/system": "workspace:^"
14+
}
15+
}

scripts/sizeSnapshot/webpack.config.js

+38-23
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ async function getWebpackEntries() {
2121

2222
return {
2323
id: entryName,
24-
path: path.relative(workspaceRoot, path.dirname(componentPath)),
24+
import: `@mui/material/${componentName}`,
2525
};
2626
},
2727
);
2828

2929
const corePackagePath = path.join(workspaceRoot, 'packages/mui-base/build');
30-
const coreComponents = (await glob(path.join(corePackagePath, '([A-Z])*/index.js'))).map(
30+
const baseComponents = (await glob(path.join(corePackagePath, '([A-Z])*/index.js'))).map(
3131
(componentPath) => {
3232
const componentName = path.basename(path.dirname(componentPath));
3333
let entryName = componentName;
@@ -38,7 +38,7 @@ async function getWebpackEntries() {
3838

3939
return {
4040
id: entryName,
41-
path: path.relative(workspaceRoot, path.dirname(componentPath)),
41+
import: `@mui/base/${componentName}`,
4242
};
4343
},
4444
);
@@ -50,7 +50,7 @@ async function getWebpackEntries() {
5050

5151
return {
5252
id: componentName,
53-
path: path.relative(workspaceRoot, path.dirname(componentPath)),
53+
import: `@mui/lab/${componentName}`,
5454
};
5555
},
5656
);
@@ -62,7 +62,7 @@ async function getWebpackEntries() {
6262

6363
return {
6464
id: `@mui/joy/${componentName}`,
65-
path: path.relative(workspaceRoot, path.dirname(componentPath)),
65+
import: `@mui/joy/${componentName}`,
6666
};
6767
},
6868
);
@@ -72,62 +72,63 @@ async function getWebpackEntries() {
7272
// WARNING: Changing the name will break tracking of bundle size over time
7373
// If the name of the package changes, rename its display name in https://github.com/eps1lon/mui-contributor-dashboard/blob/main/src/pages/SizeComparison.tsx
7474
id: '@material-ui/core',
75-
path: path.join(path.relative(workspaceRoot, materialPackagePath), 'index.js'),
75+
import: '@mui/material',
7676
},
7777
...materialComponents,
7878
{
7979
id: '@material-ui/lab',
80-
path: path.join(path.relative(workspaceRoot, labPackagePath), 'index.js'),
80+
import: '@mui/lab',
8181
},
8282
...labComponents,
8383
{
8484
id: '@material-ui/styles',
85-
path: 'packages/mui-styles/build/index.js',
85+
import: '@mui/styles',
8686
},
8787
{
8888
id: '@material-ui/private-theming',
89-
path: 'packages/mui-private-theming/build/index.js',
89+
import: '@mui/private-theming',
9090
},
9191
{
9292
id: '@material-ui/system',
93-
path: 'packages/mui-system/build/index.js',
93+
import: '@mui/system',
9494
},
9595
{
9696
id: 'createBox',
97-
path: 'packages/mui-system/build/createBox/index.js',
97+
import: '@mui/system/createBox',
9898
},
9999
{
100100
id: 'createStyled',
101-
path: 'packages/mui-system/build/createStyled/index.js',
101+
import: '@mui/system/createStyled',
102102
},
103103
{
104104
id: '@material-ui/core/styles/createTheme',
105-
path: 'packages/mui-material/build/styles/createTheme.js',
105+
importName: 'createTheme',
106+
import: '@mui/material/styles',
106107
},
107108
{
108109
id: 'colorManipulator',
109-
path: 'packages/mui-system/build/colorManipulator/index.js',
110+
import: '@mui/system/colorManipulator',
110111
},
111112
{
112113
id: 'useAutocomplete',
113-
path: 'packages/mui-lab/build/useAutocomplete/index.js',
114+
import: '@mui/lab/useAutocomplete',
114115
},
115116
{
116117
id: '@material-ui/core/useMediaQuery',
117-
path: 'packages/mui-material/build/useMediaQuery/index.js',
118+
import: '@mui/material/useMediaQuery',
118119
},
119120
{
120121
id: '@material-ui/core/useScrollTrigger',
121-
path: 'packages/mui-material/build/useScrollTrigger/index.js',
122+
import: '@mui/material/useScrollTrigger',
122123
},
123124
{
124125
id: '@material-ui/unstyled',
125-
path: path.join(path.relative(workspaceRoot, corePackagePath), 'index.js'),
126+
import: '@mui/base',
126127
},
127-
...coreComponents,
128+
...baseComponents,
128129
{
129130
id: '@material-ui/utils',
130-
path: 'packages/mui-utils/build/esm/index.js',
131+
import: '@mui/utils',
131132
},
132133
// TODO: Requires webpack v5
133134
// Resolution of webpack/acorn to 7.x is blocked by nextjs (https://github.com/vercel/next.js/issues/11947)
@@ -138,7 +139,7 @@ async function getWebpackEntries() {
138139
// },
139140
{
140141
id: '@mui/joy',
141-
path: path.join(path.relative(workspaceRoot, joyPackagePath), 'index.js'),
142+
import: '@mui/joy',
142143
},
143144
...joyComponents,
144145
];
@@ -148,6 +149,8 @@ function createWebpackConfig(entry, environment) {
148149
const analyzerMode = environment.analyze ? 'static' : 'disabled';
149150
const concatenateModules = !environment.accurateBundles;
150151

152+
const importNames = entry.importName ? `{ ${entry.importName} as foo }` : '* as foo';
153+
151154
/**
152155
* @type {import('webpack').Configuration}
153156
*/
@@ -160,7 +163,7 @@ function createWebpackConfig(entry, environment) {
160163
concatenateModules,
161164
minimizer: [
162165
new TerserPlugin({
163-
test: /\.js(\?.*)?$/i,
166+
test: /\.m?js(\?.*)?$/i,
164167
}),
165168
],
166169
},
@@ -174,6 +177,11 @@ function createWebpackConfig(entry, environment) {
174177
},
175178
path: path.join(__dirname, 'build'),
176179
},
180+
resolve: {
181+
alias: {
182+
'@mui/utils': '@mui/utils/esm',
183+
},
184+
},
177185
plugins: [
178186
new CompressionPlugin(),
179187
new BundleAnalyzerPlugin({
@@ -186,7 +194,14 @@ function createWebpackConfig(entry, environment) {
186194
reportFilename: `${entry.id}.html`,
187195
}),
188196
],
189-
entry: { [entry.id]: path.join(workspaceRoot, entry.path) },
197+
// A context to the current dir, which has a node_modules folder with workspace dependencies
198+
context: __dirname,
199+
entry: {
200+
// This format is a data: url combined with inline matchResource to obtain a virtual entry.
201+
// See https://github.com/webpack/webpack/issues/6437#issuecomment-874466638
202+
// See https://webpack.js.org/api/loaders/#inline-matchresource
203+
[entry.id]: `./index.js!=!data:text/javascript,import ${importNames} from '${entry.import}';console.log(foo);`,
204+
},
190205
// TODO: 'browserslist:modern'
191206
// See https://github.com/webpack/webpack/issues/14203
192207
target: 'web',

0 commit comments

Comments
 (0)