【发布时间】:2021-05-17 19:44:09
【问题描述】:
这是我想要实现的目标。
我想制作一个包,用于共享 typescript 界面,或在前端 (react) 和后端 (nestjs) 之间共享的通用配置
我创建了一个名为“shared”的项目,并在我的 package.json 中创建了一个链接。
像这样:"shared": "file:../shared",
我的 React 非常好用,可以在哪里使用我的界面或“共享”中的任何内容而不会出现任何错误!
我在我的nestjs项目中做了同样的事情,编辑器中没有错误,我可以在node_modules中看到共享包。但是当我编译项目时,它失败了:
错误:找不到模块“共享/接口/用户”
所以我猜问题出在我的 nestjs conf 或 webpack 中……但我不知道是什么。
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"moduleResolution": "node",
},
}
webpack-hmr.config.js
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
module.exports = function (options) {
return {
...options,
entry: ['webpack/hot/poll?500', options.entry],
watch: true,
externals: [
nodeExternals({
allowlist: ['webpack/hot/poll?500'],
}),
],
plugins: [
...options.plugins,
new webpack.HotModuleReplacementPlugin(),
new RunScriptWebpackPlugin({ name: options.output.filename }),
new webpack.WatchIgnorePlugin( {paths: [/\.js$/, /\.d\.ts$/] }),
],
};
};
如果您有任何想法 :) 谢谢大家!
【问题讨论】:
-
我认为使用 lerna & yarn 命名空间 & typescripts 路径别名,你可以实现它
-
在运行 webpack 时,是否可以尝试添加
--display-error-details参数并发布结果?这在过去对我有所帮助,您会惊讶地发现,有时根本原因实际上是一个完全不相关的问题。
标签: typescript nestjs importerror