-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
68 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]; | ||
} | ||
} | ||
} | ||
}; | ||
} |