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

Code behind if (browser) check is included in server bundle and fails #1534

Closed
mattjennings opened this issue May 24, 2021 · 10 comments
Closed
Labels
bug Something isn't working vite

Comments

@mattjennings
Copy link

mattjennings commented May 24, 2021

Describe the bug
When importing the monaco-editor package, SvelteKit fails to build with both node and static adapters. It works in dev, and the module is imported dynamically in an onMount.

Logs

When running svelte-kit build, it ends with:

> Using @sveltejs/adapter-node
> self is not defined
ReferenceError: self is not defined
    at file:///Users/mattjennings/Desktop/sk/.svelte-kit/output/server/app.js:7056:1
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async prerender (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index5.js:79:14)
    at async Object.prerender (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index5.js:296:5)
    at async adapt (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@sveltejs/adapter-node/index.js:28:4)
    at async adapt (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index5.js:322:2)
    at async file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/cli.js:616:5
 ERROR  Command failed with exit code 1.

see Stacktraces for the full build output.

To Reproduce
I've created a reproduction repo here: https://github.com/mattjennings/sveltekit-monaco-bug

Simply try building the project to reproduce. If you go to routes/index.svelte and uncomment the onMount block, the build will succeed.

You'll also notice the self.MonacoEnvironment = ... assignment in there, but that is not related to the error.

Expected behavior
It seems like dynamic imports in onMount are still being included in the server build? I had expected that anything imported in onMount would be kept separate (and that the build would succeed).

Stacktraces
Full build output:

Stack trace
> svelte-kit build

vite v2.3.3 building for production...
✓ 874 modules transformed.
.svelte-kit/output/client/_app/assets/codicon.4946e746.ttf              69.30kb
.svelte-kit/output/client/_app/manifest.json                            35.54kb
.svelte-kit/output/client/_app/layout.svelte-6282c985.js                0.49kb / brotli: 0.29kb
.svelte-kit/output/client/_app/error.svelte-7f337df8.js                 1.19kb / brotli: 0.57kb
.svelte-kit/output/client/_app/chunks/preload-helper-9f12a5fd.js        0.61kb / brotli: 0.31kb
.svelte-kit/output/client/_app/pages/index.svelte-2ffa0cfe.js           1.00kb / brotli: 0.53kb
.svelte-kit/output/client/_app/chunks/json.worker-d8aa2be0.js           0.10kb / brotli: 0.10kb
.svelte-kit/output/client/_app/chunks/jsonMode-a15b3f81.js              12.42kb / brotli: 3.36kb
.svelte-kit/output/client/_app/start-9286caee.js                        16.71kb / brotli: 5.32kb
.svelte-kit/output/client/_app/chunks/tsMode-c6cd1600.js                26.17kb / brotli: 6.15kb
.svelte-kit/output/client/_app/chunks/abap-59780eb7.js                  11.26kb / brotli: 3.60kb
.svelte-kit/output/client/_app/chunks/apex-47d08499.js                  3.74kb / brotli: 1.47kb
.svelte-kit/output/client/_app/chunks/azcli-65222778.js                 0.69kb / brotli: 0.24kb
.svelte-kit/output/client/_app/chunks/bat-82890a0d.js                   1.67kb / brotli: 0.72kb
.svelte-kit/output/client/_app/chunks/bicep-fc9f7b04.js                 2.30kb / brotli: 0.75kb
.svelte-kit/output/client/_app/chunks/cameligo-1bc07545.js              1.85kb / brotli: 0.77kb
.svelte-kit/output/client/_app/chunks/clojure-f837f4b2.js               9.28kb / brotli: 3.15kb
.svelte-kit/output/client/_app/chunks/coffee-d629f24d.js                3.37kb / brotli: 1.10kb
.svelte-kit/output/client/_app/chunks/cpp-8c64c5fd.js                   5.08kb / brotli: 1.79kb
.svelte-kit/output/client/_app/chunks/csharp-2c4b59c7.js                4.28kb / brotli: 1.47kb
.svelte-kit/output/client/_app/chunks/csp-88727219.js                   1.25kb / brotli: 0.41kb
.svelte-kit/output/client/_app/chunks/css-df55a83f.js                   4.27kb / brotli: 1.16kb
.svelte-kit/output/client/_app/chunks/dart-e19c6755.js                  4.01kb / brotli: 1.41kb
.svelte-kit/output/client/_app/chunks/dockerfile-62e05121.js            1.69kb / brotli: 0.52kb
.svelte-kit/output/client/_app/chunks/ecl-b7141c6f.js                   5.08kb / brotli: 1.88kb
.svelte-kit/output/client/_app/chunks/elixir-2fd128c3.js                9.32kb / brotli: 2.13kb
.svelte-kit/output/client/_app/chunks/fsharp-c340ab3d.js                2.78kb / brotli: 1.12kb
.svelte-kit/output/client/_app/chunks/go-ea3ce64d.js                    2.46kb / brotli: 0.99kb
.svelte-kit/output/client/_app/chunks/graphql-aeda9477.js               2.07kb / brotli: 0.82kb
.svelte-kit/output/client/_app/chunks/handlebars-76409c68.js            6.19kb / brotli: 1.20kb
.svelte-kit/output/client/_app/chunks/hcl-e1d32cb0.js                   3.37kb / brotli: 1.26kb
.svelte-kit/output/client/_app/chunks/ini-ba157987.js                   0.94kb / brotli: 0.43kb
.svelte-kit/output/client/_app/chunks/html-65dfd18e.js                  4.32kb / brotli: 1.00kb
.svelte-kit/output/client/_app/chunks/java-e2bc782a.js                  2.76kb / brotli: 1.12kb
.svelte-kit/output/client/_app/chunks/javascript-b41f8387.js            0.81kb / brotli: 0.38kb
.svelte-kit/output/client/_app/chunks/typescript-1b5be4c1.js            4.80kb / brotli: 1.69kb
.svelte-kit/output/client/_app/chunks/julia-6640e74c.js                 6.79kb / brotli: 2.26kb
.svelte-kit/output/client/_app/chunks/kotlin-cac7b678.js                3.22kb / brotli: 1.24kb
.svelte-kit/output/client/_app/chunks/lexon-22f82d14.js                 2.24kb / brotli: 0.77kb
.svelte-kit/output/client/_app/chunks/less-fec9a614.js                  3.67kb / brotli: 1.21kb
.svelte-kit/output/client/_app/chunks/lua-0b861259.js                   1.94kb / brotli: 0.77kb
.svelte-kit/output/client/_app/chunks/liquid-1dc49f29.js                3.43kb / brotli: 1.26kb
.svelte-kit/output/client/_app/chunks/m3-4c2295fc.js                    2.61kb / brotli: 1.09kb
.svelte-kit/output/client/_app/chunks/mips-a57496b9.js                  2.38kb / brotli: 0.93kb
.svelte-kit/output/client/_app/chunks/markdown-4250b190.js              3.57kb / brotli: 1.17kb
.svelte-kit/output/client/_app/chunks/msdax-31b2bc8e.js                 4.66kb / brotli: 1.59kb
.svelte-kit/output/client/_app/chunks/objective-c-d279fc53.js           2.21kb / brotli: 0.87kb
.svelte-kit/output/client/_app/chunks/pascal-6df14a60.js                2.78kb / brotli: 1.13kb
.svelte-kit/output/client/_app/chunks/pascaligo-0200c07a.js             1.82kb / brotli: 0.75kb
.svelte-kit/output/client/_app/chunks/mysql-7886ae85.js                 14.02kb / brotli: 4.34kb
.svelte-kit/output/client/_app/chunks/perl-0d15268d.js                  7.92kb / brotli: 2.64kb
.svelte-kit/output/client/_app/chunks/php-ecd9a89a.js                   7.70kb / brotli: 1.73kb
.svelte-kit/output/client/_app/chunks/pgsql-90e87c73.js                 17.08kb / brotli: 5.25kb
.svelte-kit/output/client/_app/chunks/postiats-93337459.js              7.54kb / brotli: 2.12kb
.svelte-kit/output/client/_app/chunks/powershell-f25808aa.js            3.06kb / brotli: 1.11kb
.svelte-kit/output/client/_app/chunks/pug-92442c92.js                   4.58kb / brotli: 1.41kb
.svelte-kit/output/client/_app/chunks/powerquery-ddcecd40.js            16.41kb / brotli: 4.05kb
.svelte-kit/output/client/_app/chunks/python-3a69f06c.js                3.08kb / brotli: 1.18kb
.svelte-kit/output/client/_app/chunks/r-0bb21523.js                     2.90kb / brotli: 1.05kb
.svelte-kit/output/client/_app/chunks/redis-001791e4.js                 3.34kb / brotli: 1.24kb
.svelte-kit/output/client/_app/chunks/razor-f8edd2c4.js                 8.13kb / brotli: 1.81kb
.svelte-kit/output/client/_app/chunks/restructuredtext-ddc776ab.js      3.72kb / brotli: 1.19kb
.svelte-kit/output/client/_app/chunks/redshift-8fe36169.js              11.37kb / brotli: 3.62kb
.svelte-kit/output/client/_app/chunks/ruby-0a195806.js                  8.17kb / brotli: 2.23kb
.svelte-kit/output/client/_app/chunks/rust-1f688ecd.js                  3.72kb / brotli: 1.53kb
.svelte-kit/output/client/_app/chunks/sb-7fe52546.js                    1.65kb / brotli: 0.70kb
.svelte-kit/output/client/_app/chunks/scheme-a48b61d8.js                1.59kb / brotli: 0.69kb
.svelte-kit/output/client/_app/chunks/scala-8665daeb.js                 7.01kb / brotli: 1.81kb
.svelte-kit/output/client/_app/chunks/scss-ef27f337.js                  6.12kb / brotli: 1.47kb
.svelte-kit/output/client/_app/chunks/shell-7c3cd3e3.js                 2.72kb / brotli: 0.96kb
.svelte-kit/output/client/_app/chunks/sophia-d02f84fb.js                2.56kb / brotli: 1.01kb
.svelte-kit/output/client/_app/chunks/st-594518e7.js                    7.08kb / brotli: 1.88kb
.svelte-kit/output/client/_app/chunks/swift-478632e9.js                 4.08kb / brotli: 1.44kb
.svelte-kit/output/client/_app/chunks/solidity-068c7c92.js              18.02kb / brotli: 2.44kb
.svelte-kit/output/client/_app/chunks/sql-6fca7a91.js                   17.67kb / brotli: 5.60kb
.svelte-kit/output/client/_app/chunks/tcl-502d8d5a.js                   3.35kb / brotli: 1.15kb
.svelte-kit/output/client/_app/chunks/systemverilog-11bbff0f.js         7.09kb / brotli: 2.31kb
.svelte-kit/output/client/_app/chunks/twig-713f1a5b.js                  5.70kb / brotli: 1.30kb
.svelte-kit/output/client/_app/chunks/xml-d20a8f8a.js                   1.93kb / brotli: 0.66kb
.svelte-kit/output/client/_app/chunks/vb-9aeba90d.js                    5.52kb / brotli: 1.77kb
.svelte-kit/output/client/_app/chunks/json.worker-b5720134.js           0.03kb / brotli: 0.03kb
.svelte-kit/output/client/_app/assets/pages/index.svelte-2e2e5df6.css   0.03kb / brotli: 0.04kb
.svelte-kit/output/client/_app/assets/start-a8cd1609.css                0.16kb / brotli: 0.10kb
.svelte-kit/output/client/_app/chunks/htmlMode-95fbc5cf.js              110.40kb / brotli: 22.64kb
.svelte-kit/output/client/_app/chunks/yaml-a357325a.js                  3.32kb / brotli: 1.05kb
.svelte-kit/output/client/_app/chunks/cssMode-3b029fb5.js               240.81kb / brotli: 44.98kb
.svelte-kit/output/client/_app/assets/editor.main-294d49d0.css          69.72kb / brotli: 11.06kb
.svelte-kit/output/client/_app/chunks/vendor-0adc8b7f.js                256.60kb / brotli: 62.60kb
.svelte-kit/output/client/_app/chunks/editor.main-f48cabfe.js           2096.05kb / brotli: skipped (large chunk)

(!) Some chunks are larger than 500kb after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/guide/en/#outputmanualchunks
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
vite v2.3.3 building SSR bundle for production...
✓ 869 modules transformed.
.svelte-kit/output/server/chunks/json.worker-e1207c06.js   0.09kb
.svelte-kit/output/server/app.js                           5876.65kb

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-node
> self is not defined
ReferenceError: self is not defined
    at file:///Users/mattjennings/Desktop/sk/.svelte-kit/output/server/app.js:7056:1
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async prerender (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index5.js:79:14)
    at async Object.prerender (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index5.js:296:5)
    at async adapt (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@sveltejs/adapter-node/index.js:28:4)
    at async adapt (file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index5.js:322:2)
    at async file:///Users/mattjennings/Desktop/sk/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sveltejs/kit/dist/cli.js:616:5
 ERROR  Command failed with exit code 1.

Information about your SvelteKit Installation:

Diagnostics

System:
OS: macOS 11.3.1
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 42.08 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.2.0 - ~/.nvm/versions/node/v16.2.0/bin/node
npm: 7.13.0 - ~/.nvm/versions/node/v16.2.0/bin/npm
Browsers:
Brave Browser: 90.1.24.82
Chrome: 90.0.4430.212
Safari: 14.1
Safari Technology Preview: 14.2
npmPackages:
@sveltejs/kit: next => 1.0.0-next.109
svelte: ^3.34.0 => 3.38.2

  • node / static adapters

Severity
I don't think it's an issue specific to monaco-editor, so I'd say it's a high severity.

Additional context
Someone using firebase with SvelteKit is running into the same error. Is there something in common between the two packages?

@benmccann
Copy link
Member

wrap it in an if (browser) check: https://kit.svelte.dev/faq#integrations

@mattjennings
Copy link
Author

mattjennings commented May 24, 2021

The package is imported inside onMount as the FAQ suggests. Adding an additional browser check does not solve the issue:

<script>
  import { onMount } from "svelte";
  import { browser } from "$app/env";
  let divEl;

  onMount(async () => {
    if (browser) {
      const Monaco = await import("monaco-editor");
      const jsonWorker = await import(
        "monaco-editor/esm/vs/language/json/json.worker?worker"
      ).then((res) => res.default);

      self.MonacoEnvironment = {
        getWorker: function () {
          return new jsonWorker();
        },
      };

      const editor = Monaco.editor.create(divEl, {
        value: JSON.stringify(
          {
            something: 123,
          },
          null,
          "\t"
        ),
        language: "json",
        automaticLayout: true,
      });

      return () => {
        editor.dispose();
      };
    }
  });
</script>

<div bind:this={divEl} />

@benmccann

This comment has been minimized.

@mattjennings

This comment has been minimized.

@mattjennings

This comment has been minimized.

@benmccann benmccann removed the invalid label May 24, 2021
@benmccann benmccann reopened this May 24, 2021
@benmccann benmccann changed the title Node/static adapter fails to build when importing monaco-editor Code behind if (browser) check is included in server bundle and fails May 24, 2021
@benmccann
Copy link
Member

Hmm, it does look like there might be a packaging bug here. The interesting thing is that even when I wrap the Monaco usage in an if (browser) check it still ends up in .svelte-kit/output/server/app.js. I would have expected Vite would have removed that code from the bundle

@benmccann benmccann added the vite label May 24, 2021
@benmccann
Copy link
Member

I checked this out a bit more and SvelteKit's esbuild packaging isn't getting called in this pathway, so the packaging bug has to be in Vite. Since the bug looks to reside in Vite, I'm going to go ahead and close this. I'd recommend you file an issue over in the Vite repo

@benmccann benmccann added the bug Something isn't working label May 24, 2021
@mattjennings
Copy link
Author

Will do, thanks for looking into this further.

@mattjennings
Copy link
Author

So the offending line here is the ?worker in the jsonWorker import. Removing that and wrapping the onMount in a browser check fixes the error (but causes jsonWorker not to work). I was not able to reproduce this problem in a vite + react + ssr project, so I'm not sure. I figured I'd note it here anyway.

Re: SvelteKit's esbuild packaging not being called - vite bundles the build with rollup right? So it shouldn't be getting called anyway?

@mattjennings
Copy link
Author

Using ?worker&inline instead of ?worker in the import fixes everything. Seems like ?worker imports will always get included in the app.js, even when inside a browser check. When using &inline I can hoist the import back to the top, though.

final result:

<script>
  import { onMount } from "svelte";
  import { browser } from "$app/env";
  import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker&inline";
  let divEl;

  if (browser) {
    onMount(async () => {
      const Monaco = await import("monaco-editor");

      self.MonacoEnvironment = {
        getWorker: function () {
          return new jsonWorker();
        },
      };

      const editor = Monaco.editor.create(divEl, {
        value: JSON.stringify(
          {
            something: 123,
          },
          null,
          "\t"
        ),
        language: "json",
        automaticLayout: true,
      });

      return () => {
        editor.dispose();
      };
    });
  }
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vite
Projects
None yet
Development

No branches or pull requests

2 participants