Decide laziness when resources are added.

This used to happen when we ran the ImportScanner, but the earlier we can
make this decision the better.
This commit is contained in:
Ben Newman
2016-01-19 20:43:26 -05:00
parent b04c837027
commit 18cfba4ddf
2 changed files with 31 additions and 33 deletions

View File

@@ -372,6 +372,31 @@ class ResourceSlot {
return fileOptions && fileOptions[name];
}
_isLazy(options) {
let lazy = this._getOption("lazy", options);
if (typeof lazy === "boolean") {
return lazy;
}
const sourcePath = this.inputResource.path;
if (sourcePath.endsWith(".json")) {
// JSON files have no side effects, so there is no reason for them
// ever to be evaluated eagerly.
return true;
}
// If file.lazy was not previously defined, mark the file lazy if it
// is contained by an imports directory. Note that any files contained
// by a node_modules directory will already have been marked lazy in
// PackageSource#_inferFileOptions.
return this.packageSourceBatch.useMeteorInstall &&
files.pathDirname(sourcePath)
.split(files.pathSep)
.indexOf("imports") >= 0;
}
addStylesheet(options) {
const self = this;
if (! self.sourceProcessor) {
@@ -387,7 +412,7 @@ class ResourceSlot {
// XXX do we need to call convertSourceMapPaths here like we did
// in legacy handlers?
sourceMap: options.sourceMap,
lazy: self._getOption("lazy", options),
lazy: self._isLazy(options),
});
}
@@ -415,7 +440,7 @@ class ResourceSlot {
sourceMap: options.sourceMap,
// intentionally preserve a possible `undefined` value for files
// in apps, rather than convert it into `false` via `!!`
lazy: self._getOption("lazy", options),
lazy: self._isLazy(options),
bare: !! self._getOption("bare", options),
mainModule: !! self._getOption("mainModule", options),
});
@@ -442,7 +467,7 @@ class ResourceSlot {
servePath: self.packageSourceBatch.unibuild.pkg._getServePath(
options.path),
hash: sha1(options.data),
lazy: self._getOption("lazy", options),
lazy: self._isLazy(options),
});
}
@@ -464,7 +489,7 @@ class ResourceSlot {
self.outputResources.push({
type: options.section,
data: new Buffer(files.convertToStandardLineEndings(options.data), 'utf8'),
lazy: self._getOption("lazy", options),
lazy: self._isLazy(options),
});
}
}

View File

@@ -54,11 +54,8 @@ export default class ImportScanner {
const absPath = pathJoin(this.sourceRoot, file.sourcePath);
// Files that are not eagerly evaluated (lazy) will only be included
// in the bundle if they are actually imported.
file.lazy = this._isFileLazy(file);
// Files that are eagerly evaluated are effectively "imported" as
// entry points.
// in the bundle if they are actually imported. Files that are
// eagerly evaluated are effectively "imported" as entry points.
file.imported = ! file.lazy;
file.installPath = this._getInstallPath(absPath);
@@ -106,30 +103,6 @@ export default class ImportScanner {
return this.outputFiles;
}
_isFileLazy(file) {
if (typeof file.lazy === "boolean") {
return file.lazy;
}
if (file.sourcePath.endsWith(".json")) {
// JSON files have no side effects, so there is no reason for them
// ever to be evaluated eagerly.
return true;
}
// If file.lazy was not previously defined, mark the file lazy if it
// is contained by an imports directory. Note that any files contained
// by a node_modules directory will already have been marked lazy in
// PackageSource#_inferFileOptions. The reason we can't do all our
// lazy marking in the _inferFileOptions method is that we don't know
// then whether the current app or package is using the modules
// package. At this point, we know the modules package must be in use,
// because the ImportScanner is only ever used when modules are used.
return this._splitPath(
pathDirname(file.sourcePath)
).indexOf("imports") >= 0;
}
_scanFile(file) {
const absPath = pathJoin(this.sourceRoot, file.sourcePath);
file.deps = file.deps || keys(