From d76557e5fb4bdf61d2883ef8c1013d51d8f13481 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 22 Dec 2020 18:00:22 -0500 Subject: [PATCH] test: test for define --- .../playground/define/__tests__/define.spec.ts | 10 ++++++++++ packages/playground/define/index.html | 17 +++++++++++++++++ packages/playground/define/package.json | 10 ++++++++++ packages/playground/define/vite.config.js | 13 +++++++++++++ packages/vite/src/node/plugins/define.ts | 7 ++++++- 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 packages/playground/define/__tests__/define.spec.ts create mode 100644 packages/playground/define/index.html create mode 100644 packages/playground/define/package.json create mode 100644 packages/playground/define/vite.config.js diff --git a/packages/playground/define/__tests__/define.spec.ts b/packages/playground/define/__tests__/define.spec.ts new file mode 100644 index 00000000000000..7466d60e4cc5bc --- /dev/null +++ b/packages/playground/define/__tests__/define.spec.ts @@ -0,0 +1,10 @@ +test('string', async () => { + const defines = require('../vite.config.js').define + + expect(await page.textContent('.string')).toBe(String(defines.__STRING__)) + 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) + ) +}) diff --git a/packages/playground/define/index.html b/packages/playground/define/index.html new file mode 100644 index 00000000000000..0d8c878a15b3f6 --- /dev/null +++ b/packages/playground/define/index.html @@ -0,0 +1,17 @@ +

Define

+ +

String

+

Number

+

Boolean

+

Object

+ + diff --git a/packages/playground/define/package.json b/packages/playground/define/package.json new file mode 100644 index 00000000000000..fd68a46c35162d --- /dev/null +++ b/packages/playground/define/package.json @@ -0,0 +1,10 @@ +{ + "name": "test-define", + "private": true, + "version": "0.0.0", + "scripts": { + "dev": "vite", + "build": "vite build", + "debug": "node --inspect-brk ../../vite/bin/vite" + } +} diff --git a/packages/playground/define/vite.config.js b/packages/playground/define/vite.config.js new file mode 100644 index 00000000000000..86d84f5f8c20d5 --- /dev/null +++ b/packages/playground/define/vite.config.js @@ -0,0 +1,13 @@ +module.exports = { + define: { + __STRING__: 'hello', + __NUMBER__: 123, + __BOOLEAN__: true, + __OBJ__: { + foo: 1, + bar: { + baz: 2 + } + } + } +} diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index e44e6777506445..29b31107fb1045 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -10,6 +10,11 @@ import { isCSSRequest } from './css' * to avoid transform cost during dev. */ export function buildDefinePlugin(config: ResolvedConfig): Plugin { + const userDefine: Record = {} + for (const key in config.define) { + userDefine[key] = JSON.stringify(config.define[key]) + } + const individualEnvKeys: Record = {} for (const key in config.env) { individualEnvKeys[`import.meta.env.${key}`] = JSON.stringify( @@ -18,7 +23,7 @@ export function buildDefinePlugin(config: ResolvedConfig): Plugin { } const replacements: Record = { - ...config.define, + ...userDefine, ...individualEnvKeys, 'import.meta.env.': `({}).`, 'import.meta.env': JSON.stringify(config.env),