WearyMonkey / ngtemplate-loader

Include AngularJS templates in the Webpack bundle and preload the template cache.
MIT License
238 stars 78 forks source link

Implement proper chained loading using module.exports instead of stri… #23

Open m0ppers opened 9 years ago

m0ppers commented 9 years ago

…ng magic

I am trying to use jade templates as partials and I simply couldn't get it to run.

the jade loader is returning a function and I am picking it up, create the final template string by piping it through apply and then I want to pipe the result to ngtemplate.

This is the result:

ERROR in .!./~/apply-loader!./~/jade-loader!./test/src/test.jade
Module parse failed: /Users/mop/projects/ngtemplate-loader/index.js!/Users/mop/projects/ngtemplate-loader/node_modules/apply-loader/index.js!/Users/mop/projects/ngtemplate-loader/node_modules/jade-loader/index.js!/Users/mop/projects/ngtemplate-loader/test/src/test.jade Line 2: Unexpected token var
You may need an appropriate loader to handle this file type.
| var path = '/Users/mop/projects/ngtemplate-loader/test/src/test.jade';
| var html = var jade = require("/Users/mop/projects/ngtemplate-loader/node_modules/jade/lib/runtime.js");
| 
| module.exports = function template(locals) {

After digging into the code I stumpled upon the string magic (/module.exports/) in this module. I am a total newbie to webpack but this doesn't seem to be the correct way to do it.

I had a look here:

https://github.com/mogelbrod/apply-loader/blob/master/index.js

and this seems to be a solid way to properly chain loaders.

WearyMonkey commented 9 years ago

Yes I see how that way is better. But I don't see why you are using apply-loader in the test? Doesn't your change make it un-necessary?

m0ppers commented 9 years ago

This is related to the jade loader. It will return a function (so you can supply template variables). ng-template however will require a string. To do the conversion the apply loader is needed. Without it the ngtemplate would be the function body of the jade loader which is not really helpful :)

See https://github.com/webpack/jade-loader/issues/15

WearyMonkey commented 9 years ago

Got it thanks. Will do some testing this weekend.

e-cloud commented 8 years ago

There is not only one use case of yours. In common case, ngtemplate-loader process a string with module.exports = ...... Therefore, you can chain another loader like raw or html before ngtemplate to get it work but not change its source to poor compatibility.