我正在尝试实现它以查看它是如何工作的,但我无法让它构建
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/,如果我注释掉导入css并从webpack中删除css加载器,则构建正常。
答案 0 :(得分:0)
如果您在项目中使用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 github homepage上提供了安装指南以及webpack
配置sass-loader
的其他示例。
要安装sass-loader
,请运行npm install sass-loader node-sass --save-dev
,更多信息here。
以下是您的webpack配置的已编辑版本(已移除loaders
,因为它们在webpack 3中不受支持,并替换为使用js
处理babel-loader
文件的规则:< / p>
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
]
}