feat(babel): transform preset names in the plugin migrator (#4629)

This commit is contained in:
Haoqun Jiang
2019-09-29 22:13:31 +08:00
committed by GitHub
parent c53a49dc98
commit c42acfcca3
13 changed files with 100 additions and 10 deletions

View File

@@ -4,3 +4,4 @@ packages/test
temp
entry-wc.js
dist
__testfixtures__

View File

@@ -0,0 +1,7 @@
module.exports = {
presets: [
["@vue/app", {
polyfills: []
}]
]
}

View File

@@ -0,0 +1,7 @@
module.exports = {
presets: [
["@vue/cli-plugin-babel/preset", {
polyfills: []
}]
]
}

View File

@@ -0,0 +1,3 @@
module.exports = {
presets: ["@vue/app"]
}

View File

@@ -0,0 +1,3 @@
module.exports = {
presets: ["@vue/cli-plugin-babel/preset"]
}

View File

@@ -0,0 +1,9 @@
const config = {
presets: [
[require("@vue/babel-preset-app"), {
polyfills: []
}]
]
}
module.exports = config

View File

@@ -0,0 +1,9 @@
const config = {
presets: [
[require("@vue/cli-plugin-babel/preset"), {
polyfills: []
}]
]
}
module.exports = config

View File

@@ -0,0 +1,3 @@
module.exports = {
presets: [`@vue/app`]
}

View File

@@ -0,0 +1,3 @@
module.exports = {
presets: [`@vue/cli-plugin-babel/preset`]
}

View File

@@ -0,0 +1,9 @@
jest.autoMockOff()
const { defineTest } = require('jscodeshift/dist/testUtils')
defineTest(__dirname, 'usePluginPreset', null, 'default')
defineTest(__dirname, 'usePluginPreset', null, 'customConfig')
defineTest(__dirname, 'usePluginPreset', null, 'require')
defineTest(__dirname, 'usePluginPreset', null, 'templateLiteral')

View File

@@ -0,0 +1,41 @@
module.exports = function (fileInfo, api) {
const j = api.jscodeshift
const root = j(fileInfo.source)
root
.find(j.Literal, { value: '@vue/app' })
.forEach(({ node }) => {
node.value = '@vue/cli-plugin-babel/preset'
})
root
.find(j.Literal, { value: '@vue/babel-preset-app' })
.forEach(({ node }) => {
node.value = '@vue/cli-plugin-babel/preset'
})
const templateLiterals = root
.find(j.TemplateLiteral, {
expressions: { length: 0 }
})
templateLiterals
.find(j.TemplateElement, {
value: {
cooked: '@vue/app'
}
})
.forEach(({ node }) => {
node.value = { cooked: '@vue/cli-plugin-babel/preset', raw: '@vue/cli-plugin-babel/preset' }
})
templateLiterals
.find(j.TemplateElement, {
value: {
cooked: '@vue/babel-preset-app'
}
})
.forEach(({ node }) => {
node.value = { cooked: '@vue/cli-plugin-babel/preset', raw: '@vue/cli-plugin-babel/preset' }
})
return root.toSource()
}

View File

@@ -1,16 +1,6 @@
const { chalk } = require('@vue/cli-shared-utils')
module.exports = (api) => {
// TODO: backport this part to v3
// if (api.fromVersion('<=3.5.3')) {
// // add core-js@2 as dependency
// api.extendPackage({
// dependencies: {
// 'core-js': '^2.6.5'
// }
// })
// }
if (api.fromVersion('^3')) {
api.extendPackage({
dependencies: {
@@ -18,6 +8,8 @@ module.exports = (api) => {
}
}, true)
api.transformScript('babel.config.js', require('../codemods/usePluginPreset'))
// TODO: implement a codemod to migrate polyfills
api.exitLog(`core-js has been upgraded from v2 to v3.
If you have any custom polyfills defined in ${chalk.yellow('babael.config.js')}, please be aware their names may have been changed.

View File

@@ -29,6 +29,9 @@
"peerDependencies": {
"@vue/cli-service": "^3.0.0 || ^4.0.0-0"
},
"devDependencies": {
"jscodeshift": "^0.6.4"
},
"publishConfig": {
"access": "public"
}