gpt4 book ai didi

reactjs - 目前没有配置加载器来处理这个文件

转载 作者:行者123 更新时间:2023-12-05 02:52:14 25 4
gpt4 key购买 nike

我有一个 TypeScript(TS)、React 项目,我用它来保存我所有的 React 组件。我正在从这个项目中创建一个 NPM 包,然后我在单独的 React 项目中使用它。为了解决之前的 .css 和图像文件不属于我的包的问题,​​我现在尝试实现 Webpack,但我的知识确实存在漏洞。

尝试运行 webpack 时出现多个类似的错误:
您可能需要一个合适的加载器来处理这种文件类型,目前没有配置加载器来处理这种文件。

我已经安装了所有我认为必要的加载器:

  • 样式加载器
  • ts-loader
  • 文件加载器

webpack.config.js

const path = require('path');

module.exports = {
entry: './src/index.ts',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|svg|jpg)$/,
use: [
'file-loader',
],
},
]
}
};

tsconfig.json

{
"compilerOptions": {
"outDir": "dist",
"target": "esnext",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"jsx": "react",
"noImplicitAny": true,
"sourceMap": true,
},
"include": [
"src"
]
}

包.json

{
"name": "@private_feed/generic.project",
"version": "0.1.7",
"files": [
"dist"
],
"main": "dist/index.js",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"@types/jest": "^24.9.1",
"@types/node": "^12.12.47",
"@types/react": "^16.9.41",
"@types/react-dom": "^16.9.8",
"@types/styled-components": "^5.1.0",
"cross-env": "^7.0.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"styled-components": "^5.1.1"
},
"scripts": {
"start": "react-scripts start",
"test": "react-scripts test",
"eject": "react-scripts eject",
"storybook": "start-storybook -p 9009 -s public",
"build-storybook": "build-storybook -s public",
"build": "webpack"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@storybook/addon-actions": "^5.3.19",
"@storybook/addon-knobs": "^5.3.19",
"@storybook/addon-links": "^5.3.19",
"@storybook/addons": "^5.3.19",
"@storybook/preset-create-react-app": "^3.1.2",
"@storybook/react": "^5.3.19",
"css-loader": "^3.6.0",
"file-loader": "^6.0.0",
"style-loader": "^1.2.1",
"ts-loader": "^7.0.5",
"typescript": "^3.9.6",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12"
},
"babel": {
"presets": [
"react-app"
]
},
"description": "This project was bootstrapped with [Create React App].",
"author": "",
"license": "ISC"
}

最佳答案

我通过反复试验发现了问题。 webpack.config.js 对两个单独的模块部分不满意。

以下是导致成功捆绑的配置。

webpack.config.js

const path = require('path');

module.exports = {
entry: './src/index.ts',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|svg|jpg)$/,
use: [
'file-loader',
],
},
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
]
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};

关于reactjs - 目前没有配置加载器来处理这个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62711364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com