Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

svelte kit 中使用时 vite 编译抛出错误 #97

Closed
SOVLOOKUP opened this issue May 25, 2021 · 8 comments
Closed

svelte kit 中使用时 vite 编译抛出错误 #97

SOVLOOKUP opened this issue May 25, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@SOVLOOKUP
Copy link

SOVLOOKUP commented May 25, 2021

svelte kit 中使用hooks时 vite 编译抛出错误

运行记录

❯ cnpm run dev

> [email protected] dev /home/sovlookup/桌面/my-app/test/my-app
> svelte-kit dev


  SvelteKit v1.0.0-next.109

  local:   http://localhost:3000
  network: not exposed

  Use --host to expose server to other devices on this network


下午4:18:56 [vite] Failed to load source map for /node_modules/_@[email protected]@@midwayjs/core/dist/index.js?v=6e81c65f.
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_@[email protected]@@midwayjs/core/dist/index.js?v=6e81c65f:
ReferenceError: exports is not defined
    at /node_modules/_@[email protected]@@midwayjs/core/dist/index.js?v=6e81c65f:12:23
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
exports is not defined
ReferenceError: exports is not defined
    at /node_modules/_@[email protected]@@midwayjs/core/dist/index.js?v=6e81c65f:10:23
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] new dependencies found: @midwayjs/hooks, updating...
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@is-path-inside/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@is-path-inside/index.js?v=6e81c65f:2:14
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@chalk/source/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@chalk/source/index.js?v=6e81c65f:2:20
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@jiti/lib/index.js?v=6e81c65f:
ReferenceError: module is not defined
    at /node_modules/[email protected]@jiti/lib/index.js?v=6e81c65f:12:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@globby/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@globby/index.js?v=6e81c65f:2:12
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@consola/dist/consola.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@consola/dist/consola.js?v=6e81c65f:1:90
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@pkg-dir/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@pkg-dir/index.js?v=6e81c65f:2:14
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@koa-static-cache/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@koa-static-cache/index.js?v=6e81c65f:1:14
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@art-template/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@art-template/index.js?v=6e81c65f:1:18
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Failed to load source map for /node_modules/_@[email protected]@@midwayjs/decorator/dist/index.js?v=6e81c65f.
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_@[email protected]@@midwayjs/decorator/dist/index.js?v=6e81c65f:
ReferenceError: exports is not defined
    at /node_modules/_@[email protected]@@midwayjs/decorator/dist/index.js?v=6e81c65f:12:23
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@axios/index.js?v=6e81c65f:
ReferenceError: module is not defined
    at /node_modules/[email protected]@axios/index.js?v=6e81c65f:1:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/[email protected]@upath/build/code/upath.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/[email protected]@upath/build/code/upath.js?v=6e81c65f:23:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:57 [vite] Error when evaluating SSR module /node_modules/[email protected]@axios/index.js:
ReferenceError: module is not defined
    at /node_modules/[email protected]@axios/index.js:1:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:57 [vite] Error when evaluating SSR module /node_modules/[email protected]@lodash.clonedeep/index.js:
ReferenceError: module is not defined
    at /node_modules/[email protected]@lodash.clonedeep/index.js:1748:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:57 [vite] Error when evaluating SSR module /node_modules/[email protected]@debug/src/index.js:
ReferenceError: module is not defined
    at /node_modules/[email protected]@debug/src/index.js:9:2
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
 > node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:8:15: error: No matching export in "browser-external:path" for import "join"
    8 │ import path, { join as join$1, resolve } from 'path';
      ╵                ~~~~

 > node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:8:31: error: No matching export in "browser-external:path" for import "resolve"
    8 │ import path, { join as join$1, resolve } from 'path';
      ╵                                ~~~~~~~

 > node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:16:9: error: No matching export in "browser-external:fs" for import "existsSync"
    16 │ import { existsSync, readFileSync } from 'fs';
       ╵          ~~~~~~~~~~

 > node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:16:21: error: No matching export in "browser-external:fs" for import "readFileSync"
    16 │ import { existsSync, readFileSync } from 'fs';
       ╵                      ~~~~~~~~~~~~

 > node_modules/_@[email protected]@@midwayjs/hooks/dist/midwayjs-hooks.esm.js:2:9: error: No matching export in "node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js" for import "ApiConfig"
    2 │ export { ApiConfig, defineConfig, hooks, superjson } from '@midwayjs/hook...
      ╵          ~~~~~~~~~

下午4:18:57 [vite] error while updating dependencies:
Error: Build failed with 5 errors:
node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:8:15: error: No matching export in "browser-external:path" for import "join"
node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:8:31: error: No matching export in "browser-external:path" for import "resolve"
node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:16:9: error: No matching export in "browser-external:fs" for import "existsSync"
node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:16:21: error: No matching export in "browser-external:fs" for import "readFileSync"
node_modules/_@[email protected]@@midwayjs/hooks/dist/midwayjs-hooks.esm.js:2:9: error: No matching export in "node_modules/_@[email protected]@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js" for import "ApiConfig"
    at failureErrorWithLog (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:1443:15)
    at /home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:1125:28
    at runOnEndCallbacks (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:915:63)
    at buildResponseToResult (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:1123:7)
    at /home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:1230:14
    at /home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:606:9
    at handleIncomingPacket (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:703:9)
    at Socket.readFromStdout (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@esbuild/lib/main.js:573:7)
    at Socket.emit (node:events:365:28)
    at Socket.emit (node:domain:470:12)

复现代码

https://github.com/SOVLOOKUP/midway-svelte
https://codesandbox.io/s/keen-curran-pich3

期望的解决方案

使hooks能与kit一同使用或者给出解决方向

@Lxxyx Lxxyx added the enhancement New feature or request label May 25, 2021
@Lxxyx
Copy link
Member

Lxxyx commented May 25, 2021

可以参考:https://github.com/midwayjs/hooks/blob/master/examples/svelte/README.md

TODO:增加 svelte kit 的支持

@SOVLOOKUP
Copy link
Author

SOVLOOKUP commented May 25, 2021

我尝试在kit中引入hooks的vite插件时碰到了这个问题 sveltejs/kit#1551

@Lxxyx
Copy link
Member

Lxxyx commented May 25, 2021

我尝试在kit中引入hooks的vite插件时碰到了这个问题 sveltejs/kit#1551

看起来是和导入的方式有关系。js 里应该认得是 module.exports,这个是 export default 的。你可以试试看

import hooks from '@midwayjs/vite-plugin-hooks';

hooks.default()

Lxxyx added a commit that referenced this issue May 25, 2021
@SOVLOOKUP
Copy link
Author

修改后还是编译失败,貌似是 cjs 和 esm 的兼容问题

vite-plugin-svelte 似乎会进行对其他插件进行引用和某种处理,可能需要一些兼容性上的修改

❯ cnpm run dev

> [email protected] dev /home/sovlookup/桌面/my-app/test/my-app
> svelte-kit dev

[vite-plugin-svelte] failed to require config /home/sovlookup/桌面/my-app/test/my-app/svelte.config.js
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/sovlookup/桌面/my-app/test/my-app/svelte.config.js
require() of ES modules is not supported.
require() of /home/sovlookup/桌面/my-app/test/my-app/svelte.config.js from /home/sovlookup/桌面/my-app/test/my-app/node_modules/_@[email protected]@@sveltejs/vite-plugin-svelte/dist/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename svelte.config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/sovlookup/桌面/my-app/test/my-app/package.json.

    at new NodeError (node:internal/errors:363:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at n (/home/sovlookup/桌面/my-app/test/my-app/node_modules/[email protected]@jiti/dist/v8cache.js:2:2472)
    at loadSvelteConfig (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_@[email protected]@@sveltejs/vite-plugin-svelte/dist/index.js:383:14)
    at async resolveOptions (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_@[email protected]@@sveltejs/vite-plugin-svelte/dist/index.js:585:24)
    at async Object.configResolved (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_@[email protected]@@sveltejs/vite-plugin-svelte/dist/index.js:755:17)
    at async Promise.all (index 0) {
  code: 'ERR_REQUIRE_ESM'
}

@SOVLOOKUP
Copy link
Author

SOVLOOKUP commented May 26, 2021

原因是vite-plugin-svelte加载配置文件时,动态import无法正常工作 https://github.com/sveltejs/vite-plugin-svelte/issues/47。
如下,把 svelte.config.js 改成 cjs 格式就可以了:

const preprocess = require('svelte-preprocess');
const hooks = require('@midwayjs/vite-plugin-hooks');

/** @type {import('@sveltejs/kit').Config} */
module.exports = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: preprocess(),

	kit: {
		// hydrate the <div id="svelte"> element in src/app.html
		target: '#svelte',
		vite: {
			plugins: [ hooks.default() ]
		}
	}
};

其中遇到一个错误是midway的cli读取 tsconfig.json 文件时报错,原因是json格式错误,可能是没有排除文件中注释直接解析导致的错误( tsconfig.json 中是允许多行注释的)。这个问题或许该开另一个issue?

@Lxxyx
Copy link
Member

Lxxyx commented May 26, 2021

原因是vite-plugin-svelte加载配置文件时,动态import无法正常工作 https://github.com/sveltejs/vite-plugin-svelte/issues/47。
如下,把 svelte.config.js 改成 cjs 格式就可以了:

const preprocess = require('svelte-preprocess');
const hooks = require('@midwayjs/vite-plugin-hooks');

/** @type {import('@sveltejs/kit').Config} */
module.exports = {
// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: preprocess(),

kit: {
// hydrate the

element in src/app.html
target: '#svelte',
vite: {
plugins: [ hooks.default() ]
}
}
};
其中遇到一个错误是midway的cli读取 tsconfig.json 文件时报错,原因是json格式错误,可能是没有排除文件中注释直接解析导致的错误( tsconfig.json 中是允许多行注释的)。这个问题或许该开另一个issue?

可以提交到 https://github.com/midwayjs/cli 中哈

@SOVLOOKUP
Copy link
Author

SOVLOOKUP commented Jun 2, 2021

我已经向 vite-plugin-svelte 提交了兼容性修改:sveltejs/vite-plugin-svelte#49
等待上游代码更新即可解决这个issue

@SOVLOOKUP
Copy link
Author

该问题已经解决

Lxxyx added a commit that referenced this issue Nov 9, 2021
Lxxyx added a commit that referenced this issue Nov 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants