Skip to content

Commit 891c882

Browse files
author
sky
committed
fix: async chunk write disk for dynamic import
1 parent 7a9f8c7 commit 891c882

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

config/plugin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
'use strict';
2+
const ReactSSRDynamicChunkPlugin = require('./plugin/react-ssr-dynamic-chunk-webpack-plugin');
3+
exports.vuessrchunk = {
4+
type: ['server'],
5+
name: new ReactSSRDynamicChunkPlugin(),
6+
args: {
7+
}
8+
};
29

310
exports.modulereplacement = false;
411

512
exports.ignore = false;
13+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
const path = require('path');
3+
const fs = require('fs');
4+
const mkdirp = require('mkdirp');
5+
6+
class ReactSSRDynamicChunkPlugin {
7+
constructor(opts) {
8+
this.opts = Object.assign({ }, { chunk: true }, opts);
9+
}
10+
11+
apply(compiler) {
12+
compiler.hooks.emit.tap('ReactSSRDynamicChunkPlugin', (compilation, callback) => {
13+
const buildPath = compilation.options.output.path;
14+
15+
if (!fs.existsSync(buildPath)) {
16+
mkdirp.sync(buildPath);
17+
}
18+
19+
compilation.chunks.forEach(chunk => {
20+
if (!this.opts.chunk) {
21+
return;
22+
}
23+
24+
const asyncChunks = chunk.getAllAsyncChunks();
25+
26+
asyncChunks && asyncChunks.forEach(asyncChunk => {
27+
asyncChunk.files.forEach(filename => {
28+
const filepath = path.join(buildPath, filename);
29+
const source = compilation.assets[filename].source();
30+
fs.writeFileSync(filepath, source, 'utf8');
31+
});
32+
})
33+
});
34+
callback && callback();
35+
});
36+
}
37+
}
38+
39+
module.exports = ReactSSRDynamicChunkPlugin;

0 commit comments

Comments
 (0)