javoire / browserify-ng-html2js

Browserify transform to compile html templates into angular template modules
MIT License
27 stars 17 forks source link

Example of angular code #11

Closed lambert-velir closed 8 years ago

lambert-velir commented 9 years ago

Hi-

I'm trying to pipe my index.js file though this transform to add my templates to the $templateCache. Is this transform supposed to parse out any templateUrl declarations? In my index.js, I have:

// index.js
app.config(function($routeProvider){
    $routeProvider
        .when("/bar", {
            templateUrl: "bar/bar.html", 
            controller: require("./bar/barCtrl.js")
        });
});

My gulp task looks like this:

// gulp task
browserify("index.js")
    .transform(ngHtml2Js({
      module: "templates"
    }))
    .bundle()
    .pipe(source("bundle-index.js"))
    .pipe(gulp.dest("/js-generated/"));

Is this transform supposed to recognize bar/bar.html and add it to my bundle?

mchambaud commented 9 years ago

@lambert-velir try requiring your template.

// index.js
require('bar/bar.html');

app.config(function($routeProvider){
    $routeProvider
        .when("/bar", {
            templateUrl: "bar/bar.html", 
            controller: require("./bar/barCtrl.js")
        });
});
lambert-velir commented 9 years ago

Cool, require("./bar/bar.html"); works, thanks!

@javoire, you might want to consider adding this to the documentation. This wasn't apparent to me.

javoire commented 9 years ago

Hmm yeah. I'm thinking though that the very concept behind browserify is to require() the files you need in your final js output, i.e. all the files that will be sent and processed through browserfys pipeline. So if a html needs to be processed by browserify and compiled to an angular template it needs to be required :) But maybe it won't hurt to show that in an example. I'll give it some thoughts. Cheers

mchambaud commented 9 years ago

@javoire it would be nice if we did not need to require the templates!

javoire commented 9 years ago

True. But that's how browserify works! You require all the source files you want to be a part of your bundle. How would browserify know where your templates are if you don't require them? ^^

mchambaud commented 9 years ago

It would be quite interesting if a node script could look for templateUrl: "path/to/template.html" and inject a require tag before running browserify.

Hopefully it wouldn't be too costly to run.

javoire commented 9 years ago

Yeah I guess that could make sense ^^

fnhipster commented 9 years ago

Hey guys, maybe I'm doing something but I'm using the require and it is working. However, my karma testing are failing with SyntaxError: Unexpected token (1:0) while parsing file:...

I already installed karma-ng-html2js-preprocessor and followed its documentation. The issue is that Karma is getting that require('file.html') and failing. Any hints?

javoire commented 9 years ago

Hmm, well karma would have to use browserify somehow also, is it?

javoire commented 9 years ago

What you can do otherwise, is to bundle (browserify) the code before running karma, so that karma will take the finished bundle.js (or whatever you've named you output file) as it's input in karma.conf.json

fnhipster commented 9 years ago

Hi Jonatan, to bundle the file previous to running Karma is not an option due to how the application and the tests have been set up in the past. We are using browserify as a preprocessor in Karma. I'm currently investigation how can I add ng-html2js as a transform in browserify options in Karma

browserify: {
      debug: true,
      transform: [ 'ng-html2j' ]
    }
toru-sean commented 8 years ago

i agree that it would be better if the documentation said that you need to require the templates in your code. I only started using browserify today so it wasn't at all obvious to me. Great plugin though!

javoire commented 8 years ago

agreed :) re-opening

arif-dewi commented 8 years ago

Please, man! Add this to the documentation - I spent 2 hours until got this.

javoire commented 8 years ago

closed in #37