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

ES target ignored in certain cases [cloudflare-workers] #2679

Closed
half2me opened this issue Oct 25, 2021 · 11 comments · Fixed by #2692
Closed

ES target ignored in certain cases [cloudflare-workers] #2679

half2me opened this issue Oct 25, 2021 · 11 comments · Fixed by #2692
Labels
documentation Improvements or additions to documentation pkg:adapter-cloudflare-workers
Milestone

Comments

@half2me
Copy link
Contributor

half2me commented Oct 25, 2021

Describe the bug

When updating recently to a newer version of SvelteKit and Cloudflare workers adapter I started getting an error when trying to deploy my code with wrangler publish

👀  ./workers-site/index.js 7390:28
Module parse failed: Unexpected token (7390:28)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|   
|     
>     console.log(request ?? "foobar");
|     
|  
Error: webpack returned an error. You may be able to resolve this issue by running npm install.

I use typescript, and I have target: es2019 specified in my tsconfig.json, so it should be compiling down the ??.
The interesting thing is this only happens under certain conditions. When I use the ?? operator in a .svelte file, it gets compiled to es2019 without any issues. But if the code is in hooks.ts, it doesn't get compiled down. This used to work just fine, so the issue must be in something changed in the last 2-3 weeks at most.

My current work-around to fix the issue, is to add to svelte.config.js: kit.vite.build.target = 'es2019'. After I add this, everything builds just fine.

Reproduction

  • Initialise a new SvelteKit demo app with typescript.
  • Add some code using an esnext feature like ?? to hooks.ts
  • Configure cloudflare workers adapter (This might not even be related, but its what I'm using atm)
  • Run npm run build
  • Look inside the workers-site/index.js output file, you can see the ?? being left in the code.

Logs

No response

System Info

System:
    OS: macOS 11.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 1.36 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.0.1 - /usr/local/bin/node
    npm: 8.1.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 94.0.4606.81
    Safari: 15.0
  npmPackages:
    @sveltejs/adapter-cloudflare-workers: ^1.0.0-next.20 => 1.0.0-next.25 
    @sveltejs/kit: next => 1.0.0-next.189 
    svelte: ^3.38.3 => 3.44.0

Severity

serious, but I can work around it

Additional Information

I'm unsure if this is a bug in Svelte, SvelteKit, Cloudflare worker adapter, or something completely different. All I know is I never had this issue until just recently, and my code has been using esnext features all along, and I only just started having issues after my most recent upgrade.

@benmccann
Copy link
Member

This probably changed as a result of #2604

I guess Cloudflare workers must use an old version of V8 without support for ??. Perhaps @lukeed know what version of ES is supported there?

@pngwn
Copy link
Member

pngwn commented Oct 25, 2021

The Workers runtime is updated at least once a week, to at least the version that is currently used by Chrome's stable release.

From the docs

This does not look like a problem with the v8 runtime but rather a problem with the bundling that wrangler/cf worker deploy does for you ahead of deployment when your project is set to type = webpack in your wrangler.toml. It looks like whatever version of webpack wrangler uses doesn't not support this syntax and they don't have the relevant loaders to handle it. The docs suggest that only webpack 4 is supported, i'd recommend a custom build command here to skip the webpack bundling process: custom builds.

@lukeed
Copy link
Member

lukeed commented Oct 26, 2021

Workers is always latest V8 so ?? is fine/supported. As @pngwn said, this is other tooling (webpack) freaking out. Use type = "javascript" and define a custom build instead (link above)

@half2me
Copy link
Contributor Author

half2me commented Oct 26, 2021

Thanks for all the help guys. Like I said, I wasn't sure what part of the pipeline was causing the issue as I never really dug into how the wrangler.toml worked, but I'll definitely have a look at this.

@half2me
Copy link
Contributor Author

half2me commented Oct 26, 2021

@lukeed Is there any "build" step required for wrangler? I mean there is already a single index.js file in the workers-site directory. package.json only has a reference to it, and nothing else. Is there any other "build" step required?

@half2me
Copy link
Contributor Author

half2me commented Oct 26, 2021

I've made the following changes to my wrangler.toml:

type = "javascript"

[build]
command = ""

[build.upload]
format = "service-worker"

The empty build command seems to work fine

@pngwn
Copy link
Member

pngwn commented Oct 26, 2021

You don't need the custom build as far as I'm aware. You can just remove that empty command, should work fine without it.

@half2me
Copy link
Contributor Author

half2me commented Oct 26, 2021

You don't need the custom build as far as I'm aware. You can just remove that empty command, should work fine without it.

It definitely needs to be there. If I remove it, I get:

Error: ⚠️  Workers Sites requires using a bundler, and your configuration indicates that you aren't using one. You can fix this by:
* setting your project type to "webpack" to use our automatically configured webpack bundler.
* setting your project type to "javascript", and configuring a build command in the `[build]` section if you wish to use your choice of bundler.

@benmccann
Copy link
Member

Seems like a good change to make to the readme: https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare-workers/README.md

Want to send a PR?

@lukeed
Copy link
Member

lukeed commented Oct 27, 2021

You need the build.command key to be present. I believe there's an undocumented default of running npm run build, but you should just define "npm run build" to be explicit.

type = "javascript"

[build]
command = "npm run build"
# ^^ runs the `svelte-kit build` command

[build.upload]
format = "service-worker"

@benmccann benmccann added the documentation Improvements or additions to documentation label Oct 27, 2021
@benmccann benmccann added this to the 1.0 milestone Oct 27, 2021
@half2me
Copy link
Contributor Author

half2me commented Oct 27, 2021

You need the build.command key to be present. I believe there's an undocumented default of running npm run build, but you should just define "npm run build" to be explicit.

type = "javascript"

[build]
command = "npm run build"
# ^^ runs the `svelte-kit build` command

[build.upload]
format = "service-worker"

@lukeed this is not the case. If I change the command to npm run build it will run the svelte-kit build command, but if I leave it empty, it will skip it. This is useful, as the build might have already been done, and its nice to be able to just tell wrangler to publish it, without re-building.

@benmccann I'll make a PR to modify the docs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation pkg:adapter-cloudflare-workers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants