簡體   English   中英

Babel - 如何在根目錄之外設置別名?

[英]Babel - how to set aliases to outside of the root directory?

我有一個單體倉庫:

domain/
  entities/
    account.ts
  ...
mobile/
    src/
      app.ts
  node_modules/
  package.json
  babel.config.js

我想設置一個別名,以便app.ts可以簡單地調用:

import { Account } from 'domain/entities/account'

代替

import { Account } from '../../../domain/entities/account'

我試着這樣做:

const path = require('path')

module.exports = (api) => {
  api.cache(true)

  return {
    presets: ['module:metro-react-native-babel-preset'],
    plugins: [
      ['module:react-native-dotenv', {
        moduleName: 'react-native-dotenv'
      }],
      [
        'module-resolver',
        {
          extensions: [
            '.js',
            '.jsx',
            '.ts',
            '.tsx'
          ],
          alias: {
            domain: path.resolve(__dirname, '../domain')
          }
        }
      ]
    ]
  }
}

但不幸的是,它不起作用。 它拋出:

Error: Unable to resolve module /home/kibe/work/iros-customer-frontend/domain/entities/account from ...

None of these files exist:
  * ../domain/entities/account(.js|.jsx|.ts|.tsx)

如何從主目錄外部導入內容?

Go 到“域”文件夾並添加一個新文件 package.json,內容如下

{
  "name": "domain"
}

現在你像這樣導入

import { Account } from 'domain/entities/account'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM