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

Normalize ids before looking up in named export map #406

Merged
merged 4 commits into from Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 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,11 +40,14 @@ export default function commonjs(options = {}) {
} catch (err) {
resolvedId = resolve(id);
}

// ASSERT: resolvedId is a normalized path
bterlson marked this conversation as resolved.
Show resolved Hide resolved
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];
}
}
Expand Down Expand Up @@ -81,14 +84,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
36 changes: 36 additions & 0 deletions test/test.js
Expand Up @@ -822,5 +822,41 @@ exports.shuffleArray = shuffleArray_1;
`
);
});

it.only('normalizes paths used in the named export map', async () => {
bterlson marked this conversation as resolved.
Show resolved Hide resolved
// 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, '\\');
bterlson marked this conversation as resolved.
Show resolved Hide resolved
}
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);
});
});
});