内联require.js的文字!使用咕噜 [英] Inlining require.js text! using Grunt

查看:294
本文介绍了内联require.js的文字!使用咕噜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试与咕噜今天下午要求JS。我是文本模块的大风扇,并用它在我的模板带来的。在基于非咕噜项目中,我使用了 inlineText stubModules 要求JS选项在线模板文件,它伟大工程。不过,我遇到了麻烦这跟咕噜工作。

I've been experimenting with Grunt and Require JS this afternoon. I'm a big fan of the text module and use it to bring in my templates. In non-Grunt based projects I used the inlineText and stubModules Require JS options to in-line the template files and it works great. However, I'm having trouble getting this to work with Grunt.

需要配置

require.config({
    paths: {
        // Using Bower for dependency management
        text: '../components/requirejs-text/text'
    }
});

用法

define(['text!template.html'], function (html) {
    // Do stuff with html
});

Gruntfile.js

requirejs: {
    dist: {
        options: {
            baseUrl: 'app/scripts',
            optimize: 'none',
            preserveLicenseComments: false,
            useStrict: true,
            wrap: true,
            inlineText: true,
            stubModules: ['text']
        }
    }
}

运行后咕噜我得到在控制台中各种错误:

After running grunt I get various errors in the console:


  • 系统文件未找到关于 /dist/components/requirejs-text/text.js

  • A 为模块加载超时:文本template.html_unnormalized2

  • A File Not Found on /dist/components/requirejs-text/text.js
  • A Load timeout for modules: text!template.html_unnormalized2

然后两个问题:


  • 这似乎并没有被内联(然后存根)的 text.js code

  • 这似乎并没有被内联 template.html 文件

  • It doesn't seem to be inlining (and then stubbing) the text.js code
  • It doesn't seem to be inlining the template.html file

任何为什么它不工作的想法?

Any ideas why it's not working?

推荐答案

您看到的错误,因为你需要告诉 r.js 在哪里文本模块。

You see the error because you need to indicate to r.js where is the text module.

您可以通过添加路径配置做到这一点:

You can do that by adding a paths configuration:

requirejs: {
    dist: {
        options: {
          baseUrl: 'app/scripts',
          optimize: 'none',
          preserveLicenseComments: false,
          useStrict: true,
          wrap: true,
          inlineText: true,
          stubModules: ['text'],
          paths: {
             'text': 'libs/text' // relative to baseUrl
          }
       }
    }
}

然后,你需要在 text.js 模块下载到相应的目录。

Then you'll need to download the text.js module into the appropriate directory.

但是,为什么你的 require.config 不工作?

But why your require.config is not working?

由于该 r.js 需要在某个点来评估的配置。你没有在这个问题提哪里是你的 require.config ,但要评估它的情况下,你需要指明是 r.js (见的 https://github.com/jrburke/r.js/blob/master/build/example.build.js#L35 ):

Because the r.js needs to evaluate the configuration at some point. You didn't mention in the question where is your require.config, but in case that you want to evaluate it you need to indicate where is to r.js (see https://github.com/jrburke/r.js/blob/master/build/example.build.js#L35):

requirejs: {
    dist: {
        options: {
          baseUrl: 'app/scripts',
          optimize: 'none',
          preserveLicenseComments: false,
          useStrict: true,
          wrap: true,
          inlineText: true,
          stubModules: ['text'],
          mainConfigFile: '../config.js' // here is your require.config

          // Optionally you can use paths to override the configuration
          paths: {
             'text': 'libs/text' // relative to baseUrl
          }
       }
    }
}

这篇关于内联require.js的文字!使用咕噜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆