【问题标题】:Angular modules as node modules?Angular 模块作为节点模块?
【发布时间】:2015-02-06 16:25:38
【问题描述】:

我真的很喜欢angular's core modules,我正在尝试在 node/io.js 环境中使用其中的一些功能;即:将 Angular 模块转换为 commonjs 格式的 ES5 模块

我尝试使用 traceur 手动构建 Angular 模块,但没有成功(生成循环依赖),还尝试调整 Angular 项目的 gulpfile.js 以生成 commonjs 格式的 ES5 输出:

var _COMPILER_CONFIG_JS_DEFAULT = {
  sourceMaps: true,
  annotations: true, // parse annotations
  types: true, // parse types
  script: false, // parse as a module
  memberVariables: true, // parse class fields
  modules: 'commonjs'
};

... 正在运行 build/transpile.js.dev 任务,但我一直面临这条消息:

TypeError: Cannot call method 'map' of undefined

即使更改默认配置中的sourceMaps,我也无法摆脱这个map 错误。

我还注意到他们的编译器工具,它似乎实际上处理了输出格式。但我没有机会改变它的选择。我只能在 System.register 格式的文件中使用默认配置,而我无法转换为 commonjs。

经历过这一切的人有没有在 commonjs 风格的项目中使用 angular 模块的解决方案?请注意,我想将其用作库,并且不想将我的项目转换为另一种类型的模块系统。

欢迎任何建议,谢谢!

【问题讨论】:

标签: angularjs node.js ecmascript-6 porting traceur


【解决方案1】:

使用 CommonJS 包装器来映射 Angular 模块,例如这个例子:

# angular is required globally
{{name}} = angular.module '{{name}}', []
module.exports = {{name}}

【讨论】:

    【解决方案2】:

    我认为使用 Angular 模块和构建步骤将所有文件连接到一个主文件是一种好方法。

    这是我正在工作的项目的 gulpfile.js。

    var gulp = require('gulp');
    var concat = require('gulp-concat');
    
    
        var path = 'app/js/';
        var srcFiles = [path + 'midi.js',
                        path + 'keyboard.js',
                        path + 'analyser.js',
                        path + 'services/amp.js',
                        path + 'services/lfo-amp.js',
                        path + 'services/osc.js',
                        path + 'services/filter.js',
                        path + 'services/lfo.js',
                        path + 'audio.js',
                        path + 'synth.js',
                        path + 'app.js'];
    
        gulp.task('default', function () {
          gulp.src(srcFiles)
            .pipe(concat('app.js'))
            .pipe(gulp.dest('dist/js/'))
        });
    

    gulp.src 接受要连接的文件数组,并将所有文件保持在正确的顺序。现在,您可以根据需要将 Angular 项目分解为主要文件,而不必担心尝试加载一堆脚本标签而导致服务器瓶颈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 2023-03-04
      • 2017-04-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      相关资源
      最近更新 更多