diff --git a/package.json b/package.json index b694fa7355ff67..48613436ec786e 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "dependencies": { "@babel/parser": "^7.9.4", "@rollup/plugin-alias": "^3.1.0", + "@rollup/plugin-json": "^4.0.3", "@rollup/plugin-node-resolve": "^7.1.3", "@rollup/plugin-replace": "^2.3.2", "@types/koa": "^2.11.3", diff --git a/src/node/build.ts b/src/node/build.ts index 4468340941fb95..3571e72283c51d 100644 --- a/src/node/build.ts +++ b/src/node/build.ts @@ -97,6 +97,7 @@ export async function build(options: BuildOptions = {}): Promise { // TODO proxy cssModules config ...rollupPluginVueOptions }), + require('@rollup/plugin-json')(), require('@rollup/plugin-node-resolve')({ rootDir: root }), diff --git a/src/node/server.ts b/src/node/server.ts index 6b60956e1f1901..4a7e2d8c95fcff 100644 --- a/src/node/server.ts +++ b/src/node/server.ts @@ -7,6 +7,7 @@ import { moduleResolvePlugin } from './serverPluginModuleResolve' import { vuePlugin } from './serverPluginVue' import { hmrPlugin, HMRWatcher } from './serverPluginHmr' import { serveStaticPlugin } from './serverPluginServeStatic' +import { jsonPlugin } from './serverPluginJson' export { Resolver } @@ -30,6 +31,7 @@ const internalPlugins: Plugin[] = [ moduleRewritePlugin, moduleResolvePlugin, vuePlugin, + jsonPlugin, hmrPlugin, serveStaticPlugin ] diff --git a/src/node/serverPluginJson.ts b/src/node/serverPluginJson.ts new file mode 100644 index 00000000000000..997c974b7a1a03 --- /dev/null +++ b/src/node/serverPluginJson.ts @@ -0,0 +1,17 @@ +import { Plugin } from './server' +import { readBody } from './utils' + +export const jsonPlugin: Plugin = ({ app }) => { + app.use(async (ctx, next) => { + await next() + // handle .json imports + if (ctx.path.endsWith('.json')) { + const referer = ctx.get('referer') + // only rewrite json if referer is not a page (fetch/ajax requests) + if (/\.\w+$/.test(referer) && !referer.endsWith('.html')) { + ctx.type = 'js' + ctx.body = `export default ${await readBody(ctx.body)}` + } + } + }) +} diff --git a/src/node/serverPluginModuleRewrite.ts b/src/node/serverPluginModuleRewrite.ts index dffb44a9b71140..2e1aca60ecd932 100644 --- a/src/node/serverPluginModuleRewrite.ts +++ b/src/node/serverPluginModuleRewrite.ts @@ -3,7 +3,6 @@ import path from 'path' import slash from 'slash' import LRUCache from 'lru-cache' import MagicString from 'magic-string' -import { Readable } from 'stream' import { init as initLexer, parse as parseImports } from 'es-module-lexer' import { InternalResolver } from './resolver' import { @@ -14,6 +13,7 @@ import { rewriteFileWithHMR, hmrClientPublicPath } from './serverPluginHmr' +import { readBody } from './utils' const debug = require('debug')('vite:rewrite') @@ -105,22 +105,6 @@ export const moduleRewritePlugin: Plugin = ({ app, watcher, resolver }) => { }) } -async function readBody(stream: Readable | string): Promise { - if (stream instanceof Readable) { - return new Promise((resolve, reject) => { - let res = '' - stream - .on('data', (chunk) => (res += chunk)) - .on('error', reject) - .on('end', () => { - resolve(res) - }) - }) - } else { - return stream - } -} - function rewriteImports( source: string, importer: string, diff --git a/src/node/utils.ts b/src/node/utils.ts index 39a02b35b25651..4d58b20dc1c967 100644 --- a/src/node/utils.ts +++ b/src/node/utils.ts @@ -3,6 +3,7 @@ import { promises as fs } from 'fs' import LRUCache from 'lru-cache' import os from 'os' import { Context } from 'koa' +import { Readable } from 'stream' const imageRE = /\.(png|jpe?g|gif|svg)(\?.*)?$/ const mediaRE = /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/ @@ -62,6 +63,24 @@ export async function cachedRead( return content } +export async function readBody( + stream: Readable | Buffer | string +): Promise { + if (stream instanceof Readable) { + return new Promise((resolve, reject) => { + let res = '' + stream + .on('data', (chunk) => (res += chunk)) + .on('error', reject) + .on('end', () => { + resolve(res) + }) + }) + } else { + return typeof stream === 'string' ? stream : stream.toString() + } +} + export function getIPv4AddressList(): string[] { const networkInterfaces = os.networkInterfaces() let result: string[] = [] diff --git a/yarn.lock b/yarn.lock index df043a99bbd11f..f1ebff7b6a469e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -451,6 +451,13 @@ dependencies: slash "^3.0.0" +"@rollup/plugin-json@^4.0.3": + version "4.0.3" + resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-4.0.3.tgz#747e2c2884c5a0fa00b66c9c0f3f1012cddca534" + integrity sha512-QMUT0HZNf4CX17LMdwaslzlYHUKTYGuuk34yYIgZrNdu+pMEfqMS55gck7HEeHBKXHM4cz5Dg1OVwythDdbbuQ== + dependencies: + "@rollup/pluginutils" "^3.0.8" + "@rollup/plugin-node-resolve@^7.1.3": version "7.1.3" resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-7.1.3.tgz#80de384edfbd7bfc9101164910f86078151a3eca"