From 5d30e6f26ee5ce9e2e4c7182348e34faa82e647f Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 20 Oct 2021 00:27:44 +0200 Subject: [PATCH] fix(alias): pass on isEntry flag and custom options (#1017) --- packages/alias/package.json | 2 +- packages/alias/src/index.ts | 10 ++-- packages/alias/test/test.js | 98 ++++++++++++++++++++++++++++++++++++- pnpm-lock.yaml | 58 ++++++++++++++++++++-- 4 files changed, 158 insertions(+), 10 deletions(-) diff --git a/packages/alias/package.json b/packages/alias/package.json index 81f2b0ec1..ea749adc0 100755 --- a/packages/alias/package.json +++ b/packages/alias/package.json @@ -54,7 +54,7 @@ "@rollup/plugin-node-resolve": "^8.4.0", "@rollup/plugin-typescript": "^5.0.2", "del-cli": "^3.0.1", - "rollup": "^2.23.0", + "rollup": "^2.58.0", "typescript": "^4.1.2" }, "types": "types/index.d.ts", diff --git a/packages/alias/src/index.ts b/packages/alias/src/index.ts index 99e8ec805..419a587b7 100755 --- a/packages/alias/src/index.ts +++ b/packages/alias/src/index.ts @@ -83,7 +83,7 @@ export default function alias(options: RollupAliasOptions = {}): Plugin { // enforce void return value }); }, - resolveId(importee, importer) { + resolveId(importee, importer, resolveOptions) { const importeeId = normalizeId(importee); const importerId = normalizeId(importer); @@ -99,10 +99,14 @@ export default function alias(options: RollupAliasOptions = {}): Plugin { const customResolver = getCustomResolver(matchedEntry, options); if (customResolver) { - return customResolver.call(this, updatedId, importerId, {}); + return customResolver.call(this, updatedId, importerId, resolveOptions); } - return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => { + return this.resolve( + updatedId, + importer, + Object.assign({ skipSelf: true }, resolveOptions) + ).then((resolved) => { let finalResult: PartialResolvedId | null = resolved; if (!finalResult) { finalResult = { id: updatedId }; diff --git a/packages/alias/test/test.js b/packages/alias/test/test.js index 6f52c98c3..d85985ab7 100755 --- a/packages/alias/test/test.js +++ b/packages/alias/test/test.js @@ -32,8 +32,10 @@ function resolveWithRollup(plugins, tests) { // The buildStart hook is the first to have access to this.resolve // We map the tests to an array of resulting ids Promise.all( - tests.map(({ source, importer }) => - this.resolve(source, importer).then((result) => (result ? result.id : null)) + tests.map(({ source, importer, options }) => + this.resolve(source, importer, options).then((result) => + result ? result.id : null + ) ) ) ); @@ -437,3 +439,95 @@ test('Alias + rollup-plugin-node-resolve', (t) => ) ); })); + +test('Forwards isEntry and custom options to a custom resolver', (t) => { + const resolverCalls = []; + return resolveAliasWithRollup( + { + entries: { + entry: 'entry-point', + nonEntry: 'non-entry-point' + }, + customResolver: (...args) => { + resolverCalls.push(args); + return args[0]; + } + }, + [ + { source: 'entry', importer: '/src/importer.js', options: { isEntry: true } }, + { + source: 'nonEntry', + importer: '/src/importer.js', + options: { isEntry: false, custom: { test: 42 } } + } + ] + ).then((result) => { + t.deepEqual(resolverCalls, [ + [ + 'entry-point', + '/src/importer.js', + { + custom: void 0, + isEntry: true + } + ], + [ + 'non-entry-point', + '/src/importer.js', + { + custom: { test: 42 }, + isEntry: false + } + ] + ]); + t.deepEqual(result, ['entry-point', 'non-entry-point']); + }); +}); + +test('Forwards isEntry and custom options to other plugins', (t) => { + const resolverCalls = []; + return resolveWithRollup( + [ + alias({ + entries: { + entry: 'entry-point', + nonEntry: 'non-entry-point' + } + }), + { + name: 'test', + resolveId(...args) { + resolverCalls.push(args); + } + } + ], + [ + { source: 'entry', importer: '/src/importer.js', options: { isEntry: true } }, + { + source: 'nonEntry', + importer: '/src/importer.js', + options: { isEntry: false, custom: { test: 42 } } + } + ] + ).then((result) => { + t.deepEqual(resolverCalls, [ + [ + 'entry-point', + '/src/importer.js', + { + custom: void 0, + isEntry: true + } + ], + [ + 'non-entry-point', + '/src/importer.js', + { + custom: { test: 42 }, + isEntry: false + } + ] + ]); + t.deepEqual(result, ['entry-point', 'non-entry-point']); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index eda7f1e14..473f7f459 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -71,16 +71,16 @@ importers: '@rollup/plugin-node-resolve': ^8.4.0 '@rollup/plugin-typescript': ^5.0.2 del-cli: ^3.0.1 - rollup: ^2.23.0 + rollup: ^2.58.0 slash: ^3.0.0 typescript: ^4.1.2 dependencies: slash: 3.0.0 devDependencies: - '@rollup/plugin-node-resolve': 8.4.0_rollup@2.32.1 - '@rollup/plugin-typescript': 5.0.2_rollup@2.32.1+typescript@4.1.2 + '@rollup/plugin-node-resolve': 8.4.0_rollup@2.58.0 + '@rollup/plugin-typescript': 5.0.2_rollup@2.58.0+typescript@4.1.2 del-cli: 3.0.1 - rollup: 2.32.1 + rollup: 2.58.0 typescript: 4.1.2 packages/auto-install: @@ -2209,6 +2209,22 @@ packages: rollup: 2.39.0 dev: true + /@rollup/plugin-node-resolve/8.4.0_rollup@2.58.0: + resolution: {integrity: sha512-LFqKdRLn0ShtQyf6SBYO69bGE1upV6wUhBX0vFOUnLAyzx5cwp8svA0eHUnu8+YU57XOkrMtfG63QOpQx25pHQ==} + engines: {node: '>= 8.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0 + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@2.58.0 + '@types/resolve': 1.17.1 + builtin-modules: 3.1.0 + deep-freeze: 0.0.1 + deepmerge: 4.2.2 + is-module: 1.0.0 + resolve: 1.20.0 + rollup: 2.58.0 + dev: true + /@rollup/plugin-node-resolve/9.0.0_rollup@2.32.1: resolution: {integrity: sha512-gPz+utFHLRrd41WMP13Jq5mqqzHL3OXrfj3/MkSyB6UBIcuNt9j60GCbarzMzdf1VHFpOxfQh/ez7wyadLMqkg==} engines: {node: '>= 10.0.0'} @@ -2252,6 +2268,20 @@ packages: typescript: 4.2.4 dev: true + /@rollup/plugin-typescript/5.0.2_rollup@2.58.0+typescript@4.1.2: + resolution: {integrity: sha512-CkS028Itwjqm1uLbFVfpJgtVtnNvZ+og/m6UlNRR5wOOnNTWPcVQzOu5xGdEX+WWJxdvWIqUq2uR/RBt2ZipWg==} + engines: {node: '>=8.0.0'} + peerDependencies: + rollup: ^2.14.0 + tslib: '*' + typescript: '>=3.4.0' + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@2.58.0 + resolve: 1.20.0 + rollup: 2.58.0 + typescript: 4.1.2 + dev: true + /@rollup/plugin-typescript/6.1.0_rollup@2.32.1+typescript@4.1.2: resolution: {integrity: sha512-hJxaiE6WyNOsK+fZpbFh9CUijZYqPQuAOWO5khaGTUkM8DYNNyA2TDlgamecE+qLOG1G1+CwbWMAx3rbqpp6xQ==} engines: {node: '>=8.0.0'} @@ -2307,6 +2337,18 @@ packages: picomatch: 2.2.2 rollup: 2.58.0 + /@rollup/pluginutils/3.1.0_rollup@2.58.0: + resolution: {integrity: sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==} + engines: {node: '>= 8.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0 + dependencies: + '@types/estree': 0.0.39 + estree-walker: 1.0.1 + picomatch: 2.2.2 + rollup: 2.58.0 + dev: true + /@rollup/pluginutils/4.1.1: resolution: {integrity: sha512-clDjivHqWGXi7u+0d2r2sBi4Ie6VLEAzWMIkvJLnDmxoOhBYOTfzGbOQBA32THHm11/LiJbd01tJUpJsbshSWQ==} engines: {node: '>= 8.0.0'} @@ -7363,6 +7405,14 @@ packages: fsevents: 2.3.2 dev: true + /rollup/2.58.0: + resolution: {integrity: sha512-NOXpusKnaRpbS7ZVSzcEXqxcLDOagN6iFS8p45RkoiMqPHDLwJm758UF05KlMoCRbLBTZsPOIa887gZJ1AiXvw==} + engines: {node: '>=10.0.0'} + hasBin: true + optionalDependencies: + fsevents: 2.3.2 + dev: true + /run-parallel/1.1.9: resolution: {integrity: sha512-DEqnSRTDw/Tc3FXf49zedI638Z9onwUotBMiUFKmrO2sdFKIbXamXGQ3Axd4qgphxKB4kw/qP1w5kTxnfU1B9Q==} dev: true