midwayjs / hooks

"Zero" Api / Type Safe / Fullstack Kit / Powerful Backend
MIT License
689 stars 62 forks source link

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

Closed SOVLOOKUP closed 3 years ago

SOVLOOKUP commented 3 years ago

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

运行记录

❯ cnpm run dev

> ~TODO~@0.0.1 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/_@midwayjs_core@2.10.14@@midwayjs/core/dist/index.js?v=6e81c65f.
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_@midwayjs_core@2.10.14@@midwayjs/core/dist/index.js?v=6e81c65f:
ReferenceError: exports is not defined
    at /node_modules/_@midwayjs_core@2.10.14@@midwayjs/core/dist/index.js?v=6e81c65f:12:23
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
exports is not defined
ReferenceError: exports is not defined
    at /node_modules/_@midwayjs_core@2.10.14@@midwayjs/core/dist/index.js?v=6e81c65f:10:23
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@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/_is-path-inside@3.0.3@is-path-inside/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_is-path-inside@3.0.3@is-path-inside/index.js?v=6e81c65f:2:14
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_chalk@4.1.1@chalk/source/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_chalk@4.1.1@chalk/source/index.js?v=6e81c65f:2:20
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_jiti@1.9.2@jiti/lib/index.js?v=6e81c65f:
ReferenceError: module is not defined
    at /node_modules/_jiti@1.9.2@jiti/lib/index.js?v=6e81c65f:12:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_globby@11.0.3@globby/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_globby@11.0.3@globby/index.js?v=6e81c65f:2:12
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_consola@2.15.3@consola/dist/consola.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_consola@2.15.3@consola/dist/consola.js?v=6e81c65f:1:90
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_pkg-dir@5.0.0@pkg-dir/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_pkg-dir@5.0.0@pkg-dir/index.js?v=6e81c65f:2:14
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_koa-static-cache@5.1.4@koa-static-cache/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_koa-static-cache@5.1.4@koa-static-cache/index.js?v=6e81c65f:1:14
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_art-template@4.13.2@art-template/index.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_art-template@4.13.2@art-template/index.js?v=6e81c65f:1:18
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Failed to load source map for /node_modules/_@midwayjs_decorator@2.10.14@@midwayjs/decorator/dist/index.js?v=6e81c65f.
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_@midwayjs_decorator@2.10.14@@midwayjs/decorator/dist/index.js?v=6e81c65f:
ReferenceError: exports is not defined
    at /node_modules/_@midwayjs_decorator@2.10.14@@midwayjs/decorator/dist/index.js?v=6e81c65f:12:23
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_axios@0.21.1@axios/index.js?v=6e81c65f:
ReferenceError: module is not defined
    at /node_modules/_axios@0.21.1@axios/index.js?v=6e81c65f:1:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:56 [vite] Error when evaluating SSR module /node_modules/_upath@2.0.1@upath/build/code/upath.js?v=6e81c65f:
ReferenceError: require is not defined
    at /node_modules/_upath@2.0.1@upath/build/code/upath.js?v=6e81c65f:23:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:57 [vite] Error when evaluating SSR module /node_modules/_axios@0.21.1@axios/index.js:
ReferenceError: module is not defined
    at /node_modules/_axios@0.21.1@axios/index.js:1:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:57 [vite] Error when evaluating SSR module /node_modules/_lodash.clonedeep@4.5.0@lodash.clonedeep/index.js:
ReferenceError: module is not defined
    at /node_modules/_lodash.clonedeep@4.5.0@lodash.clonedeep/index.js:1748:1
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
下午4:18:57 [vite] Error when evaluating SSR module /node_modules/_debug@4.3.1@debug/src/index.js:
ReferenceError: module is not defined
    at /node_modules/_debug@4.3.1@debug/src/index.js:9:2
    at instantiateModule (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_vite@2.3.3@vite/dist/node/chunks/dep-e9a16784.js:68197:166)
 > node_modules/_@midwayjs_hooks-core@2.1.0@@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/_@midwayjs_hooks-core@2.1.0@@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/_@midwayjs_hooks-core@2.1.0@@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/_@midwayjs_hooks-core@2.1.0@@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/_@midwayjs_hooks@2.1.0@@midwayjs/hooks/dist/midwayjs-hooks.esm.js:2:9: error: No matching export in "node_modules/_@midwayjs_hooks-core@2.1.0@@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/_@midwayjs_hooks-core@2.1.0@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:8:15: error: No matching export in "browser-external:path" for import "join"
node_modules/_@midwayjs_hooks-core@2.1.0@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:8:31: error: No matching export in "browser-external:path" for import "resolve"
node_modules/_@midwayjs_hooks-core@2.1.0@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:16:9: error: No matching export in "browser-external:fs" for import "existsSync"
node_modules/_@midwayjs_hooks-core@2.1.0@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js:16:21: error: No matching export in "browser-external:fs" for import "readFileSync"
node_modules/_@midwayjs_hooks@2.1.0@@midwayjs/hooks/dist/midwayjs-hooks.esm.js:2:9: error: No matching export in "node_modules/_@midwayjs_hooks-core@2.1.0@@midwayjs/hooks-core/dist/midwayjs-hooks-core.esm.js" for import "ApiConfig"
    at failureErrorWithLog (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@esbuild/lib/main.js:1443:15)
    at /home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@esbuild/lib/main.js:1125:28
    at runOnEndCallbacks (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@esbuild/lib/main.js:915:63)
    at buildResponseToResult (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@esbuild/lib/main.js:1123:7)
    at /home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@esbuild/lib/main.js:1230:14
    at /home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@esbuild/lib/main.js:606:9
    at handleIncomingPacket (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@esbuild/lib/main.js:703:9)
    at Socket.readFromStdout (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_esbuild@0.11.23@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 commented 3 years ago

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

TODO:增加 svelte kit 的支持

SOVLOOKUP commented 3 years ago

我尝试在kit中引入hooks的vite插件时碰到了这个问题 https://github.com/sveltejs/kit/issues/1551

Lxxyx commented 3 years ago

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

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

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

hooks.default()
SOVLOOKUP commented 3 years ago

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

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

❯ cnpm run dev

> ~TODO~@0.0.1 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/_@sveltejs_vite-plugin-svelte@1.0.0-next.10@@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/_jiti@1.9.2@jiti/dist/v8cache.js:2:2472)
    at loadSvelteConfig (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_@sveltejs_vite-plugin-svelte@1.0.0-next.10@@sveltejs/vite-plugin-svelte/dist/index.js:383:14)
    at async resolveOptions (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_@sveltejs_vite-plugin-svelte@1.0.0-next.10@@sveltejs/vite-plugin-svelte/dist/index.js:585:24)
    at async Object.configResolved (/home/sovlookup/桌面/my-app/test/my-app/node_modules/_@sveltejs_vite-plugin-svelte@1.0.0-next.10@@sveltejs/vite-plugin-svelte/dist/index.js:755:17)
    at async Promise.all (index 0) {
  code: 'ERR_REQUIRE_ESM'
}
SOVLOOKUP commented 3 years ago

原因是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 commented 3 years ago

原因是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 commented 3 years ago

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

SOVLOOKUP commented 3 years ago

该问题已经解决