Skip to content

Commit

Permalink
feat: introduce new loadInclude hook (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Aug 21, 2022
1 parent f264963 commit 7482dc1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Currently supports:
| [`transform`](https://rollupjs.org/guide/en/#transformers) ||||| ✅ <sup>3</sup> |
| [`enforce`](https://rollupjs.org/guide/en/#enforce) | ❌ <sup>2</sup> |||| ❌ <sup>2</sup> |
| [`resolveId`](https://rollupjs.org/guide/en/#resolveid) ||||||
| `loadInclude`<sup>1</sup> ||||||
| [`load`](https://rollupjs.org/guide/en/#load) ||||| ✅ <sup>3</sup> |
| [`watchChange`](https://rollupjs.org/guide/en/#watchchange) ||||||

Expand Down
2 changes: 1 addition & 1 deletion src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function getEsbuildPlugin <UserOptions = {}> (

let code: string | undefined, map: SourceMap | null | undefined

if (plugin.load) {
if (plugin.load && (!plugin.loadInclude || plugin.loadInclude(id))) {
const result = await plugin.load.call(Object.assign(context, pluginContext), id)
if (typeof result === 'string') {
code = result
Expand Down
10 changes: 10 additions & 0 deletions src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export function toRollupPlugin (plugin: UnpluginOptions, containRollupOptions =
}
}

if (plugin.load && plugin.loadInclude) {
const _load = plugin.load
plugin.load = function (id) {
if (plugin.loadInclude && !plugin.loadInclude(id)) {
return null
}
return _load.call(this, id)
}
}

if (plugin.rollup && containRollupOptions) {
Object.assign(plugin, plugin.rollup)
}
Expand Down
13 changes: 12 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,25 @@ export interface UnpluginBuildContext {
export interface UnpluginOptions {
name: string;
enforce?: 'post' | 'pre' | undefined;

buildStart?: (this: UnpluginBuildContext) => Promise<void> | void;
buildEnd?: (this: UnpluginBuildContext) => Promise<void> | void;
transformInclude?: (id: string) => boolean | null | undefined;
transform?: (this: UnpluginBuildContext & UnpluginContext, code: string, id: string) => Thenable<TransformResult>;
load?: (this: UnpluginBuildContext & UnpluginContext, id: string) => Thenable<TransformResult>
resolveId?: (id: string, importer: string | undefined, options: { isEntry: boolean }) => Thenable<string | ExternalIdResult | null | undefined>
watchChange?: (this: UnpluginBuildContext, id: string, change: { event: 'create' | 'update' | 'delete' }) => void

/**
* Custom predicate function to filter modules to be loaded.
* When omitted, all modules will be included (might have potential perf impact on Webpack).
*/
loadInclude?: (id: string) => boolean | null | undefined;
/**
* Custom predicate function to filter modules to be transformed.
* When omitted, all modules will be included (might have potential perf impact on Webpack).
*/
transformInclude?: (id: string) => boolean | null | undefined;

// framework specify extends
rollup?: Partial<RollupPlugin>
webpack?: (compiler: WebpackCompiler) => void
Expand Down
16 changes: 10 additions & 6 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,18 @@ export function getWebpackPlugin<UserOptions = {}> (
// load hook
if (plugin.load) {
compiler.options.module.rules.push({
include (id) { // Don't run load hook for external modules
include (id) {
if (id.startsWith(plugin.__virtualModulePrefix)) {
// If module is a virtual one, we first need to strip its prefix and decode it
const decodedId = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length))
return !externalModules.has(decodedId)
} else {
return !externalModules.has(id)
id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length))
}

// load include filter
if (plugin.loadInclude && !plugin.loadInclude(id)) {
return false
}

// Don't run load hook for external modules
return !externalModules.has(id)
},
enforce: plugin.enforce,
use: [{
Expand Down
9 changes: 6 additions & 3 deletions test/fixtures/virtual-module/unplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ module.exports = createUnplugin(() => {
resolveId (id) {
return id.startsWith('virtual/') ? id : null
},
loadInclude (id) {
return id.startsWith('virtual/')
},
load (id) {
if (id === 'virtual/1') {
return 'export default "VIRTUAL:ONE"'
}
if (id === 'virtual/2') {
} else if (id === 'virtual/2') {
return 'export default "VIRTUAL:TWO"'
} else {
throw new Error(`Unexpected id: ${id}`)
}
return null
}
}
})

0 comments on commit 7482dc1

Please sign in to comment.