Merge remote-tracking branch 'nuxt/master'

This commit is contained in:
Alexandre Chopin 2016-11-09 23:43:48 +01:00
commit 659df9cab6
19 changed files with 135 additions and 56 deletions

View File

@ -1,7 +1,7 @@
## Under development, will be release soon :fire: ## Under development, will be release soon :fire:
# nuxt.js <img src="https://raw.githubusercontent.com/nuxt/nuxt.js/master/examples/hello-world/static/nuxt.png"/>
>A minimalistic framework for server-rendered Vue applications (inspired from [Next.js](https://github.com/zeit/next.js)) > Nuxt.js is a minimalistic framework for server-rendered Vue applications (inspired by [Next.js](https://github.com/zeit/next.js))
## How to use ## How to use
@ -65,9 +65,11 @@ const options = {
routes: [], // see examples/custom-routes routes: [], // see examples/custom-routes
css: ['/dist/boostrap.css'] // see examples/global-css css: ['/dist/boostrap.css'] // see examples/global-css
store: true // see examples/vuex-store store: true // see examples/vuex-store
vendor: ['axios'], // see examples/plugins-vendor
plugins: ['public/plugin.js'], // see examples/plugins-vendor plugins: ['public/plugin.js'], // see examples/plugins-vendor
loading: false or { color: 'blue', failedColor: 'red' } or 'components/my-loader' // see examples/custom-loading loading: false or { color: 'blue', failedColor: 'red' } or 'components/my-loader' // see examples/custom-loading
build: {
vendor: ['axios'] // see examples/plugins-vendor
}
} }
// Launch nuxt build with given options // Launch nuxt build with given options

View File

@ -1,5 +1,7 @@
module.exports = { module.exports = {
vendor: ['axios'], // Add axios in the vendor.bundle.js build: {
vendor: ['axios'] // Add axios in the vendor.bundle.js
},
loading: { loading: {
color: '#4FC08D', color: '#4FC08D',
failedColor: '#bf5050', failedColor: '#bf5050',

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -0,0 +1,21 @@
module.exports = {
build: {
filenames: {
css: 'app.css', // default: style.css
vendor: 'vendor.js', // default: vendor.bundle.js
app: 'app.js' // default: nuxt.bundle.js
},
vendor: ['lodash'],
// Loaders config (Webpack 2)
loaders: [
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url',
options: {
limit: 100000, // 100KO
name: 'img/[name].[ext]?[hash]'
}
}
]
}
}

View File

@ -0,0 +1,10 @@
{
"name": "custom-build",
"description": "",
"dependencies": {
"nuxt": "latest"
},
"scripts": {
"start": "nuxt"
}
}

View File

@ -0,0 +1,16 @@
<template>
<div class="container">
<p><img src="../assets/nuxt.png" /></p>
<p>This image is included as data:image/png;base64...</p>
<p>In the source code, the files generated are based on the build.filenames data.</p>
<p>If you look at the <a href="/_nuxt/vendor.js">vendor.js</a>, lodash has been included (cmd/ctrl + F "lodash").</p>
</div>
</template>
<style>
.container {
font-size: 20px;
text-align: center;
padding: 100px;
}
</style>

View File

@ -1,4 +1,9 @@
<template> <template>
<nuxt-head>
<title>I am the title of the page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</nuxt-head>
<h1>This page has a title 🤔</h1> <h1>This page has a title 🤔</h1>
</template> </template>

View File

@ -2,7 +2,7 @@
<div class="container"> <div class="container">
<img src="/static/nuxt.png" /> <img src="/static/nuxt.png" />
<h2>About</h2> <h2>About</h2>
<p><router-link to='/'>Home</router-link></p> <p><router-link to="/">Home</router-link></p>
</div> </div>
</template> </template>

View File

@ -2,7 +2,7 @@
<div class="container"> <div class="container">
<img src="/static/nuxt.png" /> <img src="/static/nuxt.png" />
<h2>Hello World.</h2> <h2>Hello World.</h2>
<p><router-link to='/about'>About</router-link></p> <p><router-link to="/about">About</router-link></p>
</div> </div>
</template> </template>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -1,18 +1,20 @@
# Using external modules and plugings with Nuxt.js # Using external modules and plugings with Nuxt.js
## Configuration: `vendor` ## Configuration: `build.vendor`
> Nuxt.js allows you to add modules inside the `vendor.bundle.js` file generated to reduce the size of the app bundle. It's really useful when using external modules (like `axios` for example) > Nuxt.js allows you to add modules inside the `vendor.bundle.js` file generated to reduce the size of the app bundle. It's really useful when using external modules (like `axios` for example)
To add a module/file inside the vendor bundle, add the `vendor` key inside `nuxt.config.js`: To add a module/file inside the vendor bundle, add the `build.vendor` key inside `nuxt.config.js`:
```js ```js
const { join } = require('path') const { join } = require('path')
module.exports = { module.exports = {
vendor: [ build: {
'axios', // node module vendor: [
join(__dirname, './js/my-library.js') // custom file 'axios', // node module
] join(__dirname, './js/my-library.js') // custom file
]
}
} }
``` ```
@ -35,7 +37,9 @@ Then, I add my file inside the `plugins` key of `nuxt.config.js`:
const { join } = require('path') const { join } = require('path')
module.exports = { module.exports = {
vendor: ['vue-notifications'], build: {
vendor: ['vue-notifications']
},
plugins: [ join(__dirname, './plugins/vue-notifications') ] plugins: [ join(__dirname, './plugins/vue-notifications') ]
} }
``` ```

View File

@ -1,6 +1,8 @@
const { join } = require('path') const { join } = require('path')
module.exports = { module.exports = {
vendor: ['axios', 'mini-toastr', 'vue-notifications'], build: {
vendor: ['axios', 'mini-toastr', 'vue-notifications']
},
plugins: [ join(__dirname, './plugins/vue-notifications.js') ] plugins: [ join(__dirname, './plugins/vue-notifications.js') ]
} }

View File

@ -72,4 +72,4 @@ export default {
## Context ## Context
To see the list of available keys in `context`, take a look at [this documentation](https://github.com/Atinux/nuxt.js/tree/master/examples/async-data#context). To see the list of available keys in `context`, take a look at [this documentation](https://github.com/nuxt/nuxt.js/tree/master/examples/async-data#context).

View File

@ -1,14 +1,14 @@
<template> <template>
<div id="app"> <div id="app">
<% if (loading) { %><loading ref="loading"></loading><% } %> <% if (loading) { %><nuxt-loading ref="loading"></nuxt-loading><% } %>
<router-view v-if="!err"></router-view> <router-view v-if="!err"></router-view>
<error-page v-if="err" :error="err"></error-page> <nuxt-error v-if="err" :error="err"></nuxt-error>
</div> </div>
</template> </template>
<script> <script>
import ErrorPage from '<%= components.ErrorPage %>' import NuxtError from '<%= components.ErrorPage %>'
<% if (loading) { %>import Loading from '<%= (typeof loading === "string" ? loading : "./components/Loading.vue") %>'<% } %> <% if (loading) { %>import NuxtLoading from '<%= (typeof loading === "string" ? loading : "./components/Loading.vue") %>'<% } %>
export default { export default {
data () { data () {
@ -34,7 +34,7 @@ export default {
} }
}, },
components: { components: {
ErrorPage<%= (loading ? ',\n\t\tLoading' : '') %> NuxtError<%= (loading ? ',\n\t\tNuxtLoading' : '') %>
} }
} }
</script> </script>

View File

@ -172,7 +172,7 @@ Promise.all(resolveComponents)
router.beforeEach(render.bind(_app)) router.beforeEach(render.bind(_app))
// Call window.onModulesLoaded for jsdom testing (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading) // Call window.onModulesLoaded for jsdom testing (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
if (typeof window.onNuxtReady === 'function') { if (typeof window.onNuxtReady === 'function') {
window.onNuxtReady() window.onNuxtReady(_app)
} }
}) })
.catch((err) => { .catch((err) => {

View File

@ -13,7 +13,44 @@ const { createBundleRenderer } = require('vue-server-renderer')
const { join, resolve } = require('path') const { join, resolve } = require('path')
const r = resolve const r = resolve
const defaults = {
filenames: {
css: 'style.css',
vendor: 'vendor.bundle.js',
app: 'nuxt.bundle.js'
},
vendor: [],
loaders: []
}
const defaultsLoaders = [
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url',
options: {
limit: 1000, // 1KO
name: 'img/[name].[ext]?[hash]'
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url',
query: {
limit: 1000, // 1 KO
name: 'fonts/[name].[hash:7].[ext]'
}
}
]
module.exports = function * () { module.exports = function * () {
if (this.options.build === false) {
return Promise.resolve()
}
// Defaults build options
if (this.options.build && Array.isArray(this.options.build.loaders)) {
this.options.build = _.defaultsDeep(this.options.build, defaults)
} else {
this.options.build = _.defaultsDeep(this.options.build, defaults, { loaders: defaultsLoaders })
}
/* /*
** Check if pages dir exists and warn if not ** Check if pages dir exists and warn if not
*/ */
@ -125,7 +162,6 @@ module.exports = function * () {
webpackRunServer.call(this) webpackRunServer.call(this)
] ]
} }
return this
} }
function addGlobalWebpackConfig (config) { function addGlobalWebpackConfig (config) {
@ -142,37 +178,40 @@ function addGlobalWebpackConfig (config) {
join(this.dir, 'node_modules') join(this.dir, 'node_modules')
] ]
} }
config.module.rules = config.module.rules.concat(this.options.build.loaders)
return config return config
} }
function getWebpackClientConfig () { function getWebpackClientConfig () {
var config = require(r(__dirname, 'webpack', 'client.config.js')) var config = require(r(__dirname, 'webpack', 'client.config.js'))
config = _.cloneDeep(config)
// Entry // Entry
config.entry.app = r(this.dir, '.nuxt', 'client.js') config.entry.app = r(this.dir, '.nuxt', 'client.js')
// Add vendors // Add vendors
if (this.options.store) config.entry.vendor.push('vuex') if (this.options.store) config.entry.vendor.push('vuex')
config.entry.vendor = config.entry.vendor.concat(this.options.vendor) config.entry.vendor = config.entry.vendor.concat(this.options.build.vendor)
// extract vendor chunks for better caching // extract vendor chunks for better caching
config.plugins.push( config.plugins.push(
new webpack.optimize.CommonsChunkPlugin({ new webpack.optimize.CommonsChunkPlugin({
name: 'vendor', name: 'vendor',
filename: this.options.filenames.vendor filename: this.options.build.filenames.vendor
}) })
) )
// Output // Output
config.output.path = r(this.dir, '.nuxt', 'dist') config.output.path = r(this.dir, '.nuxt', 'dist')
config.output.filename = this.options.filenames.app config.output.filename = this.options.build.filenames.app
// Extract text plugin // Extract text plugin
if (this.isProd) { if (this.isProd) {
const ExtractTextPlugin = require('extract-text-webpack-plugin') const ExtractTextPlugin = require('extract-text-webpack-plugin')
let plugin = config.plugins.find((plugin) => plugin instanceof ExtractTextPlugin) let plugin = config.plugins.find((plugin) => plugin instanceof ExtractTextPlugin)
if (plugin) plugin.filename = this.options.filenames.css if (plugin) plugin.filename = this.options.build.filenames.css
} }
return addGlobalWebpackConfig.call(this, config) return addGlobalWebpackConfig.call(this, config)
} }
function getWebpackServerConfig () { function getWebpackServerConfig () {
var config = require(r(__dirname, 'webpack', 'server.config.js')) var config = require(r(__dirname, 'webpack', 'server.config.js'))
config = _.cloneDeep(config)
// Entry // Entry
config.entry = r(this.dir, '.nuxt', 'server.js') config.entry = r(this.dir, '.nuxt', 'server.js')
// Output // Output

View File

@ -30,22 +30,6 @@ module.exports = {
options: { options: {
presets: ['es2015', 'stage-2'] presets: ['es2015', 'stage-2']
} }
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url',
options: {
limit: 1000, // 1KO
name: 'img/[name].[ext]?[hash]'
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url',
query: {
limit: 1000, // 1 KO
name: 'fonts/[name].[hash:7].[ext]'
}
} }
] ]
} }

View File

@ -17,13 +17,7 @@ class Nuxt {
constructor (options = {}, cb) { constructor (options = {}, cb) {
var defaults = { var defaults = {
filenames: {
css: 'style.css',
vendor: 'vendor.bundle.js',
app: 'nuxt.bundle.js'
},
routes: [], routes: [],
vendor: [],
plugins: [], plugins: [],
css: [], css: [],
store: false, store: false,
@ -56,9 +50,9 @@ class Nuxt {
this.build = build.bind(this) this.build = build.bind(this)
// Launch build and set this.renderer // Launch build and set this.renderer
return co(this.build) return co(this.build)
.then((nuxt) => { .then(() => {
if (typeof cb === 'function') cb(null, nuxt) if (typeof cb === 'function') cb(null, this)
return nuxt return this
}) })
.catch((err) => { .catch((err) => {
if (typeof cb === 'function') cb(err) if (typeof cb === 'function') cb(err)
@ -125,9 +119,9 @@ class Nuxt {
APP: html, APP: html,
context: context, context: context,
files: { files: {
css: join('/_nuxt/', self.options.filenames.css), css: join('/_nuxt/', self.options.build.filenames.css),
vendor: join('/_nuxt/', self.options.filenames.vendor), vendor: join('/_nuxt/', self.options.build.filenames.vendor),
app: join('/_nuxt/', self.options.filenames.app) app: join('/_nuxt/', self.options.build.filenames.app)
} }
}) })
return app return app

View File

@ -1,10 +1,10 @@
{ {
"name": "nuxt", "name": "nuxt",
"version": "0.2.3", "version": "0.2.5",
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)", "description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"repository": "Atinux/nuxt.js", "repository": "nuxt/nuxt.js",
"bin": { "bin": {
"nuxt": "./bin/nuxt" "nuxt": "./bin/nuxt"
}, },