Skip to content

feat(cli): Added makeJSOnlyValue to generator API. Provides convenien… #3568

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

Merged
merged 1 commit into from
Apr 9, 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
8 changes: 8 additions & 0 deletions docs/dev-guide/generator-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ Add a message to be printed when the generator exits (after any other standard m
- **Usage**:
Convenience method for generating a JS config file from JSON

## makeJSOnlyValue

- **Arguments**
- `{any} str` - JS expression as a string

- **Usage**:
Turns a string expression into executable JS for .js config files

## injectImports

- **Arguments**
Expand Down
20 changes: 20 additions & 0 deletions packages/@vue/cli/__tests__/Generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,3 +656,23 @@ test('extract config files', async () => {
expect(fs.readFileSync('/jest.config.js', 'utf-8')).toMatch(js(configs.jest))
expect(fs.readFileSync('/.browserslistrc', 'utf-8')).toMatch('> 1%\nnot <= IE8')
})

test('generate a JS-Only value from a string', async () => {
const jsAsString = 'true ? "alice" : "bob"'

const generator = new Generator('/', { plugins: [
{
id: 'test',
apply: api => {
api.extendPackage({
testScript: api.makeJSOnlyValue(jsAsString)
})
}
}
] })

await generator.generate({})

expect(generator.pkg).toHaveProperty('testScript')
expect(typeof generator.pkg.testScript).toBe('function')
})
10 changes: 10 additions & 0 deletions packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ class GeneratorAPI {
return `module.exports = ${stringifyJS(value, null, 2)}`
}

/**
* Turns a string expression into executable JS for JS configs.
* @param {*} str JS expression as a string
*/
makeJSOnlyValue (str) {
const fn = () => {}
fn.__expression = str
return fn
}

/**
* Add import statements to a file.
*/
Expand Down