-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
138 lines (137 loc) · 4.3 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
const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const { getPageEntry, getHtmlPluginEntry } = require('./config/utils/entryUtils')
const { IS_DEV } = require('./config/utils/constants')
const { BASE_CONFIG_ENV } = require('./config/config')
module.exports = {
entry: getPageEntry,
output: {
path: path.resolve(__dirname, './dist'),
filename: '[name]/index.[chunkhash].js', // filename 指列在 entry 中,打包后输出的文件的名称
chunkFilename: '[name]/[name].[chunkhash].js', // chunkFilename 指未列在 entry 中,却又需要被打包出来的文件的名称
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
/**
* loader 是从右到左执行,顺序不能颠倒
* 1. 最先执行 sass-loader ,将 sass 文件转为css
* 2. css-loader 将转换后的css文件转为 js模块
* 3. style-loader 将 css 插入到HTML中的<style>标签中
*/
// 'style-loader', // 将 JS 字符串生成为 style 节点
MiniCssExtractPlugin.loader, // 不可与style-loader同时使用
{
loader: 'css-loader', // 将 CSS 转化成 CommonJS 模块
options: {
importLoaders: 2,
},
},
'postcss-loader', // 处理兼容,px2rem等
'sass-loader', // 将 Sass 编译成 CSS
{
// 全局sass资源
loader: 'sass-resources-loader',
options: {
resources: [
path.resolve(__dirname, './', 'src/assets/styles/global.scss'),
],
},
},
],
},
{
test: /\.jsx?$/,
enforce: 'pre',
loader: 'eslint-loader',
include: /src/,
},
{
test: /\.(jsx|js)?$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: 'babel-loader',
},
],
},
{
test: /\.(png|jpg|jpe?g|gif|svg)(\?.*)?$/,
use: {
loader: 'url-loader',
options: {
limit: 10000,
name: 'img/[name].[hash:7].[ext]',
esModule: false,
},
},
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
use: {
loader: 'url-loader',
options: {
limit: 10000,
name: 'video/[name].[hash:7].[ext]',
esModule: false,
},
},
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: {
loader: 'url-loader',
options: {
limit: 10000,
name: 'font/[name].[hash:7].[ext]',
esModule: false,
},
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
...getHtmlPluginEntry(),
// 向浏览器环境注入环境变量
new MiniCssExtractPlugin({
filename: '[name]/index.[contenthash].css',
}),
new webpack.DefinePlugin({
'process.env': {
IS_DEV, // 非正式环境都认为开发环境,一般用于vConsole等调试工具的初始化判断,便于非正式环境调试
...BASE_CONFIG_ENV,
},
}),
new CopyPlugin({
patterns: [
{
// For GitHub pages
from: path.resolve(__dirname, './README.md'),
to: path.resolve(__dirname, './dist/README.md'),
force: true,
},
],
}),
],
resolve: {
extensions: ['.js', '.jsx', '.json'],
alias: {
'@': path.resolve(__dirname, './', 'src/'),
'@api': path.resolve(__dirname, './', 'src/api/'),
'@assets': path.resolve(__dirname, './', 'src/assets/'),
'@constants': path.resolve(__dirname, './', 'src/constants/'),
'@components': path.resolve(__dirname, './', 'src/components/'),
'@store': path.resolve(__dirname, './', 'src/store/'),
'@utils': path.resolve(__dirname, './', 'src/utils/'),
'@demo': path.resolve(__dirname, './', 'src/pages/demo/'),
'@tiktok': path.resolve(__dirname, './', 'src/pages/tiktok/'),
'@wechat': path.resolve(__dirname, './', 'src/pages/wechat/'),
},
},
}