Comparing version 0.1.1 to 0.1.2
/// <reference types="node" /> | ||
import { ServerResponse } from 'http'; | ||
import { parse, compileTemplate, compileStyle } from '@vue/compiler-sfc'; | ||
export declare function resolveVue(cwd: string): any; | ||
export declare function resolveCompiler(cwd: string): { | ||
parse: typeof parse; | ||
compileTemplate: typeof compileTemplate; | ||
compileStyle: typeof compileStyle; | ||
}; | ||
export declare function resolveModule(id: string, cwd: string, res: ServerResponse): void; |
@@ -10,6 +10,62 @@ "use strict"; | ||
const fileToIdMap = new Map(); | ||
const resolveCache = new Map(); | ||
const sendWithCache = (res, id, modulePath) => { | ||
resolveCache.set(id, modulePath); | ||
utils_1.sendJSStream(res, modulePath); | ||
}; | ||
function resolveVue(cwd) { | ||
if (resolveCache.has('vue')) { | ||
return resolveCache.get('vue'); | ||
} | ||
let vuePath; | ||
let compilerPath; | ||
try { | ||
// see if user has local vue installation | ||
const userVuePkg = resolve_from_1.default(cwd, 'vue/package.json'); | ||
vuePath = path_1.default.join(path_1.default.dirname(userVuePkg), 'dist/vue.runtime.esm-browser.js'); | ||
// also resolve matching sfc | ||
try { | ||
const compilerPkgPath = resolve_from_1.default(cwd, '@vue/compiler-sfc/package.json'); | ||
const compilerPkg = require(compilerPkgPath); | ||
if (compilerPkg.version !== require(userVuePkg).version) { | ||
throw new Error(); | ||
} | ||
compilerPath = path_1.default.join(path_1.default.dirname(compilerPkgPath), compilerPkg.main); | ||
} | ||
catch (e) { | ||
// user has local vue but has no compiler-sfc | ||
console.error(`[vite] Error: a local installation of \`vue\` is detected but ` + | ||
`no matching \`@vue/compiler-sfc\` is found. Make sure to install ` + | ||
`both and use the same version.`); | ||
compilerPath = require.resolve('@vue/compiler-sfc'); | ||
} | ||
} | ||
catch (e) { | ||
// user has no local vue, use vite's dependency version | ||
vuePath = require.resolve('vue/dist/vue.runtime.esm-browser.js'); | ||
compilerPath = require.resolve('@vue/compiler-sfc'); | ||
} | ||
resolveCache.set('vue', vuePath); | ||
resolveCache.set('@vue/compiler-sfc', compilerPath); | ||
return vuePath; | ||
} | ||
exports.resolveVue = resolveVue; | ||
function resolveCompiler(cwd) { | ||
resolveVue(cwd); | ||
return require(resolveCache.get('@vue/compiler-sfc')); | ||
} | ||
exports.resolveCompiler = resolveCompiler; | ||
// TODO support custom imports map e.g. for snowpack web_modules | ||
function resolveModule(id, cwd, res) { | ||
let modulePath; | ||
if (id === 'vue') { | ||
// special handling for vue | ||
return utils_1.sendJSStream(res, resolveVue(cwd)); | ||
} | ||
// already cached | ||
let modulePath = resolveCache.get(id); | ||
if (modulePath) { | ||
return utils_1.sendJSStream(res, modulePath); | ||
} | ||
// handle source map requests | ||
let sourceMapPath = undefined; | ||
// TODO support custom imports map e.g. for snowpack web_modules | ||
if (id.endsWith('.map')) { | ||
@@ -27,16 +83,12 @@ sourceMapPath = id; | ||
modulePath = resolve_from_1.default(cwd, `${id}/package.json`); | ||
if (id === 'vue') { | ||
modulePath = path_1.default.join(path_1.default.dirname(modulePath), 'dist/vue.runtime.esm-browser.js'); | ||
// module resolved, try to locate its "module" entry | ||
const pkg = require(modulePath); | ||
modulePath = path_1.default.join(path_1.default.dirname(modulePath), pkg.module || pkg.main); | ||
fileToIdMap.set(path_1.default.basename(modulePath), id); | ||
// this is a source map request. | ||
if (sourceMapPath) { | ||
modulePath = path_1.default.join(path_1.default.dirname(modulePath), sourceMapPath); | ||
return sendWithCache(res, sourceMapPath, modulePath); | ||
} | ||
else { | ||
// module resolved, try to locate its "module" entry | ||
const pkg = require(modulePath); | ||
modulePath = path_1.default.join(path_1.default.dirname(modulePath), pkg.module || pkg.main); | ||
fileToIdMap.set(path_1.default.basename(modulePath), id); | ||
// this is a source map request. | ||
if (sourceMapPath) { | ||
modulePath = path_1.default.join(path_1.default.dirname(modulePath), sourceMapPath); | ||
} | ||
} | ||
utils_1.sendJSStream(res, modulePath); | ||
sendWithCache(res, id, modulePath); | ||
} | ||
@@ -43,0 +95,0 @@ catch (e) { |
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import { SFCDescriptor } from '@vue/compiler-sfc'; | ||
export declare function parseSFC(filename: string, saveCache?: boolean): Promise<[SFCDescriptor, SFCDescriptor | undefined] | []>; | ||
export declare function parseSFC(cwd: string, filename: string, saveCache?: boolean): Promise<[SFCDescriptor, SFCDescriptor | undefined] | []>; | ||
export declare function vueMiddleware(cwd: string, req: IncomingMessage, res: ServerResponse): Promise<void>; |
@@ -9,8 +9,8 @@ "use strict"; | ||
const fs_1 = require("fs"); | ||
const compiler_sfc_1 = require("@vue/compiler-sfc"); | ||
const utils_1 = require("./utils"); | ||
const moduleRewriter_1 = require("./moduleRewriter"); | ||
const moduleResolver_1 = require("./moduleResolver"); | ||
const hash_sum_1 = __importDefault(require("hash-sum")); | ||
const cache = new Map(); | ||
async function parseSFC(filename, saveCache = false) { | ||
async function parseSFC(cwd, filename, saveCache = false) { | ||
let content; | ||
@@ -23,3 +23,3 @@ try { | ||
} | ||
const { descriptor, errors } = compiler_sfc_1.parse(content, { | ||
const { descriptor, errors } = moduleResolver_1.resolveCompiler(cwd).parse(content, { | ||
filename | ||
@@ -42,3 +42,3 @@ }); | ||
const filename = path_1.default.join(cwd, pathname.slice(1)); | ||
const [descriptor] = await parseSFC(filename, true /* save last accessed descriptor on the client */); | ||
const [descriptor] = await parseSFC(cwd, filename, true /* save last accessed descriptor on the client */); | ||
if (!descriptor) { | ||
@@ -52,6 +52,6 @@ res.statusCode = 404; | ||
if (query.type === 'template') { | ||
return compileSFCTemplate(res, descriptor.template, filename, pathname, descriptor.styles.some((s) => s.scoped)); | ||
return compileSFCTemplate(cwd, res, descriptor.template, filename, pathname, descriptor.styles.some((s) => s.scoped)); | ||
} | ||
if (query.type === 'style') { | ||
return compileSFCStyle(res, descriptor.styles[Number(query.index)], query.index, filename, pathname); | ||
return compileSFCStyle(cwd, res, descriptor.styles[Number(query.index)], query.index, filename, pathname); | ||
} | ||
@@ -89,4 +89,4 @@ // TODO custom blocks | ||
} | ||
function compileSFCTemplate(res, template, filename, pathname, scoped) { | ||
const { code, errors } = compiler_sfc_1.compileTemplate({ | ||
function compileSFCTemplate(cwd, res, template, filename, pathname, scoped) { | ||
const { code, errors } = moduleResolver_1.resolveCompiler(cwd).compileTemplate({ | ||
source: template.content, | ||
@@ -104,5 +104,5 @@ filename, | ||
} | ||
function compileSFCStyle(res, style, index, filename, pathname) { | ||
function compileSFCStyle(cwd, res, style, index, filename, pathname) { | ||
const id = hash_sum_1.default(pathname); | ||
const { code, errors } = compiler_sfc_1.compileStyle({ | ||
const { code, errors } = moduleResolver_1.resolveCompiler(cwd).compileStyle({ | ||
source: style.content, | ||
@@ -109,0 +109,0 @@ filename, |
@@ -22,3 +22,3 @@ "use strict"; | ||
// check which part of the file changed | ||
const [descriptor, prevDescriptor] = await vueCompiler_1.parseSFC(file); | ||
const [descriptor, prevDescriptor] = await vueCompiler_1.parseSFC(cwd, file); | ||
if (!descriptor || !prevDescriptor) { | ||
@@ -25,0 +25,0 @@ // the file has never been accessed yet |
{ | ||
"name": "vite", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Evan You", |
@@ -49,2 +49,4 @@ # vite | ||
Go to `http://localhost:3000`, edit the `.vue` file to see changes hot-updated instantly. | ||
## How It Works | ||
@@ -61,1 +63,7 @@ | ||
- For libraries that provide ES modules builds that work in browsers, you can also directly import them from a CDN. | ||
## TODOs | ||
- SourceMap | ||
- Snowpack integration | ||
- Custom imports map support |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30953
707
68
9