Skip to content

Commit 5c90e1e

Browse files
fix: the writeToFile option has compatibility with webpack@5 (#459)
1 parent 4ee0f29 commit 5c90e1e

7 files changed

+55
-61
lines changed

azure-pipelines.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ jobs:
5353
node-8-canary:
5454
node_version: ^8.9.0
5555
webpack_version: next
56-
continue_on_error: true
5756
steps:
5857
- task: NodeTool@0
5958
inputs:
@@ -108,7 +107,6 @@ jobs:
108107
node-8-canary:
109108
node_version: ^8.9.0
110109
webpack_version: next
111-
continue_on_error: true
112110
steps:
113111
- task: NodeTool@0
114112
inputs:
@@ -163,7 +161,6 @@ jobs:
163161
node-8-canary:
164162
node_version: ^8.9.0
165163
webpack_version: next
166-
continue_on_error: true
167164
steps:
168165
- script: 'git config --global core.autocrlf input'
169166
displayName: 'Config git core.autocrlf'
@@ -198,4 +195,4 @@ jobs:
198195
displayName: 'Publish test results'
199196
- script: curl -s https://codecov.io/bash | bash -s -- -t $(CODECOV_TOKEN)
200197
condition: succeededOrFailed()
201-
displayName: 'Submit coverage data to codecov'
198+
displayName: 'Submit coverage data to codecov'

lib/fs.js

+50-45
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,68 @@ const DevMiddlewareError = require('./DevMiddlewareError');
1212
module.exports = {
1313
toDisk(context) {
1414
const compilers = context.compiler.compilers || [context.compiler];
15+
1516
for (const compiler of compilers) {
16-
compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => {
17-
const { assets } = compilation;
18-
const { log } = context;
19-
const { writeToDisk: filter } = context.options;
20-
let { outputPath } = compiler;
17+
compiler.hooks.emit.tap('WebpackDevMiddleware', (compilation) => {
18+
compiler.hooks.assetEmitted.tapAsync(
19+
'WebpackDevMiddleware',
20+
(file, content, callback) => {
21+
let targetFile = file;
2122

22-
if (outputPath === '/') {
23-
outputPath = compiler.context;
24-
}
23+
const queryStringIdx = targetFile.indexOf('?');
2524

26-
for (const assetPath of Object.keys(assets)) {
27-
let targetFile = assetPath;
25+
if (queryStringIdx >= 0) {
26+
targetFile = targetFile.substr(0, queryStringIdx);
27+
}
2828

29-
const queryStringIdx = targetFile.indexOf('?');
29+
let { outputPath } = compiler;
3030

31-
if (queryStringIdx >= 0) {
32-
targetFile = targetFile.substr(0, queryStringIdx);
33-
}
31+
// TODO Why? Need remove in future major release
32+
if (outputPath === '/') {
33+
outputPath = compiler.context;
34+
}
3435

35-
const targetPath = path.isAbsolute(targetFile)
36-
? targetFile
37-
: path.join(outputPath, targetFile);
38-
const allowWrite =
39-
filter && typeof filter === 'function' ? filter(targetPath) : true;
36+
outputPath = compilation.getPath(outputPath, {});
4037

41-
if (allowWrite) {
42-
const asset = assets[assetPath];
43-
let content = asset.source();
38+
const targetPath = path.join(outputPath, targetFile);
4439

45-
if (!Buffer.isBuffer(content)) {
46-
// TODO need remove in next major release
47-
if (Array.isArray(content)) {
48-
content = content.join('\n');
49-
}
40+
const { writeToDisk: filter } = context.options;
41+
const allowWrite =
42+
filter && typeof filter === 'function'
43+
? filter(targetPath)
44+
: true;
5045

51-
content = Buffer.from(content, 'utf8');
46+
if (!allowWrite) {
47+
return callback();
5248
}
5349

54-
mkdirp.sync(path.dirname(targetPath));
55-
56-
try {
57-
fs.writeFileSync(targetPath, content, 'utf-8');
58-
59-
log.debug(
60-
colors.cyan(
61-
`Asset written to disk: ${path.relative(
62-
process.cwd(),
63-
targetPath
64-
)}`
65-
)
66-
);
67-
} catch (e) {
68-
log.error(`Unable to write asset to disk:\n${e}`);
69-
}
50+
const { log } = context;
51+
const dir = path.dirname(targetPath);
52+
53+
return mkdirp(dir, (mkdirpError) => {
54+
if (mkdirpError) {
55+
return callback(mkdirpError);
56+
}
57+
58+
return fs.writeFile(targetPath, content, (writeFileError) => {
59+
if (writeFileError) {
60+
return callback(writeFileError);
61+
}
62+
63+
log.debug(
64+
colors.cyan(
65+
`Asset written to disk: ${path.relative(
66+
process.cwd(),
67+
targetPath
68+
)}`
69+
)
70+
);
71+
72+
return callback();
73+
});
74+
});
7075
}
71-
}
76+
);
7277
});
7378
}
7479
},

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = [
1717
{
1818
test: /\.(svg|html)$/,
1919
loader: 'file-loader',
20-
query: { name: '[name].[ext]' },
20+
options: { name: '[name].[ext]' },
2121
},
2222
],
2323
},

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = [
1515
{
1616
test: /\.(svg|html)$/,
1717
loader: 'file-loader',
18-
query: { name: '[name].[ext]' },
18+
options: { name: '[name].[ext]' },
1919
},
2020
],
2121
},

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
{
1414
test: /\.(svg|html)$/,
1515
loader: 'file-loader',
16-
query: { name: '[name].[ext]' },
16+
options: { name: '[name].[ext]' },
1717
},
1818
],
1919
},

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
{
1414
test: /\.(svg|html)$/,
1515
loader: 'file-loader',
16-
query: { name: '[name].[ext]' },
16+
options: { name: '[name].[ext]' },
1717
},
1818
],
1919
},

test/server.test.js

-8
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,13 @@ describe('Server', () => {
8888
request(app)
8989
.get('/public/bundle.js')
9090
.expect('Content-Type', 'application/javascript; charset=UTF-8')
91-
// TODO(michael-ciniawsky) investigate the need for this test
92-
.expect('Content-Length', '4631')
9391
.expect(200, /console\.log\('Hey\.'\)/, done);
9492
});
9593

9694
it('HEAD request to bundle file', (done) => {
9795
request(app)
9896
.head('/public/bundle.js')
9997
.expect('Content-Type', 'application/javascript; charset=UTF-8')
100-
.expect('Content-Length', '4631')
10198
// eslint-disable-next-line no-undefined
10299
.expect(200, undefined, done);
103100
});
@@ -112,7 +109,6 @@ describe('Server', () => {
112109
request(app)
113110
.get('/public/svg.svg')
114111
.expect('Content-Type', 'image/svg+xml; charset=UTF-8')
115-
.expect('Content-Length', '4778')
116112
.expect(200, done);
117113
});
118114

@@ -189,8 +185,6 @@ describe('Server', () => {
189185
request(app)
190186
.post('/public/bundle.js')
191187
.expect('Content-Type', 'application/javascript; charset=UTF-8')
192-
// TODO(michael-ciniawsky) investigate the need for this test
193-
.expect('Content-Length', '4631')
194188
.expect(200, /console\.log\('Hey\.'\)/, done);
195189
});
196190

@@ -248,8 +242,6 @@ describe('Server', () => {
248242
it('GET request to bundle file', (done) => {
249243
request(app)
250244
.get('/bundle.js')
251-
// TODO(michael-ciniawsky) investigate the need for this test
252-
.expect('Content-Length', '4631')
253245
.expect(200, /console\.log\('Hey\.'\)/, done);
254246
});
255247
});

0 commit comments

Comments
 (0)