Skip to content

Commit 10e5434

Browse files
committed
ref(nuxt): Build the Nuxt module in-house instead of @nuxt/module-builder
Replace @nuxt/module-builder with a plain rollup + tsc setup for the build/module output: rollup bundles the module entry and emits the runtime files one-to-one (preserveModules), tsc emits the declarations, and a small script writes module.cjs, module.json and types.d.ts. This drops a build tool that consumes the TypeScript compiler API, so the package no longer pins us to a specific compiler.
1 parent f0bcefb commit 10e5434

9 files changed

Lines changed: 131 additions & 351 deletions

packages/nuxt/build.config.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/nuxt/generate-build-stubs.bash

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/nuxt/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"local-pkg": "^1.1.2"
6767
},
6868
"devDependencies": {
69-
"@nuxt/module-builder": "^0.8.4",
7069
"@nuxt/nitro-server": "^3.21.6",
7170
"nitro": "^3.0.260311-beta",
7271
"nuxi": "^3.25.1",
@@ -76,7 +75,7 @@
7675
"scripts": {
7776
"build": "run-s build:types build:transpile",
7877
"build:dev": "yarn build",
79-
"build:nuxt-module": "bash ./generate-build-stubs.bash && nuxt-module-build build --outDir build/module",
78+
"build:nuxt-module": "rollup -c rollup.module.config.mjs && tsc -p tsconfig.module.json && node scripts/build-module-meta.mjs",
8079
"build:transpile": "rollup -c rollup.npm.config.mjs && yarn build:nuxt-module",
8180
"build:types": "tsc -p tsconfig.types.json",
8281
"build:watch": "run-p build:transpile:watch",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { readdirSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
import esbuild from 'rollup-plugin-esbuild';
4+
5+
// The Nuxt module ships two kinds of output that live side by side in `build/module`:
6+
// - `module.mjs`: the module entry, bundled from `src/module.ts`.
7+
// - `runtime/**`: the files Nuxt injects into the consuming app, emitted one-to-one
8+
// (never bundled) because the app's own build re-processes them.
9+
// This config replaces `@nuxt/module-builder` so the package builds with plain rollup + tsc
10+
// and doesn't couple us to a build tool that consumes the TypeScript compiler API.
11+
12+
// Anything that isn't a relative path is provided by the consuming app or Node at runtime
13+
// (this covers `@sentry/*`, `nuxt/app`, `#imports`, node builtins), so it stays external.
14+
const isExternal = id => !id.startsWith('.') && !id.startsWith('/') && !id.startsWith('\0');
15+
16+
const transpile = esbuild({
17+
target: 'es2020',
18+
// Don't read a per-package tsconfig; pin only what affects codegen.
19+
tsconfig: false,
20+
tsconfigRaw: { compilerOptions: { useDefineForClassFields: false } },
21+
sourceMap: false,
22+
});
23+
24+
function runtimeEntrypoints(dir = 'src/runtime', acc = []) {
25+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
26+
const full = join(dir, entry.name);
27+
if (entry.isDirectory()) {
28+
runtimeEntrypoints(full, acc);
29+
} else if (entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts')) {
30+
acc.push(full);
31+
}
32+
}
33+
34+
return acc;
35+
}
36+
37+
export default [
38+
{
39+
input: 'src/module.ts',
40+
output: { file: 'build/module/module.mjs', format: 'esm' },
41+
external: isExternal,
42+
plugins: [transpile],
43+
},
44+
{
45+
input: runtimeEntrypoints(),
46+
output: {
47+
dir: 'build/module/runtime',
48+
format: 'esm',
49+
preserveModules: true,
50+
preserveModulesRoot: 'src/runtime',
51+
entryFileNames: '[name].js',
52+
},
53+
external: isExternal,
54+
plugins: [transpile],
55+
},
56+
];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { readFileSync, writeFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
// Emits the three non-compiled artifacts a Nuxt module needs alongside `module.mjs`,
5+
// replacing what `@nuxt/module-builder` used to generate:
6+
// - `module.cjs`: a CJS shim. It can't statically require the ESM-only `@nuxt/kit`,
7+
// so it lazily imports the ESM entry instead.
8+
// - `module.json`: module metadata Nuxt reads. Keep the `meta` fields in sync with
9+
// `defineNuxtModule({ meta })` in `src/module.ts`.
10+
// - `types.d.ts`: the type entry referenced by the `./module` export.
11+
12+
const outDir = 'build/module';
13+
const { version } = JSON.parse(readFileSync('package.json', 'utf-8'));
14+
15+
writeFileSync(
16+
join(outDir, 'module.cjs'),
17+
`module.exports = function(...args) {
18+
return import('./module.mjs').then(m => m.default.call(this, ...args))
19+
}
20+
const _meta = module.exports.meta = require('./module.json')
21+
module.exports.getMeta = () => Promise.resolve(_meta)
22+
`,
23+
);
24+
25+
writeFileSync(
26+
join(outDir, 'module.json'),
27+
`${JSON.stringify(
28+
{
29+
name: '@sentry/nuxt/module',
30+
configKey: 'sentry',
31+
compatibility: { nuxt: '>=3.7.0' },
32+
version,
33+
},
34+
null,
35+
2,
36+
)}\n`,
37+
);
38+
39+
writeFileSync(join(outDir, 'types.d.ts'), "export { type ModuleOptions, default } from './module'\n");

packages/nuxt/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
33

4-
"include": ["src/**/*", "build.config.ts"],
4+
"include": ["src/**/*"],
55

66
"compilerOptions": {
77
// package-specific options

packages/nuxt/tsconfig.module.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["src/module.ts", "src/runtime/**/*"],
4+
"compilerOptions": {
5+
"declaration": true,
6+
"declarationMap": false,
7+
"emitDeclarationOnly": true,
8+
"outDir": "build/module",
9+
"rootDir": "src"
10+
}
11+
}

packages/nuxt/tsconfig.types.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"extends": "./tsconfig.json",
3-
"exclude": ["build.config.ts"],
43
"compilerOptions": {
54
"declaration": true,
65
"declarationMap": true,

0 commit comments

Comments
 (0)