webpack.config.js 没有设置全局变量

Vie*_*nic 6 node.js reactjs webpack laravel-mix less-loader

为什么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);
Run Code Online (Sandbox Code Playgroud)

这里有完整的仓库: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).
Run Code Online (Sandbox Code Playgroud)

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,如下面的屏幕截图所示:

在此处输入图片说明

理想情况下,当您运行它时,您应该在浏览器上看到以下内容:

在此处输入图片说明

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

在此处输入图片说明

但不幸的是我还没有能够到达那里。

关于如何使它工作的任何想法?

提前致谢!

Ame*_*icA 1

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

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

我记得loaders密钥是针对Webpack版本1或2的,在版本3之后他们强烈推荐使用rules,请更改为rules并尝试一下。我不知道,也许它来自 Laravel webpack 配置。

不管怎样,之后less-loader也请添加这个加载器:

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

Run Code Online (Sandbox Code Playgroud)