Skip to content

Commit

Permalink
Add isEntry flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 26, 2021
1 parent d7a81f0 commit c1cb94a
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 17 deletions.
7 changes: 5 additions & 2 deletions browser/resolveId.ts
Expand Up @@ -12,18 +12,21 @@ 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<ResolvedId | null>,
skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
): Promise<ResolveIdResult> {
const pluginResult = await resolveIdViaPlugins(
source,
importer,
pluginDriver,
moduleLoaderResolveId,
skip,
customOptions
customOptions,
isEntry
);
if (pluginResult == null) {
throwNoFileSystem('path.resolve');
Expand Down
1 change: 0 additions & 1 deletion docs/01-command-line-reference.md
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion docs/05-plugin-development.md
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/ModuleLoader.ts
Expand Up @@ -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<ResolvedId | null> => {
return this.addDefaultsToResolvedId(
Expand All @@ -162,7 +163,8 @@ export class ModuleLoader {
this.pluginDriver,
this.resolveId,
skip,
customOptions
customOptions,
typeof isEntry === 'boolean' ? isEntry : !importer
),

importer,
Expand Down Expand Up @@ -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
))
Expand Down Expand Up @@ -529,7 +531,8 @@ export class ModuleLoader {
this.pluginDriver,
this.resolveId,
null,
EMPTY_OBJECT
EMPTY_OBJECT,
true
);
if (resolveIdResult == null) {
return error(
Expand Down Expand Up @@ -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
));
Expand Down
4 changes: 2 additions & 2 deletions src/rollup/types.d.ts
Expand Up @@ -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<ResolvedId | null>;
/** @deprecated Use `this.resolve` instead */
resolveId: (source: string, importer?: string) => Promise<string | null>;
Expand Down Expand Up @@ -237,7 +237,7 @@ export type ResolveIdHook = (
this: PluginContext,
source: string,
importer: string | undefined,
options: { custom?: CustomPluginOptions }
options: { custom?: CustomPluginOptions; isEntry: boolean }
) => Promise<ResolveIdResult> | ResolveIdResult;

export type IsExternal = (
Expand Down
5 changes: 3 additions & 2 deletions src/utils/PluginContext.ts
Expand Up @@ -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',
Expand Down
7 changes: 5 additions & 2 deletions src/utils/resolveId.ts
Expand Up @@ -13,18 +13,21 @@ 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<ResolvedId | null>,
skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
): Promise<ResolveIdResult> {
const pluginResult = await resolveIdViaPlugins(
source,
importer,
pluginDriver,
moduleLoaderResolveId,
skip,
customOptions
customOptions,
isEntry
);
if (pluginResult != null) return pluginResult;

Expand Down
9 changes: 6 additions & 3 deletions src/utils/resolveIdViaPlugins.ts
Expand Up @@ -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<ResolvedId | null>,
skip: { importer: string | undefined; plugin: Plugin; source: string }[] | null,
customOptions: CustomPluginOptions | undefined
customOptions: CustomPluginOptions | undefined,
isEntry: boolean
): Promise<ResolveIdResult> {
let skipped: Set<Plugin> | null = null;
let replaceContext: ReplaceContext | null = null;
Expand All @@ -32,19 +34,20 @@ 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
);
}
});
}
return pluginDriver.hookFirst(
'resolveId',
[source, importer, { custom: customOptions }],
[source, importer, { custom: customOptions, isEntry }],
replaceContext,
skipped
);
Expand Down
72 changes: 72 additions & 0 deletions 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}`);
}
}
}
]
}
};
@@ -0,0 +1 @@
console.log('with');
@@ -0,0 +1 @@
console.log('without');
1 change: 1 addition & 0 deletions test/function/samples/resolveid-is-entry/dep.js
@@ -0,0 +1 @@
export default 42;
3 changes: 3 additions & 0 deletions test/function/samples/resolveid-is-entry/main.js
@@ -0,0 +1,3 @@
import value from './dep.js';

assert.strictEqual(value, 42);
@@ -0,0 +1 @@
console.log('with');
@@ -0,0 +1 @@
console.log('with entry');
@@ -0,0 +1 @@
console.log('with non entry');
@@ -0,0 +1 @@
console.log('without');
@@ -0,0 +1 @@
console.log('without entry');
@@ -0,0 +1 @@
console.log('without non entry');

0 comments on commit c1cb94a

Please sign in to comment.