mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-03-12 04:03:17 -05:00
feat: add transformScript to GeneratorAPI (#4188)
(cherry picked from commit 5460ca498f)
This commit is contained in:
@@ -727,3 +727,36 @@ test('generate a JS-Only value from a string', async () => {
|
||||
expect(generator.pkg).toHaveProperty('testScript')
|
||||
expect(typeof generator.pkg.testScript).toBe('function')
|
||||
})
|
||||
|
||||
test('run a codemod on the entry file', async () => {
|
||||
// A test codemod that tranforms `new Vue` to `new TestVue`
|
||||
const codemod = (fileInfo, api) => {
|
||||
const j = api.jscodeshift
|
||||
return j(fileInfo.source)
|
||||
.find(j.NewExpression, {
|
||||
callee: { name: 'Vue' },
|
||||
arguments: [{ type: 'ObjectExpression' }]
|
||||
})
|
||||
.replaceWith(({ node }) => {
|
||||
node.callee.name = 'TestVue'
|
||||
return node
|
||||
})
|
||||
.toSource()
|
||||
}
|
||||
|
||||
const generator = new Generator('/', { plugins: [
|
||||
{
|
||||
id: 'test',
|
||||
apply: api => {
|
||||
api.render({
|
||||
'main.js': path.join(templateDir, 'entry.js')
|
||||
})
|
||||
|
||||
api.transformScript('main.js', codemod)
|
||||
}
|
||||
}
|
||||
] })
|
||||
|
||||
await generator.generate()
|
||||
expect(fs.readFileSync('/main.js', 'utf-8')).toMatch(/new TestVue/)
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ const resolve = require('resolve')
|
||||
const { isBinaryFileSync } = require('isbinaryfile')
|
||||
const semver = require('semver')
|
||||
const mergeDeps = require('./util/mergeDeps')
|
||||
const runCodemod = require('./util/runCodemod')
|
||||
const stringifyJS = require('./util/stringifyJS')
|
||||
const ConfigTransform = require('./ConfigTransform')
|
||||
const { getPluginLink, toShortPluginId, loadModule } = require('@vue/cli-shared-utils')
|
||||
@@ -300,6 +301,22 @@ class GeneratorAPI {
|
||||
return fn
|
||||
}
|
||||
|
||||
/**
|
||||
* Run codemod on a script file or the script part of a .vue file
|
||||
* @param {string} file the path to the file to transform
|
||||
* @param {Codemod} codemod the codemod module to run
|
||||
* @param {object} options additional options for the codemod
|
||||
*/
|
||||
transformScript (file, codemod, options) {
|
||||
this._injectFileMiddleware(files => {
|
||||
files[file] = runCodemod(
|
||||
codemod,
|
||||
{ path: this.resolve(file), source: files[file] },
|
||||
options
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Add import statements to a file.
|
||||
*/
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
"shortid": "^2.2.11",
|
||||
"slash": "^2.0.0",
|
||||
"validate-npm-package-name": "^3.0.0",
|
||||
"vue-jscodeshift-adapter": "2.0.2",
|
||||
"vue-jscodeshift-adapter": "^2.0.2",
|
||||
"yaml-front-matter": "^3.4.1"
|
||||
},
|
||||
"engines": {
|
||||
|
||||
Reference in New Issue
Block a user