erratic-gate/webpack.config.js

163 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-07-15 13:45:52 +02:00
const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const webpackMerge = require('webpack-merge')
const nodeExternals = require('webpack-node-externals')
2019-05-27 19:29:29 +02:00
2019-07-15 13:45:52 +02:00
const isProduction = process.env.NODE_ENV === 'production'
const projectRoot = resolve(__dirname)
const sourceFolder = resolve(projectRoot, 'src')
const buildFolder = resolve(projectRoot, 'dist')
const htmlTemplateFile = resolve(sourceFolder, 'index.html')
const babelRule = {
test: /\.(js|tsx?)$/,
use: 'babel-loader'
}
2019-07-23 13:56:11 +02:00
const fileRule = {
2019-07-25 21:59:52 +02:00
test: /\.(png|svg|jpg|gif|ico)$/,
2019-07-23 13:56:11 +02:00
use: ['file-loader']
}
2019-07-18 11:42:21 +02:00
const cssAndSass = [
isProduction
? MiniCssExtractPlugin.loader
: {
loader: 'style-loader',
options: {
singleton: true
}
},
'css-loader'
]
const cssRule = {
test: /\.css$/,
use: cssAndSass
}
2019-07-15 13:45:52 +02:00
const sassRule = {
test: /\.scss$/,
use: [
2019-07-18 11:42:21 +02:00
...cssAndSass,
2019-07-15 13:45:52 +02:00
{
loader: 'sass-loader',
options: {
includePaths: [sourceFolder]
2019-03-06 09:25:58 +01:00
}
2019-07-15 13:45:52 +02:00
}
]
}
const serverConfig = {
mode: 'production',
target: 'node',
externals: [nodeExternals()],
module: {
rules: [babelRule]
},
resolve: {
extensions: ['.ts', '.js', '.json']
},
entry: [resolve(sourceFolder, 'server')],
output: {
filename: 'server.js',
path: buildFolder,
publicPath: '/'
},
node: {
__dirname: false
}
}
2019-07-15 13:45:52 +02:00
const baseConfig = {
mode: 'none',
2019-07-23 21:53:59 +02:00
entry: ['babel-regenerator-runtime', resolve(sourceFolder, 'index')],
2019-07-15 13:45:52 +02:00
output: {
filename: 'js/[name].js',
path: buildFolder,
publicPath: '/'
},
module: {
2019-07-23 13:56:11 +02:00
rules: [babelRule, sassRule, cssRule, fileRule]
2019-03-06 09:25:58 +01:00
},
resolve: {
2019-07-23 13:56:11 +02:00
extensions: [
'.js',
'.ts',
'.tsx',
'.scss',
'.png',
'.svg',
'.jpg',
'.gif'
]
2019-07-25 21:59:52 +02:00
}
2019-07-15 13:45:52 +02:00
}
const devConfig = {
mode: 'development',
plugins: [
new HtmlWebpackPlugin({
template: htmlTemplateFile,
chunksSortMode: 'dependency'
})
2019-06-02 22:32:30 +02:00
],
2019-07-15 13:45:52 +02:00
devtool: 'inline-source-map',
devServer: {
historyApiFallback: true
}
}
const prodConfig = {
mode: 'production',
2019-06-02 22:32:30 +02:00
optimization: {
2019-07-15 13:45:52 +02:00
minimize: true,
nodeEnv: 'production'
},
plugins: [
new MiniCssExtractPlugin({
filename: 'css/[name].min.css'
}),
new OptimizeCssAssetsWebpackPlugin(),
new HtmlWebpackPlugin({
template: htmlTemplateFile,
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
},
inject: true
2019-07-25 21:59:52 +02:00
// favicon: faviconPath
2019-07-15 13:45:52 +02:00
}),
new HtmlWebpackInlineSourcePlugin()
],
devtool: 'source-map'
}
function getFinalConfig() {
if (process.env.NODE_ENV === 'production') {
console.info('Running production config')
return [webpackMerge(baseConfig, prodConfig), serverConfig]
} else if (process.env.NODE_ENV === 'server') {
console.info('Running server config')
return [serverConfig]
2019-06-02 22:32:30 +02:00
}
2019-07-15 13:45:52 +02:00
console.info('Running development config')
return webpackMerge(baseConfig, devConfig)
}
module.exports = getFinalConfig()