Skip to content

Commit

Permalink
wip: process sources' paths
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Jul 14, 2023
1 parent e2c841c commit cea0fa0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default defineConfig({
plugins: [
dts({
copyDtsFiles: true,
outDir: ['dist', 'types'],
outDir: ['dist', 'types/inner'],
// include: ['src/index.ts'],
exclude: ['src/ignore'],
staticImport: true,
Expand Down
37 changes: 35 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
Array.from(outputFiles.entries()),
async ([path, content]) => {
const isMapFile = path.endsWith('.map')
const baseDir = dirname(path)

if (!isMapFile && content) {
content = clearPureImport ? removePureImport(content) : content
Expand All @@ -497,6 +498,19 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
)
content = cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content

if (isMapFile) {
try {
const sourceMap: { sources: string[] } = JSON.parse(content)

sourceMap.sources = sourceMap.sources.map(source => {
return normalizePath(relative(dirname(path), resolve(baseDir, source)))
})
content = JSON.stringify(sourceMap)
} catch (e) {
logger.warn(`${logPrefix} ${yellow('Processing source map fail:')} ${path}`)
}
}

await writeOutput(path, content, outDir)
}
)
Expand Down Expand Up @@ -617,8 +631,27 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
const relativePath = relative(outDir, wroteFile)

await Promise.all(
extraOutDirs.map(async outDir => {
await writeOutput(resolve(outDir, relativePath), content, outDir, false)
extraOutDirs.map(async targetOutDir => {
const path = resolve(targetOutDir, relativePath)

if (wroteFile.endsWith('.map')) {
const relativeOutDir = relative(outDir, targetOutDir)

if (relativeOutDir) {
try {
const sourceMap: { sources: string[] } = JSON.parse(content)

sourceMap.sources = sourceMap.sources.map(source => {
return normalizePath(relative(relativeOutDir, source))
})
content = JSON.stringify(sourceMap)
} catch (e) {
logger.warn(`${logPrefix} ${yellow('Processing source map fail:')} ${path}`)
}
}
}

await writeOutput(path, content, targetOutDir, false)
})
)
})
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function VueResolver(): Resolver {
if (!program.getCompilerOptions().declarationMap) return outputs

const [beforeScript] = code.split(/\s*<script.*>/)
const beforeLines = Math.max(beforeScript.split('\n').length - 1, 0)
const beforeLines = beforeScript.split('\n').length

for (const output of outputs) {
if (output.path.endsWith('.map')) {
Expand All @@ -46,7 +46,7 @@ export function VueResolver(): Resolver {
source.replace(/\.vue\.ts$/, '.vue')
)

if (beforeLines) {
if (beforeScript && beforeScript !== code && beforeLines) {
sourceMap.mappings = `${base64VLQEncode([0, 0, beforeLines, 0])};${
sourceMap.mappings
}`
Expand Down

0 comments on commit cea0fa0

Please sign in to comment.