【问题标题】:change requireJS from adding the .js file extension automatically on intern.js将 requireJS 更改为在 intern.js 上自动添加 .js 文件扩展名
【发布时间】:2017-01-04 16:26:08
【问题描述】:

目前我正在为 intern.js 编写自定义 html 报告器。我使用的模板引擎是 marko.js。 marko.js 有带有“.marko”的扩展文件供我输入我的 html 语法 该文件在正常的node.js(common.js)中正确生成

当我将相同的代码集成到 intern.js 时出现问题。当我这样做时,internjs 使用的 requirejs(AMD) 会自动将 .js 文件扩展名添加到我的 marko 扩展名中

var template = require('./hello-world.marko');

这使得文件变为hello-world.marko.js,这导致markojs中的代码中断

自定义 html 报告器代码如下

define(function (require) {

    // require('intern/dojo/node!marko/node-require').install();
    var fs = require('intern/dojo/node!fs');

    var template = require('./hello-world.marko');
    console.log(template);
    function JsonReporter(config) {
        config = config || {};
        this.output = config.output;
    }

    JsonReporter.prototype = {
        runEnd(executor) {
            // console.log("toJson: " + JSON.stringify(executor.suites))
            data = JSON.stringify(executor.suites);
            template.renderToString(data,
                function (err, output) {
                    console.log(output);
                    fs.writeFile('result.html', output, function (err) {
                        if (err) return console.log(err);
                        console.log('Save done');
                    });
                });
        },
    }
    return JsonReporter;

})

【问题讨论】:

    标签: requirejs amd intern marko


    【解决方案1】:

    require 函数并不真正用于在 Node 的加载器或 AMD 加载器中加载任意文本资源。在 Node 中,无论你是否运行 Intern,都可以使用 fs.readFilefs.readFileSync。在 Intern 的基于 Dojo 的 AMD 环境中,您还可以使用 dojo/text 加载程序插件,如下所示:

    var template = require('dojo/text!./hello-world.marko');
    

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 2013-02-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多