Skip to content

Commit

Permalink
fix: automatically add types in generated tsconfig (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-julien authored Feb 7, 2025
1 parent 4b7b548 commit f7422a5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export default defineBuildConfig({
},
],
declaration: true,
externals: ['#imports', 'h3', 'nitropack'],
externals: ['#imports', 'h3', 'nitropack', 'nitro-opentelemetry'],
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"nitropack": "^2.10.4",
"typescript": "^5.6.2",
"unbuild": "^2.0.0",
"vitest": "^2.1.5"
"vitest": "^2.1.5",
"nitro-opentelemetry": "link:./"
},
"packageManager": "pnpm@9.15.4",
"dependencies": {
Expand Down
7 changes: 1 addition & 6 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// https://nitro.unjs.io/guide/typescript
{
"extends": "./.nitro/types/tsconfig.json",
"compilerOptions": {
"types": [
"nitro-opentelemetry"
]
}
"extends": "./.nitro/types/tsconfig.json"
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

13 changes: 8 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import MagicString from 'magic-string'
import { getPresetFile, isPresetEntry } from './presets'
import { normalize } from "pathe"
import type { Nuxt } from "@nuxt/schema"
import { fileURLToPath } from 'node:url'
import defu from 'defu'

async function module(nitro: Nitro) {
Expand All @@ -17,7 +16,7 @@ async function module(nitro: Nitro) {
nitro.options.entry = await getPresetFile(nitro)
}

if(nitro.options.otel?.preset !== false) {
if (nitro.options.otel?.preset !== false) {
nitro.hooks.hook('rollup:before', (nitro, rollupConfig) => {
if (!rollupConfig.plugins) rollupConfig.plugins = [];
const plugins = rollupConfig.plugins
Expand All @@ -30,7 +29,7 @@ async function module(nitro: Nitro) {
return true
});
}

(rollupConfig.plugins as Plugin[]).push({
name: 'inject-init-plugin',
async transform(code, id) {
Expand All @@ -39,7 +38,7 @@ async function module(nitro: Nitro) {
if (normalizedId.includes('runtime/entries') || this.getModuleInfo(id)?.isEntry) {
const s = new MagicString(code)
s.prepend(`import '#nitro-opentelemetry/init';`)

return {
code: s.toString(),
map: s.generateMap({ hires: true }),
Expand Down Expand Up @@ -85,7 +84,11 @@ async function module(nitro: Nitro) {
inline: [nitro.options.errorHandler]
})
}

nitro.options.typescript.tsConfig = defu(nitro.options.typescript.tsConfig, {
compilerOptions: {
types: ['nitro-opentelemetry']
}
})
nitro.options.plugins.push(await resolvePath('nitro-opentelemetry/runtime/plugin', {
extensions: ['.mjs', '.ts']
}))
Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/basic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"extends": "./.nitro/types/tsconfig.json",
"compilerOptions": {
"types": ["nitro-opentelemetry"]
}
"extends": "./.nitro/types/tsconfig.json"
}

0 comments on commit f7422a5

Please sign in to comment.