From c1cb94a8cc64caac521c56f9984cb434554cf0e2 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sun, 26 Sep 2021 07:25:46 +0200 Subject: [PATCH] Add isEntry flag --- browser/resolveId.ts | 7 +- docs/01-command-line-reference.md | 1 - docs/05-plugin-development.md | 2 +- src/ModuleLoader.ts | 11 +-- src/rollup/types.d.ts | 4 +- src/utils/PluginContext.ts | 5 +- src/utils/resolveId.ts | 7 +- src/utils/resolveIdViaPlugins.ts | 9 ++- .../samples/resolveid-is-entry/_config.js | 72 +++++++++++++++++++ .../resolveid-is-entry/chunkWithImporter.js | 1 + .../chunkWithoutImporter.js | 1 + .../samples/resolveid-is-entry/dep.js | 1 + .../samples/resolveid-is-entry/main.js | 3 + .../resolutionWithImporter.js | 1 + .../resolutionWithImporterEntry.js | 1 + .../resolutionWithImporterNonEntry.js | 1 + .../resolutionWithoutImporter.js | 1 + .../resolutionWithoutImporterEntry.js | 1 + .../resolutionWithoutImporterNonEntry.js | 1 + 19 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 test/function/samples/resolveid-is-entry/_config.js create mode 100644 test/function/samples/resolveid-is-entry/chunkWithImporter.js create mode 100644 test/function/samples/resolveid-is-entry/chunkWithoutImporter.js create mode 100644 test/function/samples/resolveid-is-entry/dep.js create mode 100644 test/function/samples/resolveid-is-entry/main.js create mode 100644 test/function/samples/resolveid-is-entry/resolutionWithImporter.js create mode 100644 test/function/samples/resolveid-is-entry/resolutionWithImporterEntry.js create mode 100644 test/function/samples/resolveid-is-entry/resolutionWithImporterNonEntry.js create mode 100644 test/function/samples/resolveid-is-entry/resolutionWithoutImporter.js create mode 100644 test/function/samples/resolveid-is-entry/resolutionWithoutImporterEntry.js create mode 100644 test/function/samples/resolveid-is-entry/resolutionWithoutImporterNonEntry.js diff --git a/browser/resolveId.ts b/browser/resolveId.ts index 59b57b0d584..945768fb94f 100644 --- a/browser/resolveId.ts +++ b/browser/resolveId.ts @@ -12,10 +12,12 @@ export async function resolveId( source: string, importer: string | undefined, customOptions: CustomPluginOptions | undefined, + isEntry: boolean | undefined, skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null ) => Promise, skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null, - customOptions: CustomPluginOptions | undefined + customOptions: CustomPluginOptions | undefined, + isEntry: boolean ): Promise { const pluginResult = await resolveIdViaPlugins( source, @@ -23,7 +25,8 @@ export async function resolveId( pluginDriver, moduleLoaderResolveId, skip, - customOptions + customOptions, + isEntry ); if (pluginResult == null) { throwNoFileSystem('path.resolve'); diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 2e2a76074b7..1b255e445ef 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -469,7 +469,6 @@ rollup -c --environment INCLUDE_DEPS,BUILD:production will set `process.env.INCLUDE_DEPS === 'true'` and `process.env.BUILD === 'production'`. You can use this option several times. In that case, subsequently set variables will overwrite previous definitions. This enables you for instance to overwrite environment variables in `package.json` scripts: ```json -// in package.json { "scripts": { "build": "rollup -c --environment INCLUDE_DEPS,BUILD:production" diff --git a/docs/05-plugin-development.md b/docs/05-plugin-development.md index aab049175e2..a2c2ce7fed9 100644 --- a/docs/05-plugin-development.md +++ b/docs/05-plugin-development.md @@ -171,7 +171,7 @@ function onlyDefaultForEntriesPlugin() { return { name: 'only-default-for-entries', async resolveId(source, importer, options) { - if (isEntry) { + if (options.isEntry) { // We need to skip this plugin to avoid an infinite loop const resolution = await this.resolve(source, importer, { skipSelf: true, ...options }); // If it cannot be resolved, return `null` so that Rollup displays an error diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 717c373b29c..1386485fda9 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -149,6 +149,7 @@ export class ModuleLoader { source: string, importer: string | undefined, customOptions: CustomPluginOptions | undefined, + isEntry: boolean | undefined, skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null = null ): Promise => { return this.addDefaultsToResolvedId( @@ -162,7 +163,8 @@ export class ModuleLoader { this.pluginDriver, this.resolveId, skip, - customOptions + customOptions, + typeof isEntry === 'boolean' ? isEntry : !importer ), importer, @@ -483,7 +485,7 @@ export class ModuleLoader { (module.resolvedIds[source] = module.resolvedIds[source] || this.handleResolveId( - await this.resolveId(source, module.id, EMPTY_OBJECT), + await this.resolveId(source, module.id, EMPTY_OBJECT, false), source, module.id )) @@ -529,7 +531,8 @@ export class ModuleLoader { this.pluginDriver, this.resolveId, null, - EMPTY_OBJECT + EMPTY_OBJECT, + true ); if (resolveIdResult == null) { return error( @@ -585,7 +588,7 @@ export class ModuleLoader { return (module.resolvedIds[specifier] = module.resolvedIds[specifier] || this.handleResolveId( - await this.resolveId(specifier, module.id, EMPTY_OBJECT), + await this.resolveId(specifier, module.id, EMPTY_OBJECT, false), specifier, module.id )); diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 1c0d877ece9..2d0b3d9fad1 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -204,7 +204,7 @@ export interface PluginContext extends MinimalPluginContext { resolve: ( source: string, importer?: string, - options?: { custom?: CustomPluginOptions; skipSelf?: boolean } + options?: { custom?: CustomPluginOptions; isEntry?: boolean; skipSelf?: boolean } ) => Promise; /** @deprecated Use `this.resolve` instead */ resolveId: (source: string, importer?: string) => Promise; @@ -237,7 +237,7 @@ export type ResolveIdHook = ( this: PluginContext, source: string, importer: string | undefined, - options: { custom?: CustomPluginOptions } + options: { custom?: CustomPluginOptions; isEntry: boolean } ) => Promise | ResolveIdResult; export type IsExternal = ( diff --git a/src/utils/PluginContext.ts b/src/utils/PluginContext.ts index 30499fe58cc..24b1abe735f 100644 --- a/src/utils/PluginContext.ts +++ b/src/utils/PluginContext.ts @@ -157,18 +157,19 @@ export function getPluginContext( return wrappedModuleIds(); }, parse: graph.contextParse.bind(graph), - resolve(source, importer, { custom, skipSelf } = BLANK) { + resolve(source, importer, { custom, isEntry, skipSelf } = BLANK) { return graph.moduleLoader.resolveId( source, importer, custom, + isEntry, skipSelf ? [{ importer, plugin, source }] : null ); }, resolveId: getDeprecatedContextHandler( (source: string, importer: string | undefined) => graph.moduleLoader - .resolveId(source, importer, BLANK) + .resolveId(source, importer, BLANK, undefined) .then(resolveId => resolveId && resolveId.id), 'resolveId', 'resolve', diff --git a/src/utils/resolveId.ts b/src/utils/resolveId.ts index ebc553598e4..c6a908f926f 100644 --- a/src/utils/resolveId.ts +++ b/src/utils/resolveId.ts @@ -13,10 +13,12 @@ export async function resolveId( source: string, importer: string | undefined, customOptions: CustomPluginOptions | undefined, + isEntry: boolean | undefined, skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null ) => Promise, skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null, - customOptions: CustomPluginOptions | undefined + customOptions: CustomPluginOptions | undefined, + isEntry: boolean ): Promise { const pluginResult = await resolveIdViaPlugins( source, @@ -24,7 +26,8 @@ export async function resolveId( pluginDriver, moduleLoaderResolveId, skip, - customOptions + customOptions, + isEntry ); if (pluginResult != null) return pluginResult; diff --git a/src/utils/resolveIdViaPlugins.ts b/src/utils/resolveIdViaPlugins.ts index c8d0d9c96a1..23a14a332d6 100644 --- a/src/utils/resolveIdViaPlugins.ts +++ b/src/utils/resolveIdViaPlugins.ts @@ -16,10 +16,12 @@ export function resolveIdViaPlugins( source: string, importer: string | undefined, customOptions: CustomPluginOptions | undefined, + isEntry: boolean | undefined, skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null ) => Promise, skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null, - customOptions: CustomPluginOptions | undefined + customOptions: CustomPluginOptions | undefined, + isEntry: boolean ): Promise { let skipped: Set | null = null; let replaceContext: ReplaceContext | null = null; @@ -32,11 +34,12 @@ export function resolveIdViaPlugins( } replaceContext = (pluginContext, plugin): PluginContext => ({ ...pluginContext, - resolve: (source, importer, { custom, skipSelf } = BLANK) => { + resolve: (source, importer, { custom, isEntry, skipSelf } = BLANK) => { return moduleLoaderResolveId( source, importer, custom, + isEntry, skipSelf ? [...skip, { importer, plugin, source }] : skip ); } @@ -44,7 +47,7 @@ export function resolveIdViaPlugins( } return pluginDriver.hookFirst( 'resolveId', - [source, importer, { custom: customOptions }], + [source, importer, { custom: customOptions, isEntry }], replaceContext, skipped ); diff --git a/test/function/samples/resolveid-is-entry/_config.js b/test/function/samples/resolveid-is-entry/_config.js new file mode 100644 index 00000000000..2cbc44ca45e --- /dev/null +++ b/test/function/samples/resolveid-is-entry/_config.js @@ -0,0 +1,72 @@ +const assert = require('assert'); +const path = require('path'); + +const ID_MAIN = path.join(__dirname, 'main.js'); + +module.exports = { + description: 'sends correct isEntry information to resolveId hooks', + options: { + plugins: [ + { + async buildStart() { + return Promise.all([ + this.emitFile({ type: 'chunk', id: 'chunkWithoutImporter.js' }), + this.emitFile({ type: 'chunk', id: './chunkWithImporter.js', importer: ID_MAIN }), + this.resolve('./resolutionWithoutImporter'), + this.resolve('./resolutionWithoutImporterEntry', undefined, { isEntry: true }), + this.resolve('./resolutionWithoutImporterNonEntry', undefined, { isEntry: false }), + this.resolve('./resolutionWithImporter', ID_MAIN), + this.resolve('./resolutionWithImporterEntry', ID_MAIN, { isEntry: true }), + this.resolve('./resolutionWithImporterNonEntry', ID_MAIN, { isEntry: false }) + ]); + }, + resolveId(source, importer, { isEntry }) { + switch (source) { + case ID_MAIN: + assert.strictEqual(importer, undefined, source); + assert.strictEqual(isEntry, true, source); + break; + case './dep.js': + assert.strictEqual(importer, ID_MAIN, source); + assert.strictEqual(isEntry, false, source); + break; + case 'chunkWithoutImporter.js': + assert.strictEqual(importer, undefined, source); + assert.strictEqual(isEntry, true, source); + break; + case './chunkWithImporter.js': + assert.strictEqual(importer, ID_MAIN, source); + assert.strictEqual(isEntry, true, source); + break; + case './resolutionWithoutImporter': + assert.strictEqual(importer, undefined, source); + assert.strictEqual(isEntry, true, source); + break; + case './resolutionWithoutImporterEntry': + assert.strictEqual(importer, undefined, source); + assert.strictEqual(isEntry, true, source); + break; + case './resolutionWithoutImporterNonEntry': + assert.strictEqual(importer, undefined, source); + assert.strictEqual(isEntry, false, source); + break; + case './resolutionWithImporter': + assert.strictEqual(importer, ID_MAIN, source); + assert.strictEqual(isEntry, false, source); + break; + case './resolutionWithImporterEntry': + assert.strictEqual(importer, ID_MAIN, source); + assert.strictEqual(isEntry, true, source); + break; + case './resolutionWithImporterNonEntry': + assert.strictEqual(importer, ID_MAIN, source); + assert.strictEqual(isEntry, false, source); + break; + default: + throw new Error(`Unexpected resolution of ${source}`); + } + } + } + ] + } +}; diff --git a/test/function/samples/resolveid-is-entry/chunkWithImporter.js b/test/function/samples/resolveid-is-entry/chunkWithImporter.js new file mode 100644 index 00000000000..090d82c998d --- /dev/null +++ b/test/function/samples/resolveid-is-entry/chunkWithImporter.js @@ -0,0 +1 @@ +console.log('with'); diff --git a/test/function/samples/resolveid-is-entry/chunkWithoutImporter.js b/test/function/samples/resolveid-is-entry/chunkWithoutImporter.js new file mode 100644 index 00000000000..63b0787fe6a --- /dev/null +++ b/test/function/samples/resolveid-is-entry/chunkWithoutImporter.js @@ -0,0 +1 @@ +console.log('without'); diff --git a/test/function/samples/resolveid-is-entry/dep.js b/test/function/samples/resolveid-is-entry/dep.js new file mode 100644 index 00000000000..7a4e8a723a4 --- /dev/null +++ b/test/function/samples/resolveid-is-entry/dep.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/function/samples/resolveid-is-entry/main.js b/test/function/samples/resolveid-is-entry/main.js new file mode 100644 index 00000000000..57c61d3c5f2 --- /dev/null +++ b/test/function/samples/resolveid-is-entry/main.js @@ -0,0 +1,3 @@ +import value from './dep.js'; + +assert.strictEqual(value, 42); diff --git a/test/function/samples/resolveid-is-entry/resolutionWithImporter.js b/test/function/samples/resolveid-is-entry/resolutionWithImporter.js new file mode 100644 index 00000000000..090d82c998d --- /dev/null +++ b/test/function/samples/resolveid-is-entry/resolutionWithImporter.js @@ -0,0 +1 @@ +console.log('with'); diff --git a/test/function/samples/resolveid-is-entry/resolutionWithImporterEntry.js b/test/function/samples/resolveid-is-entry/resolutionWithImporterEntry.js new file mode 100644 index 00000000000..26f0cb2cc4d --- /dev/null +++ b/test/function/samples/resolveid-is-entry/resolutionWithImporterEntry.js @@ -0,0 +1 @@ +console.log('with entry'); diff --git a/test/function/samples/resolveid-is-entry/resolutionWithImporterNonEntry.js b/test/function/samples/resolveid-is-entry/resolutionWithImporterNonEntry.js new file mode 100644 index 00000000000..daa7098b591 --- /dev/null +++ b/test/function/samples/resolveid-is-entry/resolutionWithImporterNonEntry.js @@ -0,0 +1 @@ +console.log('with non entry'); diff --git a/test/function/samples/resolveid-is-entry/resolutionWithoutImporter.js b/test/function/samples/resolveid-is-entry/resolutionWithoutImporter.js new file mode 100644 index 00000000000..63b0787fe6a --- /dev/null +++ b/test/function/samples/resolveid-is-entry/resolutionWithoutImporter.js @@ -0,0 +1 @@ +console.log('without'); diff --git a/test/function/samples/resolveid-is-entry/resolutionWithoutImporterEntry.js b/test/function/samples/resolveid-is-entry/resolutionWithoutImporterEntry.js new file mode 100644 index 00000000000..63d99dd46fa --- /dev/null +++ b/test/function/samples/resolveid-is-entry/resolutionWithoutImporterEntry.js @@ -0,0 +1 @@ +console.log('without entry'); diff --git a/test/function/samples/resolveid-is-entry/resolutionWithoutImporterNonEntry.js b/test/function/samples/resolveid-is-entry/resolutionWithoutImporterNonEntry.js new file mode 100644 index 00000000000..cb47020f873 --- /dev/null +++ b/test/function/samples/resolveid-is-entry/resolutionWithoutImporterNonEntry.js @@ -0,0 +1 @@ +console.log('without non entry');