【问题标题】:How to reference a NodeJS module w/ Webpack如何使用 Webpack 引用 NodeJS 模块
【发布时间】:2016-11-16 02:56:13
【问题描述】:

当使用 SystemJS 编写 Angular2 应用程序时,我可以做一个

// map tells the System loader where to look for things
var map = {
  'app':                        'app', // 'dist',
  '@angular':                   'node_modules/@angular',
  'rxjs':                       'node_modules/rxjs'
};

当我打电话给import {} from 时,告诉 Angular 在哪里可以找到东西。在这种情况下,如果我现在想使用 RxJS,我可以import { Observable } from 'rxjs/Rx'

但是,如果我使用 Webpack,这相当于什么?

【问题讨论】:

    标签: angularjs node.js angular webpack systemjs


    【解决方案1】:

    Webpack 会自动执行此操作。除非您使用代码拆分。
    假设你有:
    模块A:
    import {fn} "moduleB"

    模块 B:
    export default fn

    Webpack 将包含模块 A(您的条目)的所有依赖项,因此您将能够使用 fn。
    现在,如果您使用代码拆分,则需要使用 require.ensure
    例如:

    require.ensure(["module-a", "module-b"], function(require) {
        var a = require("module-a");
        // ...
    });
    

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 2021-04-07
      相关资源
      最近更新 更多