使用antd-theme-webpack-plugin报错Error LessError: Cannot find module ‘antd/lib/style/themes/default.less

问题

我使用的依赖:

"antd-theme-webpack-plugin": "^1.3.9"

vue.config.js 里配置了

const path = require("path");

const AntDesignThemePlugin = require('antd-theme-webpack-plugin');

const options = {
    antDir: path.join(__dirname, './node_modules/ant-design-vue'),
    stylesDir: path.join(__dirname, './src'),
    varFile: path.join(__dirname, './src/assets/styles/theme/variables.less'),
    themeVariables: ['@primary-color'],
    generateOnce: false
}

const themePlugin = new AntDesignThemePlugin(options);

module.exports = {
    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    'primary-color': '#1DA57A',
                },
                javascriptEnabled: true
            },
        }
    },
    configureWebpack: {
        plugins: [ themePlugin ]
    },
}

启动服务就报错了:错误如下

Error LessError: Cannot find module 'antd/lib/style/themes/default.less'

在这里插入图片描述
LessError: error evaluating function darken: color.toHSL is not a function

在这里插入图片描述

原因排查分析

既然报错说找不到 antd/lib/style/themes/default.less 文件,我们可以去全局搜索哪里使用了 antd/lib/style/themes/default.less

我们可以看到在 ant-design-vue-pro\node_modules\antd-theme-webpack-plugin\node_modules\antd-theme-generator\index.js 文件的 362 行使用了。所以问题就定位到了这个文件,而这个文件所在的包是 antd-theme-generatorantd-theme-webpack-plugin 里面使用了 antd-theme-generator

在这里插入图片描述

我们找到https://github.com/mzohaibqc/antd-theme-webpack-plugin/blob/master/package.json,可以看到依赖的是 1.2.8

在这里插入图片描述

我们查看其他版本的,比如:1.2.3,发现代码 fileContent = @import "~antd/lib/style/themes/default.less";\n${fileContent}; 注释掉了,所以断定是这个依赖的版本有问题。

在这里插入图片描述

解决

其实上面的版本 "antd-theme-generator": "^1.2.3",要测试出来符合 antd-theme-webpack-plugin: 1.3.9,需要我们独自分离这两个包,因为我们安装的是 antd-theme-webpack-plugin: 1.3.9 依赖,而 antd-theme-generator 的版本是由 antd-theme-webpack-plugin 控制的。

我的做法是自己复制一份 antd-theme-webpack-plugin 的代码,当做我们的自定义插件,然后自己主动安装 "antd-theme-generator": "^1.2.3" 即可。

我们新建一个 ant-design-vue-pro\webpack-plugins\antd-theme-webpack-plugin.js 插件:里面的代码直接复制 antd-theme-webpack-plugin 的代码。

// https://github.com/mzohaibqc/antd-theme-webpack-plugin/blob/master/index.js

const { generateTheme } = require("antd-theme-generator");
const webpack = require("webpack");
const { RawSource } = webpack.sources || require("webpack-sources");
const path = require("path");

class AntDesignThemePlugin {
    constructor(options) {
        const defaultOptions = {
            varFile: path.join(__dirname, "../../src/styles/variables.less"),
            antDir: path.join(__dirname, "../../node_modules/antd"),
            stylesDir: path.join(__dirname, "../../src/styles/antd"),
            themeVariables: ["@primary-color"],
            indexFileName: "index.html",
            generateOnce: false,
            lessUrl: "https://cdnjs.cloudflare.com/ajax/libs/less.js/2.7.2/less.min.js",
            publicPath: "",
        };
        this.options = Object.assign(defaultOptions, options);
        this.generated = false;
        this.version = webpack.version;
    }

    apply(compiler) {
        const pluginName = "AntDesignThemePlugin";

        if (this.version.startsWith("5.")) {
            compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
                compilation.hooks.processAssets.tapAsync(
                    {
                        name: pluginName,
                        stage: 
                            webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
                    },
                    (assets, callback) =>
                        this.addAssets(compilation, assets, callback)
                );
            });
        } else {
            compiler.hooks.emit.tapAsync(pluginName, (compilation, callback) =>
                this.addAssets(compilation, compilation.assets, callback)
            );
        }
    }

    addAssets(compilation, assets, callback) {
        this.generateIndexContent(assets, compilation);

        if (this.options.generateOnce && this.colors) {
            this.generateColorStylesheet(compilation, this.colors);
            return callback();
        }

        generateTheme(this.options)
            .then((css) => {
                if (this.options.generateOnce) {
                    this.colors = css;
                }
                this.generateColorStylesheet(compilation, css);
                callback();
            })
            .catch((err) => {
                callback(err);
            });
    }

    generateIndexContent(assets, compilation) {
        if (
            this.options.indexFileName &&
            this.options.indexFileName in assets
        ) {
            const index = assets[this.options.indexFileName];
            let content = index.source();

            if (!content.match(/\/color\.less/g)) {
                const less = `
                    <link rel="stylesheet/less" type="text/css" href="${this.options.publicPath}/color.less" />
                    <script>
                        window.less = {
                        async: false,
                        env: 'production'
                        };
                    </script>
                    <script type="text/javascript" src="${this.options.lessUrl}"></script>
                `;

                const updatedContent = content
                    .replace(less, "")
                    .replace(/<body>/gi, `<body>${less}`);

                if (this.version.startsWith("5.")) {
                    compilation.updateAsset(
                        this.options.indexFileName,
                        new RawSource(updatedContent),
                        { size: updatedContent.length }
                    );
                    return;
                }

                index.source = () => updatedContent;
                index.size = () => updatedContent.length;
            }
        }
    }

    generateColorStylesheet(compilation, source) {
        if (this.version.startsWith("5.")) {
            compilation.emitAsset("color.less", new RawSource(source), {
                size: source.length,
            });
            return;
        }

        compilation.assets["color.less"] = {
            source: () => source,
            size: () => source.length,
        };
    }
}

module.exports = AntDesignThemePlugin;

然后安装依赖,webpack-sources 有就不需要安装了。然后我从 1.2.0 开始往上找,找到了 1.2.3 版本不会报错。

npm install antd-theme-generator@1.2.3 -D

最后修改 vue.config.js 配置里的引用自己定义的插件,其他不需要改动。

const AntDesignThemePlugin = require('./webpack-plugins/antd-theme-webpack-plugin');

启动服务,就没有报错了。

在这里插入图片描述

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯小默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值