-
Notifications
You must be signed in to change notification settings - Fork 1
/
lasso-minify-transpile-inline-plugin.js
60 lines (50 loc) · 1.82 KB
/
lasso-minify-transpile-inline-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const fs = require('fs');
const { promisify } = require('util');
const babel = require('babel-core');
const { get } = require('./get');
const readFileAsync = promisify(fs.readFile);
const DEPENDENCY_TYPE = 'minify-transpile-inline';
const DEPENDENCY_PROPS = {
// Declare which properties can be passed to the dependency type
properties: {
'path': 'string',
'inline': true,
'type': 'string'
},
// Validation checks and initialization based on properties:
async init(/* context */) {
if (!this.path) {
throw new Error('"path" property is required');
}
if (!this.inline) {
throw new Error('"inline" property is required');
}
if (!this.type || this.type !== DEPENDENCY_TYPE) {
throw new Error('"type" property is required');
}
// NOTE: resolvePath can be used to resolve a provided relative path to a full path
this.path = this.resolvePath(this.path);
},
// Read the resource:
async read(/* context */) {
let src = await readFileAsync(this.path, { encoding: 'utf8' }) || '';
if (src) {
// filename is needed here until we bump to @babel/babel-core ^7
src = babel.transform(src, {
minified: true,
comments: false,
filename: this.path.substr(this.path.lastIndexOf('/') + 1)
});
}
return get(src, 'code', '');
},
// getSourceFile is optional and is only used to determine the last modified time
// stamp and to give the output file a reasonable name when bundling is disabled
getSourceFile: function() {
return this.path;
}
};
module.exports = (lasso) => {
lasso.dependencies.registerJavaScriptType(DEPENDENCY_TYPE, DEPENDENCY_PROPS);
};