From cb709fde46b6d6656b09e684d23183b3d289acde Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Wed, 21 Aug 2019 13:51:00 -0700 Subject: [PATCH 1/3] Normalize ids before looking up in named export map --- src/index.js | 9 +++++++-- test/test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 8da0742..ceef2c7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ import { realpathSync, existsSync } from 'fs'; -import { extname, resolve } from 'path'; +import { extname, resolve, normalize } from 'path'; import { sync as nodeResolveSync, isCore } from 'resolve'; import { createFilter } from 'rollup-pluginutils'; import { peerDependencies } from '../package.json'; @@ -40,11 +40,14 @@ export default function commonjs(options = {}) { } catch (err) { resolvedId = resolve(id); } + + // ASSERT: resolvedId is a normalized path customNamedExports[resolvedId] = options.namedExports[id]; if (existsSync(resolvedId)) { const realpath = realpathSync(resolvedId); if (realpath !== resolvedId) { + // ASSERT: realpath is a normalized path customNamedExports[realpath] = options.namedExports[id]; } } @@ -81,6 +84,8 @@ export default function commonjs(options = {}) { return null; } + const normalizedId = normalize(id); + const transformed = transformCommonjs( this.parse, code, @@ -88,7 +93,7 @@ export default function commonjs(options = {}) { this.getModuleInfo(id).isEntry, ignoreGlobal, ignoreRequire, - customNamedExports[id], + customNamedExports[normalizedId], sourceMap, allowDynamicRequire, ast diff --git a/test/test.js b/test/test.js index 8927c6f..f78035b 100644 --- a/test/test.js +++ b/test/test.js @@ -822,5 +822,41 @@ exports.shuffleArray = shuffleArray_1; ` ); }); + + it.only('normalizes paths used in the named export map', async () => { + // Deliberately denormalizes file paths and ensures named exports + // continue to work. + function hookedResolve() { + const resolvePlugin = resolve(); + const oldResolve = resolvePlugin.resolveId; + resolvePlugin.resolveId = async function(source) { + const result = await oldResolve.apply(resolvePlugin, arguments); + if (source === 'external') { + result.id = result.id.replace(/\\/g, '/'); + return result; + } else if (source.match(/custom-named-exports/)) { + result.id = result.id.replace(/\//g, '\\'); + } + return result; + }; + + return resolvePlugin; + } + + const bundle = await rollup({ + input: 'samples/custom-named-exports/main.js', + plugins: [ + hookedResolve(), + commonjs({ + namedExports: { + 'samples/custom-named-exports/secret-named-exporter.js': ['named'], + external: ['message'] + } + }) + ] + }); + + await executeBundle(bundle); + }); }); }); From 53e04782ef433052e64c3ba7b155ae084e90c0df Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Thu, 22 Aug 2019 12:32:05 -0700 Subject: [PATCH 2/3] Fix path normalization tests on linux --- test/test.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/test.js b/test/test.js index f78035b..c1787c8 100644 --- a/test/test.js +++ b/test/test.js @@ -823,20 +823,18 @@ exports.shuffleArray = shuffleArray_1; ); }); - it.only('normalizes paths used in the named export map', async () => { + it('normalizes paths used in the named export map', async () => { // Deliberately denormalizes file paths and ensures named exports // continue to work. function hookedResolve() { const resolvePlugin = resolve(); const oldResolve = resolvePlugin.resolveId; - resolvePlugin.resolveId = async function(source) { + resolvePlugin.resolveId = async function() { const result = await oldResolve.apply(resolvePlugin, arguments); - if (source === 'external') { - result.id = result.id.replace(/\\/g, '/'); - return result; - } else if (source.match(/custom-named-exports/)) { - result.id = result.id.replace(/\//g, '\\'); + if (result) { + result.id = result.id.replace(/\/|\\/, path.sep); } + return result; }; From a451c8afb03a46997b653c7ff3fa731c9962fe55 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Thu, 22 Aug 2019 12:34:23 -0700 Subject: [PATCH 3/3] clarify normalization comments --- src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index ceef2c7..dd9e74c 100644 --- a/src/index.js +++ b/src/index.js @@ -41,13 +41,14 @@ export default function commonjs(options = {}) { resolvedId = resolve(id); } - // ASSERT: resolvedId is a normalized path + // Note: customNamedExport's keys must be normalized file paths. + // resolve and nodeResolveSync both return normalized file paths + // so no additional normalization is necessary. customNamedExports[resolvedId] = options.namedExports[id]; if (existsSync(resolvedId)) { const realpath = realpathSync(resolvedId); if (realpath !== resolvedId) { - // ASSERT: realpath is a normalized path customNamedExports[realpath] = options.namedExports[id]; } }