首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内联require.js文本!使用Grunt

内联require.js文本!使用Grunt
EN

Stack Overflow用户
提问于 2013-04-07 19:15:50
回答 1查看 3.8K关注 0票数 11

我一直在用Grunt做实验,今天下午我需要JS。我非常喜欢text模块,并使用它来引入我的模板。在非基于Grunt的项目中,我使用了inlineTextstubModules需要JS选项来内联模板文件,而且效果很好。然而,我很难让这个和Grunt一起工作。

需要Config

代码语言:javascript
复制
require.config({
    paths: {
        // Using Bower for dependency management
        text: '../components/requirejs-text/text'
    }
});

使用

代码语言:javascript
复制
define(['text!template.html'], function (html) {
    // Do stuff with html
});

Gruntfile.js

代码语言:javascript
复制
requirejs: {
    dist: {
        options: {
            baseUrl: 'app/scripts',
            optimize: 'none',
            preserveLicenseComments: false,
            useStrict: true,
            wrap: true,
            inlineText: true,
            stubModules: ['text']
        }
    }
}

运行grunt后,控制台中会出现各种错误:

  • /dist/components/requirejs-text/text.js上找不到的文件
  • 一个Load timeout for modules: text!template.html_unnormalized2

因此,有两个问题:

  • 它似乎并不是内联(然后是顽固地) text.js代码
  • 它似乎没有插入template.html文件

知道为什么不行吗?

EN

回答 1

Stack Overflow用户

发布于 2013-09-14 18:31:48

您会看到错误,因为您需要向r.js指示text模块在哪里。

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

代码语言:javascript
复制
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模块下载到适当的目录中。

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

因为r.js需要在某个时候对配置进行评估。您没有在问题中提到您的require.config在哪里,但是如果您想对其进行评估,则需要指出r.js的位置(参见https://github.com/jrburke/r.js/blob/master/build/example.build.js#L35):

代码语言:javascript
复制
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
          }
       }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15866683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档