Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
feat(dev): support script block src import handling
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Jul 5, 2020
1 parent 77a989d commit 87eaebe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
5 changes: 4 additions & 1 deletion playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
<div class="scoped">scoped css works!</div>
<!-- <h2>Module css</h2>-->
<!-- <div :class="$style.module">module css works!</div>-->
<SrcImport/>
</div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import SrcImport from './components/SrcImport.vue'
export default {
name: 'App',
components: {
HelloWorld
HelloWorld,
SrcImport
}
}
</script>
Expand Down
12 changes: 12 additions & 0 deletions playground/src/components/SrcImport.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<template>
<div>
<h2>Src Import</h2>
<div>src import works! Import value is {{value}}.</div>
</div>
</template>

<script src="./srcImport.ts" lang="ts"></script>

<style lang="scss" scoped>
</style>
7 changes: 7 additions & 0 deletions playground/src/components/srcImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
data() {
return {
value: 0,
}
},
}
24 changes: 19 additions & 5 deletions src/serverPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { transform } from './esbuildService'
import { normalizeComponentCode } from './componentNormalizer'
import { vueHotReloadCode } from './vueHotReload'
import path from 'path'
import { resolveImport } from 'vite/dist/server/serverPluginModuleRewrite'
import { InternalResolver } from 'vite/dist/resolver'

const vueTemplateCompiler = require('vue-template-compiler')
// const debug = require('debug')('vite:sfc')
Expand Down Expand Up @@ -86,7 +88,13 @@ export const vuePlugin: ServerPlugin = ({
// rely on vite internal sfc parse....
await next()
ctx.type = 'js'
ctx.body = await parseSFC(root, filePath, publicPath, descriptor)
ctx.body = await parseSFC(
root,
filePath,
publicPath,
descriptor,
resolver
)
return
}

Expand All @@ -112,7 +120,8 @@ async function parseSFC(
root: string,
filePath: string,
publicPath: string,
descriptor: SFCDescriptor
descriptor: SFCDescriptor,
resolver: InternalResolver
): Promise<string> {
const hasFunctional =
descriptor.template && descriptor.template.attrs.functional
Expand All @@ -127,10 +136,15 @@ async function parseSFC(
// script
let scriptImport = `var script = {}`
if (descriptor.script) {
let code = descriptor.script.content
if (descriptor.script.lang === 'ts') {
const scriptBlock = descriptor.script
let code = scriptBlock.content
if (scriptBlock && scriptBlock.src) {
const srcPath = resolveImport(root, publicPath, scriptBlock.src, resolver)
code = readFile(resolver.requestToFile(srcPath))
}
if (scriptBlock.lang === 'ts') {
code = (
await transform(descriptor.script.content, publicPath, {
await transform(code, publicPath, {
loader: 'ts',
})
).code
Expand Down

0 comments on commit 87eaebe

Please sign in to comment.