From afc874a11af82b6737e5690a9617bc5464b77d34 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 8 Dec 2017 17:48:37 +0330 Subject: [PATCH] perf(SSR): bundle with native async/await and less transpilations --- lib/builder/builder.js | 37 +++++++++++++++--------- lib/builder/webpack/base.config.js | 6 ++-- lib/builder/webpack/client.config.js | 2 +- lib/builder/webpack/server.config.js | 2 +- lib/builder/webpack/vue-loader.config.js | 4 +-- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 5878a5e4fd..6000724fbd 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -128,19 +128,6 @@ export default class Builder { // Call before hook 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 if (isPureObject(this.options.build.postcss)) { if (isPureObject(this.options.build.postcss.plugins)) { @@ -193,6 +180,30 @@ export default class Builder { 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() { debug('Generating files...') // -- Templates -- diff --git a/lib/builder/webpack/base.config.js b/lib/builder/webpack/base.config.js index 762598e3df..cf4bf49cdc 100644 --- a/lib/builder/webpack/base.config.js +++ b/lib/builder/webpack/base.config.js @@ -14,7 +14,7 @@ import WarnFixPlugin from './warnfix-plugin' | webpack config files |-------------------------------------------------------------------------- */ -export default function webpackBaseConfig(name) { +export default function webpackBaseConfig({ name, isServer }) { const nodeModulesDir = join(__dirname, '..', 'node_modules') const config = { @@ -65,13 +65,13 @@ export default function webpackBaseConfig(name) { { test: /\.vue$/, loader: 'vue-loader', - options: this.vueLoader() + options: this.vueLoader({ isServer }) }, { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/, - options: Object.assign({}, this.babelOptions) + options: this.getBabelOptions({ isServer }) }, { test: /\.css$/, use: this.styleLoader('css') }, { test: /\.less$/, use: this.styleLoader('less', 'less-loader') }, diff --git a/lib/builder/webpack/client.config.js b/lib/builder/webpack/client.config.js index f11c81a9c4..258af68f31 100644 --- a/lib/builder/webpack/client.config.js +++ b/lib/builder/webpack/client.config.js @@ -25,7 +25,7 @@ debug.color = 2 // Force green color |-------------------------------------------------------------------------- */ export default function webpackClientConfig() { - let config = base.call(this, 'client') + let config = base.call(this, { name: 'client', isServer: false }) // Entry points config.entry.app = resolve(this.options.buildDir, 'client.js') diff --git a/lib/builder/webpack/server.config.js b/lib/builder/webpack/server.config.js index 451e70c304..fcdd5bfc8b 100644 --- a/lib/builder/webpack/server.config.js +++ b/lib/builder/webpack/server.config.js @@ -12,7 +12,7 @@ import base from './base.config.js' |-------------------------------------------------------------------------- */ 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 let env = {} diff --git a/lib/builder/webpack/vue-loader.config.js b/lib/builder/webpack/vue-loader.config.js index c5b608b6cf..cb291f8960 100644 --- a/lib/builder/webpack/vue-loader.config.js +++ b/lib/builder/webpack/vue-loader.config.js @@ -1,4 +1,4 @@ -export default function vueLoader() { +export default function vueLoader({ isServer }) { // https://vue-loader.vuejs.org/en const config = { postcss: this.options.build.postcss, @@ -8,7 +8,7 @@ export default function vueLoader() { loaders: { 'js': { loader: 'babel-loader', - options: Object.assign({}, this.babelOptions) + options: this.getBabelOptions({ isServer }) }, // Note: do not nest the `postcss` option under `loaders` 'css': this.styleLoader('css', [], true),