在我的reactJS应用程序中 - 我将所有.scss文件包含在一个main.scss文件中 - 在src文件夹中的styles文件夹下。我有以下webpack配置。尝试将main.scss文件直接包含在我的主要组件中 - 在' @ import'中收到错误我导入其他scss文件的位置。如果我包含单独的scss文件样式是好的 - 但如何使用一个main.scss文件以及如何包含它?
错误:意外的令牌,预期((1:8)
1 | @import' mycomponent';
module.exports = {
entry: [
'webpack-hot-middleware/client',
path.resolve(__dirname, 'src'),
],
output: {
path: path.resolve(__dirname, 'src'),
filename: 'bundle.js',
publicPath: '/',
},
plugins: [
new ExtractTextPlugin('bundle.css', { allChunks: true }),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development'),
WEBPACK: true,
},
}),
],
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
query: {
presets: ['react-hmre'],
},
},
include: path.resolve(__dirname, 'src'),
},
{
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
'css-loader',
'sass-loader?outputStyle=expanded'.
]
}),
test: /\.scss$/,
exclude: /node_modules/
}
],
},
};
index.js
import React from 'react';
import { render } from 'react-dom';
import { createBrowserHistory } from 'history';
import { ConnectedRouter, routerMiddleware } from 'react-router-redux';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import App from './components/homepage';
import reducers from './reducers';
import '../styles/main.scss';
const history = createBrowserHistory();
const store = createStore(reducers, applyMiddleware(routerMiddleware(history)));
render(
<Provider store={store}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>
, document.getElementById('app'));
if (process.env.NODE_ENV === 'development' && module.hot) {
module.hot.accept();
module.hot.accept('./reducers', () => {
const nextRootReducer = require('./reducers').default; // eslint-disable-line global-require
store.replaceReducer(nextRootReducer(store.asyncReducers));
});
}
答案 0 :(得分:3)
您应该在顶级React组件中通过javascript导入.scss
文件。
import './styles/main.scss';
Webpack会将其包含在您的捆绑包中。
对于生产,您需要使用Extract Text Plugin作为webpack,它将从import语句中导出单独的css文件。
答案 1 :(得分:3)
Webpack对.js
个文件进行操作。您需要使用ExtractTextPlugin将.scss
转换为.css
文件:
{test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [{
loader: "css-loader" // translates CSS into CommonJS
}, {
loader: "sass-loader"
}]
})
}
将此webpack入口点设置为main.scss
文件所在的目录,然后使用以下行在该目录中创建index.js
:
require("./main.scss")
(或import ./main.scss
)
此处包含的路径:<link rel="stylesheet" type="text/css" href="./styles/main.scss">
应该是生成的.css
文件的路径。
答案 2 :(得分:3)
以下设置适用于我。
webpack.config.js
{
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
'css-loader', //knows how to deal with css
'autoprefixer-loader?browsers=last 3 versions',
'sass-loader?outputStyle=expanded' //this one is applied first.
]
}),
test: /\.scss$/,
exclude: /node_modules/
}
顶部index.js 中的
import "./styles/main.scss";
import "./reactApp";
main.scss的例子
// Import any of your custom variables for bootstrap here first.
@import "my-theme/custom";
// Then, import bootstrap scss
@import "~bootstrap/scss/bootstrap";
//for bootstrap 3 using bootstrap-sass
$icon-font-path: '~bootstrap-sass/assets/fonts/bootstrap/';
@import '~bootstrap-sass/assets/stylesheets/_bootstrap.scss';
// Finally import any custom styles.
@import "my-theme/master-theme";
答案 3 :(得分:2)
我只是按照你的方式导入它们,不要重复使用id或类名,除非你希望样式是相同的:
import '../styles/main1.scss';
import '../styles/main2.scss';
....
喜欢那样等等