From 5f9d2eed47fa5121ccc3f5ecd1cbff276c0e8ca9 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 9 Nov 2018 11:14:29 -0500 Subject: [PATCH 1/4] Appending the _tmp_shared contents via an earlier hook --- lib/webpack/shared-entry-concat-plugin.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/webpack/shared-entry-concat-plugin.js b/lib/webpack/shared-entry-concat-plugin.js index fc48fb4a..0350cd89 100644 --- a/lib/webpack/shared-entry-concat-plugin.js +++ b/lib/webpack/shared-entry-concat-plugin.js @@ -37,7 +37,7 @@ function getChunkFilename(stats, chunkName) { } SharedEntryConcatPlugin.prototype.apply = function(compiler) { - const done = (stats) => { + const emit = (stats) => { if (stats.hasErrors()) { return; } @@ -78,9 +78,9 @@ SharedEntryConcatPlugin.prototype.apply = function(compiler) { fs.unlinkSync(tmpEntryBootstrapFile); }; - compiler.hooks.done.tap( + compiler.hooks.emit.tap( { name: 'SharedEntryConcatPlugin' }, - done + emit ); }; From fbf61214f044b88a2612a496b8d90408112724ab Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 9 Nov 2018 11:23:11 -0500 Subject: [PATCH 2/4] removing non-existent method on this hook --- lib/webpack/shared-entry-concat-plugin.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/webpack/shared-entry-concat-plugin.js b/lib/webpack/shared-entry-concat-plugin.js index 0350cd89..eadf7228 100644 --- a/lib/webpack/shared-entry-concat-plugin.js +++ b/lib/webpack/shared-entry-concat-plugin.js @@ -38,10 +38,6 @@ function getChunkFilename(stats, chunkName) { SharedEntryConcatPlugin.prototype.apply = function(compiler) { const emit = (stats) => { - if (stats.hasErrors()) { - return; - } - /* * This is a hack. See ConfigGenerator.buildEntryConfig() * for other details. From c2ba1d67153b3bc321a8c32ae9cef7eb175a9bb6 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 9 Nov 2018 11:48:52 -0500 Subject: [PATCH 3/4] updating shared-entry-contact-plugin code for emit --- lib/plugins/shared-entry-concat.js | 3 +- lib/webpack/shared-entry-concat-plugin.js | 45 +++++++++++++++-------- package.json | 1 + 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/lib/plugins/shared-entry-concat.js b/lib/plugins/shared-entry-concat.js index c49def11..ce1e9f42 100644 --- a/lib/plugins/shared-entry-concat.js +++ b/lib/plugins/shared-entry-concat.js @@ -24,8 +24,7 @@ module.exports = function(plugins, webpackConfig) { plugins.push({ plugin: new SharedEntryConcatPlugin( - webpackConfig.sharedCommonsEntryName, - webpackConfig.outputPath + webpackConfig.sharedCommonsEntryName ), priority: PluginPriorities.SharedEntryContactPlugin }); diff --git a/lib/webpack/shared-entry-concat-plugin.js b/lib/webpack/shared-entry-concat-plugin.js index eadf7228..07b4f6e0 100644 --- a/lib/webpack/shared-entry-concat-plugin.js +++ b/lib/webpack/shared-entry-concat-plugin.js @@ -10,16 +10,15 @@ 'use strict'; const fs = require('fs'); -const path = require('path'); const sharedEntryTmpName = require('../utils/sharedEntryTmpName'); +const RawSource = require('webpack-sources/lib/RawSource'); -function SharedEntryConcatPlugin(sharedEntryName, buildDir) { +function SharedEntryConcatPlugin(sharedEntryName) { this.sharedEntryName = sharedEntryName; - this.buildDir = buildDir; } -function getChunkFilename(stats, chunkName) { - const chunk = stats.compilation.namedChunks.get(chunkName); +function getChunkFilename(compilation, chunkName) { + const chunk = compilation.namedChunks.get(chunkName); if (!chunk) { throw new Error(`Cannot find chunk ${chunkName}`); @@ -36,8 +35,21 @@ function getChunkFilename(stats, chunkName) { return jsFiles[0]; } +/** + * @param {Source} asset + * @return {string} + */ +function getAssetSource(asset) { + let content = asset.source(); + if (Buffer.isBuffer(content)) { + content = Buffer.toString('utf-8'); + } + + return content; +} + SharedEntryConcatPlugin.prototype.apply = function(compiler) { - const emit = (stats) => { + const emit = (compilation) => { /* * This is a hack. See ConfigGenerator.buildEntryConfig() * for other details. @@ -55,23 +67,26 @@ SharedEntryConcatPlugin.prototype.apply = function(compiler) { * executed. This fixes that. */ - const sharedEntryOutputFile = path.join(this.buildDir, getChunkFilename(stats, this.sharedEntryName)); - const tmpEntryBootstrapFile = path.join(this.buildDir, getChunkFilename(stats, sharedEntryTmpName)); + const sharedEntryOutputFile = getChunkFilename(compilation, this.sharedEntryName); + const tmpEntryFile = getChunkFilename(compilation, sharedEntryTmpName); + const assets = compilation.assets; + + const sharedEntryAsset = assets[sharedEntryOutputFile]; + const tmpEntryAsset = assets[tmpEntryFile]; - if (!fs.existsSync(sharedEntryOutputFile)) { + if (typeof sharedEntryAsset === 'undefined') { throw new Error(`Could not find shared entry output file: ${sharedEntryOutputFile}`); } - if (!fs.existsSync(tmpEntryBootstrapFile)) { - throw new Error(`Could not find temporary shared entry bootstrap file: ${tmpEntryBootstrapFile}`); + if (typeof assets[tmpEntryFile] === 'undefined') { + throw new Error(`Could not find temporary shared entry bootstrap file: ${tmpEntryFile}`); } - fs.writeFileSync( - sharedEntryOutputFile, - [fs.readFileSync(sharedEntryOutputFile), fs.readFileSync(tmpEntryBootstrapFile)].join('\n') + assets[sharedEntryOutputFile] = new RawSource( + [getAssetSource(sharedEntryAsset), getAssetSource(tmpEntryAsset)].join('\n') ); - fs.unlinkSync(tmpEntryBootstrapFile); + delete(assets[tmpEntryFile]); }; compiler.hooks.emit.tap( diff --git a/package.json b/package.json index c5c025d4..f5abc338 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "webpack-cli": "^3.0.0", "webpack-dev-server": "^3.1.4", "webpack-manifest-plugin": "^2.0.2", + "webpack-sources": "^1.3.0", "yargs": "^8.0.1" }, "devDependencies": { From 42ec9ef9912b8b254aef582a8ea1fe0e741422dc Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 9 Nov 2018 12:04:00 -0500 Subject: [PATCH 4/4] Using a static, not random filename for the _tmp_shared entry This small, internal change was causing the final output code to be *slightly* different. --- lib/config-generator.js | 12 +++++++++--- lib/webpack/shared-entry-concat-plugin.js | 1 - 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 0c1927be..fed64aee 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -48,6 +48,7 @@ const path = require('path'); const stringEscaper = require('./utils/string-escaper'); const crypto = require('crypto'); const logger = require('./logger'); +const os = require('os'); class ConfigGenerator { /** @@ -131,14 +132,19 @@ class ConfigGenerator { * * See shared-entry-concat-plugin.js for more details. */ - const tmpFileObject = tmp.fileSync(); + const staticHashKey = crypto + .createHash('md4') + .update(this.webpackConfig.outputPath) + .digest('hex') + .slice(0, 8); + const tmpFilename = path.join(os.tmpdir(), '_webpack_encore_tmp_module' + staticHashKey + '.js'); const pathToRequire = path.resolve(this.webpackConfig.getContext(), this.webpackConfig.sharedCommonsEntryFile); fs.writeFileSync( - tmpFileObject.name, + tmpFilename, `require('${stringEscaper(pathToRequire)}')` ); - entry[sharedEntryTmpName] = tmpFileObject.name; + entry[sharedEntryTmpName] = tmpFilename; } if (this.webpackConfig.copyFilesConfigs.length > 0) { diff --git a/lib/webpack/shared-entry-concat-plugin.js b/lib/webpack/shared-entry-concat-plugin.js index 07b4f6e0..0f57f7b4 100644 --- a/lib/webpack/shared-entry-concat-plugin.js +++ b/lib/webpack/shared-entry-concat-plugin.js @@ -9,7 +9,6 @@ 'use strict'; -const fs = require('fs'); const sharedEntryTmpName = require('../utils/sharedEntryTmpName'); const RawSource = require('webpack-sources/lib/RawSource');