From 90d0d94c76e51c3b55cdd2b19978c8cd78edcf85 Mon Sep 17 00:00:00 2001 From: GregWeil Date: Thu, 17 Jan 2019 04:35:48 -0500 Subject: [PATCH] fix: remove querystring from filenames when writing to disk (#361) --- lib/fs.js | 5 +-- .../server-test/webpack.querystring.config.js | 20 +++++++++++ test/tests/server.js | 33 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/server-test/webpack.querystring.config.js diff --git a/lib/fs.js b/lib/fs.js index 1823b67a3..6bd5e9baa 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -26,8 +26,9 @@ module.exports = { for (const assetPath of Object.keys(assets)) { const asset = assets[assetPath]; const source = asset.source(); - const isAbsolute = path.isAbsolute(assetPath); - const writePath = isAbsolute ? assetPath : path.join(outputPath, assetPath); + const [assetPathClean] = assetPath.split('?'); + const isAbsolute = path.isAbsolute(assetPathClean); + const writePath = isAbsolute ? assetPathClean : path.join(outputPath, assetPathClean); const relativePath = path.relative(process.cwd(), writePath); const allowWrite = filter && typeof filter === 'function' ? filter(writePath) : true; diff --git a/test/fixtures/server-test/webpack.querystring.config.js b/test/fixtures/server-test/webpack.querystring.config.js new file mode 100644 index 000000000..ba5877b06 --- /dev/null +++ b/test/fixtures/server-test/webpack.querystring.config.js @@ -0,0 +1,20 @@ +'use strict'; + +module.exports = { + mode: 'development', + context: __dirname, + entry: './foo.js', + output: { + filename: 'bundle.js?[contenthash]', + path: '/' + }, + module: { + rules: [ + { + test: /\.(svg|html)$/, + loader: 'file-loader', + query: { name: '[name].[ext]' } + } + ] + } +}; diff --git a/test/tests/server.js b/test/tests/server.js index 834f6911d..0babd86f4 100644 --- a/test/tests/server.js +++ b/test/tests/server.js @@ -11,6 +11,7 @@ const request = require('supertest'); const middleware = require('../../'); const webpackConfig = require('../fixtures/server-test/webpack.config'); const webpackMultiConfig = require('../fixtures/server-test/webpack.array.config'); +const webpackQuerystringConfig = require('../fixtures/server-test/webpack.querystring.config'); const webpackClientServerConfig = require('../fixtures/server-test/webpack.client.server.config'); describe('Server', () => { @@ -460,6 +461,38 @@ describe('Server', () => { }); }); + function querystringToDisk(value, done) { + app = express(); + const compiler = webpack(webpackQuerystringConfig); + instance = middleware(compiler, { + stats: 'errors-only', + logLevel, + writeToDisk: value + }); + app.use(instance); + app.use((req, res) => { + res.sendStatus(200); + }); + listen = listenShorthand(done); + } + + describe('write to disk without including querystrings', () => { + before((done) => { + querystringToDisk(true, done); + }); + after(close); + + it('should find the bundle file on disk with no querystring', (done) => { + request(app).get('/foo/bar') + .expect(200, () => { + const bundlePath = path.join(__dirname, '../fixtures/server-test/bundle.js'); + assert(fs.existsSync(bundlePath)); + fs.unlinkSync(bundlePath); + done(); + }); + }); + }); + function multiToDisk(value, done) { app = express(); const compiler = webpack(webpackMultiConfig);