Skip to content

Commit

Permalink
Improved name generation for unbundled dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-steele-idem committed Jan 11, 2016
1 parent 65c935e commit 7c34bf6
Showing 1 changed file with 16 additions and 47 deletions.
63 changes: 16 additions & 47 deletions lib/BundleMappings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Bundle = require('./Bundle');
var nodePath = require('path');
var fileSep = nodePath.sep;

var InlinePos = require('./InlinePos');
var raptorModulesUtil = require('raptor-modules/util');
Expand Down Expand Up @@ -183,54 +184,27 @@ 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) {
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
relPath = dependency.getUnbundledTarget(lassoContext) + '-' + pageBundleName;
sourceFile = dependency.getUnbundledTarget(lassoContext) + '-' + pageBundleName.replace(/[\\\/]/g, '-');
}

if (!relPath && dependency.getSourceFile) {
// The dependency does not have an unbundled target so we'll
// use the source file path to determine the appropriate
// output bundle.
if (!sourceFile && dependency.getSourceFile) {
sourceFile = dependency.getSourceFile();
}

if (sourceFile && sourceFile.indexOf(fileSep) !== -1) {
//var sourceDir = nodePath.dirname(sourceFile);
var projectRoot = this.config.getProjectRoot();

if (sourceFile) {
//var sourceDir = nodePath.dirname(sourceFile);
var projectRoot = this.config.getProjectRoot();

if (sourceFile.startsWith(projectRoot + '/')) {
// 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 modulePkg = tryGetModuleRootPackage(nodePath.dirname(sourceFile));
if (sourceFile.startsWith(projectRoot + fileSep)) {
relPath = sourceFile.substring(projectRoot.length);
} else {
var dirname = nodePath.dirname(sourceFile);
if (dirname) {
var modulePkg = tryGetModuleRootPackage(dirname);
if (modulePkg) {
relPath = sourceFile.substring(modulePkg.__dirname.length);
if (modulePkg.__dirname !== projectRoot) {
Expand All @@ -243,10 +217,8 @@ BundleMappings.prototype = {
}
}

if (unbundledTargetPrefix && relPath) {
// If an unbundledTargetPrefix is provided then add that
// to the start of the relative path.
relPath = nodePath.join(unbundledTargetPrefix, relPath);
if (sourceFile && !relPath) {
relPath = sourceFile;
}

bundle = this.addDependencyToBundle(
Expand All @@ -258,12 +230,9 @@ 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,
Expand Down

0 comments on commit 7c34bf6

Please sign in to comment.