diff --git a/tools/isobuild/bundler.js b/tools/isobuild/bundler.js index fc71b2ca59..87b079f87f 100644 --- a/tools/isobuild/bundler.js +++ b/tools/isobuild/bundler.js @@ -1354,7 +1354,17 @@ class Target { inputHashesByJsFile.set(jsf, file.hash()); if (file.targetPath.startsWith("dynamic/")) { + // Dynamic files consist of a single anonymous function + // expression, which some minifiers (e.g. UglifyJS) either fail to + // parse or mistakenly eliminate as dead code. To avoid these + // problems, we temporarily name the function __minifyJs. + file._contents = Buffer.concat([ + MINIFY_RENAMED_FUNCTION, + file.contents().slice(MINIFY_PLAIN_FUNCTION.length) + ]); + dynamicFiles.push(jsf); + } else { staticFiles.push(jsf); } @@ -1382,8 +1392,19 @@ class Target { function handle(source, dynamic) { source._minifiedFiles.forEach(file => { - if (typeof file.data === "string") { - file.data = Buffer.from(file.data, "utf8"); + // Remove the function name __minifyJs that was added above. + if (typeof file.data === 'string') { + file.data = Buffer.from( + file.data + .replace(/^\s*function\s+__minifyJs\s*\(/, + "function("), + "utf8" + ); + } else if (dynamic) { + file.data = Buffer.concat([ + MINIFY_PLAIN_FUNCTION, + file.data.slice(MINIFY_RENAMED_FUNCTION.length) + ]); } const newFile = new File({ diff --git a/tools/isobuild/linker.js b/tools/isobuild/linker.js index 2450ca09f7..61de162f30 100644 --- a/tools/isobuild/linker.js +++ b/tools/isobuild/linker.js @@ -685,7 +685,7 @@ _.extend(File.prototype, { _getClosureHeader() { if (this.meteorInstallOptions) { - const headerParts = ["function module("]; + const headerParts = ["function("]; if (this.source.match(/\b__dirname\b/)) { headerParts.push("require,exports,module,__filename,__dirname");