Mic*_*elB 8 webpack monorepo nestjs turborepo
编辑:可以轻松重现问题的存储库:
\nhttps://github.com/sebastiangug/turborepo-nestjs
\n我有一个turborepo 项目,其中有几个共享各种包的nestjs 应用程序。
\n我已将其配置为通过 CLI 根据文档和最新版本使用 webpack。不幸的是,它似乎没有按预期编译应用程序,导致从 /packages/ 内的共享包中抛出的“意外令牌导出”。
\n我的包管理是pnpm
.
错误:
\ngraphql-server:build: SyntaxError: Unexpected token \'export\'\ngraphql-server:build: at Object.compileFunction (node:vm:352:18)\n
Run Code Online (Sandbox Code Playgroud)\n版本/依赖项:
\ngraphql-server:build: SyntaxError: Unexpected token \'export\'\ngraphql-server:build: at Object.compileFunction (node:vm:352:18)\n
Run Code Online (Sandbox Code Playgroud)\n生产版本会抛出相同的错误,并将所有内容捆绑到一个 server.js 文件中。对于 HMR,使用文档中的此配置:
\n "dependencies": {\n "@nestjs/common": "9.0.8",\n "@nestjs/core": "9.0.8",\n "@nestjs/platform-express": "9.0.8",\n "@nestjs/graphql": "10.0.21",\n "@nestjs/apollo": "10.0.19",\n "graphql": "16.5.0",\n "apollo-server-express": "3.6.7",\n "reflect-metadata": "0.1.13",\n "rimraf": "3.0.2",\n "rxjs": "7.5.5"\n },\n "devDependencies": {\n "@nestjs/cli": "8.2.8",\n "@nestjs/schematics": "8.0.11",\n "@nestjs/testing": "9.0.8",\n "@types/express": "4.17.13",\n "@types/jest": "28.1.2",\n "@types/node": "16.0.0",\n "@types/supertest": "2.0.12",\n "@typescript-eslint/eslint-plugin": "5.30.0",\n "@typescript-eslint/parser": "5.30.0",\n "eslint": "8.18.0",\n "eslint-config-prettier": "8.5.0",\n "eslint-config-custom-nest": "workspace:*",\n "eslint-plugin-prettier": "4.2.1",\n "eslint-plugin-import": "2.26.0",\n "eslint-plugin-unicorn": "43.0.1",\n "jest": "28.1.2",\n "prettier": "2.7.1",\n "source-map-support": "0.5.21",\n "supertest": "6.2.3",\n "ts-jest": "28.0.5",\n "ts-node": "10.8.1",\n "tsconfig-paths": "3.10.1",\n "typescript": "4.7.4",\n "webpack-node-externals": "3.0.0",\n "webpack": "5.74.0",\n "run-script-webpack-plugin": "0.1.1",\n "ts-loader": "9.3.1",\n "webpack-cli": "4.10.0",\n "@yeo/tsconfig": "workspace:*",\n "@yeo/nest-config": "workspace:*",\n "@yeo/tracer": "workspace:*",\n "@yeo/entities": "workspace:*"\n },\n
Run Code Online (Sandbox Code Playgroud)\n应用结构:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 apps\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nest-app1\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 nest-app2\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 packages\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 config\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 config.module.ts\n
Run Code Online (Sandbox Code Playgroud)\n
我可以想到两种方法来解决这个问题。
使用 NestJS monorepo 模式。请参阅此处的文档
或者
使用 Typescript 编译器(或类似于 tsup 的类似编译器)向每个包 Package.jsonbuild
添加脚本。dev
还要在 Package.json 中添加main
和types
字段,并指向编译后的 js 文件。
下面是一个简单的示例,展示了名为共享的包的 Package.json 的样子。请注意,这假设存在 tsconfig.json 文件(如果不存在,请创建一个文件或将配置选项传递给 tsc)
{
"name": "shared",
"main": "dist/index.js",
"types": "dist/index.js",
"scripts": {
"build": "tsc --build",
"dev": "tsc --watch"
},
"dependencies": {
"typescript": "^4.9.4"
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要与不使用 NestJs 的应用程序共享包和/或在 monorepo 之外共享包,则后一种方法可能更好。如果 NestJs 是唯一的框架并且单个 Package.json 可以满足您的要求,那么第一种方法可能更好。
归档时间: |
|
查看次数: |
1721 次 |
最近记录: |