Skip to content

Commit 0054f3a

Browse files
committed
bug #441 Appending the _tmp_shared contents via an earlier hook (weaverryan)
This PR was squashed before being merged into the master branch (closes #441). Discussion ---------- Appending the _tmp_shared contents via an earlier hook Fixes #435 and fixes #434 Commits ------- 42ec9ef Using a static, not random filename for the _tmp_shared entry c2ba1d6 updating shared-entry-contact-plugin code for emit fbf6121 removing non-existent method on this hook 5f9d2ee Appending the _tmp_shared contents via an earlier hook
2 parents 019fe8c + 42ec9ef commit 0054f3a

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

lib/config-generator.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const path = require('path');
4949
const stringEscaper = require('./utils/string-escaper');
5050
const crypto = require('crypto');
5151
const logger = require('./logger');
52+
const os = require('os');
5253

5354
class ConfigGenerator {
5455
/**
@@ -132,14 +133,19 @@ class ConfigGenerator {
132133
*
133134
* See shared-entry-concat-plugin.js for more details.
134135
*/
135-
const tmpFileObject = tmp.fileSync();
136+
const staticHashKey = crypto
137+
.createHash('md4')
138+
.update(this.webpackConfig.outputPath)
139+
.digest('hex')
140+
.slice(0, 8);
141+
const tmpFilename = path.join(os.tmpdir(), '_webpack_encore_tmp_module' + staticHashKey + '.js');
136142
const pathToRequire = path.resolve(this.webpackConfig.getContext(), this.webpackConfig.sharedCommonsEntryFile);
137143
fs.writeFileSync(
138-
tmpFileObject.name,
144+
tmpFilename,
139145
`require('${stringEscaper(pathToRequire)}')`
140146
);
141147

142-
entry[sharedEntryTmpName] = tmpFileObject.name;
148+
entry[sharedEntryTmpName] = tmpFilename;
143149
}
144150

145151
if (this.webpackConfig.copyFilesConfigs.length > 0) {

lib/plugins/shared-entry-concat.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ module.exports = function(plugins, webpackConfig) {
2424

2525
plugins.push({
2626
plugin: new SharedEntryConcatPlugin(
27-
webpackConfig.sharedCommonsEntryName,
28-
webpackConfig.outputPath
27+
webpackConfig.sharedCommonsEntryName
2928
),
3029
priority: PluginPriorities.SharedEntryContactPlugin
3130
});

lib/webpack/shared-entry-concat-plugin.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99

1010
'use strict';
1111

12-
const fs = require('fs');
13-
const path = require('path');
1412
const sharedEntryTmpName = require('../utils/sharedEntryTmpName');
13+
const RawSource = require('webpack-sources/lib/RawSource');
1514

16-
function SharedEntryConcatPlugin(sharedEntryName, buildDir) {
15+
function SharedEntryConcatPlugin(sharedEntryName) {
1716
this.sharedEntryName = sharedEntryName;
18-
this.buildDir = buildDir;
1917
}
2018

21-
function getChunkFilename(stats, chunkName) {
22-
const chunk = stats.compilation.namedChunks.get(chunkName);
19+
function getChunkFilename(compilation, chunkName) {
20+
const chunk = compilation.namedChunks.get(chunkName);
2321

2422
if (!chunk) {
2523
throw new Error(`Cannot find chunk ${chunkName}`);
@@ -36,12 +34,21 @@ function getChunkFilename(stats, chunkName) {
3634
return jsFiles[0];
3735
}
3836

39-
SharedEntryConcatPlugin.prototype.apply = function(compiler) {
40-
const done = (stats) => {
41-
if (stats.hasErrors()) {
42-
return;
43-
}
37+
/**
38+
* @param {Source} asset
39+
* @return {string}
40+
*/
41+
function getAssetSource(asset) {
42+
let content = asset.source();
43+
if (Buffer.isBuffer(content)) {
44+
content = Buffer.toString('utf-8');
45+
}
4446

47+
return content;
48+
}
49+
50+
SharedEntryConcatPlugin.prototype.apply = function(compiler) {
51+
const emit = (compilation) => {
4552
/*
4653
* This is a hack. See ConfigGenerator.buildEntryConfig()
4754
* for other details.
@@ -59,28 +66,31 @@ SharedEntryConcatPlugin.prototype.apply = function(compiler) {
5966
* executed. This fixes that.
6067
*/
6168

62-
const sharedEntryOutputFile = path.join(this.buildDir, getChunkFilename(stats, this.sharedEntryName));
63-
const tmpEntryBootstrapFile = path.join(this.buildDir, getChunkFilename(stats, sharedEntryTmpName));
69+
const sharedEntryOutputFile = getChunkFilename(compilation, this.sharedEntryName);
70+
const tmpEntryFile = getChunkFilename(compilation, sharedEntryTmpName);
71+
const assets = compilation.assets;
72+
73+
const sharedEntryAsset = assets[sharedEntryOutputFile];
74+
const tmpEntryAsset = assets[tmpEntryFile];
6475

65-
if (!fs.existsSync(sharedEntryOutputFile)) {
76+
if (typeof sharedEntryAsset === 'undefined') {
6677
throw new Error(`Could not find shared entry output file: ${sharedEntryOutputFile}`);
6778
}
6879

69-
if (!fs.existsSync(tmpEntryBootstrapFile)) {
70-
throw new Error(`Could not find temporary shared entry bootstrap file: ${tmpEntryBootstrapFile}`);
80+
if (typeof assets[tmpEntryFile] === 'undefined') {
81+
throw new Error(`Could not find temporary shared entry bootstrap file: ${tmpEntryFile}`);
7182
}
7283

73-
fs.writeFileSync(
74-
sharedEntryOutputFile,
75-
[fs.readFileSync(sharedEntryOutputFile), fs.readFileSync(tmpEntryBootstrapFile)].join('\n')
84+
assets[sharedEntryOutputFile] = new RawSource(
85+
[getAssetSource(sharedEntryAsset), getAssetSource(tmpEntryAsset)].join('\n')
7686
);
7787

78-
fs.unlinkSync(tmpEntryBootstrapFile);
88+
delete(assets[tmpEntryFile]);
7989
};
8090

81-
compiler.hooks.done.tap(
91+
compiler.hooks.emit.tap(
8292
{ name: 'SharedEntryConcatPlugin' },
83-
done
93+
emit
8494
);
8595
};
8696

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"webpack-cli": "^3.0.0",
5454
"webpack-dev-server": "^3.1.4",
5555
"webpack-manifest-plugin": "^2.0.2",
56+
"webpack-sources": "^1.3.0",
5657
"yargs": "^8.0.1"
5758
},
5859
"devDependencies": {

0 commit comments

Comments
 (0)