Skip to content

Commit 1b415fc

Browse files
committed
Build visualizer output fix (#7701)
* Fixed treemap, treesun and treenet visualizer output * Enhance build documentation and optimize plugin usage for release builds
1 parent 1ccaa36 commit 1b415fc

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.idea/
33
.vscode/
44
build
5-
tree*.html
5+
tree*.*.html
66
coverage
77
docs
88
examples/dist

build.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* - bundleState (unbundled, bundled)
1010
* Example: target:esm:release:bundled
1111
*
12-
* treemap - Enable treemap build visualization.
13-
* treenet - Enable treenet build visualization.
14-
* treesun - Enable treesun build visualization.
12+
* treemap - Enable treemap build visualization (release only).
13+
* treenet - Enable treenet build visualization (release only).
14+
* treesun - Enable treesun build visualization (release only).
1515
*/
1616

1717
import { execSync } from 'child_process';

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@
110110
"build:esm": "npm run build target:esm",
111111
"build:esm:release": "npm run build target:esm:release",
112112
"build:esm:debug": "npm run build target:esm:debug",
113-
"build:treemap": "npm run build target:umd treemap",
114-
"build:treenet": "npm run build target:umd treenet",
115-
"build:treesun": "npm run build target:umd treesun",
113+
"build:treemap": "npm run build target:release treemap",
114+
"build:treenet": "npm run build target:release treenet",
115+
"build:treesun": "npm run build target:release treesun",
116116
"build:sourcemaps": "npm run build -- -m",
117117
"watch": "npm run build -- -w",
118118
"watch:release": "npm run build target:release -- -w",

utils/rollup-build-target.mjs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ import { version, revision } from './rollup-version-revision.mjs';
1919
import { getBanner } from './rollup-get-banner.mjs';
2020
import { babelOptions } from './rollup-babel-options.mjs';
2121

22+
import { dirname, resolve as pathResolve } from 'path';
23+
import { fileURLToPath } from 'url';
24+
25+
// Find path to the repo root
26+
// @ts-ignore import.meta not allowed by tsconfig module:es6, but it works
27+
const __filename = fileURLToPath(import.meta.url);
28+
const __dirname = dirname(__filename);
29+
const rootDir = pathResolve(__dirname, '..');
30+
2231
/** @typedef {import('rollup').RollupOptions} RollupOptions */
2332
/** @typedef {import('rollup').OutputOptions} OutputOptions */
2433
/** @typedef {import('rollup').ModuleFormat} ModuleFormat */
@@ -111,31 +120,32 @@ function getJSCCOptions(buildType, isUMD) {
111120
}
112121

113122
/**
123+
* @param {string} type - The type of the output (e.g., 'umd', 'es').
114124
* @returns {OutputOptions['plugins']} - The output plugins.
115125
*/
116-
function getOutPlugins() {
126+
function getOutPlugins(type) {
117127
const plugins = [
118128
terser()
119129
];
120130

121131
if (process.env.treemap) {
122132
plugins.push(visualizer({
123-
filename: 'treemap.html',
133+
filename: `treemap.${type}.html`,
124134
brotliSize: true,
125135
gzipSize: true
126136
}));
127137
}
128138

129139
if (process.env.treenet) {
130140
plugins.push(visualizer({
131-
filename: 'treenet.html',
141+
filename: `treenet.${type}.html`,
132142
template: 'network'
133143
}));
134144
}
135145

136146
if (process.env.treesun) {
137147
plugins.push(visualizer({
138-
filename: 'treesun.html',
148+
filename: `treesun.${type}.html`,
139149
template: 'sunburst'
140150
}));
141151
}
@@ -146,6 +156,10 @@ function getOutPlugins() {
146156
/**
147157
* Build a target that Rollup is supposed to build (bundled and unbundled).
148158
*
159+
* For faster subsequent builds, the unbundled and release builds are cached in the HISTORY map to
160+
* be used for bundled and minified builds. They are stored in the HISTORY map with the key:
161+
* `<debug|release|profiler>-<umd|esm>-<bundled>`.
162+
*
149163
* @param {object} options - The build target options.
150164
* @param {'umd'|'esm'} options.moduleFormat - The module format.
151165
* @param {'debug'|'release'|'profiler'|'min'} options.buildType - The build type.
@@ -160,6 +174,9 @@ function buildTarget({ moduleFormat, buildType, bundleState, input = 'src/index.
160174
const isMin = buildType === 'min';
161175
const bundled = isUMD || isMin || bundleState === 'bundled';
162176

177+
const prefix = `${OUT_PREFIX[buildType]}`;
178+
const file = `${prefix}${isUMD ? '.js' : '.mjs'}`;
179+
163180
const targets = [];
164181

165182
// bundle from unbundled
@@ -178,7 +195,7 @@ function buildTarget({ moduleFormat, buildType, bundleState, input = 'src/index.
178195
sourcemap: isDebug && 'inline',
179196
name: 'pc',
180197
preserveModules: false,
181-
file: `${dir}/${OUT_PREFIX[buildType]}.mjs`
198+
file: `${dir}/${prefix}.mjs`
182199
}
183200
};
184201

@@ -198,7 +215,7 @@ function buildTarget({ moduleFormat, buildType, bundleState, input = 'src/index.
198215
const target = {
199216
input: release.output.file,
200217
output: {
201-
plugins: getOutPlugins(),
218+
banner: isUMD ? getBanner(BANNER[buildType]) : undefined,
202219
file: `${dir}/${OUT_PREFIX[buildType]}${isUMD ? '.js' : '.mjs'}`
203220
},
204221
context: isUMD ? 'this' : undefined
@@ -217,14 +234,15 @@ function buildTarget({ moduleFormat, buildType, bundleState, input = 'src/index.
217234
input,
218235
output: {
219236
banner: bundled ? getBanner(BANNER[buildType]) : undefined,
220-
plugins: isMin ? getOutPlugins() : undefined,
237+
plugins: buildType === 'release' ? getOutPlugins(isUMD ? 'umd' : 'es') : undefined,
221238
format: isUMD ? 'umd' : 'es',
222239
indent: '\t',
223240
sourcemap: bundled && isDebug && 'inline',
224241
name: 'pc',
225242
preserveModules: !bundled,
226-
file: bundled ? `${dir}/${OUT_PREFIX[buildType]}${isUMD ? '.js' : '.mjs'}` : undefined,
227-
dir: !bundled ? `${dir}/${OUT_PREFIX[buildType]}` : undefined,
243+
preserveModulesRoot: !bundled ? rootDir : undefined,
244+
file: bundled ? `${dir}/${file}` : undefined,
245+
dir: !bundled ? `${dir}/${prefix}` : undefined,
228246
entryFileNames: chunkInfo => `${chunkInfo.name.replace(/node_modules/g, 'modules')}.js`
229247
},
230248
plugins: [

0 commit comments

Comments
 (0)