Skip to content

Commit

Permalink
build: Deduplicate build emissions
Browse files Browse the repository at this point in the history
Certain files are emitted again without changes in subsequent
build passes. This patch adds a Vite plugin that allows us to
declare allowed file extensions to emit in each build config.

Bug: T349566
Change-Id: I79d6a890baa463695a292c92c199506f3b3de040
  • Loading branch information
tyhopp authored and jenkins-bot committed Nov 10, 2023
1 parent 13e74da commit 69ef173
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
26 changes: 15 additions & 11 deletions packages/codex/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import autoprefixer from 'autoprefixer';
import postcssRtlcss from 'postcss-rtlcss';
import { codexIconNames, getComponentEntryPoints } from './build/utils.mjs';
import generateCodexBundle from './build/generateCodexBundle.mjs';
import emitAllowlist from './build/vite-plugin-emit-allowlist.mjs';

const __dirname = url.fileURLToPath( /** @type {url.URL} */ ( new URL( '.', import.meta.url ) ) );
const componentMap = getComponentEntryPoints( resolve( __dirname, 'src', 'components' ) );
Expand Down Expand Up @@ -89,21 +90,26 @@ const baseLibraryConfig = mergeConfig( baseConfig, {
}
}
} );
// Minify the CJS and UMD builds
const minifiedLibraryConfig = mergeConfig( baseLibraryConfig, {
build: {
lib: {
formats: [ 'cjs', 'umd' ]
}
}
} );
// Don't minify the ES build

// Don't minify the ES build, emit the mjs file
const unminifiedLibraryConfig = mergeConfig( baseLibraryConfig, {
build: {
lib: {
formats: [ 'es' ]
},
minify: false
},
plugins: [
emitAllowlist( [ 'mjs' ] )
]
} );

// Minify the CJS and UMD builds, emit all files
const minifiedLibraryConfig = mergeConfig( baseLibraryConfig, {
build: {
lib: {
formats: [ 'cjs', 'umd' ]
}
}
} );

Expand Down Expand Up @@ -153,8 +159,6 @@ await build( {
} );

// Build the Codex bundles
// Build the unminified files first and the minified files second, otherwise
// the minified CSS files are overwritten with unminified ones
await generateCodexBundle( unminifiedLibraryConfig );
await generateCodexBundle( minifiedLibraryConfig );
await generateCodexBundle( splitConfig );
Expand Down
17 changes: 14 additions & 3 deletions packages/codex/build/generateCodexBundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { build, mergeConfig } from 'vite';
import autoprefixer from 'autoprefixer';
import rtlcss from 'rtlcss';
import * as url from 'url';
import emitAllowlist from './vite-plugin-emit-allowlist.mjs';

const __dirname = url.fileURLToPath( /** @type {url.URL} */ ( new URL( '.', import.meta.url ) ) );

Expand Down Expand Up @@ -67,6 +68,7 @@ export default async function generateCodexBundle( bundleConfig = {} ) {
};

switch ( mode ) {
// Emit all files for the default mode, and only CSS files for other modes
case '':
overrides = {
css: {
Expand All @@ -86,7 +88,10 @@ export default async function generateCodexBundle( bundleConfig = {} ) {
replacement: resolve( __dirname, '../../codex-design-tokens/dist/theme-wikimedia-ui-legacy.less' )
}
]
}
},
plugins: [
emitAllowlist( [ 'css' ] )
]
};
break;
case 'rtl':
Expand All @@ -98,7 +103,10 @@ export default async function generateCodexBundle( bundleConfig = {} ) {
autoprefixer()
]
}
}
},
plugins: [
emitAllowlist( [ 'css' ] )
]
};
break;
case 'legacy-rtl':
Expand All @@ -118,7 +126,10 @@ export default async function generateCodexBundle( bundleConfig = {} ) {
replacement: resolve( __dirname, '../../codex-design-tokens/dist/theme-wikimedia-ui-legacy.less' )
}
]
}
},
plugins: [
emitAllowlist( [ 'css' ] )
]
};
break;
}
Expand Down
39 changes: 39 additions & 0 deletions packages/codex/build/vite-plugin-emit-allowlist.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* A Vite/Rollup plugin to allow only files with specific extensions to be emitted.
*
* @param {Iterable<string>} extensions A list of extensions to allow, e.g. ['css', 'mjs']
* @return {import('vite').Plugin}
*
* @example
* import { build } from 'vite';
* import emitAllowlist from './vite-plugin-emit-allowlist.mjs';
*
* const config = {
* plugins: [
* emitAllowlist(['mjs', 'css'])
* ]
* ];
*
* await build(config);
*/
export default function emitAllowlist( extensions = [] ) {
const allowedExtensions = new Set( extensions );

return {
name: 'emit-allowlist',
generateBundle: {
order: 'post',
handler( _, bundle = {} ) {
for ( const fileName in bundle ) {
const fileExtension = fileName.split( '.' ).pop();

if ( fileExtension && allowedExtensions.has( fileExtension ) ) {
continue;
}

delete bundle[ fileName ];
}
}
}
};
}

0 comments on commit 69ef173

Please sign in to comment.