-
-
Notifications
You must be signed in to change notification settings - Fork 638
Description
Context
-
Node.js does not consume sourcemaps by default and should be enabled:
- CLI:
node --enable-source-maps
(Node.js >= v12.12) - Environment variable:
NODE_OPTIONS="--enable-source-maps"
(Node.js >=v12.12) - Runtime:
module.setSourceMapsSupport
(Node.js >= 22.14) orrequire("sourcem-map-support").install
(Node.js < 22.14) (catch: should be as early as possible before loading any other modules)
- CLI:
-
Nitro (v2) uses rollup and esbuild to generate production sourcemaps, which are:
- External by default (
// # sourceMappingURL=[mod].mjs.map
), - Could be inlined using
sourcemap: "inline"
nitro config (or completely disabled usingfalse
) - Do not bundle sources (regardless of inline or external), but reference to
../src
, which means src needs to be deployed for them to work.
What can we do?
-
While ideally, runtimes and providers should consume source maps by default, they don't.
- We could follow up with runtimes and deployment providers to enable sourcemap support.
- We could enable a default polyfill for modern Node.js to consume maps (>= 22.14) (feat: polyfill to enable node sourcemaps unjs/unenv#511).
-
(not necessary) The fact that source maps point to
../src
makes them harder for production consumption, as.output
is supposed to be the only deployed directory. However, at the cost of bundling sourcemap contents into the output bundle, consumption would be much easier without needing to bundle the whole project (however, it is only necessary to log the contents of source normal tracing works, regardless)
POC
Before moving forward to enable support by default or with a flag, the effect of this idea can be replicated via Nitro config and unenv polyfill.
export default defineNitroConfig({
// Enable source map consumption by default in Node.js >= 22.14
unenv: {
// Note: currently requires unenv-nightly
polyfill: ["unenv/polyfill/source-maps"],
},
// Enable full source bundling (not required)
esbuild: {
options: {
sourcesContent: true,
},
},
rollupConfig: {
output: {
sourcemapExcludeSources: false,
},
},
});
Results
POC available on nitro-deploys
✅ Works out of the box
- Node.js
- Firebase App hosting (deploy)
- Koyeb (deploy)
- Netlify functions (deploy)
- Vercel (deploy)
- Improvement: we can enable
shouldAddSourcemapSupport
for Node.js 20 support.
- Improvement: we can enable