使用 r.js 打包单页应用程序,该应用程序使用 'text' 加载视图

11

我正在尝试使用grunt将一个SPA应用程序 (requirejs, durandal 2, knockout) 打包成一个单独的main-build.js文件,但是使用durandal加载视图的'text'插件出现了严重问题。

在开发环境中,我成功地使用'text'按照构建durandal应用程序的标准方式动态加载视图。不同之处在于,我需要对视图进行一些服务器端模板处理,因此它们实际上是动态生成的。

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

在我的grunt配置中,我使用了inlineText: false选项 - 我已经检查过它是否抑制了构建中的'text!*'模块。但当我运行应用程序时,我得到了以下错误信息:

Uncaught TypeError: undefined is not a function来自text.load内部的以下行:

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

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

我的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']);
};
2个回答

10

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

要解决此问题,您可以将RequireJS包含到捆绑包中,而不是使用Almond。在此处有一个示例。您需要在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'
}

有没有办法使用r.js来构建优化的包,但仍然包括完整版本的require js(而不是almond)?我不确定接下来该怎么做... - Kieran Benton
嗨Thorn,我也遇到了同样的问题,但我正在使用gulp-durandal。你能帮我解释/展示更多细节来修复这个问题吗?我对这些东西非常陌生。谢谢! - Fariz Azmi

0

只是想为这个问题添加一些额外的背景信息,因为我一直回来找它来获取指导,并最终解决了我的问题。我正在使用 Durandal 和 TypeScript 进行优化的 grunt。我在文本插件周围遇到了一些异常,例如

<div data-bind="compose: { model: someObject, view: '../../components/something/something.html' }"></div>

最终我移除了相对路径,改为执行以下操作:

<div data-bind="compose: { model: someObject, view: 'components/something/something.html' }"></div>

有了这个,我能够让所有优化后的构建都正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接