Skip to content

Commit 113eb65

Browse files
committed
feat: add transformScript to GeneratorAPI (#4188)
(cherry picked from commit 5460ca4)
1 parent 818e5dd commit 113eb65

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

packages/@vue/cli/__tests__/Generator.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,36 @@ test('generate a JS-Only value from a string', async () => {
727727
expect(generator.pkg).toHaveProperty('testScript')
728728
expect(typeof generator.pkg.testScript).toBe('function')
729729
})
730+
731+
test('run a codemod on the entry file', async () => {
732+
// A test codemod that tranforms `new Vue` to `new TestVue`
733+
const codemod = (fileInfo, api) => {
734+
const j = api.jscodeshift
735+
return j(fileInfo.source)
736+
.find(j.NewExpression, {
737+
callee: { name: 'Vue' },
738+
arguments: [{ type: 'ObjectExpression' }]
739+
})
740+
.replaceWith(({ node }) => {
741+
node.callee.name = 'TestVue'
742+
return node
743+
})
744+
.toSource()
745+
}
746+
747+
const generator = new Generator('/', { plugins: [
748+
{
749+
id: 'test',
750+
apply: api => {
751+
api.render({
752+
'main.js': path.join(templateDir, 'entry.js')
753+
})
754+
755+
api.transformScript('main.js', codemod)
756+
}
757+
}
758+
] })
759+
760+
await generator.generate()
761+
expect(fs.readFileSync('/main.js', 'utf-8')).toMatch(/new TestVue/)
762+
})

packages/@vue/cli/lib/GeneratorAPI.js

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const resolve = require('resolve')
66
const { isBinaryFileSync } = require('isbinaryfile')
77
const semver = require('semver')
88
const mergeDeps = require('./util/mergeDeps')
9+
const runCodemod = require('./util/runCodemod')
910
const stringifyJS = require('./util/stringifyJS')
1011
const ConfigTransform = require('./ConfigTransform')
1112
const { getPluginLink, toShortPluginId, loadModule } = require('@vue/cli-shared-utils')
@@ -300,6 +301,22 @@ class GeneratorAPI {
300301
return fn
301302
}
302303

304+
/**
305+
* Run codemod on a script file or the script part of a .vue file
306+
* @param {string} file the path to the file to transform
307+
* @param {Codemod} codemod the codemod module to run
308+
* @param {object} options additional options for the codemod
309+
*/
310+
transformScript (file, codemod, options) {
311+
this._injectFileMiddleware(files => {
312+
files[file] = runCodemod(
313+
codemod,
314+
{ path: this.resolve(file), source: files[file] },
315+
options
316+
)
317+
})
318+
}
319+
303320
/**
304321
* Add import statements to a file.
305322
*/

packages/@vue/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"shortid": "^2.2.11",
5757
"slash": "^2.0.0",
5858
"validate-npm-package-name": "^3.0.0",
59-
"vue-jscodeshift-adapter": "2.0.2",
59+
"vue-jscodeshift-adapter": "^2.0.2",
6060
"yaml-front-matter": "^3.4.1"
6161
},
6262
"engines": {

0 commit comments

Comments
 (0)