Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add transformScript to GeneratorAPI #4188

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/@vue/cli/__tests__/Generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
})
17 changes: 17 additions & 0 deletions packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const merge = require('deepmerge')
const resolve = require('resolve')
const { isBinaryFileSync } = require('isbinaryfile')
const mergeDeps = require('./util/mergeDeps')
const runCodemod = require('./util/runCodemod')
const stringifyJS = require('./util/stringifyJS')
const ConfigTransform = require('./ConfigTransform')
const { getPluginLink, toShortPluginId } = require('@vue/cli-shared-utils')
Expand Down Expand Up @@ -250,6 +251,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.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"shortid": "^2.2.11",
"slash": "^3.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": {
Expand Down