From fff2e4666118e509306850da45b5834cedd64e6e Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 24 Nov 2015 21:21:47 -0500 Subject: [PATCH] Process all package source files with compiler plugins. Files not explicitly added with api.addFiles are currently ignored by the Linker, but that will change once we have a module system. --- .../compiler-deprecated-compile-step.js | 16 +++++--- tools/isobuild/compiler-plugin.js | 17 +++++---- tools/isobuild/linker.js | 14 +++++++ tools/isobuild/package-source.js | 37 ++++++++++++++++++- 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/tools/isobuild/compiler-deprecated-compile-step.js b/tools/isobuild/compiler-deprecated-compile-step.js index c893f3e314..41dfed9d09 100644 --- a/tools/isobuild/compiler-deprecated-compile-step.js +++ b/tools/isobuild/compiler-deprecated-compile-step.js @@ -361,11 +361,14 @@ exports.makeCompileStep = function (sourceItem, file, inputSourceArch, options) throw new Error("'sourcePath' option must be supplied to addJavaScript. Consider passing inputPath."); } - // By default, use fileOptions for the `bare` option but also allow - // overriding it with the options - var bare = fileOptions.bare; - if (options.hasOwnProperty("bare")) { - bare = options.bare; + const fileOptions = self.inputResource.fileOptions; + + function getOption(name) { + // By default, use fileOptions for these options but also allow + // overriding them with the options. + return _.has(options, name) + ? options.name + : fileOptions && fileOptions[name]; } var data = new Buffer( @@ -384,7 +387,8 @@ exports.makeCompileStep = function (sourceItem, file, inputSourceArch, options) hash: watch.sha1(data), sourceMap: convertSourceMapPaths(options.sourceMap, files.convertToStandardPath), - bare: !! bare + lazy: !! getOption("lazy"), + bare: !! getOption("bare"), }); }, diff --git a/tools/isobuild/compiler-plugin.js b/tools/isobuild/compiler-plugin.js index 40d88833d8..039b00d509 100644 --- a/tools/isobuild/compiler-plugin.js +++ b/tools/isobuild/compiler-plugin.js @@ -376,12 +376,14 @@ class ResourceSlot { throw Error("addJavaScript on non-source ResourceSlot?"); } - // By default, use the 'bare' option given to addFiles, but allow the option - // passed to addJavaScript to override it. - var bare = self.inputResource.fileOptions && - self.inputResource.fileOptions.bare; - if (options.hasOwnProperty('bare')) { - bare = options.bare; + const fileOptions = self.inputResource.fileOptions; + + function getOption(name) { + // By default, use fileOptions for these options but also allow + // overriding them with the options. + return _.has(options, name) + ? options.name + : fileOptions && fileOptions[name]; } var data = new Buffer( @@ -396,7 +398,8 @@ class ResourceSlot { // XXX do we need to call convertSourceMapPaths here like we did // in legacy handlers? sourceMap: options.sourceMap, - bare: !! bare + lazy: !! getOption("lazy"), + bare: !! getOption("bare"), }); } diff --git a/tools/isobuild/linker.js b/tools/isobuild/linker.js index 1256c2f233..c8695fd649 100644 --- a/tools/isobuild/linker.js +++ b/tools/isobuild/linker.js @@ -109,6 +109,11 @@ _.extend(Module.prototype, { // preserving the line numbers. if (self.useGlobalNamespace) { return _.map(self.files, function (file) { + if (file.lazy) { + // Ignore lazy files unless we have a module system. + return; + } + const cacheKey = JSON.stringify([ file.sourceHash, file.bare, file.servePath]); @@ -147,9 +152,15 @@ _.extend(Module.prototype, { // Emit each file _.each(self.files, function (file) { + if (file.lazy) { + // Ignore lazy files unless we have a module system. + return; + } + if (!_.isEmpty(chunks)) { chunks.push("\n\n\n\n\n\n"); } + chunks.push(file.getPrelinkedOutput({ sourceWidth: sourceWidth, noLineNumbers: self.noLineNumbers @@ -244,6 +255,9 @@ var File = function (inputFile, module) { // the path where this file would prefer to be served if possible self.servePath = inputFile.servePath; + // True if the input file should not be evaluated eagerly. + self.lazy = !!inputFile.lazy; + // If true, don't wrap this individual file in a closure. self.bare = !!inputFile.bare; diff --git a/tools/isobuild/package-source.js b/tools/isobuild/package-source.js index 0f3ad368a7..2f2a88d3f5 100644 --- a/tools/isobuild/package-source.js +++ b/tools/isobuild/package-source.js @@ -1176,8 +1176,41 @@ _.extend(PackageSource.prototype, { arch: arch, uses: api.uses[arch], implies: api.implies[arch], - getFiles() { - return api.files[arch]; + getFiles(sourceProcessorSet, watchSet) { + const result = api.files[arch]; + const relPathToSourceObj = {}; + const sources = result.sources; + + // Files explicitly passed to api.addFiles remain at the + // beginning of api.files[arch].sources in their given order. + sources.forEach(sourceObj => { + relPathToSourceObj[sourceObj.relPath] = sourceObj; + }); + + self._findSources({ + sourceProcessorSet, + watchSet, + arch, + isApp: false + }).forEach(relPath => { + if (! _.has(relPathToSourceObj, relPath)) { + const fileOptions = self._inferFileOptions(relPath, { + arch, + isApp: false, + }); + + // Since this file was not explicitly added with + // api.addFiles, it should not be evaluated eagerly. + fileOptions.lazy = true; + + sources.push(relPathToSourceObj[relPath] = { + relPath, + fileOptions, + }); + } + }); + + return result; }, declaredExports: api.exports[arch], watchSet: watchSet