反应模块解析在SCSS上失败

shorif2000

我正在尝试实现它,以查看其工作原理,但我无法构建它

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! [email protected] build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] 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加载程序,则可以正常构建。

玛格丽特克鲁

如果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主页上提供了安装指南和其他webpack配置示例为了安装,运行更多信息,请点击此处sass-loadersass-loadernpm install sass-loader node-sass --save-dev

这是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
    ]
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章