Skip to content

Commit

Permalink
fix(webpack): Aliased module paths now properly map to the correct au…
Browse files Browse the repository at this point in the history
…relia-loader module id
  • Loading branch information
Pat Herlihy committed Sep 5, 2017
1 parent 5177bb3 commit 8970d4a
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/PreserveModuleNamePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PreserveModuleNamePlugin {
modulesBeforeConcat.splice(i--, 1, ...m["modules"]);
}

for (let module of getPreservedModules(modules)) {
for (let module of getPreservedModules(modules, alias)) {
// Even though it's imported by Aurelia, it's still possible that the module
// became the _root_ of a ConcatenatedModule.
// We use `constructor.name` rather than `instanceof` for compat. with Webpack 2.
Expand Down Expand Up @@ -66,14 +66,36 @@ export class PreserveModuleNamePlugin {
}
};

function getPreservedModules(modules: Webpack.Module[]) {
function getPreservedModules(modules: Webpack.Module[], aliases: {[key: string]: string } | null) {
return new Set(
modules.filter(m => {
// Some modules might have [preserveModuleName] already set, see ConventionDependenciesPlugin.
let value = m[preserveModuleName];
for (let r of m.reasons) {
if (!r.dependency[preserveModuleName]) continue;
value = true;

// Handle aliases
const raw: string | undefined = r.module && r.module.rawRequest;
const ext: string | RegExpMatchArray | null = m.rawRequest && m.rawRequest.match(/^.+(\.\w+$)/);
if (aliases && raw && ext) {
const alias_matches: string[] = Object.keys(aliases).filter((a: string): boolean => {
// Absolute?
if (a[a.length - 1] === '$' && a === `${raw}$`) {
return true;
}

return !!(raw.match(new RegExp(`^${a}/`)));
});

// Invalid?
if (alias_matches.length > 1) throw new Error(`Incorrect alias usage. "${raw}" is duplicated in ${alias_matches}`);
else if (alias_matches.length === 1 && ext[1]) {
m[exports.preserveModuleName] = `${raw}${ext[1]}`;
return true;
}
}

let req = removeLoaders((r.dependency as ModuleDependency).request);
// We try to find an absolute string and set that as the module [preserveModuleName], as it's the best id.
if (req && !req.startsWith(".")) {
Expand Down

0 comments on commit 8970d4a

Please sign in to comment.