Skip to content

Commit fa0fa0a

Browse files
committed
v3
1 parent 6fea14b commit fa0fa0a

File tree

5 files changed

+4247
-63
lines changed

5 files changed

+4247
-63
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ To use [EJS by tj](https://github.com/tj/ejs) use 1.x branch and 1.x.x versions.
1313
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
1414

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

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

2929
Following options can be specified in query:
3030

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

3333
`compileDebug` — see ejs compileDebug option
3434

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

3939
```javascript
4040
module: {
41-
loaders: [
42-
{test: /\.ejs$/, loader: 'ejs-compiled?htmlmin'} // enable here
43-
]
44-
},
45-
'ejs-compiled-loader': {
46-
'htmlmin': true, // or enable here
47-
'htmlminOptions': {
48-
removeComments: true
49-
}
41+
rules: [{
42+
test: /\.ejs$/,
43+
use: {
44+
loader: 'ejs-compiled-loader',
45+
options: {
46+
htmlmin: true,
47+
htmlminOptions: {
48+
removeComments: true
49+
}
50+
}
51+
}
52+
}]
5053
}
5154
```
5255

index.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1-
var ejs = require('ejs'),
2-
UglifyJS = require('uglify-js'),
3-
utils = require('loader-utils'),
4-
path = require('path'),
5-
htmlmin = require('html-minifier'),
6-
merge = require('merge');
7-
1+
var ejs = require('ejs');
2+
var path = require('path');
3+
var merge = require('merge');
4+
var utils = require('loader-utils');
5+
var terser = require('terser');
6+
var htmlmin = require('html-minifier');
87

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

12-
var query = typeof this.query === 'object' ? this.query : utils.parseQuery(this.query);
13-
var opts = merge(this.options['ejs-compiled-loader'] || {}, query);
14-
opts.client = true;
15-
16-
// Skip compile debug for production when running with
17-
// webpack --optimize-minimize
18-
if (this.minimize && opts.compileDebug === undefined) {
19-
opts.compileDebug = false;
20-
}
21-
22-
// Use filenames relative to working dir, which should be project root
23-
opts.filename = path.relative(process.cwd(), this.resourcePath);
24-
25-
if (opts.htmlmin) {
26-
source = htmlmin.minify(source, opts['htmlminOptions'] || {});
27-
}
28-
29-
var template = ejs.compile(source, opts);
30-
31-
// Beautify javascript code
32-
if (!this.minimize && opts.beautify !== false) {
33-
var ast = UglifyJS.parse(template.toString());
34-
ast.figure_out_scope();
35-
template = ast.print_to_string({beautify: true});
36-
}
11+
// wepkack3: options
12+
var options = (this.hasOwnProperty("options") && (typeof this.options['ejs-compiled-loader'] === 'object')) ? this.options['ejs-compiled-loader'] : {};
13+
14+
// webpack4: query
15+
var query = (this.hasOwnProperty("query")) ? (typeof this.query === 'object') ? this.query : utils.parseQuery(this.query) : {};
16+
17+
// merge opts from defaults,opts and query
18+
var opts = merge({
19+
client: true,
20+
compileDebug: !!this.minimize,
21+
minimize: (typeof this.minimize === 'boolean') ? this.minimize : false,
22+
beautify: false,
23+
htmlmin: (typeof this.htmlmin === 'boolean') ? this.htmlmin : false,
24+
htmlminOptions: {}
25+
}, options, query);
26+
27+
// minify html
28+
if (opts.htmlmin) source = htmlmin.minify(source, opts.htmlminOptions);
29+
30+
// compile template
31+
var template = ejs.compile(source, merge(opts, {
32+
filename: path.relative(process.cwd(), this.resourcePath),
33+
webpack: this
34+
})).toString();
35+
36+
// minify js with terser
37+
if (opts.minimize) template = terser.minify(template, { output: { beautify: opts.beautify }}).code;
3738

3839
return 'module.exports = ' + template;
39-
};
40+
41+
};

0 commit comments

Comments
 (0)