From 4af27fb29edab1aead605bbddc8c166da0dc9acc Mon Sep 17 00:00:00 2001 From: Ben Gourley Date: Mon, 14 Jan 2019 10:31:01 +0000 Subject: [PATCH] feat: Add whitelist of extensions to upload (defaults to [ '.js' ] ) CSS files (and maps for them) were being uploaded for no good reason. This feature adds a whitelist of bundle file extensions to upload source maps for. It is unlikely that anyone will ever need anything but ".js" but the option is there just in case. Fixes #24. --- source-map-uploader-plugin.js | 9 ++++ test/fixtures/e/webpack.config.js | 3 +- test/source-map-uploader-plugin.test.js | 56 ++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/source-map-uploader-plugin.js b/source-map-uploader-plugin.js index 70723a3..0a6415a 100644 --- a/source-map-uploader-plugin.js +++ b/source-map-uploader-plugin.js @@ -3,6 +3,7 @@ const upload = require('bugsnag-sourcemaps').upload const resolve = require('url').resolve const parallel = require('run-parallel-limit') +const extname = require('path').extname const LOG_PREFIX = `[BugsnagSourceMapUploaderPlugin]` const PUBLIC_PATH_ERR = @@ -17,6 +18,7 @@ class BugsnagSourceMapUploaderPlugin { this.appVersion = options.appVersion this.overwrite = options.overwrite this.endpoint = options.endpoint + this.extensions = options.extensions || [ '.js' ] this.validate() } @@ -59,6 +61,13 @@ class BugsnagSourceMapUploaderPlugin { return null } + // only include this file if its extension is in the list of desirable ones. + // we use the extension from the file on disk because the name in the chunk + // can have suffixes such as: main.js?23764 + if (!this.extensions.includes(extname(compilation.assets[source].existsAt))) { + return null + } + return { source: compilation.assets[source].existsAt, map: compilation.assets[map].existsAt, diff --git a/test/fixtures/e/webpack.config.js b/test/fixtures/e/webpack.config.js index 5165fbe..0a26069 100644 --- a/test/fixtures/e/webpack.config.js +++ b/test/fixtures/e/webpack.config.js @@ -8,7 +8,8 @@ module.exports = { new MiniCssExtractPlugin(), new BugsnagSourceMapUploaderPlugin({ apiKey: 'YOUR_API_KEY', - endpoint: `http://localhost:${process.env.PORT}` + endpoint: `http://localhost:${process.env.PORT}`, + extensions: process.env.EXTENSIONS ? process.env.EXTENSIONS.split(',') : undefined }) ], output: { diff --git a/test/source-map-uploader-plugin.test.js b/test/source-map-uploader-plugin.test.js index 79b4cc5..4ff18bd 100644 --- a/test/source-map-uploader-plugin.test.js +++ b/test/source-map-uploader-plugin.test.js @@ -120,8 +120,52 @@ test('it sends upon successful build (example project #2)', t => { }) if (process.env.WEBPACK_VERSION !== '3') { - test('it works when plugins cause a chunk to have multiple source maps', t => { - t.plan(7) + test('it ignores source maps for css files by default', t => { + t.plan(3) + const requests = [] + const end = err => { + clearTimeout(timeout) + server.close() + if (err) return t.fail(err.message) + t.end() + } + + // prevent test hanging forever + const timeout = setTimeout(end, 10000) + + const done = () => { + t.equal(requests[0].minifiedUrl, '*/dist/main.js') + t.equal(requests[0].parts[0].filename, 'main.js.map') + t.equal(requests[0].parts[1].filename, 'main.js') + end() + } + + const server = http.createServer((req, res) => { + parseFormdata(req, function (err, data) { + if (err) { + res.end('ERR') + return end(err) + } + requests.push({ + apiKey: data.fields.apiKey, + minifiedUrl: data.fields.minifiedUrl, + parts: data.parts.map(p => ({ name: p.name, filename: p.filename })) + }) + res.end('OK') + done() + }) + }) + server.listen() + exec(`${__dirname}/../node_modules/.bin/webpack`, { + env: Object.assign({}, process.env, { PORT: server.address().port }), + cwd: `${__dirname}/fixtures/e` + }, (err) => { + if (err) end(err) + }) + }) + + test('it uploads css map files if you really want', t => { + t.plan(6) const requests = [] const end = err => { clearTimeout(timeout) @@ -134,12 +178,11 @@ if (process.env.WEBPACK_VERSION !== '3') { const timeout = setTimeout(end, 10000) const done = () => { - t.equal(requests.length, 2) requests.sort((a, b) => a.minifiedUrl < b.minifiedUrl ? -1 : 1) t.equal(requests[0].minifiedUrl, '*/dist/main.css') - t.equal(requests[1].minifiedUrl, '*/dist/main.js') t.equal(requests[0].parts[0].filename, 'main.css.map') t.equal(requests[0].parts[1].filename, 'main.css') + t.equal(requests[1].minifiedUrl, '*/dist/main.js') t.equal(requests[1].parts[0].filename, 'main.js.map') t.equal(requests[1].parts[1].filename, 'main.js') end() @@ -157,12 +200,13 @@ if (process.env.WEBPACK_VERSION !== '3') { parts: data.parts.map(p => ({ name: p.name, filename: p.filename })) }) res.end('OK') - if (requests.length === 2) done() + if (requests.length < 2) return + done() }) }) server.listen() exec(`${__dirname}/../node_modules/.bin/webpack`, { - env: Object.assign({}, process.env, { PORT: server.address().port }), + env: Object.assign({}, process.env, { PORT: server.address().port, EXTENSIONS: '.js,.css' }), cwd: `${__dirname}/fixtures/e` }, (err) => { if (err) end(err)