【问题标题】:Style-Loader useable feature won't work in Webpack 2.xStyle-Loader 可用功能在 Webpack 2.x 中不起作用
【发布时间】:2017-07-25 05:50:57
【问题描述】:

我正在使用 Webpack ^2.2.1 和 Style-Loader ^0.13.2

我的 Webpack 配置中 .css 文件的规则如下所示:

            {
                test:       /\.css$/,
                use: [
                    { loader: 'style-loader', options: { useable: true } },
                    { loader: 'css-loader' }
                ]
            }

现在,当我异步 import(以前的 require.ensureSystem.import)一个 css 文件时,您收到的是一个对象而不是原始 css 数据,并附有方法:use()unuse()(因为,useable 选项设置为true)。

    import( './mod1.css' ).then( cssRaw => {
        setTimeout(() => {
            // let's assume an async use case
            cssRaw.use();
        },3000);
    });

但是,这对我不再起作用 (Uncaught TypeError: cssRaw.use is not a function)。

其实import(原System.import)这里提供的对象,并不拥有这样的方法。

我在这里做错了什么或者在 Webpack 2.x 中有什么改变吗? 这个确切的代码事先可以正常工作。


正如到目前为止的答案中提到的那样,将加载器字符串传递为

{ loader: 'style-loader/useable' }

按预期工作。如果这是应该的方式,我猜“recommended configuration for style-loader in 2.x”是非常错误的。


我在 Webpack github 页面上提交了一个已被接受的问题,今天已修复 (https://github.com/webpack-contrib/style-loader/pull/185/commits)。

【问题讨论】:

    标签: javascript css webpack webpack-style-loader


    【解决方案1】:

    将您的加载程序配置更改为以下内容:

    {
        test: /\.css$/,
        use: [
            { loader: 'style-loader/useable' },
            { loader: 'css-loader' }
        ]
    }
    

    可用似乎不是一个选项,而是一个加载器本身,在 style-loader 模块目录中可用。

    【讨论】:

    • 你是对的,如果我们按照你的描述传递加载器字符串,它就可以工作。在这种情况下,我被官方的“Webpack style-loader guide for 2.x”(webpack.js.org/loaders/style-loader/#recommended-configuration)深深地吸引了,它明确地将useable功能描述为选项。
    • 不幸的是,Webpack 文档在很多情况下绝对不是最新的,也不是正确的。
    猜你喜欢
    • 2019-02-12
    • 2017-08-21
    • 2018-12-15
    • 2018-06-19
    • 2018-08-09
    • 2017-07-15
    • 2020-07-24
    • 1970-01-01
    • 2018-01-24
    相关资源
    最近更新 更多