-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
114 lines (104 loc) · 3.88 KB
/
index.cjs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* gulp-unassert
* Gulp plugin for unassert
* Encourages programming with assertions by providing tools to compile them away.
*
* https://github.com/unassert-js/gulp-unassert
*
* Copyright (c) 2015-2022 Takuto Wada
* Licensed under the MIT license.
* https://github.com/unassert-js/gulp-unassert/blob/master/LICENSE-MIT
*/
'use strict';
const { unassertAst } = require('unassert');
const through = require('through2');
const PluginError = require('plugin-error');
const BufferStreams = require('bufferstreams');
const acorn = require('acorn');
const escodegen = require('escodegen');
const applySourceMap = require('vinyl-sourcemaps-apply');
const convert = require('convert-source-map');
const { transfer } = require('multi-stage-sourcemap');
function mergeSourceMap (incomingSourceMap, outgoingSourceMap) {
if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) {
outgoingSourceMap = JSON.parse(outgoingSourceMap);
}
if (!incomingSourceMap) {
return outgoingSourceMap;
}
return JSON.parse(transfer({ fromSourceMap: outgoingSourceMap, toSourceMap: incomingSourceMap }));
}
function overwritePropertyIfExists (name, from, to) {
if (Object.prototype.hasOwnProperty.call(from, name)) {
to.setProperty(name, from[name]);
}
}
function applyUnassertWithSourceMap (file, encoding, opt) {
const inMap = file.sourceMap;
const code = file.contents.toString(encoding);
const ast = acorn.parse(code, { ecmaVersion: 'latest', sourceType: 'module', locations: true });
const instrumented = escodegen.generate(unassertAst(ast, opt), {
file: file.relative,
sourceMap: file.relative,
sourceMapWithCode: true
});
const outMap = convert.fromJSON(instrumented.map.toString());
overwritePropertyIfExists('sources', inMap, outMap);
overwritePropertyIfExists('sourcesContent', inMap, outMap);
overwritePropertyIfExists('sourceRoot', inMap, outMap);
let reMap;
if (inMap.mappings === '') {
// when incoming SourceMap is an initial sourceMap created by gulp-sourcemaps
applySourceMap(file, outMap.toJSON());
reMap = convert.fromObject(file.sourceMap);
} else {
reMap = convert.fromObject(mergeSourceMap(inMap, outMap.toJSON()));
}
overwritePropertyIfExists('sources', inMap, reMap);
overwritePropertyIfExists('sourcesContent', inMap, reMap);
overwritePropertyIfExists('sourceRoot', inMap, reMap);
overwritePropertyIfExists('file', inMap, reMap);
file.contents = Buffer.from(instrumented.code);
file.sourceMap = reMap.toObject();
}
function applyUnassertWithoutSourceMap (code, opt) {
const ast = acorn.parse(code, { ecmaVersion: 'latest', sourceType: 'module' });
return escodegen.generate(unassertAst(ast, opt));
}
function transform (file, encoding, opt) {
if (file.sourceMap) {
applyUnassertWithSourceMap(file, encoding, opt);
} else {
file.contents = Buffer.from(applyUnassertWithoutSourceMap(file.contents.toString(encoding), opt));
}
}
module.exports = (opt) => {
return through.obj(function (file, encoding, callback) {
encoding = encoding || 'utf8';
if (file.isNull()) {
this.push(file);
} else if (file.isBuffer()) {
try {
transform(file, encoding, opt);
this.push(file);
} catch (err) {
return callback(new PluginError('gulp-unassert', err, { showStack: true }));
}
} else if (file.isStream()) {
file.contents = file.contents.pipe(new BufferStreams((err, buf, cb) => {
if (err) {
cb(new PluginError('gulp-unassert', err, { showStack: true }));
} else {
try {
const modifiedCode = applyUnassertWithoutSourceMap(buf.toString(encoding));
cb(null, Buffer.from(modifiedCode));
} catch (innerError) {
return callback(new PluginError('gulp-unassert', innerError, { showStack: true }));
}
}
}));
this.push(file);
}
callback();
});
};