Skip to content

Instantly share code, notes, and snippets.

@knitcodemonkey
Last active April 12, 2018 20:29
Show Gist options
  • Save knitcodemonkey/113b8fc40bdd7109ea676551f3f0c375 to your computer and use it in GitHub Desktop.
Save knitcodemonkey/113b8fc40bdd7109ea676551f3f0c375 to your computer and use it in GitHub Desktop.
Webpack 4 config starter
{
"name": "knitcodemonkey_starter",
"version": "1.0.0",
"description": "KnitCodeMonkey's Starter",
"repository": {},
"scripts": {
"init": "rm -rf node_modules; npm install; npm run build; npm run docs;",
"dev": "webpack --env.NODE_ENV=dev --mode=development --profile --colors",
"prod": "webpack --env.NODE_ENV=production --env.production --mode=production --optimize-minimize --progress --profile --colors",
"watch": "webpack -d --env.NODE_ENV=dev --mode=development -w --colors &",
"analyze": "rm ./stats.json; webpack --json > stats.json --env.NODE_ENV=production --env.production --mode=production --optimize-minimize --progress --profile --colors; ./node_modules/.bin/webpack-bundle-analyzer stats.json",
"docs": "./docs"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-2": "^6.3.13",
"css-loader": "^0.28.11",
"es-module-loader": "^2.2.8",
"esdoc": "^1.0.4",
"esdoc-jsx-plugin": "^1.0.0",
"esdoc-standard-plugin": "^1.0.0",
"eslint": "^4.18.1",
"eslint-plugin-jsx-a11y": "^6.0.3",
"expose-loader": "^0.7.5",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"file-loader": "^1.1.11",
"jsx-loader": "^0.13.2",
"node-sass": "^4.8.3",
"optimize-css-assets-webpack-plugin": "^4.0.0",
"promise": "^8.0.1",
"react-dropzone": "^4.2.9",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
"redux-devtools": "^3.4.1",
"require-dir": "^1.0.0",
"sass-loader": "^6.0.7",
"script-loader": "^0.7.2",
"sinon": "^4.4.6",
"style-loader": "^0.20.3",
"uglify-js": "^3.3.16",
"uglifyjs-webpack-plugin": "^1.2.4",
"url-loader": "^1.0.1",
"webpack": "^4.1.1",
"webpack-bundle-analyzer": "^2.11.1",
"webpack-cli": "^2.0.12",
"webpack-stream": "^4.0.2"
},
"dependencies": {
"classnames": "^2.2.5",
"react": "^15.6.2",
"react-dom": "^15.6.2",
}
}
const webpack = require("webpack");
const path = require("path");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
module.exports = env => {
const isProd = env.NODE_ENV === "production";
const plugins = [
new ExtractTextPlugin({
filename: getPath => getPath("[name].css").replace("css/js", "css"),
allChunks: true
})
];
if (isProd === "production") {
plugins.push(
new UglifyJsPlugin({
parallel: 8,
cache: true
})
);
}
return {
devtool: isProd ? false : "source-map",
entry: {
file1: "./src/file1.js",
file2: "./src/file2.js"
},
output: {
path: path.resolve(__dirname, "dist/"),
filename: "[name].js"
},
module: {
rules: [
{
test: require.resolve("react"),
use: [
{
loader: "expose-loader",
options: "React"
}
]
},
{
test: /\.(js|jsx)?$/,
loader: "babel-loader",
exclude: /node_modules/,
query: {
presets: ["react", "stage-2", "env"]
}
},
{
// inline base64 URLs for <=8k images, direct URLs for the rest
test: /\.(png|jpg|gif)$/,
use: [
{
loader: "url-loader",
options: {
limit: 8192,
name: "./images/[name]-[hash:6].[ext]?[hash]"
}
}
]
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: ["style-loader", "css-loader", "sass-loader"]
})
},
{
// For fonts files
test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
use: [
{
// url-loader will fallback to file-loader if limit is exceeded. It will pass all of the query args along.
loader: "url-loader",
options: {
limit: 1,
name: "./fonts/[name]-[hash:6].[ext]?[hash]"
}
}
]
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
}
]
},
externals: {
jquery: "$",
},
plugins,
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: "commons",
chunks: "initial",
minChunks: 2
}
}
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment