Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-vue): sfc src import respect alias #1544

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(plugin-vue): sfc src import respect alias
fix #1542
  • Loading branch information
underfin committed Jan 15, 2021
commit d76cce92fda244ff732a40ca64d876f3be982dd4
2 changes: 1 addition & 1 deletion packages/playground/vue/src-import/SrcImport.vue
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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