javascript - 了解 Brunch 配置中的不同导出方式

标签 javascript module brunch

所以我得到了这个配置文件:

exports.files = {
  javascripts: {
    joinTo: {
      'vendor.js': /^(?!app)/, // Files that are not in `app` dir.
      'app.js': /^app/
    }
  },
  stylesheets: {joinTo: 'app.css'}
};

exports.plugins = {
  babel: {presets: ['latest']}
};

exports.npm = {
  styles: {
    bootstrap: ['dist/css/bootstrap.css']
  }
}

当上面的代码被触发时,5 个文件被构建并编译成 3 个,正如预期的那样。

然后为了更好地理解,我将配置文件中的first export更改为:

module.exports = {
    files: {
        javascripts: {
            joinTo: {
                'vendor.js': /^(?!app)/, 
                'app.js': /^app/
            }
        },
        stylesheets: {joinTo: 'app.css'}
    }
}

exports.plugins = {
  babel: {presets: ['latest']}
};

exports.npm = {
  styles: {
    bootstrap: ['dist/css/bootstrap.css']
  }
}

现在 Bootstrap 代码没有被编译到最终的样式表中。为什么我会遇到这种行为?

最佳答案

这个问题与 Brunch 没有直接关系,但与节点如何处理导出有关。查看来自 node docs 的解释:

The exports variable is available within a module's file-level scope, and is assigned the value of module.exports before the module is evaluated.

It allows a shortcut so that module.exports.f = ... can be written more succinctly as exports.f = ... However, be aware that like any variable, if a new value is assigned to exports, it is no longer bound to module.exports:

module.exports.hello = true; // Exported from require of module
exports = { hello: false };  // Not exported, only available in the module

关于javascript - 了解 Brunch 配置中的不同导出方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46119663/

相关文章:

node.js - nestjs中模块之间的循环依赖

backbone.js - 如何使用 brunch.js 将多个 html 文件合并为一个

vagrant - 在 Vagrant/Virtualbox 中运行时,Brunch watch 不会处理后续更改的文件

javascript - 检测文档何时为 "fully active"

javascript - 类是否有望与原型(prototype)进行互操作

javascript - 模拟从 <asp :button> 上的 <button> 单击

java - IntelliJ 无法解析同一模块中相邻目录的 taglib

javascript - "Could not find a declaration file for module"导入现有 .jsx 文件时

coffeescript - 无法运行最简单的规范 : TypeError: '[object Object]' is not a constructor (evaluating 'new Options()' )

javascript - 如何使用 knockout 使窗口大小可观察