自己新写了一个基于 react 和 antd 的组件,并用 webpack 打包好并发布到了 npm 官网供其他人使用,js 和 css 是分开打包的,用下面命令安装和使用
yarn add my-app
import MyApp from 'my-app'
问题是,我 css 文件也要 import 进来,感觉很麻烦,有没有自动把 css 引入进来的,我看 antd 就是这样做的,但是不知道他是如何实现的?有大佬知道怎么做不?
下面是我的 webpack.config.js
const path = require("path");
const webpack = require("webpack");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const WebpackMd5Hash = require("webpack-md5-hash");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const resolve = dir => path.join(__dirname, ".", dir);
const isProd = process.env.NODE_ENV === "production";
module.exports = {
entry: { main: "./src/index.js" },
output: {
// path: resolve("dist"), // 输出目录
path: path.resolve(__dirname, "dist"),
filename: "[name].js", // 输出文件
//不加下面几行,被引用会被报错
libraryTarget: "umd", // 采用通用模块定义
library: "hiynn-design", // 库名称
libraryExport: "default", // 兼容 ES6(ES2015) 的模块系统、CommonJS 和 AMD 模块规范
globalObject: "this" // 兼容node和浏览器运行,避免window is not undefined情况
},
devtool: "#source-map",
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: ["style-loader", MiniCssExtractPlugin.loader, "css-loader", "postcss-loader", "sass-loader"]
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
},
resolve: {
extensions: [".js", ".jsx"]
},
// 注意:本地预览的时候要注释,否则报 require undefined
// https://stackoverflow.com/questions/45818937/webpack-uncaught-referenceerror-require-is-not-defined
externals: [nodeExternals()],
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: "[name].css"
}),
new WebpackMd5Hash()
],
//压缩js
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true
}),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css\.*(?!.*map)/g, //注意不要写成 /\.css$/g
cssProcessor: require("cssnano"),
cssProcessorOptions: {
discardComments: { removeAll: true },
// 避免 cssnano 重新计算 z-index
safe: true,
// cssnano 集成了autoprefixer的功能
// 会使用到autoprefixer进行无关前缀的清理
// 关闭autoprefixer功能
// 使用postcss的autoprefixer功能
autoprefixer: false
},
canPrint: true
})
]
}
};
你是不是用了插件, 比如mini-css-extract-plugin 把css单独打包了
如果不用插件的话, css是在js内部的