From 366380eb33c31957fa80dd1df9d4cec8477294c2 Mon Sep 17 00:00:00 2001 From: Cheng Date: Mon, 12 Oct 2020 00:54:00 +0800 Subject: [PATCH] fix(node-resolve)!: resolve hash in path (#588) * fix(node-resolve): resolve hash in path * refact(node-resolve)!: remove special handling for hashes See https://github.com/rollup/plugins/pull/588#issuecomment-702555525 --- packages/node-resolve/src/index.js | 9 ++++----- packages/node-resolve/test/fixtures/hash-in-path.js | 3 +++ packages/node-resolve/test/fixtures/hash.js | 3 --- .../test/fixtures/node_modules/test/#/foo.js | 1 + packages/node-resolve/test/test.js | 6 +++--- 5 files changed, 11 insertions(+), 11 deletions(-) create mode 100644 packages/node-resolve/test/fixtures/hash-in-path.js delete mode 100644 packages/node-resolve/test/fixtures/hash.js create mode 100644 packages/node-resolve/test/fixtures/node_modules/test/#/foo.js diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index 0c045b003..65032fd84 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -18,7 +18,7 @@ import { const builtins = new Set(builtinList); const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js'; const nullFn = () => null; -const deepFreeze = object => { +const deepFreeze = (object) => { Object.freeze(object); for (const value of Object.values(object)) { @@ -100,10 +100,9 @@ export function nodeResolve(opts = {}) { // ignore IDs with null character, these belong to other plugins if (/\0/.test(importee)) return null; - // strip hash and query params from import - const [withoutHash, hash] = importee.split('#'); - const [importPath, params] = withoutHash.split('?'); - const importSuffix = `${params ? `?${params}` : ''}${hash ? `#${hash}` : ''}`; + // strip query params from import + const [importPath, params] = importee.split('?'); + const importSuffix = `${params ? `?${params}` : ''}`; importee = importPath; const basedir = !importer || dedupe(importee) ? rootDir : dirname(importer); diff --git a/packages/node-resolve/test/fixtures/hash-in-path.js b/packages/node-resolve/test/fixtures/hash-in-path.js new file mode 100644 index 000000000..2cd225fdb --- /dev/null +++ b/packages/node-resolve/test/fixtures/hash-in-path.js @@ -0,0 +1,3 @@ +import test from 'test/#/foo'; + +export default test; diff --git a/packages/node-resolve/test/fixtures/hash.js b/packages/node-resolve/test/fixtures/hash.js deleted file mode 100644 index c4ebf73fd..000000000 --- a/packages/node-resolve/test/fixtures/hash.js +++ /dev/null @@ -1,3 +0,0 @@ -import test from 'test#foo'; - -export default test; diff --git a/packages/node-resolve/test/fixtures/node_modules/test/#/foo.js b/packages/node-resolve/test/fixtures/node_modules/test/#/foo.js new file mode 100644 index 000000000..7a4e8a723 --- /dev/null +++ b/packages/node-resolve/test/fixtures/node_modules/test/#/foo.js @@ -0,0 +1 @@ +export default 42; diff --git a/packages/node-resolve/test/test.js b/packages/node-resolve/test/test.js index 69fd157b6..8c75af1f5 100755 --- a/packages/node-resolve/test/test.js +++ b/packages/node-resolve/test/test.js @@ -240,15 +240,15 @@ test('handles package side-effects', async (t) => { delete global.sideEffects; }); -test('can resolve imports with hashes', async (t) => { +test('can resolve imports with hash in path', async (t) => { const bundle = await rollup({ - input: 'hash.js', + input: 'hash-in-path.js', onwarn: () => t.fail('No warnings were expected'), plugins: [ nodeResolve(), { load(id) { - if (id === resolve(__dirname, 'fixtures', 'node_modules', 'test', 'index.js#foo')) { + if (id === resolve(__dirname, 'fixtures', 'node_modules', 'test', '#', 'foo.js')) { return 'export default "resolved with hash"'; } return null;