Created
February 9, 2018 13:22
-
-
Save nash403/05d703db9cb59b71b13752ea06fb5338 to your computer and use it in GitHub Desktop.
Webpack base config to publish libs to production with modern code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const webpack = require("webpack"); | |
const UglifyJSPlugin = require("uglifyjs-webpack-plugin"); | |
const PROD = process.env.NODE_ENV === "production"; | |
const configureBabelLoader = browserlist => { | |
return { | |
test: /\.js$/, | |
use: { | |
loader: "babel-loader", | |
options: { | |
presets: [ | |
[ | |
"env", | |
{ | |
debug: true, | |
modules: false, | |
useBuiltIns: true, | |
targets: { | |
browsers: browserlist | |
} | |
} | |
] | |
] | |
} | |
} | |
}; | |
}; | |
const baseConfig = { | |
output: { | |
path: path.resolve(__dirname, "dist"), | |
publicPath: "/", | |
filename: "[name].js", | |
library: "LibName", | |
libraryTarget: "umd", | |
libraryExport: "default", | |
umdNamedDefine: true | |
}, | |
plugins: PROD | |
? [ | |
new UglifyJSPlugin({ | |
sourceMap: true, | |
uglifyOptions: { | |
mangle: { | |
// Works around a Safari 10 bug: | |
// https://github.com/mishoo/UglifyJS2/issues/1753 | |
safari10: true | |
} | |
} | |
}) | |
] | |
: [] | |
}; | |
const modernConfig = Object.assign({}, baseConfig, { | |
entry: { | |
'lib-name': path.resolve(__dirname, "src", "index.js") | |
}, | |
module: { | |
rules: [ | |
configureBabelLoader([ | |
// The last two versions of each browser, excluding versions | |
// that don't support <script type="module">. | |
"last 2 Chrome versions", | |
"not Chrome < 60", | |
"last 2 Safari versions", | |
"not Safari < 10.1", | |
"last 2 iOS versions", | |
"not iOS < 10.3", | |
"last 2 Firefox versions", | |
"not Firefox < 54", | |
"last 2 Edge versions", | |
"not Edge < 15" | |
]) | |
] | |
} | |
}); | |
const legacyConfig = Object.assign({}, baseConfig, { | |
entry: { | |
"lib-name-legacy": path.resolve(__dirname, "src", "index.js") | |
}, | |
module: { | |
rules: [configureBabelLoader(["> 1%", "last 2 versions", "Firefox ESR"])] | |
} | |
}); | |
module.exports = [modernConfig, legacyConfig]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment