当我编译导入css文件的父组件时,我能够使用webpack和css加载器生成css文件和js文件。但是,如果我不在父组件中导入css文件但在子组件中导入css文件,并且让webpack转换父组件文件,则不会生成css文件。问题是,有些css文件我只想在子组件中导入,但似乎css loader不允许我这样做吗?
例如:
//parent.js
import React from 'react';
import Child from './child.js'
export class Parent extends React.Component(){
render(){
return <div><Child/></div>
}
}
//child.js
import React from 'react';
import styleSheet from './a.css';
export class Child extends React.Component(){
render(){
return <div className = {styleSheet.example}>hi</div>
}
}
目前我的webpack配置看起来像这样。
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: 'parent.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'parentBundle.js',
},
resolve: {
extensions: ['.js', '.css']
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: 'css-loader',
query: {
localIdentName: '[hash:8]',
modules: true
}
}
]
})
}
]
},
plugins: [
new ExtractTextPlugin({
filename: 'testing.css',
allChunks: true
})
]
};