我一直在尝试使用nrwl工具来创建工作区。我用
创建了一个npm init nx-workspace
然后将使用create-react-app --typescript
创建的应用移到该工作区。
declare module '*.svg' {
import * as React from 'react';
export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
const src: string;
export default src;
}
然后我得到了(我认为这是webpack的抱怨)
ERROR in ./orig/logo.svg 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3">
create-react-app处理所有Webpack转换时,我希望有相同的经验。
答案 0 :(得分:0)
我想我找到了解决方法:
{
...,
"projects": {
...,
"projectName": {
...,
"architect": {
...,
"build": {
"options": {
"webpackConfig": "apps/pathToProject/webpack.config.js",
...,
...
module.exports = (config, context) => {
const { module } = config;
module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack', 'url-loader']
});
return { ...config, module };
};
(添加了模拟 /fileMock.js)
module.exports = 'test-file-stub';
module.exports = {
...,
moduleNameMapper: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/src/app/__mocks__/fileMock.js",
},
...
};