Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Normalize ids before looking up in named export map (#406)
Browse files Browse the repository at this point in the history
* Normalize ids before looking up in named export map

* Fix path normalization tests on linux

* clarify normalization comments
  • Loading branch information
bterlson authored and lukastaegert committed Aug 27, 2019
1 parent e431c29 commit fcd9826
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 8 additions & 2 deletions 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';
Expand Down Expand Up @@ -40,6 +40,10 @@ export default function commonjs(options = {}) {
} catch (err) {
resolvedId = resolve(id);
}

// 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)) {
Expand Down Expand Up @@ -81,14 +85,16 @@ export default function commonjs(options = {}) {
return null;
}

const normalizedId = normalize(id);

const transformed = transformCommonjs(
this.parse,
code,
id,
this.getModuleInfo(id).isEntry,
ignoreGlobal,
ignoreRequire,
customNamedExports[id],
customNamedExports[normalizedId],
sourceMap,
allowDynamicRequire,
ast
Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Expand Up @@ -822,5 +822,39 @@ exports.shuffleArray = shuffleArray_1;
`
);
});

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() {
const result = await oldResolve.apply(resolvePlugin, arguments);
if (result) {
result.id = result.id.replace(/\/|\\/, path.sep);
}

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);
});
});
});

0 comments on commit fcd9826

Please sign in to comment.