Skip to content

Commit

Permalink
fix(plugin-vue): sfc src import respect alias
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Jan 15, 2021
1 parent bdec0f8 commit d76cce9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/playground/vue/src-import/SrcImport.vue
@@ -1,3 +1,3 @@
<template src="./template.html"></template>
<script src="./script.ts"></script>
<style src="./style.css" scoped></style>
<style src="/@/src-import/style.css" scoped></style>
4 changes: 4 additions & 0 deletions packages/playground/vue/vite.config.ts
@@ -1,8 +1,12 @@
import path from 'path'
import { defineConfig } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import { vueI18nPlugin } from './CustomBlockPlugin'

export default defineConfig({
alias: {
'/@': __dirname
},
plugins: [vuePlugin(), vueI18nPlugin],
build: {
// to make tests faster
Expand Down
57 changes: 36 additions & 21 deletions packages/plugin-vue/src/main.ts
Expand Up @@ -42,7 +42,11 @@ export async function transformMain(
const hasScoped = descriptor.styles.some((s) => s.scoped)

// script
const { code: scriptCode, map } = await genScriptCode(descriptor, options)
const { code: scriptCode, map } = await genScriptCode(
descriptor,
options,
pluginContext
)

// template
// Check if we can use compile template as inlined render function
Expand All @@ -57,7 +61,7 @@ export async function transformMain(
let templateCode = ''
let templateMap
if (hasTemplateImport) {
;({ code: templateCode, map: templateMap } = genTemplateCode(
;({ code: templateCode, map: templateMap } = await genTemplateCode(
descriptor,
options,
pluginContext
Expand All @@ -71,10 +75,10 @@ export async function transformMain(
: ''

// styles
const stylesCode = genStyleCode(descriptor)
const stylesCode = await genStyleCode(descriptor, pluginContext)

// custom blocks
const customBlocksCode = genCustomBlockCode(descriptor)
const customBlocksCode = await genCustomBlockCode(descriptor, pluginContext)

const output: string[] = [
scriptCode,
Expand Down Expand Up @@ -148,7 +152,7 @@ export async function transformMain(
}
}

function genTemplateCode(
async function genTemplateCode(
descriptor: SFCDescriptor,
options: ResolvedOptions,
pluginContext: PluginContext
Expand All @@ -167,7 +171,7 @@ function genTemplateCode(
)
} else {
if (template.src) {
linkSrcToDescriptor(template.src, descriptor)
await linkSrcToDescriptor(template.src, descriptor, pluginContext)
}
const src = template.src || descriptor.filename
const srcQuery = template.src ? `&src` : ``
Expand All @@ -184,7 +188,8 @@ function genTemplateCode(

async function genScriptCode(
descriptor: SFCDescriptor,
options: ResolvedOptions
options: ResolvedOptions,
pluginContext: PluginContext
): Promise<{
code: string
map: RawSourceMap
Expand Down Expand Up @@ -213,7 +218,7 @@ async function genScriptCode(
}
} else {
if (script.src) {
linkSrcToDescriptor(script.src, descriptor)
await linkSrcToDescriptor(script.src, descriptor, pluginContext)
}
const src = script.src || descriptor.filename
const langFallback = (script.src && path.extname(src).slice(1)) || 'js'
Expand All @@ -231,13 +236,17 @@ async function genScriptCode(
}
}

function genStyleCode(descriptor: SFCDescriptor) {
async function genStyleCode(
descriptor: SFCDescriptor,
pluginContext: PluginContext
) {
let stylesCode = ``
let hasCSSModules = false
if (descriptor.styles.length) {
descriptor.styles.forEach((style, i) => {
for (let i = 0; i < descriptor.styles.length; i++) {
const style = descriptor.styles[i]
if (style.src) {
linkSrcToDescriptor(style.src, descriptor)
await linkSrcToDescriptor(style.src, descriptor, pluginContext)
}
const src = style.src || descriptor.filename
// do not include module in default query, since we use it to indicate
Expand All @@ -256,16 +265,20 @@ function genStyleCode(descriptor: SFCDescriptor) {
stylesCode += `\nimport ${JSON.stringify(styleRequest)}`
}
// TODO SSR critical CSS collection
})
}
}
return stylesCode
}

function genCustomBlockCode(descriptor: SFCDescriptor) {
async function genCustomBlockCode(
descriptor: SFCDescriptor,
pluginContext: PluginContext
) {
let code = ''
descriptor.customBlocks.forEach((block, index) => {
for (let index = 0; index < descriptor.customBlocks.length; index++) {
const block = descriptor.customBlocks[index]
if (block.src) {
linkSrcToDescriptor(block.src, descriptor)
await linkSrcToDescriptor(block.src, descriptor, pluginContext)
}
const src = block.src || descriptor.filename
const attrsQuery = attrsToQuery(block.attrs, block.type)
Expand All @@ -274,7 +287,7 @@ function genCustomBlockCode(descriptor: SFCDescriptor) {
const request = JSON.stringify(src + query)
code += `import block${index} from ${request}\n`
code += `if (typeof block${index} === 'function') block${index}(_sfc_main)\n`
})
}
return code
}

Expand All @@ -298,11 +311,13 @@ function genCSSModulesCode(
* with its owner SFC descriptor so that we can get the information about
* the owner SFC when compiling that file in the transform phase.
*/
function linkSrcToDescriptor(src: string, descriptor: SFCDescriptor) {
const srcFile = path.posix.resolve(
path.posix.dirname(descriptor.filename),
src
)
async function linkSrcToDescriptor(
src: string,
descriptor: SFCDescriptor,
pluginContext: PluginContext
) {
const srcFile =
(await pluginContext.resolve(src, descriptor.filename))?.id || src
setDescriptor(srcFile, descriptor)
}

Expand Down

0 comments on commit d76cce9

Please sign in to comment.