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

External imports can't build #3306

Closed
livehtml opened this issue Jan 12, 2022 · 6 comments
Closed

External imports can't build #3306

livehtml opened this issue Jan 12, 2022 · 6 comments
Labels
p3-edge-case SvelteKit cannot be used in an uncommon way
Milestone

Comments

@livehtml
Copy link

livehtml commented Jan 12, 2022

Describe the bug

I want to put in the root dir of SvelteKit my config file: app.config.js (near svelte.config.js, package.json, etc)
So I create alias $base, mark file as external and use it anywhere in app via import (import appConfig from "$base/app.config"):

config = {
    kit: {
        adapter: adapterNode({
            esbuild(defaultOptions) {
                return {
                    ...defaultOptions,
                    external: [...Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}), "*app.config.js"]
                };
            }
        }),
        vite: () => ({
            build: {
                rollupOptions: {
                    external: [/app\.config\.js/],
                    // makeAbsoluteExternalsRelative: 'ifRelativeSource'
                },
            },
            resolve: {
                alias: {
                    '$base': path.resolve('./'),
                }
            },
        })
    }
};

Everything works in DEV but when I want build via adapter-node I've got an error:

Using @sveltejs/adapter-node
Cannot find module '/home/user/Projects/Svelte/site/sources/.svelte-kit/output/server/app.config.js' imported from /home/user/Projects/Svelte/site/sources/.svelte-kit/output/server/app.js
Did you mean to import ../../../../app.config.js?
Did you mean to import ../../../../app.config.js?
at new NodeError (node:internal/errors:371:5)
at finalizeResolution (node:internal/modules/esm/resolve:416:11)
at moduleResolve (node:internal/modules/esm/resolve:932:10)
at defaultResolve (node:internal/modules/esm/resolve:1044:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
at ModuleWrap. (node:internal/modules/esm/module_job:76:40)
at link (node:internal/modules/esm/module_job:75:36)
 ELIFECYCLE  Command failed with exit code 1.

I spent much time to find solution and only possible is made path absolute during rollup build. For it helps option makeAbsoluteExternalsRelative: 'ifRelativeSource' (by default it's true in rollup and looks like error with it — rollup writes incorrect relative path)
So, with this option my config file imports from absolute path and works as expected. But I need solution to move my app in another place without hard coded imports.

Reproduction

Use code sample above

Logs

No response

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (8) x64 DO-Premium-Intel
    Memory: 4.02 GB / 15.63 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.13.2 - /usr/bin/node
    npm: 8.1.2 - /usr/bin/npm
  npmPackages:
    @sveltejs/adapter-node: ^1.0.0-next.61 => 1.0.0-next.61 
    @sveltejs/adapter-static: ^1.0.0-next.26 => 1.0.0-next.26 
    @sveltejs/kit: next => 1.0.0-next.225 
    svelte: ^3.46.0 => 3.46.0

Severity

serious, but I can work around it

Additional Information

No response

@benmccann benmccann added the p3-edge-case SvelteKit cannot be used in an uncommon way label Jan 12, 2022
@benmccann
Copy link
Member

there's no longer a second bundling step with esbuild in adapter-node, so please note the removal of the esbuild option: https://github.com/sveltejs/kit/tree/master/packages/adapter-node

@livehtml
Copy link
Author

@benmccann oh, missed it, thanks. But behaviour (error) the same.

@Rich-Harris
Copy link
Member

You should be able to do path.resolve('app.config.js') and have it correctly externalised (while still using a relative import). Will need a repro to investigate further though, I'm afraid.

@Rich-Harris Rich-Harris added this to the whenever milestone May 16, 2022
@dummdidumm
Copy link
Member

Closing due to inactivity and lack of reproduction.

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2023
@terwer
Copy link

terwer commented Nov 23, 2023

Still not work, add

  build: {
    emptyOutDir: false,
    rollupOptions: {
      external: ["/plugins/siyuan-plugin-publisher-pro/app/libs/publisher-service/publisher_service.js"],
    },
  },

but still report Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/plugins/siyuan-plugin-publisher-pro/app/libs/publisher-service/publisher_service.js' imported from /Users/terwer/Documents/mydocs/siyuan-plugins/siyuan-plugin-publisher-pro-src/apps/publisher-app/.svelte-kit/output/server/entries/pages/_layout.ts.js

@terwer
Copy link

terwer commented Nov 23, 2023

Fixed, I disable ssr import by the following code

const initWasm = async () => {
  if (!isBrowser) {
    return
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-edge-case SvelteKit cannot be used in an uncommon way
Projects
None yet
Development

No branches or pull requests

5 participants