【问题标题】:webpack.config.js not setting global less variablewebpack.config.js 没有设置全局变量
【发布时间】:2021-06-21 20:36:55
【问题描述】:

知道为什么这个webpack.config.js 没有设置全局LESS 变量:current-vehicle 定义在:/resources/scripts/theme.js 上吗?

/webpack.config.js

const merge = require("webpack-merge");
const path = require("path");
const baseConfig = require("laravel-mix/setup/webpack.config");

require('dotenv').config();

/**
 * Update the output directory and chunk filenames to work as expected.
 *
 * @param {object} config - The webpack config
 *
 * @return {object} The updated webpack config
 */
const addOutputConfiguration = config => {
  const publicPath = process.env.CDN_URL + '/js/';
  return merge(config, {
    output: {
      path: path.resolve(__dirname, "public/cdn/js"),
      publicPath,
      chunkFilename: "[name].js"
    },
    module: {
      loaders: [
        {
          loader: 'less-loader',
          options: {
            modifyVars: require("./resources/scripts/theme")
          }
        },
      ],
    },
  });
};

module.exports = addOutputConfiguration(baseConfig);

这里有完整的 repo:https://github.com/tlg-265/issue-import-less.local

设置

$ git clone https://github.com/tlg-265/issue-import-less.local
$ cd issue-import-less.local
$ npm i
$ npm run watch

Then open with the browser (you do NOT need a server).

LESS 变量:current-vehicle 在此处读取:

https://github.com/tlg-265/issue-import-less.local/blob/master/resources/assets/js/components/controls/ModalWindow/ModalWindow.less#L1

当你这样做时:$ npm run watch,你会得到错误:Variable @current-vehicle is undefined,如下面的截图所示:

理想情况下,当你运行它时,你应该在浏览器上得到以下信息:

当点击链接时,你应该得到:

但很遗憾我还没能到达那里。

知道如何让它发挥作用吗?

提前致谢!

【问题讨论】:

  • 试过了吗? link
  • 关于如何将其集成到我的存储库中的任何想法?你可以试试吗?

标签: node.js reactjs webpack laravel-mix less-loader


【解决方案1】:

你的这部分配置让我震惊:

    module: {
      loaders: [ // <==== !!!!!! here
        {
          loader: 'less-loader',
          options: {
            modifyVars: require("./resources/scripts/theme")
          }
        },
      ],
    },

我记得loaders 键是用于 Webpack 版本 1 或 2,在版本 3 之后他们强烈建议使用 rules,请将其更改为 rules 并尝试一下。不知道,可能是来自 Laravel webpack 的配置。

无论如何,在less-loader 之后也请添加这个加载器:

{ 
  loader: 'text-transform-loader',
  options: {
    prependText: '@import "./resources/scripts/theme.less"',
  },
},

【讨论】:

  • 文件:./resources/scripts/theme.less 不存在
  • 根据您的建议(我认为这是一个小错误),我尝试了类似的方法但没有运气:i.ibb.co/D7wx0z0/image.png。如您所见 1. 我删除了以前的加载程序,因为那不起作用,我试图... 2. 使用text-transform-loader 在较少文件的开头导入变量值,但仍然出现以前的错误:@ 987654330@。请记住该变量:process.env.VEHICLE 是在.env 文件上定义的(也尝试对其值进行硬编码)。您是否可以自己尝试并发布所需的git-diff 以使其正常工作?谢谢!
  • @Viewsonic,对于您的第一条评论:伙计,我只是从您有问题的代码中获取./resources/scripts/theme.less。如果它不存在,那么制作它并传递变量 init。并对其进行测试,它应该可以工作。
  • @Viewsonic,第二次。如果您在 .env 文件中添加了一些环境变量并获得了 undefined,那么您应该安装 dotenv 包并将其导入到 webpack 和项目的根目录中,例如:const dotEnv = require('dotenv');import dotEnv from 'dotenv' 然后通过 @987654340 运行它@。这样整个项目就可以理解环境变量了。
  • @Viewsonic,很高兴帮助你❤️?,我也赞成你的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多