-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwebpack.config.js
140 lines (135 loc) · 3.23 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const webpack = require('webpack');
const path = require('path');
const os = require('os');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const pkg = require('./package.json');
const CopyPlugin = require('copy-webpack-plugin');
const { name, version, homepage } = pkg;
const isProd = process.env.NODE_ENV === 'production';
const banner = `
${name} - ${version}
Responsive scroll slider
${homepage}
`;
module.exports = {
mode: isProd ? 'production' : 'development',
target: ['web', 'es5'],
entry: {
'scroll.carousel': './src/js/scroll.carousel.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: isProd ? '[name].min.js' : '[name].js',
publicPath: '/dist',
library: {
name: 'ScrollCarousel',
type: 'umd',
export: 'default',
umdNamedDefine: true
},
globalObject: 'this'
},
plugins: [
// for optimization
new RemoveEmptyScriptsPlugin(),
// css extraction
new MiniCssExtractPlugin({
filename: isProd ? '[name].min.css' : '[name].css'
}),
// add banner to files
new webpack.BannerPlugin({ banner }),
// copy types file from src to dist
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'src/js/scroll.carousel.d.ts'),
to: path.resolve(__dirname, pkg.types)
}
]
})
],
module: {
rules: [
{
test: /(\.js)$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: ['@babel/preset-env'],
plugins: ['babel-plugin-add-module-exports']
}
}
},
{
test: /\.s[ac]ss$/i,
exclude: /(node_modules)/,
use: [
// Extract CSS
MiniCssExtractPlugin.loader,
// Translates CSS into CommonJS
{
loader: 'css-loader',
options: {
sourceMap: isProd,
url: false
}
},
// Compiles Sass to CSS
{
loader: 'sass-loader',
options: {
sourceMap: !isProd
}
}
]
}
]
},
optimization: {
minimize: isProd,
minimizer: [
new CssMinimizerPlugin({
include: /\.min\.css/,
parallel: os.cpus().length,
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true }
}
]
}
}),
new TerserPlugin({
include: /\.min\.js/,
parallel: os.cpus().length,
terserOptions: {
format: {
comments: false
}
},
extractComments: false
})
]
},
devtool: isProd ? false : 'source-map',
devServer: {
static: {
directory: path.resolve(__dirname, 'example')
},
watchFiles: ['./src/**/*'],
open: true,
hot: true
},
stats: {
preset: 'minimal',
warnings: true,
publicPath: true
},
cache: true
};