From f54c2a3de68b92c368955924f2aa1241b9a1fc95 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Tue, 12 Jan 2016 10:44:27 -0700 Subject: [PATCH] Further improvements to how paths for output files are generated in unbundled mode --- lib/BundleMappings.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/BundleMappings.js b/lib/BundleMappings.js index 50550661..c92790a3 100644 --- a/lib/BundleMappings.js +++ b/lib/BundleMappings.js @@ -184,10 +184,33 @@ BundleMappings.prototype = { bundle.dependency = dependency; bundle.isExternalResource = true; } else if (this.bundlingEnabled === false) { + // Bundling is NOT enabled + + // We will try to find a relative path that will be used for + // the output file of the bundle. + // This relative path might be different from the bundle name + // if a defaultBundleName is provided + // + // NOTE: If we don't have a defaultBundleName for this dependency + // and if we don't find a relative path then we will use + // `${dependencyType}-${pageBundleName}` as the + // bundle name. + var sourceFile; var relPath; + var unbundledTargetPrefix; + + if (dependency.getUnbundledTargetPrefix) { + // The unbundled target prefix adds a common prefix to the beginning of + // relative paths for all output files + unbundledTargetPrefix = dependency.getUnbundledTargetPrefix(lassoContext); + } + if (dependency.getUnbundledTarget) { + // The dependency provides a getUnbundledTarget(lassoContext) + // so we will try to use that determine a relative path + // which will be used as the bundle name sourceFile = dependency.getUnbundledTarget(lassoContext) + '-' + pageBundleName.replace(/[\\\/]/g, '-'); } @@ -200,8 +223,16 @@ BundleMappings.prototype = { var projectRoot = this.config.getProjectRoot(); if (sourceFile.startsWith(projectRoot + fileSep)) { + // source file is within the project root directory + // so let's remove the project root directory + // path from the source file path which will + // leave us with a relative path relPath = sourceFile.substring(projectRoot.length); } else { + // source file seems to be outside the project root + // directory so we will see if we can resolve + // the module associated with the source file and + // use that build an appropriate relative path var dirname = nodePath.dirname(sourceFile); if (dirname) { var modulePkg = tryGetModuleRootPackage(dirname); @@ -221,6 +252,12 @@ BundleMappings.prototype = { relPath = sourceFile; } + if (unbundledTargetPrefix) { + // If an unbundledTargetPrefix is provided then add that + // to the start of the relative path. + relPath = nodePath.join(unbundledTargetPrefix, relPath); + } + bundle = this.addDependencyToBundle( dependency, dependency.defaultBundleName || relPath || (dependency.type + '-' + pageBundleName), @@ -230,9 +267,12 @@ BundleMappings.prototype = { // bundle.dependency = dependency; if (relPath) { + // We associate this bundle with a relative path which will + // be used as the output file for the bundle bundle.relativeOutputPath = relPath; } } else { + // Bundling is enabled //Make sure the dependency is part of a bundle. If it not part of a preconfigured bundle then put it in a page-specific bundle bundle = this.addDependencyToBundle( dependency,