Skip to content
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

Svelte-5, top level await & ESM output #956

Open
k-dahl opened this issue Jan 15, 2025 · 2 comments
Open

Svelte-5, top level await & ESM output #956

k-dahl opened this issue Jan 15, 2025 · 2 comments

Comments

@k-dahl
Copy link

k-dahl commented Jan 15, 2025

I'm unable to deploy svelte-5 with this adapter and the new svelte.js/ts files with top-level await because the lambda here gets compiled as CJS, which does not support top level await.

I tried passing in esbuild: { format: 'esm' } via the adapter config, and that gets me a little further with a successful deploy - but the resulting lambda function then fails on every request with:

{
    "errorType": "Runtime.UserCodeSyntaxError",
    "errorMessage": "SyntaxError: Cannot use import statement outside a module",
    "stack": [
        "Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module",
        "    at _loadUserApp (file:///var/runtime/index.mjs:1084:17)",
        "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
        "    at async start (file:///var/runtime/index.mjs:1282:23)",
        "    at async file:///var/runtime/index.mjs:1288:1"
    ]
}

my package.json does have "type": "module" but I suspect the issue is specific to the esbuild options.

Any insights you could provide would be greatly appreciated!

@k-dahl
Copy link
Author

k-dahl commented Jan 16, 2025

I was able to get this a bit farther by manually adding a package.json alongside the server.js output, it only has { "type": "module" } in it... that then results in a new error about not being able to require "path", which I as able to resolve with this shim via banner/js:

            esbuild: {
                format: 'esm',
                platform: 'node',
                target: 'esnext',
                minify: false,
                banner: {
                    js: "import { createRequire } from 'module';const require = createRequire(import.meta.url);",
                }
            },

so, it is working now but still requires the manual step of creating that package.json file after each deploy.

@k-dahl
Copy link
Author

k-dahl commented Jan 16, 2025

I switched the adapter to deploy: false, and added some additional build steps to stick a simple package.json file into the lambda output directory, and then run the CDK deploy in a separate build step. It does seem to be working, but I'd love to hear if there is any other way to do this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant