mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-04-22 20:38:55 -05:00
feat: add makeJSOnlyValue to generator API (#3568)
Provides convenience method for passing JS into config files. Closes issue #3535.
This commit is contained in:
committed by
Haoqun Jiang
parent
cb113971e2
commit
f69339e816
@@ -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**
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user