@@ -19,6 +19,15 @@ import { version, revision } from './rollup-version-revision.mjs';
1919import { getBanner } from './rollup-get-banner.mjs' ;
2020import { 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 ( / n o d e _ m o d u l e s / g, 'modules' ) } .js`
229247 } ,
230248 plugins : [
0 commit comments