Skip to content

Commit ac17265

Browse files
arianrhodsandlotshellscape
authored andcommitted
fix: fixes #290 - MultiCompiler exception with writeToDisk
Fix the exception thrown when `writeToDisk` is enabled in multi-compiler mode (#301) * fix writeToDisk option in multi-compiler * rmdir -> rmdirSync * use a better solution of toDisk for multi-compilers * use a shorten method name
1 parent e0d4293 commit ac17265

File tree

3 files changed

+80
-30
lines changed

3 files changed

+80
-30
lines changed

lib/fs.js

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,45 @@ const { mkdirp } = new NodeOutputFileSystem();
1212

1313
module.exports = {
1414
toDisk(context) {
15-
context.compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => {
16-
const { assets, compiler } = compilation;
17-
const { log } = context;
18-
const { writeToDisk: filter } = context.options;
19-
let { outputPath } = compiler;
15+
const compilers = context.compiler.compilers || [context.compiler];
16+
for (const compiler of compilers) {
17+
compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => {
18+
const { assets } = compilation;
19+
const { log } = context;
20+
const { writeToDisk: filter } = context.options;
21+
let { outputPath } = compiler;
2022

21-
if (outputPath === '/') {
22-
outputPath = compiler.context;
23-
}
23+
if (outputPath === '/') {
24+
outputPath = compiler.context;
25+
}
2426

25-
for (const assetPath of Object.keys(assets)) {
26-
const asset = assets[assetPath];
27-
const source = asset.source();
28-
const isAbsolute = pathabs(assetPath);
29-
const writePath = isAbsolute ? assetPath : path.join(outputPath, assetPath);
30-
const relativePath = path.relative(process.cwd(), writePath);
31-
const allowWrite = filter && typeof filter === 'function' ? filter(writePath) : true;
27+
for (const assetPath of Object.keys(assets)) {
28+
const asset = assets[assetPath];
29+
const source = asset.source();
30+
const isAbsolute = pathabs(assetPath);
31+
const writePath = isAbsolute ? assetPath : path.join(outputPath, assetPath);
32+
const relativePath = path.relative(process.cwd(), writePath);
33+
const allowWrite = filter && typeof filter === 'function' ? filter(writePath) : true;
3234

33-
if (allowWrite) {
34-
let output = source;
35+
if (allowWrite) {
36+
let output = source;
3537

36-
mkdirp.sync(path.dirname(writePath));
38+
mkdirp.sync(path.dirname(writePath));
3739

38-
if (Array.isArray(source)) {
39-
output = source.join('\n');
40-
}
40+
if (Array.isArray(source)) {
41+
output = source.join('\n');
42+
}
4143

42-
try {
43-
fs.writeFileSync(writePath, output, 'utf-8');
44-
log.debug(chalk`{cyan Asset written to disk}: ${relativePath}`);
45-
} catch (e) {
46-
log.error(`Unable to write asset to disk:\n${e}`);
44+
try {
45+
fs.writeFileSync(writePath, output, 'utf-8');
46+
log.debug(chalk`{cyan Asset written to disk}: ${relativePath}`);
47+
} catch (e) {
48+
log.error(`Unable to write asset to disk:\n${e}`);
49+
}
4750
}
4851
}
49-
}
50-
});
52+
});
53+
}
5154
},
5255

5356
setFs(context, compiler) {

test/fixtures/server-test/webpack.array.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22

3+
const path = require('path');
4+
35
module.exports = [{
46
mode: 'development',
57
context: __dirname,
68
entry: './foo.js',
79
output: {
810
filename: 'foo.js',
9-
path: '/js1',
11+
path: path.resolve(__dirname, 'js1'),
1012
publicPath: '/js1/'
1113
},
1214
module: {
@@ -24,7 +26,7 @@ module.exports = [{
2426
entry: './bar.js',
2527
output: {
2628
filename: 'bar.js',
27-
path: '/js2',
29+
path: path.resolve(__dirname, 'js2'),
2830
publicPath: '/js2/'
2931
}
3032
}];

test/tests/server.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,49 @@ describe('Server', () => {
377377
});
378378
});
379379
});
380+
381+
function multiToDisk(value, done) {
382+
app = express();
383+
const compiler = webpack(webpackMultiConfig);
384+
instance = middleware(compiler, {
385+
stats: 'errors-only',
386+
logLevel,
387+
writeToDisk: value
388+
});
389+
app.use(instance);
390+
app.use((req, res) => {
391+
res.sendStatus(200);
392+
});
393+
listen = listenShorthand(done);
394+
}
395+
396+
describe('write to disk with MultiCompiler', () => {
397+
before((done) => {
398+
multiToDisk(true, done);
399+
});
400+
after(close);
401+
402+
it('should find the bundle files on disk', (done) => {
403+
request(app).get('/foo/bar')
404+
.expect(200, () => {
405+
const bundleFiles = [
406+
'../fixtures/server-test/js1/foo.js',
407+
'../fixtures/server-test/js1/index.html',
408+
'../fixtures/server-test/js1/svg.svg',
409+
'../fixtures/server-test/js2/bar.js'
410+
];
411+
412+
for (const bundleFile of bundleFiles) {
413+
const bundlePath = path.join(__dirname, bundleFile);
414+
assert(fs.existsSync(bundlePath));
415+
fs.unlinkSync(bundlePath);
416+
}
417+
418+
fs.rmdirSync(path.join(__dirname, '../fixtures/server-test/js1/'));
419+
fs.rmdirSync(path.join(__dirname, '../fixtures/server-test/js2/'));
420+
421+
done();
422+
});
423+
});
424+
});
380425
});

0 commit comments

Comments
 (0)