如何配置browserfy与Karma使用转换Stringify为Mustache模板

How to configure browserify with Karma to use transform Stringify for Mustache templates?

本文关键字:转换 Stringify 模板 Mustache Karma 何配置 配置 browserfy      更新时间:2023-09-26

我有一个项目,我正在使用browserfy来管理我的依赖项。我使用Backbone和Mustache作为客户端的模板引擎。为了需要Mustache模板,我必须使用Stringify,如下所示,以正确地需要Mustacche模板:

gulp.task('scripts', function() {
    gulp.src(['./js/app.js'])
        .pipe(browserify({
          transform: stringify({
            extensions: ['.html'], minify: true
          })
        }))
        .pipe(gulp.dest('dist/assets/js')) });

现在,我的测试正在使用browserfy在Karma上运行。然而,当我试图要求模板Karma失败时,会抛出HTML文件解析错误。

例如:

var Template = require('../Templates/CodeEditor.html'); var CodeEditor
= Backbone.View.extend({
    el: '#CodeEdit',
    editor: {},     currentPreviewMode: "default",
    template: function() {
        return Mustache.to_html(Template, this.model.toJSON());
} });

有可能将Karma配置为使用Stringify吗?如果是,怎么办?请帮忙。

下面是我的卡拉配置。

module.exports = function(config) {   config.set({
    basePath: './js/',
    frameworks: ['browserify', 'jasmine'],
    browserify: {
      debug: true,
      transform: [ 'brfs', 'browserify-shim']
    },
    files: [
        '**/**/*.js'
    ],
    exclude: [],
    preprocessors: {
      'modules/**/*.js': [ 'browserify' ]
    },
    reporters: ['dots'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
     autoWatch: true,
    browsers: [ 'PhantomJS' ],
    singleRun: false   }); };

和包.json的一部分供参考:

  "devDependencies": {
    "brfs": "^1.4.0",
    "browserify": "^9.0.3",
    "browserify-shim": "^3.8.3",
    "chai": "^2.2.0",
    "del": "^1.1.1",
    "gulp": "^3.8.11",
    "gulp-autoprefixer": "^2.1.0",
    "gulp-browserify": "^0.5.1",
    "jasmine": "^2.2.1",
    "jasmine-core": "^2.2.0",
    "karma": "^0.12.31",
    "karma-browserify": "^4.1.2",
    "karma-chrome-launcher": "^0.1.7",
    "karma-phantomjs-launcher": "^0.1.4",
    "karma-jasmine": "^0.3.5",
    "map-stream": "0.0.5",
    "stringify": "^3.1.0"

  },
  "browserify-shim": {
    "jQuery": "global:jQuery",
    "Backbone": "global:Backbone"
  }

感谢

看看这个。可能就是你想要的。

只需添加

browserify: {
    transform: ['stringify']
},

到您的karma.conf.js(同时确保将stringify安装为节点模块)

https://gist.github.com/busypeoples/e4ec7e7c1f1a753050dd

更新:你也可以像一样为转换本身添加选项

transform: [
    ['stringify', {extensions: ['.java'], minify: true}]
]

https://github.com/Nikku/karma-browserify#transforms