From 5d7ac468091adf2d6809e6a735990bf20b28de87 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sat, 2 May 2020 14:46:02 -0400 Subject: [PATCH] feat: handle relative asset paths --- package.json | 1 + src/node/build.ts | 297 +++++++++----------------- src/node/buildPluginAsset.ts | 38 ++++ src/node/buildPluginCss.ts | 41 ++++ src/node/buildPluginHtml.ts | 23 ++ src/node/buildPluginResolve.ts | 51 +++++ src/node/index.ts | 2 +- src/node/resolveVue.ts | 33 +-- src/node/serverPluginModuleResolve.ts | 2 +- src/node/serverPluginServeStatic.ts | 16 +- src/node/serverPluginVue.ts | 4 +- src/node/utils.ts | 10 + 12 files changed, 300 insertions(+), 218 deletions(-) create mode 100644 src/node/buildPluginAsset.ts create mode 100644 src/node/buildPluginCss.ts create mode 100644 src/node/buildPluginHtml.ts create mode 100644 src/node/buildPluginResolve.ts diff --git a/package.json b/package.json index 06a1a7368782bd..4547b555e282c2 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "koa": "^2.11.0", "koa-conditional-get": "^2.0.0", "koa-etag": "^3.0.0", + "koa-send": "^5.0.0", "koa-static": "^5.0.0", "lru-cache": "^5.1.1", "magic-string": "^0.25.7", diff --git a/src/node/build.ts b/src/node/build.ts index 5ba401e311f415..d8f8973a096c62 100644 --- a/src/node/build.ts +++ b/src/node/build.ts @@ -2,77 +2,69 @@ import path from 'path' import { promises as fs } from 'fs' import { rollup as Rollup, - Plugin, InputOptions, OutputOptions, RollupOutput } from 'rollup' import { resolveVue } from './resolveVue' -import { hmrClientId } from './serverPluginHmr' import resolve from 'resolve-from' import chalk from 'chalk' import { Resolver, createResolver } from './resolver' import { Options } from 'rollup-plugin-vue' +import { scriptRE } from './utils' +import { createBuildResolvePlugin } from './buildPluginResolve' +import { createBuildHtmlPlugin } from './buildPluginHtml' +import { createBuildCssPlugin } from './buildPluginCss' +import { createBuildAssetPlugin } from './buildPluginAsset' -const debugBuild = require('debug')('vite:build') -const scriptRE = /]*>([\s\S]*?)<\/script>/gm - -interface BuildOptionsBase { +export interface BuildOptions { root?: string cdn?: boolean - cssFileName?: string resolvers?: Resolver[] + outDir?: string + assetsDir?: string // list files that are included in the build, but not inside project root. srcRoots?: string[] rollupInputOptions?: InputOptions + rollupOutputOptions?: OutputOptions + rollupPluginVueOptions?: Partial + emitAssets?: boolean write?: boolean // if false, does not write to disk. minify?: boolean silent?: boolean - rollupPluginVueOptions?: Partial -} - -interface SingleBuildOptions extends BuildOptionsBase { - rollupOutputOptions?: OutputOptions -} - -interface MultiBuildOptions extends BuildOptionsBase { - rollupOutputOptions?: OutputOptions[] } -export type BuildOptions = SingleBuildOptions | MultiBuildOptions - export interface BuildResult { - js: RollupOutput['output'] - css: string html: string + assets: RollupOutput['output'] } -export async function build(options: SingleBuildOptions): Promise -export async function build(options: MultiBuildOptions): Promise -export async function build({ - root = process.cwd(), - cdn = !resolveVue(root).hasLocalVue, - cssFileName = 'style.css', - resolvers = [], - srcRoots = [], - rollupInputOptions = {}, - rollupOutputOptions = {}, - write = true, - minify = true, - silent = false, - rollupPluginVueOptions = {} -}: BuildOptions = {}): Promise { +export async function build(options: BuildOptions = {}): Promise { process.env.NODE_ENV = 'production' - const start = Date.now() + const { + root = process.cwd(), + cdn = !resolveVue(root).hasLocalVue, + outDir = path.resolve(root, 'dist'), + assetsDir = '.', + resolvers = [], + srcRoots = [], + rollupInputOptions = {}, + rollupOutputOptions = {}, + rollupPluginVueOptions = {}, + emitAssets = true, + write = true, + minify = true, + silent = false + } = options + // lazy require rollup so that we don't load it when only using the dev server // importing it just for the types const rollup = require('rollup').rollup as typeof Rollup const indexPath = path.resolve(root, 'index.html') - // make sure to use the same verison of vue from the CDN. - const vueVersion = resolveVue(root).version - const cdnLink = `https://unpkg.com/vue@${vueVersion}/dist/vue.esm-browser.prod.js` + const cssFileName = 'style.css' + const resolvedAssetsPath = path.join(outDir, assetsDir) let indexContent: string | null = null try { @@ -84,96 +76,19 @@ export async function build({ const resolver = createResolver(root, resolvers) srcRoots.push(root) - const vitePlugin: Plugin = { - name: 'vite', - resolveId(id: string) { - if (id === hmrClientId) { - return hmrClientId - } else if (id.startsWith('/')) { - // if id starts with any of the src root directories, it's a file request - if (srcRoots.some((root) => id.startsWith(root))) { - return - } - const resolved = resolver.requestToFile(id) - debugBuild(`[resolve]`, id, `-->`, resolved) - return resolved - } else if (id === 'vue') { - if (!cdn) { - return resolveVue(root, true).vue - } else { - return { - id: cdnLink, - external: true - } - } - } else { - const request = resolver.idToRequest(id) - if (request) { - const resolved = resolver.requestToFile(request) - debugBuild(`[resolve]`, id, `-->`, request, `--> `, resolved) - return resolved - } - } - }, - load(id: string) { - if (id === hmrClientId) { - return `export const hot = {}` - } else if (id === indexPath) { - let script = '' - if (indexContent) { - let match - while ((match = scriptRE.exec(indexContent))) { - // TODO handle