简体   繁体   English

使用 Webpack 4 编译 Stylus 而不将其导入 JS 文件?

[英]Compiling Stylus with Webpack 4 without importing it into a JS file?

Something like this:像这样的东西:

var stylusCompiler = {
    name: 'stylus',
    entry: {
        above_fold: './src/css/above_fold.styl',
        site: './src/css/site.styl'
    },
    output: {
        path: path.resolve(__dirname, 'dist/css'),
        filename: '[name].bundled.css'
    },
    module: {
        rules: [
            {
                test: /\.styl$/,
                use: [
                    {
                        loader: "stylus-loader", // compiles Stylus to CSS
                        options: {
                            use: [
                                require('nib')(),
                                require('rupture')()
                            ]
                        }
                    }
                ]
            },
        ]
    }
};

Doesn't work because it seems to be expecting JS, so gives syntax errors for any css it encounters.不起作用,因为它似乎在期待 JS,因此它遇到的任何 css 都会出现语法错误。 It is parsing the stylus, because the error shows the compiled CSS.它正在解析手写笔,因为错误显示了已编译的 CSS。

Webpack by default doesn't support entry types other than JavaScript.默认情况下,Webpack 不支持 JavaScript 以外的条目类型。 It's mentioned in their v4 release that HTML and other file types will be supported in v4.x and v5.x, in future releases.在他们的 v4 版本中提到,在未来的版本中,v4.x 和 v5.x 将支持 HTML 和其他文件类型。

You can instead use MiniCssExtractPlugin that will extract your styles from the JavaScript bundle and name them accordingly.您可以改为使用MiniCssExtractPlugin从 JavaScript 包中提取您的样式并相应地命名它们。 See this Medium post about it, a bit outdated but still delivers the idea.看到这个关于它的媒体帖子,有点过时但仍然提供了这个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM