使用紗線工作區在monorepo中跨項目共享配置變量? (Sharing config variables across projects in a monorepo using yarn workspaces?)


問題描述

使用紗線工作區在monorepo中跨項目共享配置變量? (Sharing config variables across projects in a monorepo using yarn workspaces?)

是否可以在使用 yarn 工作區設置的 monorepo 的子文件夾之間共享配置變量/環境變量?我們有一個項目的 monorepo,其中幾個子文件夾是使用 create‑react‑app 構建的項目。在這些單獨的文件夾中,我們可以使用 .env 文件來指定配置值,當我們在單獨級別使用 package.jsons 中的構建/啟動腳本時,它們會很好地使用。

但是,我們還有其他子文件夾,它們只是導入到 CRA 應用程序中的庫。我們想在這些庫中指定 config/env 變量,但到目前為止還沒有找到一種方法來在我們構建或啟動導入庫的項目時傳播這些值。嘗試過庫本身以及 CRA 應用程序根文件夾中的 .env 文件,


參考解法

方法 1:

Consider the implications of reading from .env as this may adverse affect third‑party libraries and dependencies into process.env.

You can use libraries like https://github.com/motdotla/dotenv to do that:

  1. Setup a .env.file file in your lib:

  2. </ol>

    ‑ src
      ‑ index.js
    ‑ .env.file
    
    
    1. in the lib index.js file:
    import dotenv from 'dotenv'
    import path from 'path'
    
    dotenv.config({
      path: path.join(__dirname,'..','.env.file'),
    })
    
    // the rest of the file...
    
    

    方法 2:

    You can use find‑yarn‑workspace‑root to find the root directory of your repository.

    import workspacesRoot from "find‑yarn‑workspace‑root";
    import { config as dotenv } from "dotenv";
    
    const rootDirectory = workspacesRoot();
    dotenv({ path: `${rootDirectory}/.env` });
    

    (by hmleeStav Alfibashaus)

    參考文件

    1. Sharing config variables across projects in a monorepo using yarn workspaces? (CC BY‑SA 2.5/3.0/4.0)

#yarn-workspaces #monorepo #yarnpkg






相關問題

如何使用帶有打字稿和輸出文件夾的紗線工作區? (How to use yarn workspaces with typescript and out folders?)

如何在 Monorepo 中運行多個開發包 (How to run multiple packages in development inside a Monorepo)

帶有許多 npm 腳本的 Monorepo (Monorepo with Many npm-scripts)

使用紗線工作區導出枚舉時出現意外的令牌錯誤 (Unexpected token error exporting enums using yarn workspaces)

如何在 Expo 應用示例嵌套文件夾中熱重載開發包? (How to hot reload a development package in an Expo app example nested folder?)

將 monorepo 包放在包文件夾下是一種約定,還是 yarn 工作區期望這樣? (Is putting monorepo packages under a package folder a convention, or do yarn workspace expect that?)

通過創建具有循環依賴關係的 monorepo 導致錯誤 (Causing an error by creating a monorepo with circular dependencies)

Monorepo – Yarn 工作區 Typescript Node.JS 項目 – 運行 nodemon 時找不到模塊 (Monorepo – Yarn workspaces Typescript Node.JS project – cannot find module when running nodemon)

紗線工作區為每個運行 shell 命令 (Yarn workspaces run shell command for each)

紗線工作區共享包未在 create-react-app 中加載 (yarn workspaces shared package does not load in create-react-app)

如何使用 Yarn 工作區將共享依賴項添加到 monorepo? (How do I add shared dependencies to a monorepo using Yarn workspaces?)

使用紗線工作區在monorepo中跨項目共享配置變量? (Sharing config variables across projects in a monorepo using yarn workspaces?)







留言討論