【问题标题】:How do I use stylus-resources-loader with Vue CLI 3?如何在 Vue CLI 3 中使用 stylus-resources-loader?
【发布时间】:2018-07-22 20:12:16
【问题描述】:

我知道这涉及修改vue.config.js,但只是将我想要的配置粘贴到configureWebpack 对象中似乎不起作用。有其他人能够解决这个问题吗?

想要添加的配置:

module: {
            rules: [
                {
                    test: /\.vue$/,
                    use: [
                        {
                            loader: "vue-loader",
                            options: {
                                loaders: {
                                    stylus: [
                                        {
                                            loader: "stylus-resources-loader",
                                            options: {
                                                resources:
                                                    "./src/assets/_base.styl",
                                            },
                                        },
                                    ],
                                },
                            },
                        },
                    ],
                },
            ],
        },

谢谢!

【问题讨论】:

    标签: vue.js stylus vue-cli


    【解决方案1】:

    const path = require('path');
    
    module.exports = {
      chainWebpack: (config) => {
        config.module
          .rule('vue')
          .use('vue-loader')
          .tap((options) => {
            options.loaders.stylus = options.loaders.stylus.concat({
              loader: 'stylus-resources-loader',
              options: {
                resources: path.resolve('./src/assets/_base.styl'),
              },
            });
            return options;
          });
      },
    };

    更新:

    需要注意的是<style lang="stylus">lang属性的值会影响配置项的写法! 当langstylus时,stylus挂载在loader上,应该写成:options.loaders.stylus,当lang的值为styl时,应该写成@ 987654333@.

    由于@vue/cli-service/lib/webpack/CSSLoaderResolver.js中的以下代码:

    getLoader (test, loader, options = {}) {
      styl (test = /\.styl$/) {
        return this.getLoader(test, 'stylus')
      }
    
      stylus (test = /\.stylus$/) {
        return this.getLoader(test, 'stylus')
      }
    }

    参考https://stackoverflow.com/a/49086022/4723163

    【讨论】:

    • 似乎不起作用。我也在这个项目中使用 Typescript,这有什么不同吗?
    • 对不起,这是我的错。请把options.loaders.styl改成options.loaders.stylus,应该可以正常工作了。
    猜你喜欢
    • 2018-11-02
    • 2018-08-11
    • 2017-12-16
    • 2019-12-20
    • 2018-03-11
    • 2019-05-04
    • 2019-01-18
    • 2019-05-07
    • 2018-05-17
    相关资源
    最近更新 更多