我正在从 Webpack 迁移到 Parcel,并在我的组件中使用这样的文件夹别名:
import * as authActions from 'actions/authActions';
我收到此错误:
Cannot find module 'actions/authActions'
奇怪的是,它只在使用
build
时显示,它在开发模式下工作,但在生产模式下不工作。我在 package.json 中设置了我的别名,例如 the docs说:
{
"scripts: {
"build-client": "parcel build client/index.html dist/client",
"build-server": "parcel build server/index.js --target node -d dist/server",
"build-test": "yarn build-server && node ./dist/server/index.js"
},
"alias": {
"actions": "./client/actions"
}
}
这是一个服务器端渲染的应用程序,我在不同的地方导入组件,我不能使用默认的 Parcel 根,因为它是相对于入口文件的。
我怎样才能让它正确解析别名?
最佳答案
这个问题是在 parcel1 是当前版本时被问到的,所以对于将来到达这里的任何人,值得指出 Parcel 2 通过 glob aliases 支持这个问题。
您的 package.json
将包括这样的条目:
{
"alias": {
"actions/*": "./client/actions/$1"
}
}
然后,导入 ./client/actions/authActions
的所有导出,你可以写:import * as authActions from "actions/authActions";
关于javascript - 如何解决 Parceljs 中的别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54052884/