【问题标题】:Grunt amd: true for GulpGrunt amd:对 Gulp 来说是正确的
【发布时间】:2016-09-15 09:41:32
【问题描述】:

我正在做一个前端应用程序,使用:

  • RequireJs
  • BackboneJs
  • 车把

为了将我的模板 html 编译为用于车把的 js,我使用了 Grunt,并在 gruntfile.js 中使用了这些行

handlebars: {
  compile: {
      options: {                                                
          processName: function (fileName) {
              return path.basename(fileName, '.handlebars');
          },
     namespace: "Handlebars.templates",
          amd: true
    },
    files: {
        'src/templates/compiled/example.js':'src/templates/raw/example.handlebars'
    }

效果很好,因为在我的 example.js 中我有第一行:

define(['handlebars'], function(Handlebars) {

这行似乎是因为amd: true而出现的,因为如果我删除它,它就不起作用。

但问题是,在 Gulp 中如何在编译后的项目中添加这个定义?

【问题讨论】:

    标签: backbone.js gruntjs gulp requirejs handlebars.js


    【解决方案1】:

    link 解释了这一点。

    用法

    1.安装开发依赖:
    npm install --save-dev gulp-handlebars gulp-define-module
    
    2. 将require() 语句和template 任务添加到您的gulpfile
    var gulp = require('gulp');
    var defineModule = require('gulp-define-module');
    var handlebars = require('gulp-handlebars');
    
    gulp.task('templates', function() {
        // Load templates from the templates/ folder relative to where gulp was   executed
    gulp.src('source/templates/**/*.hbs')
        // Compile each Handlebars template source file to a template function
        .pipe(handlebars())
        // Define templates as AMD modules
        .pipe(defineModule('amd'))
        // Write the output into the templates folder
        .pipe(gulp.dest('build/js/templates/'));
    });
    

    【讨论】:

    • 这非常感谢,但问题仍然存在,在 gulp 文件中我有一个带有命名空间和 noRedeclare 的 '.pipe(declare({...}))',控制台告诉us 'SyntaxError: missing ) after argument list' 为每个文件。
    • @NocideMugen 检查语法有错。
    • 无论如何,这是我要克服的另一个问题,谢谢你的链接。
    猜你喜欢
    • 2022-12-20
    • 2012-01-30
    • 2014-04-06
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多