Skip to content

Commit

Permalink
feat: support sourcemap, close #33
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 7, 2021
1 parent 1498d35 commit 8dbb2d1
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/vue2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"vite-plugin-vue2": "^1.4.3"
},
"dependencies": {
"vue": "3.0.11",
"vue": "2.6.12",
"vue-template-compiler": "2.6.12"
}
}
3 changes: 3 additions & 0 deletions examples/vue2/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const config: UserConfig = {
createVuePlugin(),
ViteComponents(),
],
build: {
sourcemap: true,
},
}

export default config
3 changes: 3 additions & 0 deletions examples/vue3/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const config: UserConfig = {
],
}),
],
build: {
sourcemap: true,
},
}

export default config
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"chokidar": "^3.5.1",
"debug": "^4.3.2",
"fast-glob": "^3.2.5",
"magic-string": "^0.25.7",
"minimatch": "^3.0.4"
},
"devDependencies": {
Expand Down
9 changes: 4 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Vue2Transformer } from './transforms/vue2'

function VitePluginComponents(options: Options = {}): Plugin {
let ctx: Context
let transformers: Transformer[]
let transformer: Transformer

return {
name: 'vite-plugin-components',
Expand All @@ -17,21 +17,16 @@ function VitePluginComponents(options: Options = {}): Plugin {
options.transformer = options.transformer || 'vue2'

ctx = new Context(options, config)
transformers = [
ctx.options.transformer === 'vue2'
? Vue2Transformer(ctx)
: Vue3Transformer(ctx),
]
transformer = ctx.options.transformer === 'vue2'
? Vue2Transformer(ctx)
: Vue3Transformer(ctx)
},
configureServer(server) {
ctx.setServer(server)
},
transform(code, id) {
const { path, query } = parseId(id)
for (const trans of transformers)
code = trans(code, id, path, query)

return code
return transformer(code, id, path, query)
},
}
}
Expand Down
31 changes: 19 additions & 12 deletions src/transforms/vue2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Debug from 'debug'
import MagicString from 'magic-string'
import { Transformer } from '../types'
import { Context } from '../context'
import { pascalCase, stringifyComponentImport } from '../utils'
Expand All @@ -8,7 +9,7 @@ const debug = Debug('vite-plugin-components:transform:vue2')
export function Vue2Transformer(ctx: Context): Transformer {
return (code, id, path, query) => {
if (!(path.endsWith('.vue') || ctx.options.customLoaderMatcher(id)))
return code
return null

ctx.searchGlob()

Expand All @@ -19,30 +20,36 @@ export function Vue2Transformer(ctx: Context): Transformer {
let no = 0
const componentPaths: string[] = []

let transformed = code.replace(/_c\(['"](.+?)["']([,)])/g, (str, match, append) => {
if (match && !match.startsWith('_')) {
debug(`| ${match}`)
const name = pascalCase(match)
const s = new MagicString(code)

for (const match of code.matchAll(/_c\(['"](.+?)["']([,)])/g)) {
const [full, matchStr, append] = match

if (match.index != null && matchStr && !matchStr.startsWith('_')) {
const start = match.index
const end = start + full.length
debug(`| ${matchStr}`)
const name = pascalCase(matchStr)
componentPaths.push(name)
const component = ctx.findComponent(name, [sfcPath])
if (component) {
const var_name = `__vite_components_${no}`
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
no += 1
return `_c(${var_name}${append}`
s.overwrite(start, end, `_c(${var_name}${append}`)
}
}
return str
})

debug(transformed)
}

debug(`^ (${no})`)

ctx.updateUsageMap(sfcPath, componentPaths)

transformed = `${head.join('\n')}\n${transformed}`
s.prepend(`${head.join('\n')}\n`)

return transformed
return {
code: s.toString(),
map: s.generateMap(),
}
}
}
28 changes: 18 additions & 10 deletions src/transforms/vue3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Debug from 'debug'
import MagicString from 'magic-string'
import { Transformer } from '../types'
import { Context } from '../context'
import { pascalCase, stringifyComponentImport } from '../utils'
Expand All @@ -8,7 +9,7 @@ const debug = Debug('vite-plugin-components:transform:vue3')
export function Vue3Transformer(ctx: Context): Transformer {
return (code, id, path, query) => {
if (!(path.endsWith('.vue') || ctx.options.customLoaderMatcher(id)))
return code
return null

ctx.searchGlob()

Expand All @@ -19,28 +20,35 @@ export function Vue3Transformer(ctx: Context): Transformer {
let no = 0
const componentPaths: string[] = []

let transformed = code.replace(/_resolveComponent\("(.+?)"\)/g, (str, match) => {
if (match && !match.startsWith('_')) {
debug(`| ${match}`)
const name = pascalCase(match)
const s = new MagicString(code)

for (const match of code.matchAll(/_resolveComponent\("(.+?)"\)/g)) {
const matchedName = match[1]
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
const start = match.index
const end = start + match[0].length
debug(`| ${matchedName}`)
const name = pascalCase(matchedName)
componentPaths.push(name)
const component = ctx.findComponent(name, [sfcPath])
if (component) {
const var_name = `__vite_components_${no}`
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
no += 1
return var_name
s.overwrite(start, end, var_name)
}
}
return str
})
}

debug(`^ (${no})`)

ctx.updateUsageMap(sfcPath, componentPaths)

transformed = `${head.join('\n')}\n${transformed}`
s.prepend(`${head.join('\n')}\n`)

return transformed
return {
code: s.toString(),
map: s.generateMap(),
}
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface UILibraryOptions {

export type Matcher = (id: string) => boolean | null | undefined

export type Transformer = (code: string, id: string, path: string, query: Record<string, string>) => string
export type Transformer = (code: string, id: string, path: string, query: Record<string, string>) => null | {code: string; map: any}

/**
* Plugin options.
Expand Down

0 comments on commit 8dbb2d1

Please sign in to comment.