2016-12-29 37 views
2

我需要在我的.js和.less文件之间共享一个常量。我的解决方案包含两个步骤:有没有办法多次要求一个文件作为不同的模块

  1. 创建一个扩展名为.const的独立文件,其中包含所有共享常量。它具有JavaScript模块语法,并将正常执行。
  2. 根据需要此源的文件的扩展名创建一个将更改源代码的加载程序。所以,如果,例如,要求.less文件,然后代码转换为较少的表达式

但我不找到从一个源文件创建两个模块的方式取决于需要的模块。

有条件装载机我的源代码,该做的东西:

module.exports = function(content, sourceMap) { 
    var _ = require('lodash'); 
    var loaderUtils = require('loader-utils'); 
    var reasonResource = this._module.reasons[0].module.resource; 
    var reasonResourceExtension = _.last(reasonResource.split('.')); 

    switch (reasonResourceExtension) 
    { 
     case 'js': 
     case 'const': 
      var query = loaderUtils.parseQuery(this.query); 
      if(query.cacheable && this.cacheable) 
       this.cacheable(); 

      return content; 
      break; 
     case 'less': 
      var consts = this.exec(content, this.resource); 

      return JSON.stringify(consts); 
      break; 
     default: 
      throw new Error('Not supported import .const modules from ' + reasonResourceExtension.green); 
    } 
}; 

回答

0

问题出在less-loader。它不应用装载机,在webpack.config.js中配置。所以,解决办法是添加到参数化less-loader装载机列出

Pull request


能力然后,它开始可能写Constants.const与内容

export.modules = { debug: true } 

,然后要求其在Utilities.less

@import (reference) 'Const.const'; 

适用该装载机

module.exports = function(content, sourceMap) { 
    var _ = require('lodash'); 
    var loaderUtils = require('loader-utils'); 
    var reasonResource = this._module.reasons[0].module.resource; 
    var reasonResourceExtension = _.last(reasonResource.split('.')); 
    var query = loaderUtils.parseQuery(this.query); 

    switch (reasonResourceExtension) 
    { 
     case 'js': 
     case 'const': 
      if(query.cacheable && this.cacheable) 
       this.cacheable(); 

      return content; 
      break; 
     case 'less': 
      if(query.cacheable && this.cacheable) 
       this.cacheable(); 

      var consts = this.exec(content, this.resource); 
      var str = ''; 
      (function append(consts, prefix = '') 
      { 
       for (var key in consts) 
       { 
        let value = consts[key]; 
        if (value.constructor != Object) { 
         str += '@' + prefix + key + ': ' + toLessCssValue(consts[key]) + ';'; 
        } else { 
         throw new Error('Nested objects not supported in .const files'); 
        } 
       } 
      }(consts)); 

      return str; 
      break; 
     default: 
      throw new Error('Not supported import .const modules from ' + reasonResourceExtension.green); 
    } 

    function toLessCssValue(value) { 
     if (isNaN(value)) { 
      return value; 
     } else { 
      return 'unit(' + value + ', px)'; 
     } 
    } 
}; 
0

你也许可以指定一个JSON到少装载机(真的不知道,如果这存在,但它不应该是很难写)的使用需要加载的语法性较低的文件:

@import "!json-to-less-loader!constants.json"; 

然后,只需创建,使这个进口,并公开常量不太文件(constants.less)。从那里,您可以正常导入constants.less

我能够找到一个相反的装载机(less-to-json-loader)。所以,你可以在constants.less使用和存储您的常量在.LESS文件并创建constants.js文件导入和公开常量:

constants.less

@test: 20rem; 
@test2: @test/2; 

constants.js

module.exports = require("!less-to-json-loader!./constants.less"); 
相关问题