Nx React – 无法解析资源

huangapple go评论74阅读模式
英文:

Nx React - Assets can't be resolved

问题

我在一个包含几个库和两个应用程序的 nx monorepo 上工作。

文件结构

apps
\--api
\--funnel(使用 webpack 的 React)
\--api-e2e
\--funnel-e2e
libs
\--funnel
\----pages
\--shared
\----assets
\-------src
\--------lib
\----------'some .tsx assets'(lottie 文件)
\--------assets
\----------images
\-------------**/*.(png|webp|gif|svg)(所有其他 assets)

期望的结果

在我的库和应用程序中,我希望像这样使用资产:
import imageName from '/assets/images/<some-image-folder>/<some-image-name>.<ext>';

对于所有 svg、png、jpeg 等...

对于 svg:
import { ReactComponent from imageName } from '/assets/images/<some-image-folder>/<some-image-name>.svg';

问题

我的实际问题是,当我构建 funnel 应用程序时,运行 nx run funnel:build --verbose 时,

我的资产似乎已加载到缓存中,但每个资产都返回错误:Module not found: Error: Can't resolve '/assets/images/<some-image-folder>/<some-image-name>.<ext>' from '<whatever-lib>/<main-app>'

是的,我使用 /assets/images,因为我正在使用 Angular 功能将资产“提供”到 /assets/images

我的配置是什么

#NX 报告

Node: 16.16.0
OS: darwin x64
npm: 9.2.0

nx: 15.6.0
@nrwl/angular: 未找到
@nrwl/cypress: 15.6.0
@nrwl/detox: 未找到
@nrwl/devkit: 15.6.0
@nrwl/esbuild: 未找到
@nrwl/eslint-plugin-nx: 15.6.0
@nrwl/expo: 未找到
@nrwl/express: 15.6.3
@nrwl/jest: 15.6.0
@nrwl/js: 15.6.0
@nrwl/linter: 15.6.0
@nrwl/nest: 未找到
@nrwl/next: 未找到
@nrwl/node: 15.6.3
@nrwl/nx-cloud: 15.0.3
@nrwl/nx-plugin: 未找到
@nrwl/react: 15.6.0
@nrwl/react-native: 未找到
@nrwl/rollup: 未找到
@nrwl/schematics: 未找到
@nrwl/storybook: 未找到
@nrwl/web: 未找到
@nrwl/webpack: 15.6.3
@nrwl/workspace: 15.6.0
@nrwl/vite: 未找到
typescript: 4.8.4

#app/funnel/project.json

我的资产通过主应用程序导入,因为我不在这里构建任何子库,以下是选择器:

{  
   "input": "libs/shared/assets/src/assets/images",
   "glob": "**/*",  
   "output": "assets/images"
}

#app/funnel/webpack.config.js

由于 StackOverflow 错误,无法导入整个代码,但简单来说,我添加了 svgr 支持、file-loader 和 url-loader

module: {
      rules: [
        {
          test: /\.(webp|png|gif|jpe?g)$/i
          loader: 'file-loader',
          options: {
            name: '[path][name].[ext]',
          },
        },
        {
          test: /\.svg$/,
          use: [
            '@svgr/webpack',
            'url-loader'
          ]
        }
      ],
}

我已经尝试过的解释

#1 次尝试
我使用 Angular 功能在构建时导入资产,如上面的配置中所示。

#2 次尝试
我使用 copy-webpack-plugin 复制静态资产到 '/assets/',但没有成功。

#3 次尝试
我在主 tsconfig.base.json 中添加了一个路径 "@myapp/assets/*"
["libs/shared/assets/src/assets/**/*"]
并尝试如下提供资产:
"@myapp/assets/<path-to-image>/<image-name>.<ext>"

#4 次尝试

可行但不适合我的工作流的解决方案是将每个资产放在其依赖项旁边...

这很恶心,因为我需要复制资产,这些资产会经常变动。

请帮助。

这是小型测试仓库:https://github.com/Sosumappu/assets-monorepo-test

英文:

I'm working on a nx monorepo that has a few library and 2 apps.

File Structures

apps<br/>
\--api<br/>
\--funnel ( react with webpack )<br/>
\--api-e2e<br/>
\--funnel-e2e<br/>
libs<br/>
\--funnel<br/>
\----pages<br/>
\--shared<br/>
\----assets<br/>
\-------src<br/>
\--------lib<br/>
\----------'some .tsx assets' ( lottie files )<br/>
\--------assets<br/>
\----------images<br/>
\-------------**/*.(png|webp|gif|svg) ( all others assets )<br/>

What's expected

In my libraries and in my app, I'd like to use the assets as such:<br/>
import imageName from &#39;/assets/images/&lt;some-image-folder&gt;/&lt;some-image-name&gt;.&lt;ext&gt;&#39;;

For all svg, png, jpeg, etc...

for svgs: <br/>import { ReactComponent from imageName } from &#39;/assets/images/&lt;some-image-folder&gt;/&lt;some-image-name&gt;.svg&#39;;

Issue

My actual issues is that when i'm building the funnel app nx run funnel:build --verbose

my assets seems to be loaded into the cache but every assets return a : Module not found: Error: Can&#39;t resolve &#39;/assets/images/&lt;some-image-folder&gt;/&lt;some-image-name&gt;.&lt;ext&gt;&#39; from &#39;&lt;whatever-lib&gt;/&lt;main-app&gt;&#39;

Yes i use /assets/images As i'm using angular functionaility to "serve" the assets to /assets/images

What is my config

#NX Report

Node : 16.16.0 OS : darwin x64 npm : 9.2.0

nx : 15.6.0 <br/>@nrwl/angular : Not Found <br/>@nrwl/cypress : 15.6.0<br/> @nrwl/detox : Not Found<br/> @nrwl/devkit : 15.6.0 <br/>@nrwl/esbuild : Not Found <br/>@nrwl/eslint-plugin-nx : 15.6.0 <br/>@nrwl/expo : Not Found<br/> @nrwl/express : 15.6.3<br/> @nrwl/jest : 15.6.0<br/> @nrwl/js : 15.6.0<br/> @nrwl/linter : 15.6.0 <br/>@nrwl/nest : Not Found<br/> @nrwl/next : Not Found<br/> @nrwl/node : 15.6.3<br/> @nrwl/nx-cloud : 15.0.3 <br/>@nrwl/nx-plugin : Not Found<br/> @nrwl/react : 15.6.0<br/> @nrwl/react-native : Not Found<br/> @nrwl/rollup : Not Found<br/> @nrwl/schematics : Not Found<br/> @nrwl/storybook : Not Found <br/>@nrwl/web : Not Found <br/>@nrwl/webpack : 15.6.3<br/> @nrwl/workspace : 15.6.0<br/> @nrwl/vite : Not Found <br/>typescript : 4.8.4<br/>

#app/funnel/project.json

My assets are imported through the main app as i don't build any of the sub-lib here's the selector :<br/>

{  
   &quot;input&quot;: &quot;libs/shared/assets/src/assets/images&quot;,
   &quot;glob&quot;: &quot;**/*&quot;,  
   &quot;output&quot;: &quot;assets/images&quot;
}

#app/funnel/webpack.config.js

Couldn't import the whole code due to StackOverflow error but, in simple terms i added svgr support, file-loader & url-loader <br/>

module: {
      rules: [
        {
          test: /\.(webp|png|gif|jpe?g)$/i
          loader: &#39;file-loader&#39;,
          options: {
            name: &#39;[path][name].[ext]&#39;,
          },
        },
        {
          test: /\.svg$/,
          use: [
            &#39;@svgr/webpack&#39;,
            &#39;url-loader&#39;
          ]
        }
      ],
}

Explanation of what I've tried so far

#1rst attempt
I used the angular functionality to import assets on build such as shown on the top config with the selector.

#2nd attempt
I used copy-webpack-plugin to copy static assets to '/assets/' without success

#3rd attempt
I added a path to the main tsconfig.base.json &quot;@myapp/assets/*&quot; : [&quot;libs/shared/assets/src/assets/**/*&quot;]

and tried to serve the assets as such : "@myapp/assets/<path-to-image>/<image-name>.<ext>"

#4rth attempt

The solution that works but isn't optimized for my workflow is to put each asset next to its dependencies...

This is disgusting as I need to duplicate assets, which are subject to a lot of changes.

Please help.

Here's the small-sized test repo : https://github.com/Sosumappu/assets-monorepo-test

答案1

得分: 1

关于你的项目,这不是资产问题,而是加载SVG图像的问题。

我成功加载了它。

首先,将你的nx项目从nx 15.6.0升级到nx 15.6.3:
npx nx migrate latest

你犯了一个错误,在你的项目中用applepay.svg替换apple-pay.svg

将你的SVG文件颜色更改为红色,我们看不到它(在白色背景上是白色的)。

按照以下方式编辑webpack.config.js:

const { composePlugins, withNx } = require('@nrwl/webpack');
const { withReact } = require('@nrwl/react');

// Nx plugins for webpack.
module.exports = composePlugins(
  withNx({
    nx: {
      svgr: true,
    },
  }),
  withReact({ svgr: true }),
  (config) => {
    return config;
  }
);

shared-ui.tsx:

import styled from 'styled-components';

//Static Import
import Logo from '@github-test/shared/assets';
export const ApplePayIcon = () => {
  return <Logo />;
};

libs/shared/assets/src/index.ts:

import Logo from '-!@svgr/webpack!./assets/images/applepay.svg';
export default Logo;

在这里,你可以使用console.log(Logo),你会看到它正在创建一个React组件。

英文:

Regarding your project it is not an asset pb, but an issue with loading svg images

I managed to load it .

At first upgrade your nx project from nx 15.6.0 to nx 15.6.3 :
npx nx migrate latest

You made a mistake, replace apple-pay.svg with applepay.svg in your project

change the color of your svg file to red, we can not see it (white on white background).

edit webpack.config.js as follow :

const { composePlugins, withNx } = require(&#39;@nrwl/webpack&#39;);
const { withReact } = require(&#39;@nrwl/react&#39;);

// Nx plugins for webpack.
module.exports = composePlugins(
  withNx({
    nx: {
      svgr: true,
    },
  }),
  withReact({ svgr: true }),
  (config) =&gt; {
 
    return config;
  }
);

shared-ui.tsx

import styled from &#39;styled-components&#39;;

//Static Import
import Logo from &#39;@github-test/shared/assets&#39;;
export const ApplePayIcon = () =&gt; {
  return &lt;Logo /&gt;;
};

libs/shared/assets/src/index.ts :

import Logo from &#39;-!@svgr/webpack!./assets/images/applepay.svg&#39;;
export default Logo;

here you can console.log(Logo), you will see it is creating a react component

huangapple
  • 本文由 发表于 2023年2月8日 10:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定