From 31ef70e5602ec9c315cf2b92c8369d83e31a58cb Mon Sep 17 00:00:00 2001 From: Clark Du Date: Thu, 8 Nov 2018 20:47:53 +0000 Subject: [PATCH] fix: auto transform and warning for @nuxtjs/babel-preset-app (#4297) --- packages/config/src/options.js | 27 +++++++++++++++++++++++-- test/unit/basic.config.defaults.test.js | 24 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/config/src/options.js b/packages/config/src/options.js index 6e13e079f7..b2e6bea3a9 100644 --- a/packages/config/src/options.js +++ b/packages/config/src/options.js @@ -170,9 +170,32 @@ export function getNuxtConfig(_options) { options.build.cssSourceMap = options.dev } + const babelConfig = options.build.babel // babel cacheDirectory - if (options.build.babel.cacheDirectory === undefined) { - options.build.babel.cacheDirectory = options.dev + if (babelConfig.cacheDirectory === undefined) { + babelConfig.cacheDirectory = options.dev + } + + // TODO: remove this warn in Nuxt 3 + if (Array.isArray(babelConfig.presets)) { + const warnPreset = (presetName) => { + const oldPreset = '@nuxtjs/babel-preset-app' + const newPreset = '@nuxt/babel-preset-app' + if (presetName.includes(oldPreset)) { + presetName = presetName.replace(oldPreset, newPreset) + consola.warn('@nuxtjs/babel-preset-app has been deprecated, please use @nuxt/babel-preset-app.') + } + return presetName + } + babelConfig.presets = babelConfig.presets.map((preset) => { + const hasOptions = Array.isArray(preset) + if (hasOptions) { + preset[0] = warnPreset(preset[0]) + } else if (typeof preset === 'string') { + preset = warnPreset(preset) + } + return preset + }) } // vue config diff --git a/test/unit/basic.config.defaults.test.js b/test/unit/basic.config.defaults.test.js index e533a5c101..857060e71e 100644 --- a/test/unit/basic.config.defaults.test.js +++ b/test/unit/basic.config.defaults.test.js @@ -37,4 +37,28 @@ describe('basic config defaults', () => { options = getNuxtConfig({ globalName: 'foo?' }) expect(options.globalName).toEqual('nuxt') }) + + test('@nuxtjs/babel-preset-app has been deprecated', () => { + let options = getNuxtConfig({ + build: { + babel: { + presets: ['@nuxtjs/babel-preset-app'] + } + } + }) + expect(options.build.babel.presets).toEqual(['@nuxt/babel-preset-app']) + expect(consola.warn).toHaveBeenCalledWith('@nuxtjs/babel-preset-app has been deprecated, please use @nuxt/babel-preset-app.') + + consola.warn.mockClear() + + options = getNuxtConfig({ + build: { + babel: { + presets: [['@nuxtjs/babel-preset-app']] + } + } + }) + expect(options.build.babel.presets).toEqual([['@nuxt/babel-preset-app']]) + expect(consola.warn).toHaveBeenCalledWith('@nuxtjs/babel-preset-app has been deprecated, please use @nuxt/babel-preset-app.') + }) })