ASP.NET MVC 使用webpack

   Develop  ASP.Net    Develop  ASP.Net

删除 Content、Script 文件夹

*** 使用所有的文件、样式都使用Webpack打包

创建 packages.config

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
{
"name": "[Name]",
"version": "1.0.0",
"description": "[Description]",
"author": "OY",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack --progress --w",
"start": "yarn run dev",
"build": "cross-env NODE_ENV=production webpack --progress"
},
"dependencies": {
"babel-polyfill": "^6.26.0",
"bootstrap": "^4.0.0",
"compression-webpack-plugin": "^1.1.7",
"font-awesome": "^4.7.0",
"font-awesome-webpack": "^0.0.5-beta.2"
},
"devDependencies": {
"autoprefixer": "^8.0.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-helper-vue-jsx-merge-props": "^^2.0.3",
"babel-loader": "^7.1.2",
"babel-plugin-component": "^1.1.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-plugin-transform-vue-jsx": "^3.5.1",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-2": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"babel-runtime": "^6.26.0",
"clean-webpack-plugin": "^0.1.18",
"cross-env": "^5.1.3",
"css-loader": "^0.28.9",
"eslint": "^4.18.1",
"eslint-config-google": "^0.9.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-html": "^4.0.2",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-node": "^6.0.0",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.9",
"friendly-errors-webpack-plugin": "^1.6.1",
"happypack": "^4.0.1",
"html-webpack-plugin": "^2.30.1",
"less": "^2.3.1",
"less-loader": "^4.0.5",
"node-notifier": "^5.2.1",
"node-sass": "^4.7.2",
"portfinder": "^1.0.13",
"postcss-import": "^11.1.0",
"postcss-loader": "^2.1.0",
"postcss-url": "^7.3.0",
"sass-loader": "^6.0.6",
"uglifyjs-webpack-plugin": "^1.2.0",
"url-loader": "^0.6.2",
"webpack": "^3.11.0",
"webpack-bundle-analyzer": "^2.10.0",
"webpack-hot-middleware": "^2.21.0",
"webpack-merge": "^4.1.1"
}
}

修改 Route

*** 确保单页 route 能成功调整

1
2
3
4
5
6
7
...
routes.MapRoute(
name: "DefaultSpa",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" }
);
...

删除 _Layout.cshtml 并创建 _Layout_Template.cshtml

*** 自动生成_Layout

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" />
</head>
<body>
<div id="app-root">
@RenderBody()
</div>
</body>
</html>

创建 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
'use strict'
const path = require('path')
const os = require('os');
const webpack = require('webpack')
const merge = require('webpack-merge')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CleanWebpackPlugin = require("clean-webpack-plugin")
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const HappyPack = require('happypack');
const env = process.env.NODE_ENV == "development"
var happyThreadPool = HappyPack.ThreadPool({
size: os.cpus().length
});

function resolve(dir) {
return path.join(__dirname, '.', dir)
}

const base = {
context: __dirname,
devtool: '#source-map',
resolve: {
extensions: [
'.js'
]
},
entry: ['babel-polyfill', './src/main.js'],
module: {
rules: [
{
test: /\.js$/,
loader: 'happypack/loader?id=happybabel',
include: [resolve('src'),
resolve('./node_modules/element-ui/lib/'),
resolve('./node_modules/iviewDir/')],
exclude: /node_modules/
}, {
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: ['css-loader?minimize', 'postcss-loader'],
fallback: 'style-loader'
})
}, {
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: ['css-loader?minimize', 'postcss-loader', 'sass-loader'],
fallback: 'style-loader'
})
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf|png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 1024,
name: `assets/[name]${env ? '.[hash:7]' : ''}.[ext]`
}
}
]
},
output: {
publicPath: '/dist/',
path: path.resolve(__dirname, './dist/')
},
devtool: '#source-map',
plugins: [
new CleanWebpackPlugin(["./dist"], { "verbose": true }),
new HtmlWebpackPlugin({
filename: resolve('./Views/Shared/_Layout.cshtml'),
template: resolve('./Views/Shared/_Layout_Template.cshtml'),
inject: true,
xhtml: true,
chunksSortMode: 'dependency'
}),
new HappyPack({
id: 'happybabel',
loaders: ['babel-loader'],
threadPool: happyThreadPool,
verbose: true
})
]
};

const dev = {
output: {
filename: '[name].js',
chunkFilename: '[name].chunk.js'
},
plugins: [
new ExtractTextPlugin({
filename: '[name].css',
allChunks: true
}),
new webpack.optimize.CommonsChunkPlugin({
name: ['vender-exten', 'vender-base'],
minChunks: Infinity
})
]
};

const prod = {
output: {
filename: '[name].[hash].js',
chunkFilename: '[name].[hash].chunk.js'
},
plugins: [
new ExtractTextPlugin({
filename: '[name].[hash].css',
allChunks: true
}),
new webpack.optimize.CommonsChunkPlugin({
name: ['vender-exten', 'vender-base'],
minChunks: Infinity
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new UglifyJsPlugin({
sourceMap: true
})
]
};
const other = env ? dev : prod;
module.exports = merge(base, other)

创建 .babelrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": [ "> 1%", "last 2 versions", "not ie <= 8" ]
}
}
],
"stage-2",
"stage-3"
],
"plugins": [
"transform-runtime",
"syntax-dynamic-import"
],
"env": {
"test": {
"presets": [ "env", "stage-2", "stage-3" ]
}
}
}
  1. 删除 Content、Script 文件夹
  2. 创建 packages.config
  3. 修改 Route
  4. 删除 _Layout.cshtml 并创建 _Layout_Template.cshtml
  5. 创建 webpack.config.js
  6. 创建 .babelrc
Android 开源项目
CentOS Git 源码安装