var archinfo = require('../utils/archinfo'); var buildmessage = require('../utils/buildmessage.js'); var buildPluginModule = require('./build-plugin.js'); var colonConverter = require('../utils/colon-converter.js'); var files = require('../fs/files'); var compiler = require('./compiler.js'); var linker = require('./linker.js'); var _ = require('underscore'); var Profile = require('../tool-env/profile').Profile; import assert from "assert"; import {readAndWatchFileWithHash, sha1, WatchSet,} from '../fs/watch'; import LRUCache from 'lru-cache'; import {sourceMapLength} from '../utils/utils.js'; import {Console} from '../console/console.js'; import ImportScanner from './import-scanner'; import {cssToCommonJS} from "./css-modules"; import Resolver from "./resolver"; import {optimisticHashOrNull, optimisticStatOrNull,} from "../fs/optimistic"; import {isTestFilePath} from './test-files.js'; const hasOwn = Object.prototype.hasOwnProperty; // This file implements the new compiler plugins added in Meteor 1.2, which are // registered with the Plugin.registerCompiler API. // // Unlike legacy source handlers (Plugin.registerSourceHandler), compilers run // in the context of an entire app. That is to say, they don't run when you run // `meteor publish`; whenever they run, they have access to all the files of // their type across all packages as well as the app. This allows them to // implement cross-file and cross-package inclusion, or config files in the app // that affect how packages are processed, among other possibilities. // // Compilers can specify which extensions or filenames they process. They only // process files in packages (or the app) that directly use the plugin's package // (or that use it indirectly via the "imply" directive); just because compiler // plugins act on multiple packages at a time doesn't mean they automatically // act on all packages in your app. // // The CompilerPluginProcessor is the main entry point to this file; it is used // by the bundler to run all plugins on a target. It doesn't have much // interesting state and perhaps could have just been a function. // // It receives an ordered list of unibuilds (essentially, packages) from the // bundler. It turns them into an ordered list of PackageSourceBatch objects, // each of which represents the source files in a single package. Each // PackageSourceBatch consists of an ordered list of ResourceSlots representing // the resources in that package. The idea here is that, because Meteor executes // all JS files in the order produced by the bundler, we need to make sure to // maintain the order of packages from the bundler and the order of source files // within a package. Each ResourceSlot represents a resource (either a 'source' // resource which will be processed by a compiler plugin, or something else like // a static asset or some JavaScript produced by a legacy source handler), and // when the compiler plugin calls something like `inputFile.addJavaScript` on a // file, we replace that source file with the resource produced by the plugin. // // InputFile is a wrapper around ResourceSlot that is the object presented to // the compiler in the plugin. It is part of the documented registerCompiler // API. // Cache the (slightly post-processed) results of linker.fullLink. const CACHE_SIZE = process.env.METEOR_LINKER_CACHE_SIZE || 1024*1024*100; const CACHE_DEBUG = !! process.env.METEOR_TEST_PRINT_LINKER_CACHE_DEBUG; const LINKER_CACHE_SALT = 26; // Increment this number to force relinking. const LINKER_CACHE = new LRUCache({ max: CACHE_SIZE, // Cache is measured in bytes. We don't care about servePath. // Key is JSONification of all options plus all hashes. length (files) { return files.reduce((soFar, current) => { return soFar + current.data.length + sourceMapLength(current.sourceMap); }, 0); } }); const serverLibPackages = { // Make sure fibers is defined, if nothing else. fibers: true }; function populateServerLibPackages() { const devBundlePath = files.getDevBundle(); const nodeModulesPath = files.pathJoin( devBundlePath, "server-lib", "node_modules" ); files.readdir(nodeModulesPath).forEach(packageName => { const packagePath = files.pathJoin(nodeModulesPath, packageName); const packageStat = files.statOrNull(packagePath); if (packageStat && packageStat.isDirectory()) { serverLibPackages[packageName] = true; } }); } try { populateServerLibPackages(); } catch (e) { // At least we tried! } export class CompilerPluginProcessor { constructor({ unibuilds, arch, sourceRoot, buildMode, isopackCache, linkerCacheDir, scannerCacheDir, minifyCssResource, }) { Object.assign(this, { unibuilds, arch, sourceRoot, buildMode, isopackCache, linkerCacheDir, scannerCacheDir, minifyCssResource, }); if (linkerCacheDir) { files.mkdir_p(linkerCacheDir); } if (scannerCacheDir) { files.mkdir_p(scannerCacheDir); } } async runCompilerPlugins() { const self = this; buildmessage.assertInJob(); // plugin id -> {sourceProcessor, resourceSlots} var sourceProcessorsWithSlots = {}; const sourceBatches = []; for (const unibuild of self.unibuilds) { const { pkg: { name }, arch } = unibuild; const sourceRoot = name && self.isopackCache.getSourceRoot(name, arch) || self.sourceRoot; const batch = new PackageSourceBatch(unibuild, self, { sourceRoot, linkerCacheDir: self.linkerCacheDir, scannerCacheDir: self.scannerCacheDir, }); await batch.init(); sourceBatches.push(batch); } // If we failed to match sources with processors, we're done. if (buildmessage.jobHasMessages()) { return []; } // Find out which files go with which CompilerPlugins. _.each(sourceBatches, function (sourceBatch) { _.each(sourceBatch.resourceSlots, function (resourceSlot) { var sourceProcessor = resourceSlot.sourceProcessor; // Skip non-sources. if (! sourceProcessor) { return; } if (! _.has(sourceProcessorsWithSlots, sourceProcessor.id)) { sourceProcessorsWithSlots[sourceProcessor.id] = { sourceProcessor: sourceProcessor, resourceSlots: [] }; } sourceProcessorsWithSlots[sourceProcessor.id].resourceSlots.push( resourceSlot); }); }); // Now actually run the handlers for (const [id, data] of Object.entries(sourceProcessorsWithSlots)) { var sourceProcessor = data.sourceProcessor; var resourceSlots = data.resourceSlots; var jobTitle = [ "processing files with ", sourceProcessor.isopack.name, " (for target ", self.arch, ")" ].join(''); await Profile.time("plugin "+sourceProcessor.isopack.name, async () => { await buildmessage.enterJob({ title: jobTitle }, async function () { var inputFiles = resourceSlots.map(resourceSlot => new InputFile(resourceSlot)); const markedMethod = buildmessage.markBoundary( sourceProcessor.userPlugin.processFilesForTarget, sourceProcessor.userPlugin ); try { await markedMethod(inputFiles); } catch (e) { buildmessage.exception(e); } }); }); } return sourceBatches; } } class InputFile extends buildPluginModule.InputFile { constructor(resourceSlot) { super(); // We use underscored attributes here because this is user-visible // code and we don't want users to be accessing anything that we don't // document. this._resourceSlot = resourceSlot; // Map from absolute paths to stat objects (or null if the file does // not exist). this._statCache = Object.create(null); // Map from control file names (e.g. package.json, .babelrc) to // absolute paths, or null to indicate absence. this._controlFileCache = Object.create(null); // Map from imported module identifier strings (possibly relative) to // fully require.resolve'd module identifiers. this._resolveCache = Object.create(null); // Communicate to compiler plugins that methods like addJavaScript // accept a lazy finalizer function as a second argument, so that // compilation can be avoided until/unless absolutely necessary. this.supportsLazyCompilation = true; // Communicate to compiler plugins that this version of Meteor // is able to support top level await // TODO: maybe this should also check if the file and package meet the // minimum requirements to use top level await (file isn't bare, and // package uses core-runtime and modules) this.supportsTopLevelAwait = true; } getContentsAsBuffer() { var self = this; return self._resourceSlot.inputResource.data; } getPackageName() { var self = this; return self._resourceSlot.packageSourceBatch.unibuild.pkg.name; } isPackageFile() { return !! this.getPackageName(); } isApplicationFile() { return ! this.getPackageName(); } getSourceRoot(tolerant = false) { const sourceRoot = this._resourceSlot.packageSourceBatch.sourceRoot; if (_.isString(sourceRoot)) { return sourceRoot; } if (! tolerant) { const name = this.getPackageName(); throw new Error( "Unknown source root for " + ( name ? "package " + name : "app")); } return null; } getPathInPackage() { var self = this; return self._resourceSlot.inputResource.path; } getFileOptions() { // XXX fileOptions only exists on some resources (of type "source"). The JS // resources might not have this property. const { inputResource } = this._resourceSlot; return inputResource.fileOptions || (inputResource.fileOptions = {}); } hmrAvailable() { const fileOptions = this.getFileOptions() || {}; return this._resourceSlot.hmrAvailable() && !fileOptions.bare; } readAndWatchFileWithHash(path) { const sourceBatch = this._resourceSlot.packageSourceBatch; return readAndWatchFileWithHash( sourceBatch.unibuild.watchSet, files.convertToPosixPath(path), ); } readAndWatchFile(path) { return this.readAndWatchFileWithHash(path).contents; } _stat(absPath) { return _.has(this._statCache, absPath) ? this._statCache[absPath] : this._statCache[absPath] = optimisticStatOrNull(absPath); } // Search ancestor directories for control files (e.g. package.json, // .babelrc), and return the absolute path of the first one found, or // null if the search failed. findControlFile(basename) { let absPath = this._controlFileCache[basename]; if (typeof absPath === "string") { return absPath; } const sourceRoot = this.getSourceRoot(true); if (! _.isString(sourceRoot)) { return this._controlFileCache[basename] = null; } let dir = files.pathDirname( files.pathJoin(sourceRoot, this.getPathInPackage())); while (true) { absPath = files.pathJoin(dir, basename); const stat = this._stat(absPath); if (stat && stat.isFile()) { return this._controlFileCache[basename] = absPath; } if (files.pathBasename(dir) === "node_modules") { // The search for control files should not escape node_modules. return this._controlFileCache[basename] = null; } if (dir === sourceRoot) break; let parentDir = files.pathDirname(dir); if (parentDir === dir) break; dir = parentDir; } return this._controlFileCache[basename] = null; } _resolveCacheLookup(id, parentPath) { const byId = this._resolveCache[id]; return byId && byId[parentPath]; } _resolveCacheStore(id, parentPath, resolved) { let byId = this._resolveCache[id]; if (! byId) { byId = this._resolveCache[id] = Object.create(null); } return byId[parentPath] = resolved; } resolve(id, parentPath) { parentPath = parentPath || files.pathJoin( this.getSourceRoot(), this.getPathInPackage() ); const resId = this._resolveCacheLookup(id, parentPath); if (resId) { return resId; } const parentStat = optimisticStatOrNull(parentPath); if (! parentStat || ! parentStat.isFile()) { throw new Error("Not a file: " + parentPath); } const batch = this._resourceSlot.packageSourceBatch; const resolver = batch.getResolver({ // Make sure we use a server architecture when resolving, so that we // don't accidentally use package.json "browser" fields. // https://github.com/meteor/meteor/issues/9870 targetArch: archinfo.host(), }); const resolved = resolver.resolve(id, parentPath); if (resolved === "missing") { const error = new Error("Cannot find module '" + id + "'"); error.code = "MODULE_NOT_FOUND"; throw error; } return this._resolveCacheStore(id, parentPath, resolved.id); } require(id, parentPath) { return this._require(id, parentPath); } // This private helper method exists to prevent ambiguity between the // module-global `require` function and the method name. _require(id, parentPath) { return require(this.resolve(id, parentPath)); } getArch() { return this._resourceSlot.packageSourceBatch.processor.arch; } getSourceHash() { return this._resourceSlot.inputResource.hash; } /** * @summary Returns the extension that matched the compiler plugin. * The longest prefix is preferred. * @returns {String} */ getExtension() { return this._resourceSlot.inputResource.extension; } /** * @summary Returns a list of symbols declared as exports in this target. The * result of `api.export('symbol')` calls in target's control file such as * package.js. * @memberof InputFile * @returns {String[]} */ getDeclaredExports() { var self = this; return self._resourceSlot.packageSourceBatch.unibuild.declaredExports; } /** * @summary Returns a relative path that can be used to form error messages or * other display properties. Can be used as an input to a source map. * @memberof InputFile * @returns {String} */ getDisplayPath() { var self = this; return self._resourceSlot.packageSourceBatch.unibuild.pkg._getServePath(self.getPathInPackage()); } /** * @summary Web targets only. Add a stylesheet to the document. Not available * for linter build plugins. * @param {Object} options * @param {String} options.path The requested path for the added CSS, may not * be satisfied if there are path conflicts. * @param {String} options.data The content of the stylesheet that should be * added. * @param {String|Object} options.sourceMap A stringified JSON * sourcemap, in case the stylesheet was generated from a different * file. * @param {Function} lazyFinalizer Optional function that can be called * to obtain any remaining options that may be * expensive to compute, and thus should only be * computed if/when we are sure this CSS will be used * by the application. * @memberOf InputFile * @instance */ addStylesheet(options, lazyFinalizer) { this._resourceSlot.addStylesheet(options, lazyFinalizer); } /** * @summary Add JavaScript code. The code added will only see the * namespaces imported by this package as runtime dependencies using * ['api.use'](#PackageAPI-use). If the file being compiled was added * with the bare flag, the resulting JavaScript won't be wrapped in a * closure. * @param {Object} options * @param {String} options.path The path at which the JavaScript file * should be inserted, may not be honored in case of path conflicts. * @param {String} options.data The code to be added. * @param {String|Object} options.sourceMap A stringified JSON * sourcemap, in case the JavaScript file was generated from a * different file. * @param {Function} lazyFinalizer Optional function that can be called * to obtain any remaining options that may be * expensive to compute, and thus should only be * computed if/when we are sure this JavaScript will * be used by the application. * @memberOf InputFile * @instance */ addJavaScript(options, lazyFinalizer) { this._resourceSlot.addJavaScript(options, lazyFinalizer); } /** * @summary Add a file to serve as-is to the browser or to include on * the browser, depending on the target. On the web, it will be served * at the exact path requested. For server targets, it can be retrieved * using `Assets.getText` or `Assets.getBinary`. * @param {Object} options * @param {String} options.path The path at which to serve the asset. * @param {Buffer|String} options.data The data that should be placed in the * file. * @param {String} [options.hash] Optionally, supply a hash for the output * file. * @param {Function} lazyFinalizer Optional function that can be called * to obtain any remaining options that may be * expensive to compute, and thus should only be * computed if/when we are sure this asset will be * used by the application. * @memberOf InputFile * @instance */ addAsset(options, lazyFinalizer) { this._resourceSlot.addAsset(options, lazyFinalizer); } /** * @summary Works in web targets only. Add markup to the `head` or `body` * section of the document. * @param {Object} options * @param {String} options.section Which section of the document should * be appended to. Can only be "head" or "body". * @param {String} options.data The content to append. * @param {Function} lazyFinalizer Optional function that can be called * to obtain any remaining options that may be * expensive to compute, and thus should only be * computed if/when we are sure this HTML will be used * by the application. * @memberOf InputFile * @instance */ async addHtml(options, lazyFinalizer) { if (typeof lazyFinalizer === "function") { // For now, just call the lazyFinalizer function immediately. Since // HTML is not compiled, this immediate invocation is probably // permanently appropriate for addHtml, whereas methods like // addJavaScript benefit from waiting to call lazyFinalizer. Object.assign(options, await lazyFinalizer()); } this._resourceSlot.addHtml(options); } _reportError(message, info) { this._resourceSlot.addError(message, info); if (! this.getFileOptions().lazy) { super._reportError(message, info); } } } class ResourceSlot { constructor(unibuildResourceInfo, sourceProcessor, packageSourceBatch) { const self = this; // XXX ideally this should be an classy object, but it's not. self.inputResource = unibuildResourceInfo; // Everything but JS. self.outputResources = []; // JS, which gets linked together at the end. self.jsOutputResources = []; // Errors encountered while processing this resource. self.errors = []; self.sourceProcessor = sourceProcessor; self.packageSourceBatch = packageSourceBatch; if (self.inputResource.type === "source") { if (self.sourceProcessor) { // If we have a sourceProcessor, it will handle the adding of the // final processed JavaScript. } else if (self.inputResource.extension === "js") { self._addDirectlyToJsOutputResources(); } } else { if (self.sourceProcessor) { throw Error("sourceProcessor for non-source? " + JSON.stringify(self.inputResource)); } // Any resource that isn't handled by compiler plugins just gets passed // through. if (self.inputResource.type === "js") { self._addDirectlyToJsOutputResources(); } else { self.outputResources.push(self.inputResource); } } } // Add this resource directly to jsOutputResources without modifying the // original data. #HardcodeJs _addDirectlyToJsOutputResources() { this.addJavaScript({ ...(this.inputResource.fileOptions || {}), path: this.inputResource.path, data: this.inputResource.data, }); } _getOption(name, options) { if (options && _.has(options, name)) { return options[name]; } const fileOptions = this.inputResource.fileOptions; return fileOptions && fileOptions[name]; } _isLazy(options, isJavaScript) { let lazy = this._getOption("lazy", options); if (typeof lazy === "boolean") { return lazy; } const isApp = ! this.packageSourceBatch.unibuild.pkg.name; if (! isApp) { // Meteor package files must be explicitly added by api.addFiles or // api.mainModule, and are implicitly eager unless specified // otherwise via this.inputResource.fileOptions.lazy, which we // already checked above. return false; } // The rest of this method assumes we're considering a resource in an // application rather than a Meteor package. if (! this.packageSourceBatch.useMeteorInstall) { // If this application is somehow still not using the module system, // then everything is eagerly loaded. return false; } const { isTest = false, isAppTest = false, } = global.testCommandMetadata || {}; const runningTests = isTest || isAppTest; if (isJavaScript) { if (runningTests) { const testModule = this._getOption("testModule", options); // If we set fileOptions.testModule = true in _inferFileOptions, // then consider this module an eager entry point for tests. If we // set it to false (rather than leaving it undefined), that means // a meteor.testModule was configured in package.json, and this // test module was not it. In that case, we fall through to the // mainModule check, ignoring isTestFilePath, because we can // assume this is not an eager test module. If testModule was not // set to a boolean, then isTestFilePath should determine if this // is an eager test module. const isEagerTestModule = typeof testModule === "boolean" ? testModule : isTestFilePath(this.inputResource.path); if (isEagerTestModule) { // If we know it's eager, then it isn't lazy. return false; } if (! isAppTest) { // If running `meteor test` without the --full-app option, then // any JS modules that are not eager test modules must be lazy. return true; } } // PackageSource#_inferFileOptions (in package-source.js) sets the // mainModule option to false to indicate that a meteor.mainModule // was configured for this architecture, but this module was not it. // It's important to wait until this point (ResourceSlot#_isLazy) to // make the final call, because we can finally tell whether the // output resource is JavaScript or not (non-JS resources are not // affected by the meteor.mainModule option). const mainModule = this._getOption("mainModule", options); if (typeof mainModule === "boolean") { return ! mainModule; } } // In other words, the imports directory remains relevant for non-JS // resources, and for JS resources in the absence of an explicit // meteor.mainModule configuration in package.json. const splitPath = this.inputResource.path.split(files.pathSep); const isInImports = splitPath.indexOf("imports") >= 0; return isInImports; } _isBare(options) { return !! this._getOption("bare", options); } hmrAvailable() { return this.packageSourceBatch.hmrAvailable; } addStylesheet(options, lazyFinalizer) { if (! this.sourceProcessor) { throw Error("addStylesheet on non-source ResourceSlot?"); } // In contrast to addJavaScript, CSS resources passed to addStylesheet // default to being eager (non-lazy). options.lazy = this._isLazy(options, false); const cssResource = new CssOutputResource({ resourceSlot: this, options, lazyFinalizer, }); if (this.packageSourceBatch.useMeteorInstall && cssResource.lazy) { // If the current packageSourceBatch supports modules, and this CSS // file is lazy, add it as a lazy JS module instead of adding it // unconditionally as a CSS resource, so that it can be imported // when needed. const jsResource = this.addJavaScript(options, async () => { const result = {}; let css = await this.packageSourceBatch.processor .minifyCssResource(cssResource); if (! css && typeof css !== "string") { // The minifier didn't do anything, so we should use the // original contents of cssResource.data. css = (await cssResource.data).toString("utf8"); if (cssResource.sourceMap) { // Add the source map as an asset, and append a // sourceMappingURL comment to the end of the CSS text that // will be dynamically inserted when/if this JS module is // evaluated at runtime. Note that this only happens when the // minifier did not modify the CSS, and thus does not happen // when we are building for production. const { servePath } = this.addAsset({ path: jsResource.targetPath + ".map.json", data: JSON.stringify(cssResource.sourceMap) }); css += "\n//# sourceMappingURL=" + servePath + "\n"; } } result.data = Buffer.from(cssToCommonJS(css), "utf8"); // The JavaScript module that dynamically loads this CSS should // not inherit the source map of the original CSS output. result.sourceMap = null; return result; }); } else { // Eager CSS is added unconditionally to a combined