gpt4 book ai didi

reactjs - scss 上的 react 模块解析失败

转载 作者:行者123 更新时间:2023-12-05 06:37:15 24 4
gpt4 key购买 nike

我正在尝试实现它以查看它是如何工作的,但我无法构建它

https://ashiknesin.com/blog/build-custom-sidebar-component-react/

在我的 webpack.config.js 中,我有以下内容

var HTMLWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});

const ExtractTextPlugin = require("extract-text-webpack-plugin");

/*const extractSass = new ExtractTextPlugin({
filename: "[name].[contenthash].css",
disable: process.env.NODE_ENV === "development"
});
*/
module.exports = {
entry : __dirname + '/app/index.js',
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
],
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
}
]
},
output: {
filename: 'transformed.js',
path: __dirname + '/dist'
},
plugins: [
HTMLWebpackPluginConfig,
new ExtractTextPlugin("styles.css")
]
}

当我尝试构建它时出错

ERROR in ./app/index.js
Module parse failed: Unexpected token (20:12)
You may need an appropriate loader to handle this file type.
| render() {
| return (
| <div>
| <SideBar />
| <Header />
Child html-webpack-plugin for "index.html":
1 asset
[0] ./node_modules/html-webpack-plugin/lib/loader.js!./app/index.html 6.41 kB {0} [built]
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
[3] (webpack)/buildin/module.js 517 bytes {0} [built]
+ 1 hidden module
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! ccp-react@1.0.0 build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the ccp-react@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /opt/SP/wwwadm/home/.npm/_logs/2017-12-29T11_17_11_139Z-debug.log

我试过查看以下帖子,但它们似乎不起作用。 https://github.com/webpack-contrib/extract-text-webpack-plugin

https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/580

这是我的 webpack 文件

var HTMLWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});

module.exports = {
entry : __dirname + '/app/index.js',
module: {
rules: [{
test: /\.scss$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader"
}, {
loader: "sass-loader",
options: {
includePaths: ["absolute/path/a", "absolute/path/b"]
}
}]
}],
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
output: {
filename: 'transformed.js',
path: __dirname + '/dist'
},
plugins: [
HTMLWebpackPluginConfig
]
}

这是我的包文件

{
"name": "ccp-react",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server",
"build": "webpack"
},
"private": true,
"dependencies": {
"babel-plugin-lodash": "^3.3.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-1": "^6.24.1",
"bootstrap": "^3.3.7",
"classnames": "^2.2.5",
"core-js": "^2.5.3",
"create-react-class": "^15.6.2",
"cross-env": "^5.1.3",
"gulp": "^3.9.1",
"hammerjs": "^2.0.8",
"lodash": "^4.17.4",
"ng": "0.0.0-rc6",
"ng-cli": "^0.7.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-sidenav": "^2.1.2",
"rxjs": "^5.5.6",
"semantic": "0.0.1",
"semantic-ui": "^2.2.13",
"semantic-ui-react": "^0.77.1",
"systemjs": "^0.20.19",
"web-animations-js": "^2.3.1",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "^6.0.95",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.2",
"html-webpack-plugin": "^2.30.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.3.3",
"karma-jasmine": "^1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"node-sass": "^4.7.2",
"protractor": "~5.1.2",
"sass-loader": "^6.0.6",
"style-loader": "^0.19.1",
"ts-node": "~3.2.0",
"tslint": "~5.7.0",
"typescript": "~2.4.2",
"webpack": "^3.10.0",
"webpack-bundle-analyzer": "^2.8.2",
"webpack-dev-server": "^2.9.7"
}
}

这是我的 index.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import Sidebar from './components/sidebar';
import Header from './components/header';
import HomeContent from './components/home';

class App extends Component {
constructor(props){
super(props);

this.state = { nav: '/' };

}

render() {
return (
<div>
<Header />
<Sidebar />
<HomeContent />
</div>
);
}
};

ReactDOM.render(<App/>, document.getElementById("app"));

我的文件夹结构是

app/index.js
app/index.html
app/components/sidebar.js
app/components/home.js
app/components/header.js
dist/
docs/
package.json
webpack.config.js
.babelrc
node_modules

sidebar.js 包含 https://ashiknesin.com/blog/build-custom-sidebar-component-react/ ,如果我注释掉 import css 并从 webpack 中删除 css 加载器,它构建得很好。

最佳答案

如果你的项目中使用了Sass,你需要告诉webpack如何处理scss文件,所以你需要安装sass-loader 并将其包含在您的 webpack 配置中。最起码是这样的:

module: {
rules: [{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [{
loader: "css-loader"
}, {
loader: "sass-loader"
}],
fallback: "style-loader"
})
}]
},

sass-loader 的安装指南和其他 webpack 配置示例在 sass-loader github homepage 上提供。 .为了安装 sass-loader,运行 npm install sass-loader node-sass --save-dev,更多信息 here .

这是你的 webpack 配置的编辑版本(删除了 loaders 因为它们在 webpack 3 中不受支持,并替换为用于处理 js 文件的规则 babel-loader):

var HTMLWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});

module.exports = {
entry : __dirname + '/app/index.js',
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader"
}, {
loader: "sass-loader",
options: {
includePaths: ["absolute/path/a", "absolute/path/b"]
}
}]
}]
},
output: {
filename: 'transformed.js',
path: __dirname + '/dist'
},
plugins: [
HTMLWebpackPluginConfig
]
}

关于reactjs - scss 上的 react 模块解析失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021818/

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