Skip to content

Commit

Permalink
Merge pull request #5 from jantimon/2.x
Browse files Browse the repository at this point in the history
Add support for `webpack --optimize-minimize` and use relative filepaths
  • Loading branch information
bazilio91 committed Jun 15, 2015
2 parents 270200e + 6efcc2e commit 5b5899c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 5b5899c

Please sign in to comment.