【问题标题】:How to resolve alias in Parceljs?如何解决 Parceljs 中的别名?
【发布时间】:2019-05-31 20:21:56
【问题描述】:

我正在从 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 根,因为它是相对于入口文件的。

如何才能正确解析别名?

【问题讨论】:

    标签: javascript parceljs


    【解决方案1】:

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

    您的package.json 将包含这样的条目:

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

    然后,要导入./client/actions/authActions 的所有导出,你可以这样写:

    import * as authActions from "actions/authActions";
    

    【讨论】:

      猜你喜欢
      • 2019-06-21
      • 2021-06-01
      • 2018-09-03
      • 1970-01-01
      • 2018-11-19
      • 2020-11-10
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多