diff --git a/README.md b/README.md index 9e74a28..214acdd 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # ejs-compiled-loader for webpack -EJS loader for [webpack](http://webpack.github.io/). Uses [ejs](https://github.com/tj/ejs) function to compile templates. +EJS loader for [webpack](http://webpack.github.io/). Uses [ejs](https://github.com/mde/ejs) function to compile templates. -To use EJS by tj use 1.x branch and 1.x.x versions. +To use [EJS by tj](https://github.com/tj/ejs) use 1.x branch and 1.x.x versions. ## Installation diff --git a/index.js b/index.js index 6cdbd8b..ff4c82f 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,30 @@ var ejs = require('ejs'), uglify = require('uglify-js'), - utils = require("loader-utils"); + utils = require('loader-utils'), + path = require('path'); module.exports = function (source) { this.cacheable && this.cacheable(); var opts = utils.parseQuery(this.query); opts.client = true; - opts.filename = this.resourcePath; + + // Skip compile debug for production when running with + // webpack --optimize-minimize + if (this.minimize && opts.compileDebug === undefined) { + opts.compileDebug = false; + } + + // Use filenames relative to the context (in most cases the project root) + opts.filename = path.relative(this.context, this.resourcePath); + var template = ejs.compile(source, opts); - var ast = uglify.parser.parse(template.toString()); + // Beautify javascript code + if (!this.minimize && opts.beautify !== false) { + var ast = uglify.parser.parse(template.toString()); + template = uglify.uglify.gen_code(ast, {beautify: true}); + } - return 'module.exports = ' + uglify.uglify.gen_code(ast, {beautify: true}); + return 'module.exports = ' + template; };