7

我正在从 Webpack 迁移到 Parcel,并在我的组件中使用这样的文件夹别名:

import * as authActions from 'actions/authActions';

我收到此错误:Cannot find module 'actions/authActions'

奇怪的是,它只在使用时显示build,它在开发模式下工作,而不是在生产模式下。

我已经在 package.json 中设置了我的别名,就像文档说的那样:

{
  "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 根,因为它是相对于入口文件的。

我怎样才能让它正确解析别名?

4

1 回答 1

1

这个问题是在 parcel1 是当前版本时被问到的,所以对于将来到达这里的任何人,值得指出 Parcel 2 通过glob 别名支持这个

package.json将包括这样的条目:

{
  "alias": {
    "actions/*": "./client/actions/$1"
  }
}

然后,要导入 的所有导出./client/actions/authActions,您可以编写:

import * as authActions from "actions/authActions";
于 2021-10-20T17:14:25.067 回答