perf(SSR): bundle with native async/await and less transpilations

This commit is contained in:
Pooya Parsa 2017-12-08 17:48:37 +03:30
parent e010114014
commit afc874a11a
5 changed files with 31 additions and 20 deletions

View File

@ -128,19 +128,6 @@ export default class Builder {
// Call before hook // Call before hook
await this.nuxt.callHook('build:before', this, this.options.build) await this.nuxt.callHook('build:before', this, this.options.build)
// Babel options
this.babelOptions = _.defaults(this.options.build.babel, {
babelrc: false,
cacheDirectory: !!this.options.dev
})
if (!this.babelOptions.babelrc && !this.babelOptions.presets) {
this.babelOptions.presets = [
[require.resolve('babel-preset-vue-app'), {
targets: { ie: 9, uglify: true }
}]
]
}
// Map postcss plugins into instances on object mode once // Map postcss plugins into instances on object mode once
if (isPureObject(this.options.build.postcss)) { if (isPureObject(this.options.build.postcss)) {
if (isPureObject(this.options.build.postcss.plugins)) { if (isPureObject(this.options.build.postcss.plugins)) {
@ -193,6 +180,30 @@ export default class Builder {
return this return this
} }
getBabelOptions({ isServer }) {
const options = _.defaults({}, {
babelrc: false,
cacheDirectory: !!this.options.dev
}, this.options.build.babel)
if (typeof options.presets === 'function') {
options.presets = options.presets({ isServer })
}
if (!options.babelrc && !options.presets) {
options.presets = [
[
require.resolve('babel-preset-vue-app'),
{
targets: isServer ? { node: '8.0.0' } : { ie: 9, uglify: true }
}
]
]
}
return options
}
async generateRoutesAndFiles() { async generateRoutesAndFiles() {
debug('Generating files...') debug('Generating files...')
// -- Templates -- // -- Templates --

View File

@ -14,7 +14,7 @@ import WarnFixPlugin from './warnfix-plugin'
| webpack config files | webpack config files
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
export default function webpackBaseConfig(name) { export default function webpackBaseConfig({ name, isServer }) {
const nodeModulesDir = join(__dirname, '..', 'node_modules') const nodeModulesDir = join(__dirname, '..', 'node_modules')
const config = { const config = {
@ -65,13 +65,13 @@ export default function webpackBaseConfig(name) {
{ {
test: /\.vue$/, test: /\.vue$/,
loader: 'vue-loader', loader: 'vue-loader',
options: this.vueLoader() options: this.vueLoader({ isServer })
}, },
{ {
test: /\.js$/, test: /\.js$/,
loader: 'babel-loader', loader: 'babel-loader',
exclude: /node_modules/, exclude: /node_modules/,
options: Object.assign({}, this.babelOptions) options: this.getBabelOptions({ isServer })
}, },
{ test: /\.css$/, use: this.styleLoader('css') }, { test: /\.css$/, use: this.styleLoader('css') },
{ test: /\.less$/, use: this.styleLoader('less', 'less-loader') }, { test: /\.less$/, use: this.styleLoader('less', 'less-loader') },

View File

@ -25,7 +25,7 @@ debug.color = 2 // Force green color
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
export default function webpackClientConfig() { export default function webpackClientConfig() {
let config = base.call(this, 'client') let config = base.call(this, { name: 'client', isServer: false })
// Entry points // Entry points
config.entry.app = resolve(this.options.buildDir, 'client.js') config.entry.app = resolve(this.options.buildDir, 'client.js')

View File

@ -12,7 +12,7 @@ import base from './base.config.js'
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
export default function webpackServerConfig() { export default function webpackServerConfig() {
let config = base.call(this, 'server') let config = base.call(this, { name: 'server', isServer: true })
// env object defined in nuxt.config.js // env object defined in nuxt.config.js
let env = {} let env = {}

View File

@ -1,4 +1,4 @@
export default function vueLoader() { export default function vueLoader({ isServer }) {
// https://vue-loader.vuejs.org/en // https://vue-loader.vuejs.org/en
const config = { const config = {
postcss: this.options.build.postcss, postcss: this.options.build.postcss,
@ -8,7 +8,7 @@ export default function vueLoader() {
loaders: { loaders: {
'js': { 'js': {
loader: 'babel-loader', loader: 'babel-loader',
options: Object.assign({}, this.babelOptions) options: this.getBabelOptions({ isServer })
}, },
// Note: do not nest the `postcss` option under `loaders` // Note: do not nest the `postcss` option under `loaders`
'css': this.styleLoader('css', [], true), 'css': this.styleLoader('css', [], true),