webpack-react-typescript配置

webpack配置

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

const webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');//不参与打包直接拷贝过去
const { resolve } = require('path');
const path = require('path');
const glob = require('glob');
const HtmlWebpackPlugin = require('html-webpack-plugin');
var CompressionPlugin = require("compression-webpack-plugin");//添加压缩文件
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const PurifyCSSPlugin = require('purifycss-webpack');//从CSS中删除未使用的选择器
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');//压缩js
const extractSass = new ExtractTextPlugin({
//filename: "[name].[contenthash].css",
filename: "[name].css",
disable: false,
});

const paths = {
dist: resolve(__dirname, 'dist'),
src: resolve(__dirname, 'src'),
};
module.exports = {
//entry: "./src/index.tsx",
devtool: "source-map",
entry: ['babel-polyfill','whatwg-fetch',__dirname + "/src/index.tsx",__dirname +"/src/index.scss"],
output: {
filename: "bundle.js",
path: paths.dist,
publicPath: '/as/'
},
devServer : {
contentBase:paths.dist,
hot: true,
publicPath: '/as/',
historyApiFallback: true,
inline:true,
host:"0.0.0.0",
port:4455,
//设置代理
proxy: {
'/@': {
target: 'http://*****',
//target: 'http://*****',
pathRewrite: {'^/@' : ''}
},
'/@': {
target: 'http://*****',
//target: 'http://*****',
},
}
},


resolve: {
extensions: ['.js', '.json', '.throwss','.ts', '.tsx','jsx'],
modules: [
resolve(__dirname, '../node_modules'),
"node_modules",
],
},

module: {
rules: [
{
test: /\.json$/,
loader: "json-loader"
},
{
test: /\.tsx?$/,
use: [
{loader:'awesome-typescript-loader',
options: {
useBabel:true
}
}
]
},
{
test: /\.css$/,
loader:'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]__[hash:base64:5]',
},
{
test: /\.(sass|scss)$/,
use: extractSass.extract({
use: [{
loader: "css-loader",
options: {
sourceMap: false
}
},{
loader: "sass-loader",
options: {
sourceMap: false
}
}],
fallback: "style-loader"
})
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
loader: 'url-loader',
options: {
limit: 10000
}
},
]
},

plugins: [

new webpack.LoaderOptionsPlugin({
alias: {
'react': 'React',
'react-dom': 'ReactDOM'
}}),
new webpack.HotModuleReplacementPlugin(),
//生成index.html
new HtmlWebpackPlugin(
{
template: path.join(path.join(__dirname, 'src'), 'index.html'),template: __dirname + "/src/index.html"
}
),
extractSass,
new PurifyCSSPlugin({
paths: glob.sync(path.join(__dirname, 'dist/*.js')),
minimize:true,
purifyOptions:{
minify:true
}
}),

// new CompressionPlugin({
// asset:"[path].gz[query]",
// algorithm:"gzip",
// test:/\.js$|\.css$|\.html$/,
// minRatio:0.6
// }),

new CopyWebpackPlugin([
{ from: 'src/favicon.ico' }

]),

// 生产环境启用开发环境启用影响webpack打包速度
// new UglifyJSPlugin({

// uglifyOptions: {
// parallel:8,
// ie8: true,
// ecma: 5,
// // parse: {...options},
// // mangle: {
// // ...options,
// // properties: {
// // // mangle property options
// // }
// // },
// // output: {
// // comments: false,
// // beautify: false,
// // ...options
// // },
// // compress: {...options},
// warnings: false
// }
// })
],
};

tsconfig配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"compilerOptions": {
"allowSyntheticDefaultImports":true,//typescript import 引入
"outDir": "dist",
"sourceMap": true,
"noImplicitAny": false,
"moduleResolution": "node",
"module": "es2015",
"target": "es5",
"jsx": "preserve",
"lib": [
"dom",
"es2015"
]
},
"exclude": [
"dist",
"node_modules"
],
"awesomeTypescriptLoaderOptions": {
"useBabel":true
}
}

.babelrc配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"presets": [
"react",
["es2015", { "modules": false }],
"stage-0"
],
"plugins": [
["transform-imports", {

"lodash": {
"transform": "lodash/${member}",
"preventFullImport": true
}
}]

]
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2018 lrzeblog All Rights Reserved.

访客数 : | 访问量 :