Skip to content

Commit c2ba1d6

Browse files
committed
updating shared-entry-contact-plugin code for emit
1 parent fbf6121 commit c2ba1d6

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

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: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
'use strict';
1111

1212
const fs = require('fs');
13-
const path = require('path');
1413
const sharedEntryTmpName = require('../utils/sharedEntryTmpName');
14+
const RawSource = require('webpack-sources/lib/RawSource');
1515

16-
function SharedEntryConcatPlugin(sharedEntryName, buildDir) {
16+
function SharedEntryConcatPlugin(sharedEntryName) {
1717
this.sharedEntryName = sharedEntryName;
18-
this.buildDir = buildDir;
1918
}
2019

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

2423
if (!chunk) {
2524
throw new Error(`Cannot find chunk ${chunkName}`);
@@ -36,8 +35,21 @@ function getChunkFilename(stats, chunkName) {
3635
return jsFiles[0];
3736
}
3837

38+
/**
39+
* @param {Source} asset
40+
* @return {string}
41+
*/
42+
function getAssetSource(asset) {
43+
let content = asset.source();
44+
if (Buffer.isBuffer(content)) {
45+
content = Buffer.toString('utf-8');
46+
}
47+
48+
return content;
49+
}
50+
3951
SharedEntryConcatPlugin.prototype.apply = function(compiler) {
40-
const emit = (stats) => {
52+
const emit = (compilation) => {
4153
/*
4254
* This is a hack. See ConfigGenerator.buildEntryConfig()
4355
* for other details.
@@ -55,23 +67,26 @@ SharedEntryConcatPlugin.prototype.apply = function(compiler) {
5567
* executed. This fixes that.
5668
*/
5769

58-
const sharedEntryOutputFile = path.join(this.buildDir, getChunkFilename(stats, this.sharedEntryName));
59-
const tmpEntryBootstrapFile = path.join(this.buildDir, getChunkFilename(stats, sharedEntryTmpName));
70+
const sharedEntryOutputFile = getChunkFilename(compilation, this.sharedEntryName);
71+
const tmpEntryFile = getChunkFilename(compilation, sharedEntryTmpName);
72+
const assets = compilation.assets;
73+
74+
const sharedEntryAsset = assets[sharedEntryOutputFile];
75+
const tmpEntryAsset = assets[tmpEntryFile];
6076

61-
if (!fs.existsSync(sharedEntryOutputFile)) {
77+
if (typeof sharedEntryAsset === 'undefined') {
6278
throw new Error(`Could not find shared entry output file: ${sharedEntryOutputFile}`);
6379
}
6480

65-
if (!fs.existsSync(tmpEntryBootstrapFile)) {
66-
throw new Error(`Could not find temporary shared entry bootstrap file: ${tmpEntryBootstrapFile}`);
81+
if (typeof assets[tmpEntryFile] === 'undefined') {
82+
throw new Error(`Could not find temporary shared entry bootstrap file: ${tmpEntryFile}`);
6783
}
6884

69-
fs.writeFileSync(
70-
sharedEntryOutputFile,
71-
[fs.readFileSync(sharedEntryOutputFile), fs.readFileSync(tmpEntryBootstrapFile)].join('\n')
85+
assets[sharedEntryOutputFile] = new RawSource(
86+
[getAssetSource(sharedEntryAsset), getAssetSource(tmpEntryAsset)].join('\n')
7287
);
7388

74-
fs.unlinkSync(tmpEntryBootstrapFile);
89+
delete(assets[tmpEntryFile]);
7590
};
7691

7792
compiler.hooks.emit.tap(

package.json

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

0 commit comments

Comments
 (0)