Skip to content

Commit

Permalink
fix optimization for vue
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 14, 2020
1 parent 88284f1 commit a73754c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
15 changes: 9 additions & 6 deletions src/node/depOptimizer.ts
Expand Up @@ -152,11 +152,13 @@ export async function optimizeDeps(config: OptimizeOptions, asCommand = false) {
const filePath = path.join(cacheDir, fileName)
await fs.ensureDir(path.dirname(filePath))
await fs.writeFile(filePath, chunk.code)
console.log(
`${chalk.yellow(fileName.replace(/\.js$/, ''))} -> ${chalk.dim(
path.relative(root, filePath)
)}`
)
if (!fileName.startsWith('common/')) {
console.log(
`${chalk.yellow(fileName.replace(/\.js$/, ''))} -> ${chalk.dim(
path.relative(root, filePath)
)}`
)
}
}
}

Expand All @@ -183,7 +185,8 @@ export function getDepHash(
return cachedHash
}
let content = lookupFile(root, lockfileFormats) || ''
content += lookupFile(root, [`package.json`]) || ''
const pkg = JSON.parse(lookupFile(root, [`package.json`]) || '{}')
content += JSON.stringify(pkg.dependencies)
// also take config into account
if (configPath) {
content += fs.readFileSync(configPath, 'utf-8')
Expand Down
10 changes: 6 additions & 4 deletions src/node/server/serverPluginModuleResolve.ts
Expand Up @@ -41,10 +41,12 @@ export const moduleResolvePlugin: ServerPlugin = ({ root, app, watcher }) => {
await next()
}

// speical handling for vue runtime packages
const vuePaths = resolveVue(root)
if (id in vuePaths) {
return serve(id, (vuePaths as any)[id], 'vue')
// speical handling for vue runtime in case it's not installed
if (id === 'vue') {
const vuePath = resolveVue(root).vue
if (vuePath) {
return serve(id, vuePath, '(non-local vue)')
}
}

// already resolved and cached
Expand Down
46 changes: 20 additions & 26 deletions src/node/utils/resolveVue.ts
Expand Up @@ -4,11 +4,7 @@ import sfcCompiler from '@vue/compiler-sfc'
import chalk from 'chalk'

interface ResolvedVuePaths {
vue: string
'@vue/runtime-dom': string
'@vue/runtime-core': string
'@vue/reactivity': string
'@vue/shared': string
vue: string | undefined
compiler: string
version: string
isLocal: boolean
Expand All @@ -23,28 +19,30 @@ export function resolveVue(root: string): ResolvedVuePaths {
if (resolved) {
return resolved
}
let runtimeDomPath: string
let vuePath: string | undefined
let compilerPath: string
let isLocal = true
let vueVersion: string
let isLocal = false
let vueVersion: string | undefined

try {
// see if user has local vue installation
const userVuePkg = resolveFrom(root, 'vue/package.json')
vueVersion = require(userVuePkg).version
// as long as vue is present,
// dom, core and reactivity are guarunteed to coexist
runtimeDomPath = resolveFrom(
root,
'@vue/runtime-dom/dist/runtime-dom.esm-bundler.js'
)
// also resolve matching sfc compiler
isLocal = true
} catch (e) {}

if (isLocal) {
// user has local vue, verify that the same version of @vue/compiler-sfc
// is also installed.
// vuePath will be undefined in this case since vue itself will be
// optimized by the deps optimizer and we can just let the resolver locate
// it.
try {
const compilerPkgPath = resolveFrom(
root,
'@vue/compiler-sfc/package.json'
)
const compilerPkg = require(compilerPkgPath)
if (compilerPkg.version !== require(userVuePkg).version) {
if (compilerPkg.version !== vueVersion) {
throw new Error()
}
compilerPath = path.join(path.dirname(compilerPkgPath), compilerPkg.main)
Expand All @@ -59,22 +57,18 @@ export function resolveVue(root: string): ResolvedVuePaths {
)
compilerPath = require.resolve('@vue/compiler-sfc')
}
} catch (e) {
} else {
// user has no local vue, use vite's dependency version
isLocal = false
vueVersion = require('vue/package.json').version
runtimeDomPath = require.resolve(
vuePath = require.resolve(
'@vue/runtime-dom/dist/runtime-dom.esm-bundler.js'
)
compilerPath = require.resolve('@vue/compiler-sfc')
}

resolved = {
version: vueVersion,
vue: runtimeDomPath,
'@vue/runtime-dom': runtimeDomPath,
'@vue/runtime-core': runtimeDomPath.replace(/runtime-dom/g, 'runtime-core'),
'@vue/reactivity': runtimeDomPath.replace(/runtime-dom/g, 'reactivity'),
'@vue/shared': runtimeDomPath.replace(/runtime-dom/g, 'shared'),
version: vueVersion!,
vue: vuePath,
compiler: compilerPath,
isLocal
}
Expand Down

0 comments on commit a73754c

Please sign in to comment.