requirejs - 使用r.js打包一个使用 'text'加载 View 的SPA应用程序

标签 requirejs gruntjs durandal r.js almond

我正在尝试使用 grunt 将 SPA 应用程序(requirejs、durandal 2、knockout)构建到单个 main-build.js 文件中,并且 durandal 使用的“文本”插件遇到了严重问题加载我的 View 。

在开发中,我成功地使用“文本”按照构建 durandal 应用程序的标准方式动态加载 View 。不同之处在于我需要为 View 做一些服务器端模板,因此它们实际上是动态生成的。

考虑到这一点,我想使用 r.js 将应用程序模型、 View 模型和服务(通过 grunt-durandal 插件)打包到单个文件中,但不打包 View (.html) 和仍然根据需要动态加载它们。

在我的 grunt 配置中,我使用了 inlineText: false 选项 - 我已检查该选项是否会抑制构建中的“text!*”模块。但是当我运行该应用程序时,我得到:

未捕获的类型错误:未定义不是函数来自以下行的text.load:

var parsed = text.parseName(name),
            nonStripName = parsed.moduleName +
                (parsed.ext ? '.' + parsed.ext : ''),
            url = req.toUrl(nonStripName), // EXCEPTION THROWN HERE

正在加载的模块名称似乎是正确的(它是一个“文本!*”),但除此之外,我不知道如何继续调试此问题。我做错了什么?

我的 grunt 配置是:

/*global module, require */

module.exports = function (grunt) {
'use strict';

// library that allows config objects to be merged together
var mixIn = require('mout/object/mixIn');

var requireConfig = {
    baseUrl: 'App/',
    paths: {
        'jquery': '../Scripts/jquery-2.1.0',
        'knockout': '../Scripts/knockout-3.1.0',
        'text': '../Scripts/text',
        'durandal': '../Scripts/durandal',
        'plugins': '../Scripts/durandal/plugins',
        'transitions': '../Scripts/durandal/transitions',
    }
};

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    jshint: {
        options: {
            "-W099": true, // allowed mixed tabs and spaces
            "-W004": true, // needed to avoid errors where TS fakes inheritance
            ignores: [
                'App/main-built.js' // ingore the built/compacted file
            ]
        },
        all: [ // run jshint on these files
            'gruntfile.js',
            'App/**/*.js'
        ]
    },
    // TODO: could add jasmine here to do JS testing
    clean: {
        build: ['build/*']
    },
    copy: {
        scripts: {
            src: 'Scripts/**/**',
            dest: 'build/'
        }
    },
    durandal: {
        main: {
            src: [
                'App/**/*.*',
                '!App/main-built.js', // ignore the built file!
                'Scripts/durandal/**/*.js'
            ],
            options: {
                name: '../Scripts/almond-custom',
                baseUrl: requireConfig.baseUrl,
                mainPath: 'App/main',
                paths: mixIn(
                    {},
                    requireConfig.paths,
                    { 'almond': '../Scripts/almond-custom.js' }),
                exclude: [],
                inlineText: false, // prevent bundling of .html (since we are dynamically generating these for content)
                optimize: 'none', // turn off optimisations - uglify will run seperately by grunt to do this
                out: 'build/app/main-built.js'
            }
        }
    },
    uglify: {
        options: {
            banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> \n' +
                '* Copyright (c) <%= grunt.template.today("yyyy") %> Kieran Benton \n' +
                '*/\n'
        },
        build: {
            src: 'build/App/main.js',
            dest: 'build/App/main-built.js'
        }
    }
}
);

// loading plugin(s)
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-open');
grunt.loadNpmTasks('grunt-durandal');

// only one grunt task
grunt.registerTask('build', [
    'jshint',
    'clean',
    'copy',
    'durandal:main']);
};

最佳答案

Almond 不支持动态加载。它不实现 require.toUrl 且不支持异步加载器插件。 James Burke,RequireJS 和 Almond 的创建者,回答了同样的问题 here .

要解决此问题,您可以将 RequireJS 而不是 Almond 包含到 bundle 中。查看示例 here 。您必须在 r.js 配置的 paths 部分中为 require.js 创建别名,因为 require 是特殊的保留依赖项名称。然后,在配置的 name 字段中指定此别名,而不是 Almond。你会得到这样的结果:

options: {
    name: 'requireLib', // use the alias
    baseUrl: requireConfig.baseUrl,
    mainPath: 'App/main',
    paths: mixIn(
        {},
        requireConfig.paths,
        { 'requireLib': '../Scripts/require.js' }), // declare the alias
    exclude: [],
    inlineText: false,
    optimize: 'none',
    out: 'build/app/main-built.js'
}

关于requirejs - 使用r.js打包一个使用 'text'加载 View 的SPA应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23293185/

相关文章:

Payload 函数的 Python 随机函数不起作用

interface - TypeScript:使用 AMD 时类中的引用接口(interface)

javascript - 从下划线模板引用 require 模块内保存的枚举值

javascript - Grunt 只会创建一个文件,不会创建多个文件

angularjs - '警告 : Tried to load angular more than once. ' karma 警告

gruntjs - ionic 框架 - 命令 'grunt serve' 返回 'multiple addresses available' 并卡住

mvvm - 如何将数据(选定项目)传递给 Durandal 组合详细 View ?

DurandalJS - 为什么当用户导航时没有立即开始转换

javascript - 使用 socket.io 的单页应用程序中的特定于部分的监听器

google-maps - 当谷歌地图超时时,如何使用 RequireJS 捕获加载错误?