From 8970d4a11fba88c24af15ba1ccac09efb3cfb503 Mon Sep 17 00:00:00 2001 From: Pat Herlihy Date: Mon, 4 Sep 2017 23:14:33 -0400 Subject: [PATCH] fix(webpack): Aliased module paths now properly map to the correct aurelia-loader module id --- src/PreserveModuleNamePlugin.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/PreserveModuleNamePlugin.ts b/src/PreserveModuleNamePlugin.ts index 0d611b2..8d07120 100644 --- a/src/PreserveModuleNamePlugin.ts +++ b/src/PreserveModuleNamePlugin.ts @@ -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. @@ -66,7 +66,7 @@ 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. @@ -74,6 +74,28 @@ function getPreservedModules(modules: Webpack.Module[]) { 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(".")) {