From fd0a0d9bcaf0fd9098c5eb1a0c53226575fdcccb Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 15 Mar 2021 12:02:45 -0400 Subject: [PATCH] fix(json): support importing json with ?url and ?raw queries fux #2455 --- packages/playground/json/__tests__/json.spec.ts | 16 +++++++++++++++- packages/playground/json/index.html | 14 +++++++++++++- packages/vite/src/node/plugins/json.ts | 2 ++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/playground/json/__tests__/json.spec.ts b/packages/playground/json/__tests__/json.spec.ts index 2a9451d1c41ca7..476aabae99607e 100644 --- a/packages/playground/json/__tests__/json.spec.ts +++ b/packages/playground/json/__tests__/json.spec.ts @@ -1,3 +1,5 @@ +import { isBuild } from '../../testUtils' + const json = require('../test.json') const deepJson = require('@vue/runtime-core/package.json') const stringified = JSON.stringify(json) @@ -27,6 +29,18 @@ test('dynamic import, named', async () => { expect(await page.textContent('.dynamic-named')).toBe(json.hello) }) -test('raw fetch', async () => { +test('fetch', async () => { expect(await page.textContent('.fetch')).toBe(stringified) }) + +test('?url', async () => { + expect(await page.textContent('.url')).toMatch( + isBuild ? 'data:application/json' : '/test.json' + ) +}) + +test('?raw', async () => { + expect(await page.textContent('.raw')).toBe( + require('fs').readFileSync(require.resolve('../test.json'), 'utf-8') + ) +}) diff --git a/packages/playground/json/index.html b/packages/playground/json/index.html index 0efaea3147a164..d10145511b5996 100644 --- a/packages/playground/json/index.html +++ b/packages/playground/json/index.html @@ -10,9 +10,15 @@

Dynamic Import


 

 
-

Raw fetch

+

fetch


 
+

Importing as URL

+

+
+

Raw Import

+

+