andersekdahl/karma-common-js

require('text!template.html') support

Closed this issue · 3 comments

I think karma-common-js is better than karma-commonjs, especially I don't need to deal with node_modules paths and html-browserify transform is supported. In some projects, however, I want to use text!template.html format when requiring something templates for requirejs supporting or webpack text-loader supporting.

I made a solution in my project, which is just to replace text!template.html to template.html first in function replaceModuleName(match, moduleName). Then it can be resolved to correct path and transformed by html-browserify.

The following code is my changes at Line 73, in karma-common-js/index.js:

    function replaceModuleName(match, moduleName) {
      var modulePath;
      try {

        // additional code start
        var cap = moduleName.match(/^\w+!(.+)$/)
        if(cap)
          moduleName = cap[1];
        // additional code end

        if (moduleName.charAt(0) === '.') {
          modulePath = require.resolve(path.resolve(filePath, moduleName));
        } else {
          if (moduleName in browserBuiltins) {
            modulePath = browserBuiltins[moduleName];
          } else {

Hope this can give you some advices.

I've considered adding support for webpack, but that requires quite a bit of effort. karma-common-js currently only support browserify transforms. I don't want to add something like removing text! at the start of the require argument as that makes you think that karma-common-js supports webpack even if it currently doesn't.

I can't make any promises about supporting webpack, but as I use webpack for my own projects it's something I want to implement.

Ok, thanks. I just use the trick temporarily until karma-common-js can support webpack.
Need I close this issue?

Sounds good, you can always subscribe to #2 which I've created as a tracking bug for webpack support.