简体   繁体   English

Nx 共享资产库

[英]Nx shared asset library

I want my React app in my Nx monorepo to retrieve all the assets in my asset library called common-assets .我希望我的 Nx monorepo 中的 React 应用程序检索我的资产库中名为common-assets所有资产。

I managed to do it with my NextJS app like so:我设法用我的 NextJS 应用程序做到了,就像这样:

project.json of my NextJS app我的 NextJS 应用程序的project.json

{
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/clients",
  "projectType": "application",
  "implicitDependencies": ["common-assets"],
  "targets": {
    "build": {
      "executor": "@nrwl/next:build",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "root": "apps/clients",
        "outputPath": "dist/apps/clients",
        "assets": [
          {
            "input": "libs/common/assets/src/lib",
            "glob": "**/*",
            "output": "assets"
          }
        ]
      },
...
}

I tried to do the exact same thing for my react App by modifying the asset key in the project.json我试图通过修改项目中的资产密钥来为我的 React App 做完全相同的事情project.json

project.json of my React app:我的 React 应用程序的project.json

  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/sellers/src",
  "projectType": "application",
  "implicitDependencies": ["common-assets"],
  "targets": {
    "build": {
      "executor": "@nrwl/web:webpack",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "compiler": "babel",
        "outputPath": "dist/apps/sellers",
        "index": "apps/sellers/src/index.html",
        "baseHref": "/",
        "main": "apps/sellers/src/main.tsx",
        "polyfills": "apps/sellers/src/polyfills.ts",
        "tsConfig": "apps/sellers/tsconfig.app.json",
        "assets": [
          "apps/sellers/src/favicon.ico",
          "apps/sellers/src/assets",
          {
            "input": "libs/common/assets/src/lib",
            "glob": "**/*",
            "output": "src/assets"
          }
        ],
        "styles": ["apps/sellers/src/styles.scss"],
        "scripts": [],
        "webpackConfig": "@nrwl/react/plugins/webpack"
      },
      "configurations": {
        "development": {
          "extractLicenses": false,
          "optimization": false,
          "sourceMap": true,
          "vendorChunk": true
        },
        "production": {
          "fileReplacements": [
            {
              "replace": "apps/sellers/src/environments/environment.ts",
              "with": "apps/sellers/src/environments/environment.prod.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "namedChunks": false,
          "extractLicenses": true,
          "vendorChunk": false
        }
      }
    },
    "serve": {
      "executor": "@nrwl/web:dev-server",
      "defaultConfiguration": "development",
      "options": {
        "buildTarget": "sellers:build",
        "hmr": true
      },
      "configurations": {
        "development": {
          "buildTarget": "sellers:build:development"
        },
        "production": {
          "buildTarget": "sellers:build:production",
          "hmr": false
        }
      }
    },
    "lint": {
      "executor": "@nrwl/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["apps/sellers/**/*.{ts,tsx,js,jsx}"]
      }
    },
    "test": {
      "executor": "@nrwl/jest:jest",
      "outputs": ["coverage/apps/sellers"],
      "options": {
        "jestConfig": "apps/sellers/jest.config.ts",
        "passWithNoTests": true
      }
    }
  },
  "tags": []
}

But it's not working...但它不起作用...

I kept assets into image folder and我将资产保存在图像文件夹中,并且

"assets": [
      "apps/boost/src/favicon.ico",
      "apps/boost/src/assets",
      {
        "input": "libs/common-assets/assets/src/lib/images",
        "glob": "**/*",
        "output": "assets"
      }
    ],

I'm able to use it into other project as well我也可以将它用于其他项目

 <img alt="logo" src="/assets/logo.png" />

Do this:做这个:

{
   "input": "./libs/common-assets/src/lib/images",
   "glob": "**/*",
   "output": "/assets/"
}

Then access it with:然后访问它:

<img alt="logo" src="assets/logo.png" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nx 工作区 - 与 tailwindcss 共享库并做出反应 - Nx workspace - Shared lib with tailwindcss and react NX React - 如何为库创建默认模拟 - NX React - how to create default mock for library Nx Monorepo - 如何在主 Nest 应用程序中使用 NestJs 库 - Nx Monorepo - How to Use NestJs Library inside the Main Nest App 从 nx 中的服务应用程序导入此库时,库中的内部 json 导入中断 - internal json import in a library breaks when importing this library from a served app in nx 共享组件库最佳实践 - Shared component library best practices NX monorepo:应用程序使用库的源文件而不是 dist 文件夹中的源文件 - NX monorepo: application uses library's source files instead of ones from dist folder 如何将 libs 中的 react 库中的 css 文件导入 nx 集成 monorepo 中的 next.js 应用程序? - How to import css files from react library inside libs to next.js app in nx integrated monorepo? React 组件库无法加载自己的 static 资产 - React component library can not load its own static asset Turborepo 中共享组件库的配置文件 - Config file for shared component library in a Turborepo React Native 0.26从资产库中显示图像时找不到合适的图像URL加载程序 - React Native 0.26 No suitable image URL loader found when displaying image from asset-library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM