/
JSAsset.js
207 lines (176 loc) Β· 5.54 KB
/
JSAsset.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const {File: BabelFile} = require('babel-core');
const traverse = require('babel-traverse').default;
const codeFrame = require('babel-code-frame');
const collectDependencies = require('../visitors/dependencies');
const walk = require('babylon-walk');
const Asset = require('../Asset');
const babylon = require('babylon');
const insertGlobals = require('../visitors/globals');
const fsVisitor = require('../visitors/fs');
const envVisitor = require('../visitors/env');
const babel = require('../transforms/babel');
const generate = require('babel-generator').default;
const uglify = require('../transforms/uglify');
const SourceMap = require('../SourceMap');
const IMPORT_RE = /\b(?:import\b|export\b|require\s*\()/;
const ENV_RE = /\b(?:process\.env)\b/;
const GLOBAL_RE = /\b(?:process|__dirname|__filename|global|Buffer|define)\b/;
const FS_RE = /\breadFileSync\b/;
const SW_RE = /\bnavigator\s*\.\s*serviceWorker\s*\.\s*register\s*\(/;
const WORKER_RE = /\bnew\s*Worker\s*\(/;
class JSAsset extends Asset {
constructor(name, options) {
super(name, options);
this.type = 'js';
this.globals = new Map();
this.isAstDirty = false;
this.isES6Module = false;
this.outputCode = null;
this.cacheData.env = {};
this.sourceMap = options.rendition ? options.rendition.sourceMap : null;
}
shouldInvalidate(cacheData) {
for (let key in cacheData.env) {
if (cacheData.env[key] !== process.env[key]) {
return true;
}
}
return false;
}
mightHaveDependencies() {
return (
this.isAstDirty ||
!/.js$/.test(this.name) ||
IMPORT_RE.test(this.contents) ||
GLOBAL_RE.test(this.contents) ||
SW_RE.test(this.contents) ||
WORKER_RE.test(this.contents)
);
}
async getParserOptions() {
// Babylon options. We enable a few plugins by default.
const options = {
filename: this.name,
allowReturnOutsideFunction: true,
allowHashBang: true,
ecmaVersion: Infinity,
strictMode: false,
sourceType: 'module',
locations: true,
plugins: ['exportExtensions', 'dynamicImport']
};
// Check if there is a babel config file. If so, determine which parser plugins to enable
this.babelConfig = await babel.getConfig(this);
if (this.babelConfig) {
const file = new BabelFile(this.babelConfig);
options.plugins.push(...file.parserOpts.plugins);
}
return options;
}
async parse(code) {
const options = await this.getParserOptions();
return babylon.parse(code, options);
}
traverse(visitor) {
return traverse(this.ast, visitor, null, this);
}
traverseFast(visitor) {
return walk.simple(this.ast, visitor, this);
}
collectDependencies() {
walk.ancestor(this.ast, collectDependencies, this);
}
async pretransform() {
await babel(this);
// Inline environment variables
if (this.options.target === 'browser' && ENV_RE.test(this.contents)) {
await this.parseIfNeeded();
this.traverseFast(envVisitor);
}
}
async transform() {
if (this.options.target === 'browser') {
if (this.dependencies.has('fs') && FS_RE.test(this.contents)) {
// Check if we should ignore fs calls
// See https://github.com/defunctzombie/node-browser-resolve#skip
let pkg = await this.getPackage();
let ignore = pkg && pkg.browser && pkg.browser.fs === false;
if (!ignore) {
await this.parseIfNeeded();
this.traverse(fsVisitor);
}
}
if (GLOBAL_RE.test(this.contents)) {
await this.parseIfNeeded();
walk.ancestor(this.ast, insertGlobals, this);
}
}
if (this.isES6Module) {
await babel(this);
}
if (this.options.minify) {
await uglify(this);
}
}
async generate() {
let code;
if (this.isAstDirty) {
let opts = {
sourceMaps: this.options.sourceMaps,
sourceFileName: this.relativeName
};
let generated = generate(this.ast, opts, this.contents);
if (this.options.sourceMaps && generated.rawMappings) {
let rawMap = new SourceMap(generated.rawMappings, {
[this.relativeName]: this.contents
});
// Check if we already have a source map (e.g. from TypeScript or CoffeeScript)
// In that case, we need to map the original source map to the babel generated one.
if (this.sourceMap) {
this.sourceMap = await new SourceMap().extendSourceMap(
this.sourceMap,
rawMap
);
} else {
this.sourceMap = rawMap;
}
}
code = generated.code;
} else {
code = this.outputCode || this.contents;
}
if (this.options.sourceMaps && !this.sourceMap) {
this.sourceMap = new SourceMap().generateEmptyMap(
this.relativeName,
this.contents
);
}
if (this.globals.size > 0) {
code = Array.from(this.globals.values()).join('\n') + '\n' + code;
if (this.options.sourceMaps) {
if (!(this.sourceMap instanceof SourceMap)) {
this.sourceMap = await new SourceMap().addMap(this.sourceMap);
}
this.sourceMap.offset(this.globals.size);
}
}
return {
js: code,
map: this.sourceMap
};
}
generateErrorMessage(err) {
const loc = err.loc;
if (loc) {
err.codeFrame = codeFrame(this.contents, loc.line, loc.column + 1);
err.highlightedCodeFrame = codeFrame(
this.contents,
loc.line,
loc.column + 1,
{highlightCode: true}
);
}
return err;
}
}
module.exports = JSAsset;