Skip to content

Commit

Permalink
Merge pull request #49 from chadrien/webpack-5-compat
Browse files Browse the repository at this point in the history
Webpack 5 compatibility
  • Loading branch information
bengourley committed Dec 17, 2020
2 parents 074030d + 59ccdb6 commit 4ecc3bd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ node_js:
env:
- WEBPACK_VERSION=3
- WEBPACK_VERSION=4
- WEBPACK_VERSION=5

matrix:
exclude:
- node_js: "4"
env: WEBPACK_VERSION=4
- node_js: "6"
env: WEBPACK_VERSION=4
- node_js: "4"
env: WEBPACK_VERSION=5
- node_js: "6"
env: WEBPACK_VERSION=5
- node_js: "8"
env: WEBPACK_VERSION=5

before_script: bin/ci-install.sh
4 changes: 3 additions & 1 deletion bin/ci-install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
if [ "$WEBPACK_VERSION" -eq "3" ]; then
npm i --no-save --no-package-lock webpack@3
elif [ "$WEBPACK_VERSION" -eq "4" ]; then
npm i --no-save --no-package-lock webpack@$WEBPACK_VERSION webpack-cli@3 mini-css-extract-plugin css-loader
else
npm i --no-save --no-package-lock webpack@4 webpack-cli mini-css-extract-plugin css-loader
npm i --no-save --no-package-lock webpack@$WEBPACK_VERSION webpack-cli mini-css-extract-plugin css-loader
fi
11 changes: 8 additions & 3 deletions source-map-uploader-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { browser } = require('@bugsnag/source-maps')
const parallel = require('run-parallel-limit')
const extname = require('path').extname
const join = require('path').join
const webpackVersion = require('webpack').version

const LOG_PREFIX = '[BugsnagSourceMapUploaderPlugin]'
const PUBLIC_PATH_WARN =
Expand Down Expand Up @@ -31,6 +33,9 @@ class BugsnagSourceMapUploaderPlugin {
}

apply (compiler) {
// considering this is used to check for a version >= 5, it's fine to default to 0.0.0 in case it's not set
const webpackMajorVersion = parseInt((webpackVersion || '0.0.0').split('.')[0], 10)

const plugin = (compilation, cb) => {
const compiler = compilation.compiler
const stats = compilation.getStats().toJson()
Expand All @@ -39,7 +44,7 @@ class BugsnagSourceMapUploaderPlugin {

const chunkToSourceMapDescriptors = chunk => {
// find .map files in this chunk
const maps = chunk.files.filter(file => /.+\.map(\?.*)?$/.test(file))
const maps = chunk[webpackMajorVersion >= 5 ? 'auxiliaryFiles' : 'files'].filter(file => /.+\.map(\?.*)?$/.test(file))

if (!publicPath) {
console.warn(`${LOG_PREFIX} ${PUBLIC_PATH_WARN}`)
Expand All @@ -64,8 +69,8 @@ class BugsnagSourceMapUploaderPlugin {
return null
}

const outputChunkLocation = stripQuery(compiler.outputFileSystem.join(outputPath, source))
const outputSourceMapLocation = stripQuery(compiler.outputFileSystem.join(outputPath, map))
const outputChunkLocation = stripQuery(join(outputPath, source))
const outputSourceMapLocation = stripQuery(join(outputPath, map))

// only include this file if its extension is not in the ignore list
if (this.ignoredBundleExtensions.indexOf(extname(outputChunkLocation)) !== -1) {
Expand Down
16 changes: 10 additions & 6 deletions test/fixtures/g/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ const BugsnagSourceMapUploaderPlugin = require('../../../').BugsnagSourceMapUplo
module.exports = {
entry: './app.js',
devtool: 'hidden-source-map',
output: {
path: __dirname,
filename: './bundle.js',
publicPath: 'https://foobar.com/js',
futureEmitAssets: true
},
output: Object.assign(
{
path: __dirname,
filename: './bundle.js',
publicPath: 'https://foobar.com/js',
},
// As per webpack documentation:
// "output.futureEmitAssets option will be removed in webpack v5.0.0 and this behaviour will become the new default."
// so it can safely be omitted for webpack>=5 while still getting a similar result
parseInt(process.env.WEBPACK_VERSION, 10) < 5 ? { futureEmitAssets: true } : {}),
plugins: [
new BugsnagSourceMapUploaderPlugin({
apiKey: 'YOUR_API_KEY',
Expand Down

0 comments on commit 4ecc3bd

Please sign in to comment.