Skip to content

npm package @sentry/sveltekit cannot be bundled by rollup #16190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
ktarmyshov opened this issue May 2, 2025 · 6 comments
Closed
3 tasks done

npm package @sentry/sveltekit cannot be bundled by rollup #16190

ktarmyshov opened this issue May 2, 2025 · 6 comments

Comments

@ktarmyshov
Copy link

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/sveltekit

SDK Version

"version": "9.15.0",

Framework Version

sveltekit "version": "2.20.8",

Link to Sentry event

N/A

Reproduction Example/SDK Setup

Warning I become: [plugin node-resolve] Could not resolve import "@sentry/sveltekit"

It also seems to me that the adapter-node also does not bundle it and leaves 'import "@sentry/sveltekit" as is, which means for azure swa this must be accompanied by according package.json (not good).

Could you please take a look and maybe provide just a simple ES npm?

Thnak you,
Konstantin

Steps to Reproduce

clone this branch https://github.com/kt-npm-modules/svelte-adapter-azure-swa/tree/feat/sentry
compare results of npm run build (adapter-node) and npm run build:swa (@ktarmyshov/svelte-adapter-azure-swa)

Expected Result

Sveltekit, adapter-node and other using rollup should be able to bundle @setnry/sveltekit I suppose.

Actual Result

@sentry/sveltekit is not bundled by rollup and left as external dep.

@ktarmyshov
Copy link
Author

I was able to resolve the issue with some workaround with "@rollup/plugin-alias": "^5.1.1", "@rollup/plugin-json": "^6.1.0", "@babel/preset-typescript": "^7.27.1",

@rollup/plugin-node-resolve cannot resolve the @sentry/sveltekit for the node&cjs target

function defaultRollupOptions() {
	return {
		external: requiredExternal,
		output: {
			// inlineDynamicImports: true,
			format: 'cjs',
			sourcemap: true
		},
		plugins: [
			sourcemaps(),
			nodeResolve({
				preferBuiltins: true,
				browser: false
			}),
			commonjs({
				strictRequires: true
			}),
			json()
		]
	};
}

for the plugin-alias I added this code

{
	find: '@sentry/sveltekit',
	replacement: join(
		process.cwd(),
		'node_modules',
		'@sentry',
		'sveltekit',
		'build',
		'esm',
		'index.server.js'
	)
}

This lead to issues with @opentelemetry package and @babel/preset-typescript (very strange) - cause it could not be found and bundled. So installing @babel/preset-typescript into adapter as a dependency helped. Somehow I was not able to externalize it.

When sveltekit builds the app it seems it also cannot resolve it for the server part, suppose same issue with plugin-node-resolve. But it does not complain about it.

Although I resolved the issue, it would be nice if it was fixed in @sentry/sveltekit.

For the ref here is the app with adapter
https://github.com/kt-npm-modules/svelte-adapter-azure-swa/tree/main/tests/demo

Thank you!

@Lms24
Copy link
Member

Lms24 commented May 5, 2025

Hi, thanks for letting us know about this! At this time, I'm not 100% sure this is an SDK problem but rather that you're trying to transpile a sveltekit app to CJS which as far as I can tell no longer is done by the official adapters. My guess is that the exports conditions from our package are somehow not compatible with the bundling setup.

Also please note that you should not bundle the server side Sentry SvelteKit SDK. It has code in there that doesn't work when being bundled. Hence, we clearly instruct users to install the SDK as a dependency and not a dev dependency (svelteKit bundles devDependencies but not dependencies).

I will leave this open for now (also taking a look at #16189) but can't promise that we'll change anything.

@ktarmyshov
Copy link
Author

@Lms24
Hi, Lukas!

Thank you for the info about bundling behaviour of the SK on the SSR, I didn't know that. But I also found info, that adapter-netlify bundles SSR dependencies

I tried externalizing @sentry/sveltekit and instead adding it to the package.json and calling "npm install" before deploying to Azure Static Web apps. In this case the azure SWA managed function fails to start at all (but works locally with SWA). Unfortunately I could not find a way to get the startup error of the function app behind SWA as this seems to be an issue with Azure.

I tried this with transpiling to CJS or using ES as output for rollup, but nothing worked as soon as @sentry/sveltekit is not bundled.

I have not yet covered the code, that would not work when bundled, but I'm bundling at the moment, to get at least some functionality of sentry working. When I debug in SWA-CLI (sentry external) and debug into the module, even if I'm using ES output, it jumps to CJS code. I wonder, why it's working at all, cause SWA-CLI runs ES module worker.

Image
Image

So maybe there are issues with resolutions in cloud SWA, and it fails with 'require' statement somewhere on the way.

So at the moment I'm doing bundling with the following setup, adapter bundling (output in ES):

const ignoreWarnCodes = new Set(['THIS_IS_UNDEFINED', 'CIRCULAR_DEPENDENCY']);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _adapterNode = adapterNode();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _adapterSWA = adapterSWA({
	// external: ['@sentry/sveltekit'],
	external: ['@babel/preset-typescript/package.json'],
	apiDir: './func',
	alias: {
		'@sentry/sveltekit': join(
			process.cwd(),
			'node_modules/@sentry/sveltekit/build/esm/index.server.js'
		)
	},
	onwarn: (warning, handler) => {
		if (
			ignoreWarnCodes.has(warning.code) ||
			(warning.plugin === 'sourcemaps' && warning.code === 'PLUGIN_WARNING')
		) {
			// Ignore this warning
			return;
		}
		// Use default warning handler for all other warnings
		handler(warning);
	},
...

@ktarmyshov
Copy link
Author

So I got it working as an external dep, the issue was with cloud version of azure static web apps - it behaves differently as the local swa-cli. Will document here if something is having issues.

For the older version of adapter, it was transpiling to CJS, and using the package.json:

{
	"main": "**/index.js",
	"dependencies": {
		"@azure/functions": "^4",
	}
}

Now I have this one:

{
	"type": "module",
	"main": "*/index.js",  <== this line, swa-cli can handle "**/index.js" for type module, azure cloud cannot, so single "*" no recursion
	"module": "*/index.js", <== this is ignored in both swa-cli and azure cloud 🤷‍♂️, but type is module....
	"dependencies": {
		"@azure/functions": "^4",
		"@sentry/sveltekit": "^9.15.0"
	}
}

With this, this issue can also be closed.

One comment though: the bundle size is enormous -> 160MB. A lot of things that probably should be in dev only
Why would I need esbuild or vite deadweight in production? I guess either it should be split into dev and prod part or be bundling-friendly to avoid things like this.

8.0K    ./func/HelloWorld
4.0K    ./func/host.json
4.0K    ./func/local.settings.json
158M    ./func/node_modules <== @azure/functions (if installed alone, 2.5MB) + sentry (installed alone) 157MB
124K    ./func/package-lock.json
4.0K    ./func/package.json
1.7M    ./func/sk_render <== generated bundle without @sentry

@Lms24
Copy link
Member

Lms24 commented May 6, 2025

Thanks a lot for investigating this! I wanna follow up on it and check what we can do to simplify but realistically, this adapter is rather low on our list of priorities (sorry, just trying to be transparent).

the bundle size is enormous -> 160MB

This is for the server-side, though, right? We don't want to ship 160MB to the browser 🤞
As for the server-side, this is unfortunately a known issue with the opentelemetry instrumentation requiring a lot of dependencies and weight. Not sure why vite and esbuild would be added to your output by the SDK. We're not declaring them a dependency of the SDK (only vite as an optional peer dependency)

@ktarmyshov
Copy link
Author

Hi, Lukas!

I think this one can be closed then. I would appreciate if you could suggest on how to handle rel-path resolution in the other issue. But this one is resolved for me.

Thnaks for the support!

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

2 participants