1 | "use strict";
|
2 |
|
3 | const Asset = require('../Asset');
|
4 |
|
5 | const path = require('path');
|
6 |
|
7 | const json5 = require('json5');
|
8 |
|
9 | const _require = require('terser'),
|
10 | minify = _require.minify;
|
11 |
|
12 | class JSONAsset extends Asset {
|
13 | constructor(name, options) {
|
14 | super(name, options);
|
15 | this.type = 'js';
|
16 | }
|
17 |
|
18 | parse(code) {
|
19 | return path.extname(this.name) === '.json5' ? json5.parse(code) : null;
|
20 | }
|
21 |
|
22 | generate() {
|
23 | let code = `module.exports = ${this.ast ? JSON.stringify(this.ast, null, 2) : this.contents};`;
|
24 |
|
25 | if (this.options.minify && !this.options.scopeHoist) {
|
26 | let minified = minify(code);
|
27 |
|
28 | if (minified.error) {
|
29 | throw minified.error;
|
30 | }
|
31 |
|
32 | code = minified.code;
|
33 | }
|
34 |
|
35 | return code;
|
36 | }
|
37 |
|
38 | }
|
39 |
|
40 | module.exports = JSONAsset; |
\ | No newline at end of file |