我是ReactJS的新手。我正在尝试使用scss反应使用sass-loaders
我的webpack配置看起来像这样 -
var webpack = require('webpack');
var path = require('path');
var APP_DIR = path.resolve(__dirname + 'react-js/src/');
var BUILD_DIR = path.resolve(__dirname, 'react-js/dist/');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: './react-js/src/index.jsx',
output: {
path: BUILD_DIR,
publicPath: '/dist',
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader',
publicPath: '/dist'
})
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader!sass-loader',
publicPath: '/dist'
})
},
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['react', 'es2015']
}
}
]
},
plugins: [
new ExtractTextPlugin('styles.css'),
]
}
所有这些配置均来自各自的文件。
当我运行webpack-dev-server时,它显示以下输出 -
这是我保存scss文件时的屏幕 - 注意 - 我已从我的webpack配置文件中删除了ExtractTextPlugin。
目录结构 -
我只是没有意识到它是如何导入渲染的反应应用程序的。我认为在index.jsx中导入scss文件如下所示将使css文件成为依赖项,但它不起作用。
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
import style from '../../scss/main.scss';
ReactDOM.render(
<App />,
document.getElementById('root')
);
为什么?
被修改
的package.json
{
"name": "skippo-vendor-admin-webui",
"version": "1.0.0",
"description": "",
"main": "./react-js/src/index.jsx",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "./node_modules/.bin/webpack -d --progress --colors",
"prod": "./node_modules/.bin/webpack -p --progress --colors",
"watch": "./node_modules/.bin/webpack -d --progress --colors --watch",
"start": "./node_modules/.bin/webpack-dev-server --progress --colors --hot --inline --contentBase './react-js/'"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"chai": "^3.5.0",
"css-loader": "^0.28.1",
"extract-text-webpack-plugin": "^2.1.0",
"karma": "^1.6.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.1.1",
"karma-mocha": "^1.3.0",
"karma-sinon": "^1.0.5",
"karma-webpack": "^2.0.3",
"mocha": "^3.3.0",
"node-sass": "^4.5.2",
"sass-loader": "^6.0.3",
"sinon": "^2.2.0",
"style-loader": "^0.17.0"
},
"dependencies": {
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-redux": "^5.0.4",
"react-router": "^4.1.1"
}
}
答案 0 :(得分:1)
ExtractTextPlugin
只应用于您的生产版本,因为它将创建一个单独的编译css文件,无需任何特殊处理即可提供。
对于开发,您可以改用此规则:
{
test: /\.s?css$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
}
如果没有它们,请将3个加载器安装为dev依赖项。
导入scss文件时也不需要指定名称,只需导入它即可。
import '../../scss/main.scss';
答案 1 :(得分:0)
更改此行
import style from '../../scss/main.scss';
使用
import '../../scss/main.scss';