-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
bundled-source.js
173 lines (147 loc) · 4.66 KB
/
bundled-source.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
"use strict";
const path = require('path');
const amodroTrace = require('./amodro-trace');
const cjsTransform = require('./amodro-trace/read/cjs');
const allWriteTransforms = require('./amodro-trace/write/all');
const fs = require('../file-system');
exports.BundledSource = class {
constructor(bundler, file) {
this.bundler = bundler;
this.file = file;
this.includedIn = null;
this.includedBy = null;
this.moduleId = null;
this._contents = null;
this._deps = null;
this.requiresTransform = true;
}
get sourceMap() {
return this.file.sourceMap;
}
get path() {
return this.file.path;
}
set deps(deps) {
this._deps = deps || [];
}
get deps() {
return this._deps;
}
set contents(value) {
this._contents = value;
}
get contents() {
return this._contents === null
? (this._contents = this.file.contents.toString())
: this._contents;
}
update(file) {
this.file = file;
this._contents = null;
this.requiresTransform = true;
this.includedIn.requiresBuild = true;
}
transform() {
if (!this.requiresTransform) {
return Promise.resolve();
}
let bundler = this.bundler;
let loaderConfig = bundler.loaderConfig;
let loaderPlugins = bundler.loaderOptions.plugins;
let rootDir = process.cwd();
let moduleId = this.moduleId || (this.moduleId = this.calculateModuleId(rootDir, loaderConfig));
let that = this;
let modulePath = this.path;
console.log(`Tracing ${moduleId}...`);
for(let i = 0, ii = loaderPlugins.length; i < ii; ++i) {
let current = loaderPlugins[i];
if (current.matches(this.path)) {
return current.transform(moduleId, this.path, this.contents).then(contents => {
this.contents = contents;
this.requiresTransform = false;
});
}
}
return amodroTrace(
{
rootDir: rootDir,
id: moduleId,
traced: bundler.getTraced(),
fileExists: function(defaultExists, id, filePath) {
// always force amodro to use our fileRead func.
// this bypasses amodro bug https://github.com/amodrojs/amodro-trace/issues/6
return true;
},
fileRead: function(defaultRead, id, filePath) {
let location = calculateFileName(filePath);
let found = bundler.getItemByPath(location);
if (found) {
// ensure to use contents before stub
return found.contentsBeforeStub || found.contents;
} else {
let contents = '';
try {
contents = fs.readFileSync(location).toString();
bundler.addFile({
path: location,
contents: contents
}, that.includedBy);
} catch(e) {
console.log(`File not found or not accessible: ${location}. Requested by ${modulePath}`);
}
return contents;
}
},
includeContents: true,
readTransform: function(id, url, contents) {
return cjsTransform(url, contents);
},
writeTransform: allWriteTransforms({
stubModules: loaderConfig.stubModules,
wrapShim: loaderConfig.wrapShim
})
},
loaderConfig
).then(traceResult => {
let traced = traceResult.traced;
for (let i = 0, ii = traced.length; i < ii; ++i) {
let result = traceResult.traced[i];
let item = bundler.getItemByPath(result.path);
if (item) {
item.deps = result.deps;
if (item.requiresTransform) {
if (loaderConfig.stubModules.indexOf(result.id) >= 0) {
// retain contents before stub
item.contentsBeforeStub = item.contents;
}
item.contents = result.contents;
item.requiresTransform = false;
if (!item.moduleId) {
item.moduleId = result.id;
}
}
} else if (this.includedBy.traceDependencies) {
let newItem = bundler.addFile({
path: result.path,
contents: result.contents
}, this.includedBy);
newItem.deps = result.deps;
}
}
});
}
calculateModuleId(rootDir, loaderConfig) {
if (this.file.description) {
return this.file.description.name;
}
let baseUrl = loaderConfig.baseUrl;
let modulePath = path.relative(baseUrl, this.path);
return path.normalize(modulePath.replace(path.extname(modulePath), '')).replace(/\\/g,'\/');
}
}
function calculateFileName(filePath) {
if (filePath.indexOf('.') === -1) {
return filePath + '.js';
}
return filePath;
}