javoire / browserify-ng-html2js

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

Transformation of templates in NPM modules not possible? #30

Closed bvklingeren closed 8 years ago

bvklingeren commented 8 years ago

We are creating a shared Angular library as an NPM module containing angular directives with templates and it seems impossible to have those templates transformed in a separate Angular app with a dependency on that module. I get the same error as when we don't use browserify-ng-html2js (which enables the requiring of html templates):

/project/node_modules/ng-web-components/js/components/atomic/enrichment-level/enrichment-level.html:1
<div></div>
^
ParseError: Unexpected token

Example with simplified HTML in the template. When I copy the directory with a single directive outside of node_modules and change the require to the path of that directive, the transformation works as designed. Is this bug or a feature? Or am I doing something wrong?

Browserify command in gulpfile:

  var entry = "app.js";
  var bundler = browserify({
    entries: entry,
    debug: true
  });
  return bundler
      .transform(ngHtml2Js({
        module: 'templates', // optional module name
        extension: 'html', // optionally specify what file types to look for
        requireAngular: true
      }))
      .transform(ngAnnotate)
      .bundle()
      .on('error', browserifyError)
      .pipe(source('app.bundle.js'))
      .pipe(gulp.dest('./dist'));

  function browserifyError(err) {
    console.log(err);
  }
bvklingeren commented 8 years ago

Found the solution for my problem. Typical RTFM case: normally browserify doesn't allow transforms on modules in node_modules, except when you define in the module that transforms are to be done. See http://stackoverflow.com/questions/16110750/how-to-perform-a-transform-on-npm-module-using-browserify for the fix.