【问题标题】:Load dotenv and polyfills in brunch在早午餐中加载 dotenv 和 polyfill
【发布时间】:2017-05-01 02:33:24
【问题描述】:

我正在尝试使用构建工具 brunch 为我的 react 应用程序制作一些样板。启动很容易,但我需要一些小东西来使我的开发更加完整。其中之一是环境变量。

我尝试了一些想法,但没有任何效果。诸如使用entryPoints 加载执行功能的配置 JS 文件之类的事情:

brunch-config.js

exports.files = {
  javascripts: {
    joinTo: {
      'dependencies.js': /^(?!app\/)/,
      'bundle.js': /^app\//
    },
    entryPoints: {
      'app/confs.js': 'bundle.js'
    }
  },
  stylesheets: {
    joinTo: 'app.css'
  },
};

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

confs.js

require('dotenv').config();
require('whatwg-fetch');

但是现在,什么都行不通...有人能给我一些想法吗?

提前致谢。

编辑

我在配置和结构中改变了一些东西,思路还是一样,在应用引导中加载polifyllsenv变量。

现在confs.jsinit.js,并没有编译成bundle.js,而是在它自己的文件中编译成public/

里面的代码是一样的,brunch-config.js是:

exports.files = {
  javascripts: {
    joinTo: {
      'dependencies.js': /^(?!app\/)/,
      'bundle.js': 'app/**/*[!init].js', // all but not init.js
      'init.js': 'app/init.js' // put init.js in it's own file into public/
    }
  },
  stylesheets: {
    joinTo: 'app.css'
  },
};

exports.plugins = {
  babel: { presets: ['latest', 'react'] },
  sass: {
    options: {
      includePaths: ['node_modules/normalize-css/']
    }
  }
};

但是编译后的init.js 在他的require.register() 中是空的,无论我在文件中放入什么代码:

require.register("init.js", function(exports, require, module) {
"use strict";

});

【问题讨论】:

    标签: javascript brunch


    【解决方案1】:

    <script>require('confs');</script>
    

    在 index.html 中。

    通常,您只需要 HTML 文件中的一个初始 Javascript 文件。在此 Javascript 文件中,您需要其他软件包。这是必需的,因此以某种方式使包之间的依赖关系显式化。

    【讨论】:

    • 试过这个,但不起作用。 confs 结构是相同的,但这次我之前在index.html 中调用过它,但没有任何反应。我正在尝试另一种方法,我会更新这个。
    猜你喜欢
    • 2018-08-27
    • 2017-08-08
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2019-01-04
    • 2014-09-02
    相关资源
    最近更新 更多