Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
yetzt committed Apr 22, 2020
1 parent 6fea14b commit fa0fa0a
Show file tree
Hide file tree
Showing 5 changed files with 4,247 additions and 63 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To use [EJS by tj](https://github.com/tj/ejs) use 1.x branch and 1.x.x versions.
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)

``` javascript
var template = require("ejs-compiled!./file.ejs");
var template = require("ejs-compiled-loader!./file.ejs");
// => returns the template function compiled with ejs templating engine.

// And then use it somewhere in your code
Expand All @@ -28,7 +28,7 @@ template(data) // Pass object with data

Following options can be specified in query:

`beautify` — enable or disable uglify-js beautify of template ast
`beautify` — enable or disable terser beautify of template ast

`compileDebug` — see ejs compileDebug option

Expand All @@ -38,15 +38,18 @@ Following options can be specified in query:

```javascript
module: {
loaders: [
{test: /\.ejs$/, loader: 'ejs-compiled?htmlmin'} // enable here
]
},
'ejs-compiled-loader': {
'htmlmin': true, // or enable here
'htmlminOptions': {
removeComments: true
}
rules: [{
test: /\.ejs$/,
use: {
loader: 'ejs-compiled-loader',
options: {
htmlmin: true,
htmlminOptions: {
removeComments: true
}
}
}
}]
}
```

Expand Down
68 changes: 35 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
var ejs = require('ejs'),
UglifyJS = require('uglify-js'),
utils = require('loader-utils'),
path = require('path'),
htmlmin = require('html-minifier'),
merge = require('merge');

var ejs = require('ejs');
var path = require('path');
var merge = require('merge');
var utils = require('loader-utils');
var terser = require('terser');
var htmlmin = require('html-minifier');

module.exports = function (source) {
this.cacheable && this.cacheable();

var query = typeof this.query === 'object' ? this.query : utils.parseQuery(this.query);
var opts = merge(this.options['ejs-compiled-loader'] || {}, query);
opts.client = true;

// Skip compile debug for production when running with
// webpack --optimize-minimize
if (this.minimize && opts.compileDebug === undefined) {
opts.compileDebug = false;
}

// Use filenames relative to working dir, which should be project root
opts.filename = path.relative(process.cwd(), this.resourcePath);

if (opts.htmlmin) {
source = htmlmin.minify(source, opts['htmlminOptions'] || {});
}

var template = ejs.compile(source, opts);

// Beautify javascript code
if (!this.minimize && opts.beautify !== false) {
var ast = UglifyJS.parse(template.toString());
ast.figure_out_scope();
template = ast.print_to_string({beautify: true});
}
// wepkack3: options
var options = (this.hasOwnProperty("options") && (typeof this.options['ejs-compiled-loader'] === 'object')) ? this.options['ejs-compiled-loader'] : {};

// webpack4: query
var query = (this.hasOwnProperty("query")) ? (typeof this.query === 'object') ? this.query : utils.parseQuery(this.query) : {};

// merge opts from defaults,opts and query
var opts = merge({
client: true,
compileDebug: !!this.minimize,
minimize: (typeof this.minimize === 'boolean') ? this.minimize : false,
beautify: false,
htmlmin: (typeof this.htmlmin === 'boolean') ? this.htmlmin : false,
htmlminOptions: {}
}, options, query);

// minify html
if (opts.htmlmin) source = htmlmin.minify(source, opts.htmlminOptions);

// compile template
var template = ejs.compile(source, merge(opts, {
filename: path.relative(process.cwd(), this.resourcePath),
webpack: this
})).toString();

// minify js with terser
if (opts.minimize) template = terser.minify(template, { output: { beautify: opts.beautify }}).code;

return 'module.exports = ' + template;
};

};
Loading

0 comments on commit fa0fa0a

Please sign in to comment.