UNPKG

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