Skip to content

Commit

Permalink
fix(react): unlimited init order when using with react plugin
Browse files Browse the repository at this point in the history
fix #148
  • Loading branch information
qmhc committed Feb 23, 2023
1 parent 42a755c commit 92b82ff
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 30 deletions.
3 changes: 3 additions & 0 deletions examples/react/.gitignore
@@ -0,0 +1,3 @@
types/
dist/
node_modules/
11 changes: 0 additions & 11 deletions examples/react/types/index.d.ts

This file was deleted.

9 changes: 0 additions & 9 deletions examples/react/types/main.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion examples/react/types/vite-env.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions examples/react/vite.config.ts
Expand Up @@ -21,6 +21,7 @@ export default defineConfig({
}
},
plugins: [
react(),
dts({
libFolderPath: '../../node_modules/typescript/lib',
outputDir: ['dist', 'types'],
Expand All @@ -31,8 +32,7 @@ export default defineConfig({
skipDiagnostics: false,
rollupTypes: true,
insertTypesEntry: true
}),
react()
})
]
})

Expand Down
1 change: 0 additions & 1 deletion examples/ts/.gitignore
@@ -1,4 +1,3 @@
types/
dist/
node_modules/
components/*.d.ts
14 changes: 8 additions & 6 deletions src/plugin.ts
Expand Up @@ -95,13 +95,15 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
let allowJs = false
let transformError = false

function internalTransform(code: string, id: string) {
async function internalTransform(id: string) {
if (!project || !filter(id)) {
return
}

// some plugins (e.g. @vitejs/plugin-react) sorted before dts plugin may change
// source code when building, so we need to read the original content via the id
if (vueRE.test(id)) {
const { error, content, ext } = compileVueCode(code)
const { error, content, ext } = compileVueCode(await fs.readFile(id, 'utf-8'))

if (!transformError && error) {
logger.error(
Expand All @@ -121,7 +123,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
project.createSourceFile(`${id}.${ext || 'js'}`, content, { overwrite: true })
}
} else if (!id.includes('.vue?vue') && (tsRE.test(id) || (allowJs && jsRE.test(id)))) {
project.createSourceFile(id, code, { overwrite: true })
project.createSourceFile(id, await fs.readFile(id, 'utf-8'), { overwrite: true })
}
}

Expand Down Expand Up @@ -335,8 +337,8 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
}
},

transform(code, id) {
internalTransform(code, id)
async transform(_, id) {
await internalTransform(id)
return null
},

Expand All @@ -348,7 +350,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
const sourceFile = project.getSourceFile(normalizePath(id))

sourceFile && project.removeSourceFile(sourceFile)
internalTransform(await fs.readFile(id, 'utf-8'), id)
await internalTransform(id)
}
}
},
Expand Down

0 comments on commit 92b82ff

Please sign in to comment.