Skip to content

Commit 541c0f0

Browse files
author
310198
committed
更新项目到webpack2
1 parent fbb2f62 commit 541c0f0

20 files changed

+328
-176
lines changed

.babelrc

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"presets": ["es2015", "stage-2"],
2+
"presets": [
3+
["es2015", { "modules": false }],
4+
"stage-2"
5+
],
36
"plugins": ["transform-runtime"],
47
"comments": false,
58
"env": {

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules/
33
dist/
44
npm-debug.log
55
test/unit/coverage
6+
test/e2e/reports
7+
selenium-debug.log

README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# vue2admin
1+
# vue2admin-v2
22

3-
> 基于vue2 + vue-router + vuex + fetch + PostCSS + [element-ui](http://element.eleme.io/) 实现的一个后台管理系统基础框架。
3+
> 基于vue2 + vue-router + vuex + fetch + PostCSS + [element-ui](http://element.eleme.io/) + webpack2 实现的一个后台管理系统基础框架。
44
55
#### 已实现功能:
66
- 基础框架
@@ -14,7 +14,7 @@
1414
[点此查看运行效果](http://vue2admin.duapp.com)(用户名密码不限)
1515

1616
```
17-
说明:项目生成的UI虽然只是一个简单的框架,但是已经集成了大部分大中型项目中必须的插件服务和项目逻辑架构
17+
说明:项目生成的UI虽然只是一个简单的框架,但是已经集成了大部分大中型项目中必须的插件服务和项目逻辑架构。实现了拿来即用。
1818
```
1919

2020
## Build Setup
@@ -29,13 +29,20 @@ npm run dev
2929
# build for production with minification
3030
npm run build
3131

32+
# build for production and view the bundle analyzer report
33+
npm run build --report
34+
3235
# run unit tests
3336
npm run unit
3437

38+
# run e2e tests
39+
npm run e2e
40+
3541
# run all tests
3642
npm test
3743
```
3844

45+
3946
## 工程目录结构
4047
```
4148
src:项目源码。开发的时候代码写在这里。
@@ -65,9 +72,9 @@ src:项目源码。开发的时候代码写在这里。
6572
```
6673

6774
## todo
75+
6876
- vuex与localstorage的持久化方案(待改进:只对指定的vuex state内容进行持久化,并可指定过期时间,目前待选方案:https://github.com/liesislukas/localstorage-ttl,https://github.com/WQTeam/web-storage-cache)
6977
- 国际化
7078
- 封装fetch服务
7179
- 引入图表插件
7280
- 大项目打包方案webpack code-splitting
73-

build/build.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
// https://github.com/shelljs/shelljs
22
require('./check-versions')()
3-
require('shelljs/global')
4-
env.NODE_ENV = 'production'
53

6-
var path = require('path')
7-
var config = require('../config')
4+
process.env.NODE_ENV = 'production'
5+
86
var ora = require('ora')
7+
var path = require('path')
8+
var chalk = require('chalk')
9+
var shell = require('shelljs')
910
var webpack = require('webpack')
11+
var config = require('../config')
1012
var webpackConfig = require('./webpack.prod.conf')
1113

12-
console.log(
13-
' Tip:\n' +
14-
' Built files are meant to be served over an HTTP server.\n' +
15-
' Opening index.html over file:// won\'t work.\n'
16-
)
17-
1814
var spinner = ora('building for production...')
1915
spinner.start()
2016

2117
var assetsPath = path.join(config.build.assetsRoot, config.build.assetsSubDirectory)
22-
rm('-rf', assetsPath)
23-
mkdir('-p', assetsPath)
24-
cp('-R', 'static/*', assetsPath)
18+
shell.rm('-rf', assetsPath)
19+
shell.mkdir('-p', assetsPath)
20+
shell.config.silent = true
21+
shell.cp('-R', 'static/*', assetsPath)
22+
shell.config.silent = false
2523

2624
webpack(webpackConfig, function (err, stats) {
2725
spinner.stop()
@@ -32,5 +30,11 @@ webpack(webpackConfig, function (err, stats) {
3230
children: false,
3331
chunks: false,
3432
chunkModules: false
35-
}) + '\n')
33+
}) + '\n\n')
34+
35+
console.log(chalk.cyan(' Build complete.\n'))
36+
console.log(chalk.yellow(
37+
' Tip: built files are meant to be served over an HTTP server.\n' +
38+
' Opening index.html over file:// won\'t work.\n'
39+
))
3640
})

build/check-versions.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var semver = require('semver')
21
var chalk = require('chalk')
2+
var semver = require('semver')
33
var packageConfig = require('../package.json')
4-
var exec = function (cmd) {
5-
return require('child_process')
6-
.execSync(cmd).toString().trim()
4+
5+
function exec (cmd) {
6+
return require('child_process').execSync(cmd).toString().trim()
77
}
88

99
var versionRequirements = [

build/dev-server.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
require('./check-versions')()
2+
23
var config = require('../config')
3-
if (!process.env.NODE_ENV) process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
4+
if (!process.env.NODE_ENV) {
5+
process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
6+
}
7+
8+
var opn = require('opn')
49
var path = require('path')
510
var express = require('express')
611
var webpack = require('webpack')
7-
var opn = require('opn')
812
var proxyMiddleware = require('http-proxy-middleware')
913
var webpackConfig = process.env.NODE_ENV === 'testing'
1014
? require('./webpack.prod.conf')
1115
: require('./webpack.dev.conf')
1216

1317
// default port where dev server listens for incoming traffic
1418
var port = process.env.PORT || config.dev.port
19+
// automatically open browser, if not set will be false
20+
var autoOpenBrowser = !!config.dev.autoOpenBrowser
1521
// Define HTTP proxies to your custom API backend
1622
// https://github.com/chimurai/http-proxy-middleware
1723
var proxyTable = config.dev.proxyTable
@@ -41,7 +47,7 @@ Object.keys(proxyTable).forEach(function (context) {
4147
if (typeof options === 'string') {
4248
options = { target: options }
4349
}
44-
app.use(proxyMiddleware(context, options))
50+
app.use(proxyMiddleware(options.filter || context, options))
4551
})
4652

4753
// handle fallback for HTML5 history API
@@ -71,7 +77,7 @@ module.exports = app.listen(port, function (err) {
7177
}
7278

7379
// when env is testing, don't need open it
74-
if (process.env.NODE_ENV !== 'testing') {
80+
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
7581
opn(uri)
7682
}
7783
})

build/utils.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,24 @@ exports.cssLoaders = function (options) {
2828
// Extract CSS when that option is specified
2929
// (which is the case during production build)
3030
if (options.extract) {
31-
return ExtractTextPlugin.extract('vue-style-loader', sourceLoader)
31+
return ExtractTextPlugin.extract({
32+
use: sourceLoader,
33+
fallback: 'vue-style-loader'
34+
})
3235
} else {
3336
return ['vue-style-loader', sourceLoader].join('!')
3437
}
3538
}
3639

3740
// http://vuejs.github.io/vue-loader/en/configurations/extract-css.html
3841
return {
39-
css: generateLoaders(['css?-autoprefixer']),
40-
postcss: generateLoaders(['css?-autoprefixer']),
41-
less: generateLoaders(['css?-autoprefixer', 'less']),
42-
sass: generateLoaders(['css?-autoprefixer', 'sass?indentedSyntax']),
43-
scss: generateLoaders(['css?-autoprefixer', 'sass?outputStyle=expanded']),
44-
stylus: generateLoaders(['css?-autoprefixer', 'stylus']),
45-
styl: generateLoaders(['css?-autoprefixer', 'stylus'])
42+
css: generateLoaders(['css']),
43+
postcss: generateLoaders(['css']),
44+
less: generateLoaders(['css', 'less']),
45+
sass: generateLoaders(['css', 'sass?indentedSyntax']),
46+
scss: generateLoaders(['css', 'sass']),
47+
stylus: generateLoaders(['css', 'stylus']),
48+
styl: generateLoaders(['css', 'stylus'])
4649
}
4750
}
4851

build/vue-loader.conf.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var utils = require('./utils')
2+
var config = require('../config')
3+
var isProduction = process.env.NODE_ENV === 'production'
4+
5+
module.exports = {
6+
loaders: utils.cssLoaders({
7+
sourceMap: isProduction
8+
? config.build.productionSourceMap
9+
: config.dev.cssSourceMap,
10+
extract: isProduction
11+
}),
12+
postcss: [
13+
require('postcss-import')(),
14+
require('postcss-cssnext')()//,
15+
// require('autoprefixer')({browsers: ['last 2 versions']})
16+
]
17+
}

build/webpack.base.conf.js

+24-40
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,64 @@
11
var path = require('path')
2-
var config = require('../config')
32
var utils = require('./utils')
4-
var projectRoot = path.resolve(__dirname, '../')
3+
var config = require('../config')
4+
var vueLoaderConfig = require('./vue-loader.conf')
55

6-
var env = process.env.NODE_ENV
7-
// check env & config/index.js to decide whether to enable CSS source maps for the
8-
// various preprocessor loaders added to vue-loader at the end of this file
9-
var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
10-
var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)
11-
var useCssSourceMap = cssSourceMapDev || cssSourceMapProd
6+
function resolve (dir) {
7+
return path.join(__dirname, '..', dir)
8+
}
129

1310
module.exports = {
1411
entry: {
1512
app: './src/main.js'
1613
},
1714
output: {
1815
path: config.build.assetsRoot,
19-
publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
20-
filename: '[name].js'
16+
filename: '[name].js',
17+
publicPath: process.env.NODE_ENV === 'production'
18+
? config.build.assetsPublicPath
19+
: config.dev.assetsPublicPath
2120
},
2221
resolve: {
23-
extensions: ['', '.js', '.vue', '.json'],
24-
fallback: [path.join(__dirname, '../node_modules')],
22+
extensions: ['.js', '.vue', '.json'],
23+
modules: [
24+
resolve('src'),
25+
resolve('node_modules')
26+
],
2527
alias: {
2628
'vue$': 'vue/dist/vue.common.js',
27-
'src': path.resolve(__dirname, '../src'),
28-
'assets': path.resolve(__dirname, '../src/assets'),
29-
'components': path.resolve(__dirname, '../src/components')
29+
'src': resolve('src'),
30+
'assets': resolve('src/assets'),
31+
'components': resolve('src/components')
3032
}
3133
},
32-
resolveLoader: {
33-
fallback: [path.join(__dirname, '../node_modules')]
34-
},
3534
module: {
36-
loaders: [
35+
rules: [
3736
{
3837
test: /\.vue$/,
39-
loader: 'vue'
38+
loader: 'vue-loader',
39+
options: vueLoaderConfig
4040
},
4141
{
4242
test: /\.js$/,
43-
loader: 'babel',
44-
include: [
45-
path.join(projectRoot, 'src')
46-
],
47-
exclude: /node_modules/
48-
},
49-
{
50-
test: /\.json$/,
51-
loader: 'json'
43+
loader: 'babel-loader',
44+
include: [resolve('src'), resolve('test')]
5245
},
5346
{
5447
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
55-
loader: 'url',
48+
loader: 'url-loader',
5649
query: {
5750
limit: 10000,
5851
name: utils.assetsPath('img/[name].[hash:7].[ext]')
5952
}
6053
},
6154
{
6255
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
63-
loader: 'url',
56+
loader: 'url-loader',
6457
query: {
6558
limit: 10000,
6659
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
6760
}
6861
}
6962
]
70-
},
71-
vue: {
72-
loaders: utils.cssLoaders({sourceMap: useCssSourceMap}),
73-
postcss: [
74-
require('postcss-import')(),
75-
require('postcss-cssnext')()//,
76-
// require('autoprefixer')({browsers: ['last 7 versions']}) //postcss-cssnext插件中已包含autoprefixer
77-
],
78-
autoprefixer: true
7963
}
8064
}

build/webpack.dev.conf.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var config = require('../config')
1+
var utils = require('./utils')
22
var webpack = require('webpack')
3+
var config = require('../config')
34
var merge = require('webpack-merge')
4-
var utils = require('./utils')
55
var baseWebpackConfig = require('./webpack.base.conf')
66
var HtmlWebpackPlugin = require('html-webpack-plugin')
7-
var FriendlyErrors = require('friendly-errors-webpack-plugin')
7+
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
88

99
// add hot-reload related code to entry chunks
1010
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
@@ -13,24 +13,23 @@ Object.keys(baseWebpackConfig.entry).forEach(function (name) {
1313

1414
module.exports = merge(baseWebpackConfig, {
1515
module: {
16-
loaders: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
16+
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
1717
},
18-
// eval-source-map is faster for development
19-
devtool: '#eval-source-map',
18+
// cheap-module-eval-source-map is faster for development
19+
devtool: '#cheap-module-eval-source-map',
2020
plugins: [
2121
new webpack.DefinePlugin({
2222
'process.env': config.dev.env
2323
}),
2424
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
25-
new webpack.optimize.OccurrenceOrderPlugin(),
2625
new webpack.HotModuleReplacementPlugin(),
27-
new webpack.NoErrorsPlugin(),
26+
new webpack.NoEmitOnErrorsPlugin(),
2827
// https://github.com/ampedandwired/html-webpack-plugin
2928
new HtmlWebpackPlugin({
3029
filename: 'index.html',
3130
template: 'index.html',
3231
inject: true
3332
}),
34-
new FriendlyErrors()
33+
new FriendlyErrorsPlugin()
3534
]
3635
})

0 commit comments

Comments
 (0)