Skip to content

Commit

Permalink
feat: support importing json
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 3, 2020
1 parent 8ef6d4d commit 97dc7ba
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
// TODO proxy cssModules config
...rollupPluginVueOptions
}),
require('@rollup/plugin-json')(),
require('@rollup/plugin-node-resolve')({
rootDir: root
}),
Expand Down
2 changes: 2 additions & 0 deletions src/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -30,6 +31,7 @@ const internalPlugins: Plugin[] = [
moduleRewritePlugin,
moduleResolvePlugin,
vuePlugin,
jsonPlugin,
hmrPlugin,
serveStaticPlugin
]
Expand Down
17 changes: 17 additions & 0 deletions src/node/serverPluginJson.ts
Original file line number Diff line number Diff line change
@@ -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)}`
}
}
})
}
18 changes: 1 addition & 17 deletions src/node/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,6 +13,7 @@ import {
rewriteFileWithHMR,
hmrClientPublicPath
} from './serverPluginHmr'
import { readBody } from './utils'

const debug = require('debug')('vite:rewrite')

Expand Down Expand Up @@ -105,22 +105,6 @@ export const moduleRewritePlugin: Plugin = ({ app, watcher, resolver }) => {
})
}

async function readBody(stream: Readable | string): Promise<string> {
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,
Expand Down
19 changes: 19 additions & 0 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)(\?.*)?$/
Expand Down Expand Up @@ -62,6 +63,24 @@ export async function cachedRead(
return content
}

export async function readBody(
stream: Readable | Buffer | string
): Promise<string> {
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[] = []
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 97dc7ba

Please sign in to comment.