我正在使用Webpack 2.5.1
,React 15.5.4
。Webpack sass-loader無法編譯導入到反應jsx文檔中的sass樣式表:意外字符'@'
我試圖在反應項目中使用Bootstrap
_utilities.scss
。並且 當我嘗試編譯文檔時,Webpack報告錯誤,因爲它使用@import
語句導入其他sass部分。
這是我的條目main.jsx
文件的代碼。
import React from 'react';
import ReactDOM from 'react-dom';
import 'bootstrap/scss/_utilities.scss' // this is what causes the error
import '../stylesheets/components/gallery.scss'; // this one compiles properly if I comment the _utilities.scss import statement. It does not use @import notation.
class Gallery extends React.Component {
render(){
return (<div id="gallery" className="d-flex p-2">
<h1>using bootstrap flex-box</h1>
<p>Something something something!</p>
<button onClick={() => console.info('You clicked a button')}>Click Me!</button>
</div>)
}
}
這是我的webpack.config.js
的代碼。
const path = require('path');
const webpack = require('webpack');
const CommonChunkPlugin =
require('./node_modules/webpack/lib/optimize/CommonsChunkPlugin');
module.exports = {
target: 'web',
watch: true,
devtool: 'source-map',
entry: {
main: './components/main.jsx',
vendor: ['react', 'react-dom']
},
output: {
path: path.resolve('..', 'build', 'js'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.(css|scss)$/,
exclude: /(node_modules)/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: { sourceMap: true }
},
{
loader: 'sass-loader',
options: { sourceMap: true }
}
]
},
{
test: /(\.js|\.jsx)$/,
exclude: /(node_modules)/,
loader: 'babel-loader?cacheDirectory=true',
options: {
presets: ['es2015', 'react']
}
}
]
},
plugins: [
new CommonChunkPlugin({
name: 'vendor',
filename: 'vendor.js'
})
]
};
正如你所看到的,我使用style-loader
,css-loader
和sass-loader
編譯css
和scss
文件。
這是運行Webpack
後得到的錯誤消息。
... _utilities.scss Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type.
| @import "utilities/align";
| @import "utilities/background"; ...
那麼,有什麼我做錯了嗎?我怎樣才能解決這個問題?謝謝。
謝謝。我試過了,但沒有奏效。 – blaze
我用@toomuchdesign答案解決了它。 – blaze