Skip to content

Commit

Permalink
fix: the writeToFile option has compatibility with webpack@5 (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 30, 2019
1 parent 4ee0f29 commit 5c90e1e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 61 deletions.
5 changes: 1 addition & 4 deletions azure-pipelines.yml
Expand Up @@ -53,7 +53,6 @@ jobs:
node-8-canary:
node_version: ^8.9.0
webpack_version: next
continue_on_error: true
steps:
- task: NodeTool@0
inputs:
Expand Down Expand Up @@ -108,7 +107,6 @@ jobs:
node-8-canary:
node_version: ^8.9.0
webpack_version: next
continue_on_error: true
steps:
- task: NodeTool@0
inputs:
Expand Down Expand Up @@ -163,7 +161,6 @@ jobs:
node-8-canary:
node_version: ^8.9.0
webpack_version: next
continue_on_error: true
steps:
- script: 'git config --global core.autocrlf input'
displayName: 'Config git core.autocrlf'
Expand Down Expand Up @@ -198,4 +195,4 @@ jobs:
displayName: 'Publish test results'
- script: curl -s https://codecov.io/bash | bash -s -- -t $(CODECOV_TOKEN)
condition: succeededOrFailed()
displayName: 'Submit coverage data to codecov'
displayName: 'Submit coverage data to codecov'
95 changes: 50 additions & 45 deletions lib/fs.js
Expand Up @@ -12,63 +12,68 @@ const DevMiddlewareError = require('./DevMiddlewareError');
module.exports = {
toDisk(context) {
const compilers = context.compiler.compilers || [context.compiler];

for (const compiler of compilers) {
compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => {

This comment has been minimized.

Copy link
@bigmeow

bigmeow Sep 17, 2019

const server = new WebpackDevServer(compiler, {
    quiet: true,
    contentBase: targetDir,
    clientLogLevel: 'none',
    writeToDisk: true,
    hot: false,
    inline: false,
    watchOptions: { ignored: /node_modules/ }
  })

if writeToDisk: true , my code will not work. But it works fine in version 3.7.0

This is my webpack version: "webpack": ">=4 < 4.29",

This comment has been minimized.

Copy link
@alexander-akait

alexander-akait Sep 23, 2019

Member

Please open new issue, no need write in PR

This comment has been minimized.

Copy link
@bigmeow

bigmeow Sep 24, 2019

Semantic version compatible.
If I remove the webpack version limit and upgrade to the latest version, it will work fine.

const { assets } = compilation;
const { log } = context;
const { writeToDisk: filter } = context.options;
let { outputPath } = compiler;
compiler.hooks.emit.tap('WebpackDevMiddleware', (compilation) => {
compiler.hooks.assetEmitted.tapAsync(
'WebpackDevMiddleware',
(file, content, callback) => {
let targetFile = file;

if (outputPath === '/') {
outputPath = compiler.context;
}
const queryStringIdx = targetFile.indexOf('?');

for (const assetPath of Object.keys(assets)) {
let targetFile = assetPath;
if (queryStringIdx >= 0) {
targetFile = targetFile.substr(0, queryStringIdx);
}

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

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

const targetPath = path.isAbsolute(targetFile)
? targetFile
: path.join(outputPath, targetFile);
const allowWrite =
filter && typeof filter === 'function' ? filter(targetPath) : true;
outputPath = compilation.getPath(outputPath, {});

if (allowWrite) {
const asset = assets[assetPath];
let content = asset.source();
const targetPath = path.join(outputPath, targetFile);

if (!Buffer.isBuffer(content)) {
// TODO need remove in next major release
if (Array.isArray(content)) {
content = content.join('\n');
}
const { writeToDisk: filter } = context.options;
const allowWrite =
filter && typeof filter === 'function'
? filter(targetPath)
: true;

content = Buffer.from(content, 'utf8');
if (!allowWrite) {
return callback();
}

mkdirp.sync(path.dirname(targetPath));

try {
fs.writeFileSync(targetPath, content, 'utf-8');

log.debug(
colors.cyan(
`Asset written to disk: ${path.relative(
process.cwd(),
targetPath
)}`
)
);
} catch (e) {
log.error(`Unable to write asset to disk:\n${e}`);
}
const { log } = context;
const dir = path.dirname(targetPath);

return mkdirp(dir, (mkdirpError) => {
if (mkdirpError) {
return callback(mkdirpError);
}

return fs.writeFile(targetPath, content, (writeFileError) => {
if (writeFileError) {
return callback(writeFileError);
}

log.debug(
colors.cyan(
`Asset written to disk: ${path.relative(
process.cwd(),
targetPath
)}`
)
);

return callback();
});
});
}
}
);
});
}
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/server-test/webpack.array.config.js
Expand Up @@ -17,7 +17,7 @@ module.exports = [
{
test: /\.(svg|html)$/,
loader: 'file-loader',
query: { name: '[name].[ext]' },
options: { name: '[name].[ext]' },
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/server-test/webpack.client.server.config.js
Expand Up @@ -15,7 +15,7 @@ module.exports = [
{
test: /\.(svg|html)$/,
loader: 'file-loader',
query: { name: '[name].[ext]' },
options: { name: '[name].[ext]' },
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/server-test/webpack.config.js
Expand Up @@ -13,7 +13,7 @@ module.exports = {
{
test: /\.(svg|html)$/,
loader: 'file-loader',
query: { name: '[name].[ext]' },
options: { name: '[name].[ext]' },
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/server-test/webpack.querystring.config.js
Expand Up @@ -13,7 +13,7 @@ module.exports = {
{
test: /\.(svg|html)$/,
loader: 'file-loader',
query: { name: '[name].[ext]' },
options: { name: '[name].[ext]' },
},
],
},
Expand Down
8 changes: 0 additions & 8 deletions test/server.test.js
Expand Up @@ -88,16 +88,13 @@ describe('Server', () => {
request(app)
.get('/public/bundle.js')
.expect('Content-Type', 'application/javascript; charset=UTF-8')
// TODO(michael-ciniawsky) investigate the need for this test
.expect('Content-Length', '4631')
.expect(200, /console\.log\('Hey\.'\)/, done);
});

it('HEAD request to bundle file', (done) => {
request(app)
.head('/public/bundle.js')
.expect('Content-Type', 'application/javascript; charset=UTF-8')
.expect('Content-Length', '4631')
// eslint-disable-next-line no-undefined
.expect(200, undefined, done);
});
Expand All @@ -112,7 +109,6 @@ describe('Server', () => {
request(app)
.get('/public/svg.svg')
.expect('Content-Type', 'image/svg+xml; charset=UTF-8')
.expect('Content-Length', '4778')
.expect(200, done);
});

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

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

0 comments on commit 5c90e1e

Please sign in to comment.