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

fix(defineOption): only allow string values #6052

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/playground/alias/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
minify: false
},
define: {
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: true
__VUE_OPTIONS_API__: JSON.stringify(true),
__VUE_PROD_DEVTOOLS__: JSON.stringify(true)
}
}
4 changes: 2 additions & 2 deletions packages/playground/define/__tests__/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ test('string', async () => {
expect(await page.textContent('.number')).toBe(String(defines.__NUMBER__))
expect(await page.textContent('.boolean')).toBe(String(defines.__BOOLEAN__))
expect(await page.textContent('.object')).toBe(
JSON.stringify(defines.__OBJ__, null, 2)
JSON.stringify(JSON.parse(defines.__OBJ__), null, 2)
)
expect(await page.textContent('.env-var')).toBe(
JSON.parse(defines['process.env.SOMEVAR'])
)
expect(await page.textContent('.process-as-property')).toBe(
defines.__OBJ__.process.env.SOMEVAR
JSON.parse(defines.__OBJ__).process.env.SOMEVAR
)
expect(await page.textContent('.spread-object')).toBe(
JSON.stringify({ SOMEVAR: defines['process.env.SOMEVAR'] })
Expand Down
8 changes: 4 additions & 4 deletions packages/playground/define/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module.exports = {
define: {
__EXP__: '1 + 1',
__STRING__: '"hello"',
__NUMBER__: 123,
__BOOLEAN__: true,
__OBJ__: {
__NUMBER__: JSON.stringify(123),
__BOOLEAN__: JSON.stringify(true),
__OBJ__: JSON.stringify({
foo: 1,
bar: {
baz: 2
Expand All @@ -14,7 +14,7 @@ module.exports = {
SOMEVAR: '"PROCESS MAY BE PROPERTY"'
}
}
},
}),
'process.env.SOMEVAR': '"SOMEVAR"'
}
}
2 changes: 1 addition & 1 deletion packages/playground/resolve/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
conditions: ['custom']
},
define: {
VITE_CONFIG_DEP_TEST: a
VITE_CONFIG_DEP_TEST: JSON.stringify(a)
},
plugins: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ function viteLegacyPlugin(options = {}) {
define: {
'import.meta.env.LEGACY':
env.command === 'serve' || config.build.ssr
? false
? JSON.stringify(false)
: legacyEnvVarMarker
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-vue-jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ function vueJsxPlugin(options = {}) {
include: /\.ts$/
},
define: {
__VUE_OPTIONS_API__: optionsApi != null ? optionsApi : true,
__VUE_PROD_DEVTOOLS__: devTools != null ? devTools : false
__VUE_OPTIONS_API__: JSON.stringify(optionsApi) || 'true',
__VUE_PROD_DEVTOOLS__: JSON.stringify(devTools) || 'false'
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
config(config) {
return {
define: {
__VUE_OPTIONS_API__: config.define?.__VUE_OPTIONS_API__ ?? true,
__VUE_PROD_DEVTOOLS__: config.define?.__VUE_PROD_DEVTOOLS__ ?? false
__VUE_OPTIONS_API__:
JSON.stringify(config.define?.__VUE_OPTIONS_API__) || 'true',
__VUE_PROD_DEVTOOLS__:
JSON.stringify(config.define?.__VUE_PROD_DEVTOOLS__) || 'false'
},
ssr: {
external: ['vue', '@vue/server-renderer']
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export interface UserConfig {
* Define global variable replacements.
* Entries will be defined on `window` during dev and replaced during build.
*/
define?: Record<string, any>
define?: Record<string, string>
/**
* Array of vite plugins to use.
*/
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export function definePlugin(config: ResolvedConfig): Plugin {
const userDefine: Record<string, string> = {}
for (const key in config.define) {
const val = config.define[key]
userDefine[key] = typeof val === 'string' ? val : JSON.stringify(val)
if (typeof val !== 'string') {
throw new TypeError('values of define option should be string')
}
userDefine[key] = val
}

// during dev, import.meta properties are handled by importAnalysis plugin
Expand Down