如何让 webpack 在其他条目之前编译样式?

发布于 2025-01-09 17:55:58 字数 1442 浏览 1 评论 0原文

我想使用 PostCSS 插件来为类名提供类型安全性 (postcss-ts-classnames )。它生成一个带有 classNames 的 .d.ts 文件,作为 postcss 插件。我想用它来确保 JS 和 CSS 同步,并且我们不会保留旧的类名。当前设置有效,但仅在第一次构建之后。我想强制 webpack 首先编译 CSS,因此 .d.ts 文件在 TypeScript 编译期间始终存在。

理想情况下,我想对 webpack 说“嘿,编译它会生成一个文件作为副产品,另一个编译依赖于这个副产品”。

webpack.config.js
const path = require('path');

module.exports = {
    mode: 'development',
    entry: ['./index.ts', './index.css'],
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: [
                    "style-loader",
                    "css-loader",
                    "postcss-loader"
                ],
            },
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
};
postcss.config.js
module.exports = {
    plugins: [
        require("postcss-ts-classnames")({
            dest: "classnames.d.ts",
        }),
    ]
}

我认为 css/ts 并不那么重要,但我在 https://gist.github.com/marijnvdwerf/f44143b6c4cdc7d5c42120bb9eb24744

I want to use a PostCSS plugin that gives me type safety for classnames (postcss-ts-classnames). It generates a .d.ts file with classNames, as a postcss-plugin. I want to use this to make sure that the JS and CSS say in sync, and we don't keep old classnames around. The current setup works, but only after the first build. I'd like to force webpack to compile the CSS first, so the .d.ts file is always present during the TypeScript compilation.

Ideally I'd like to say to webpack "hey, compiling this will produce a file as byproduct, and another compilation depends on this byproduct".

webpack.config.js
const path = require('path');

module.exports = {
    mode: 'development',
    entry: ['./index.ts', './index.css'],
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: [
                    "style-loader",
                    "css-loader",
                    "postcss-loader"
                ],
            },
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
};
postcss.config.js
module.exports = {
    plugins: [
        require("postcss-ts-classnames")({
            dest: "classnames.d.ts",
        }),
    ]
}

I don't think the css/ts matter that much, but I put a full (non)-working sample on https://gist.github.com/marijnvdwerf/f44143b6c4cdc7d5c42120bb9eb24744.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文