Skip to content

Commit

Permalink
test: test for define
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 22, 2020
1 parent 014c538 commit d76557e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/playground/define/__tests__/define.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
)
})
17 changes: 17 additions & 0 deletions packages/playground/define/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Define</h1>

<p>String <code class="string"></code></p>
<p>Number <code class="number"></code></p>
<p>Boolean <code class="boolean"></code></p>
<p>Object <pre class="object"></pre></p>

<script type="module">
text('.string', __STRING__)
text('.number', __NUMBER__)
text('.boolean', __BOOLEAN__)
text('.object', JSON.stringify(__OBJ__, null, 2))

function text(el, text) {
document.querySelector(el).textContent = text
}
</script>
10 changes: 10 additions & 0 deletions packages/playground/define/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions packages/playground/define/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
define: {
__STRING__: 'hello',
__NUMBER__: 123,
__BOOLEAN__: true,
__OBJ__: {
foo: 1,
bar: {
baz: 2
}
}
}
}
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { isCSSRequest } from './css'
* to avoid transform cost during dev.
*/
export function buildDefinePlugin(config: ResolvedConfig): Plugin {
const userDefine: Record<string, string> = {}
for (const key in config.define) {
userDefine[key] = JSON.stringify(config.define[key])
}

const individualEnvKeys: Record<string, string> = {}
for (const key in config.env) {
individualEnvKeys[`import.meta.env.${key}`] = JSON.stringify(
Expand All @@ -18,7 +23,7 @@ export function buildDefinePlugin(config: ResolvedConfig): Plugin {
}

const replacements: Record<string, string | undefined> = {
...config.define,
...userDefine,
...individualEnvKeys,
'import.meta.env.': `({}).`,
'import.meta.env': JSON.stringify(config.env),
Expand Down

0 comments on commit d76557e

Please sign in to comment.