-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent infinite loops when using this.resolve in resolveId
- Loading branch information
1 parent
12489ec
commit ce27d5c
Showing
11 changed files
with
151 additions
and
49 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
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,45 @@ | ||
import { CustomPluginOptions, Plugin, PluginContext, ResolvedId } from '../rollup/types'; | ||
import { BLANK } from './blank'; | ||
import { PluginDriver, ReplaceContext } from './PluginDriver'; | ||
|
||
export function resolveIdViaPlugins( | ||
source: string, | ||
importer: string | undefined, | ||
pluginDriver: PluginDriver, | ||
moduleLoaderResolveId: ( | ||
source: string, | ||
importer: string | undefined, | ||
customOptions: CustomPluginOptions | 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 | ||
) { | ||
let skipped: Set<Plugin> | null = null; | ||
let replaceContext: ReplaceContext | null = null; | ||
if (skip) { | ||
skipped = new Set(); | ||
for (const skippedCall of skip) { | ||
if (source === skippedCall.source && importer === skippedCall.importer) { | ||
skipped.add(skippedCall.plugin); | ||
} | ||
} | ||
replaceContext = (pluginContext, plugin): PluginContext => ({ | ||
...pluginContext, | ||
resolve: (source, importer, { custom, skipSelf } = BLANK) => { | ||
return moduleLoaderResolveId( | ||
source, | ||
importer, | ||
custom, | ||
skipSelf ? [...skip, { importer, plugin, source }] : skip | ||
); | ||
} | ||
}); | ||
} | ||
return pluginDriver.hookFirst( | ||
'resolveId', | ||
[source, importer, { custom: customOptions }], | ||
replaceContext, | ||
skipped | ||
); | ||
} |
31 changes: 31 additions & 0 deletions
31
test/function/samples/prevent-context-resolve-loop/_config.js
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,31 @@ | ||
const path = require('path'); | ||
|
||
const ID_OTHER_1 = path.join(__dirname, 'other1.js'); | ||
const ID_OTHER_2 = path.join(__dirname, 'other2.js'); | ||
const ID_OTHER_3 = path.join(__dirname, 'other3.js'); | ||
|
||
module.exports = { | ||
description: 'prevents infinite loops when several plugins are calling this.resolve in resolveId', | ||
options: { | ||
plugins: [ | ||
{ | ||
name: 'first', | ||
async resolveId(source, importer) { | ||
const { id } = await this.resolve(source, importer, { skipSelf: true }); | ||
if (id === ID_OTHER_2) { | ||
return ID_OTHER_3; | ||
} | ||
} | ||
}, | ||
{ | ||
name: 'second', | ||
async resolveId(source, importer) { | ||
const { id } = await this.resolve(source, importer, { skipSelf: true }); | ||
if (id === ID_OTHER_1) { | ||
return ID_OTHER_2; | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}; |
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,5 @@ | ||
import first from './other1.js'; | ||
import second from './other2.js'; | ||
|
||
assert.strictEqual(first, 3); | ||
assert.strictEqual(second, 3); |
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 @@ | ||
export default 1; |
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 @@ | ||
export default 2; |
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 @@ | ||
export default 3; |