gpt4 book ai didi

css - 为什么 Webpack 忽略我的 CSS 文件?

转载 作者:行者123 更新时间:2023-11-28 11:27:54 25 4
gpt4 key购买 nike

我试图让 webpack 将我的 CSS 文件(使用 PostCSS)编译成一个单独的文件。从文档来看,这似乎正是 ExtractTextPlugin 应该做的。但是,我无法让 webpack 对我的 CSS 文件执行任何操作。

相关元素结构:

dist
⎣js
⎣bundle.js
public
⎣css
⎣style.css*
src
⎣css
⎣index.css

* file doesn’t exist (hasn’t been created)

webpack.config.babel.js

import webpack from 'webpack';
import path from 'path';

import ExtractTextPlugin from 'extract-text-webpack-plugin';

import { WDS_PORT } from './src/shared/config';
import { isProd } from './src/shared/util';

export default {
entry: [
'react-hot-loader/patch',
'./src/client',
],
output: {
filename: 'js/bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: isProd ? '/static/' : `http://localhost:${ WDS_PORT }/dist/`,
},
module: {
rules: [
{ test: /\.(js|jsx)$/, use: 'babel-loader', exclude: /node_modules/ },
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 1,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: 'inline',
},
},
],
}),
},
],
},
devtool: isProd ? false : 'source-map',
resolve: {
extensions: ['.js', '.jsx', '.css'],
},
devServer: {
port: WDS_PORT,
hot: true,
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new ExtractTextPlugin('./public/css/style.css'),
],
};

这将愉快地正确编译我的 javascript,但它对我的 CSS 没有任何作用。出了什么问题,我该如何解决?

最佳答案

Webpack 只处理在某个时候导入的文件。这要么被指定为入口点,要么被导入到从入口点引用的任何文件中。这些规则不会导入任何文件,只要导入通过测试(与您的情况下的正则表达式匹配),它们就会简单地应用。

您需要像导入任何其他模块一样导入您的 CSS。

import './css/index.css';

另见 Code Splitting - CSS .

关于css - 为什么 Webpack 忽略我的 CSS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com