Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from ryutamaki/feature/webpack2
Browse files Browse the repository at this point in the history
webpack v2
  • Loading branch information
ryutamaki authored Feb 5, 2017
2 parents 876dbb4 + 4c3fe47 commit 353e70c
Show file tree
Hide file tree
Showing 26 changed files with 255 additions and 187 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $ npm run dev
- HTML minified with [html-minifier](https://github.com/kangax/html-minifier).
- CSS across all components extracted into a single file and minified with [cssnano](https://github.com/ben-eb/cssnano).
- All static assets compiled with version hashes for efficient long-term caching, and a production `index.html` is auto-generated with proper URLs to these generated assets.
- Use `npm run build --report`to build with bundle size analytics.

- `npm run unit`: Unit tests run in PhantomJS with [Karma](http://karma-runner.github.io/0.13/index.html) + [Mocha](http://mochajs.org/) + [karma-webpack](https://github.com/webpack/karma-webpack).
- Supports typescript in test files.
Expand Down
7 changes: 7 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
machine:
node:
version: 6

test:
override:
- bash test.sh
15 changes: 15 additions & 0 deletions docs/proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ module.exports = {
```

The above example will proxy the request `/api/posts/1` to `http://jsonplaceholder.typicode.com/posts/1`.

## URL Matching

In addition to static urls you can also use glob patterns to match URLs, e.g. `/api/**`. See [Context Matching](https://github.com/chimurai/http-proxy-middleware#context-matching) for more details. In addition, you can provide a `filter` option that can be a custom function to determine whether a request should be proxied:

``` js
proxyTable: {
'*': {
target: 'http://jsonplaceholder.typicode.com',
filter: function (pathname, req) {
return pathname.match('^/api') && req.method === 'GET'
}
}
}
```
10 changes: 5 additions & 5 deletions docs/structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
│ │   ├── index.js # test build entry file
│ │   └── karma.conf.js # test runner config file
│ └── e2e/ # e2e tests
│    ├── specs/ # test spec files
│    ├── custom-assertions/ # custom assertions for e2e tests
│    ├── runner.js # test runner script
   └── nightwatch.conf.js # test runner config file
├── .editorconfig.js # editor config
   ├── specs/ # test spec files
   ├── custom-assertions/ # custom assertions for e2e tests
   ├── runner.js # test runner script
    └── nightwatch.conf.js # test runner config file
├── .editorconfig # editor config
├── index.html # index.html template
└── package.json # build scripts and dependencies
```
Expand Down
8 changes: 7 additions & 1 deletion meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module.exports = {
}
]
},
"router": {
"type": "confirm",
"message": "Install vue-router?"
},
"unit": {
"type": "confirm",
"message": "Setup unit tests with Karma + Mocha?"
Expand All @@ -52,7 +56,9 @@ module.exports = {
"filters": {
"config/test.env.js": "unit || e2e",
"test/unit/**/*": "unit",
"test/e2e/**/*": "e2e"
"build/webpack.test.conf.js": "unit",
"test/e2e/**/*": "e2e",
"src/router/**/*": "router"
},
"completeMessage": "To get started:\n\n cd {{destDirName}}\n npm install\n npm run dev\n\nDocumentation can be found at https://vuejs-templates.github.io/webpack"
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"description": "A full-featured Webpack setup with hot-reload, unit testing & css extraction.",
"scripts": {
"docs": "cd docs && gitbook serve",
"deploy-docs": "bash ./deploy-docs.sh"
"docs:deploy": "bash ./deploy-docs.sh"
},
"devDependencies": {
"vue-cli": "^2.8.1"
}
}
5 changes: 4 additions & 1 deletion template/.babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"presets": ["es2015", "stage-2"],
"presets": [
["es2015", { "modules": false }],
"stage-2"
],
"plugins": ["transform-runtime"],
"comments": false,
"env": {
Expand Down
3 changes: 3 additions & 0 deletions template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ npm run dev

# build for production with minification
npm run build

# build for production and view the bundle analyzer report
npm run build --report
{{#unit}}

# run unit tests
Expand Down
32 changes: 18 additions & 14 deletions template/build/build.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// https://github.com/shelljs/shelljs
require('./check-versions')()
require('shelljs/global')
env.NODE_ENV = 'production'

var path = require('path')
var config = require('../config')
process.env.NODE_ENV = 'production'

var ora = require('ora')
var path = require('path')
var chalk = require('chalk')
var shell = require('shelljs')
var webpack = require('webpack')
var config = require('../config')
var webpackConfig = require('./webpack.prod.conf')

console.log(
' Tip:\n' +
' Built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
)

var spinner = ora('building for production...')
spinner.start()

var assetsPath = path.join(config.build.assetsRoot, config.build.assetsSubDirectory)
rm('-rf', assetsPath)
mkdir('-p', assetsPath)
cp('-R', 'static/*', assetsPath)
shell.rm('-rf', assetsPath)
shell.mkdir('-p', assetsPath)
shell.config.silent = true
shell.cp('-R', 'static/*', assetsPath)
shell.config.silent = false

webpack(webpackConfig, function (err, stats) {
spinner.stop()
Expand All @@ -32,5 +30,11 @@ webpack(webpackConfig, function (err, stats) {
children: false,
chunks: false,
chunkModules: false
}) + '\n')
}) + '\n\n')

console.log(chalk.cyan(' Build complete.\n'))
console.log(chalk.yellow(
' Tip: built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
))
})
8 changes: 4 additions & 4 deletions template/build/check-versions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var semver = require('semver')
var chalk = require('chalk')
var semver = require('semver')
var packageConfig = require('../package.json')
var exec = function (cmd) {
return require('child_process')
.execSync(cmd).toString().trim()

function exec (cmd) {
return require('child_process').execSync(cmd).toString().trim()
}

var versionRequirements = [
Expand Down
14 changes: 10 additions & 4 deletions template/build/dev-server.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
require('./check-versions')()

var config = require('../config')
if (!process.env.NODE_ENV) process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
}

var opn = require('opn')
var path = require('path')
var express = require('express')
var webpack = require('webpack')
var opn = require('opn')
var proxyMiddleware = require('http-proxy-middleware')
var webpackConfig = {{#if_or unit e2e}}process.env.NODE_ENV === 'testing'
? require('./webpack.prod.conf')
: {{/if_or}}require('./webpack.dev.conf')
// default port where dev server listens for incoming traffic
var port = process.env.PORT || config.dev.port
// automatically open browser, if not set will be false
var autoOpenBrowser = !!config.dev.autoOpenBrowser
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
var proxyTable = config.dev.proxyTable
Expand Down Expand Up @@ -41,7 +47,7 @@ Object.keys(proxyTable).forEach(function (context) {
if (typeof options === 'string') {
options = { target: options }
}
app.use(proxyMiddleware(context, options))
app.use(proxyMiddleware(options.filter || context, options))
})

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

// when env is testing, don't need open it
if (process.env.NODE_ENV !== 'testing') {
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
})
11 changes: 7 additions & 4 deletions template/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ exports.cssLoaders = function (options) {
extraParamChar = '?'
}
return loader + (options.sourceMap ? extraParamChar + 'sourceMap' : '')
}).join('!')
})

// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ExtractTextPlugin.extract('vue-style-loader', sourceLoader)
return ExtractTextPlugin.extract({
loader: sourceLoader,
fallback: 'vue-style-loader'
})
} else {
return ['vue-style-loader', sourceLoader].join('!')
return ['vue-style-loader', ...sourceLoader]
}
}

Expand All @@ -54,7 +57,7 @@ exports.styleLoaders = function (options) {
var loader = loaders[extension]
output.push({
test: new RegExp('\\.' + extension + '$'),
loader: loader
use: loader
})
}
return output
Expand Down
17 changes: 17 additions & 0 deletions template/build/vue-loader.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var utils = require('./utils')
var config = require('../config')
var isProduction = process.env.NODE_ENV === 'production'

module.exports = {
loaders: utils.cssLoaders({
sourceMap: isProduction
? config.build.productionSourceMap
: config.dev.cssSourceMap,
extract: isProduction
}),
postcss: [
require('autoprefixer')({
browsers: ['last 2 versions']
})
]
}
69 changes: 26 additions & 43 deletions template/build/webpack.base.conf.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,66 @@
var path = require('path')
var config = require('../config')
var utils = require('./utils')
var projectRoot = path.resolve(__dirname, '../')
var config = require('../config')
var vueLoaderConfig = require('./vue-loader.conf')

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

module.exports = {
entry: {
app: './src/main.ts'
},
output: {
path: config.build.assetsRoot,
publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
filename: '[name].js'
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['', '.js', '.ts', '.vue', '.json'],
fallback: [path.join(__dirname, '../node_modules')],
extensions: ['.js', '.ts', '.vue', '.json'],
modules: [
resolve('src'),
resolve('node_modules')
],
alias: {
{{#if_eq build "standalone"}}
'vue$': 'vue/dist/vue.common.js',
{{/if_eq}}
'src': path.resolve(__dirname, '../src'),
'assets': path.resolve(__dirname, '../src/assets'),
'components': path.resolve(__dirname, '../src/components')
'src': resolve('src'),
'assets': resolve('src/assets'),
'components': resolve('src/components')
}
},
resolveLoader: {
fallback: [path.join(__dirname, '../node_modules')]
},
module: {
loaders: [
rules: [
{
test: /\.vue$/,
loader: 'vue'
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.ts$/,
loader: 'ts'
},
{
test: /\.json$/,
loader: 'json'
loader: 'ts-loader',
options: {appendTsSuffixTo: [/\.vue$/]}
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url',
query: {
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url',
query: {
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
vue: {
loaders: Object.assign(
utils.cssLoaders({ sourceMap: useCssSourceMap }),
{js: 'ts'}
),
postcss: [
require('autoprefixer')({
browsers: ['last 2 versions']
})
]
},
ts: {
appendTsSuffixTo: [/\.vue$/]
}
}
Loading

0 comments on commit 353e70c

Please sign in to comment.