/
getAppendScripts.js
157 lines (143 loc) Β· 4.17 KB
/
getAppendScripts.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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall react_native
*/
'use strict';
import type {Module} from '../DeltaBundler';
import CountingSet from './CountingSet';
const getInlineSourceMappingURL = require('../DeltaBundler/Serializers/helpers/getInlineSourceMappingURL');
const sourceMapString = require('../DeltaBundler/Serializers/sourceMapString');
const countLines = require('./countLines');
const nullthrows = require('nullthrows');
const path = require('path');
type Options<T: number | string> = {
+asyncRequireModulePath: string,
+createModuleId: string => T,
+getRunModuleStatement: T => string,
+inlineSourceMap: ?boolean,
+projectRoot: string,
+serverRoot: string,
+runBeforeMainModule: $ReadOnlyArray<string>,
+runModule: boolean,
+sourceMapUrl: ?string,
+sourceUrl: ?string,
...
};
function getAppendScripts<T: number | string>(
entryPoint: string,
modules: $ReadOnlyArray<Module<>>,
importBundleNames: $ReadOnlySet<string>,
options: Options<T>,
): $ReadOnlyArray<Module<>> {
const output = [];
if (importBundleNames.size) {
const importBundleNamesObject = Object.create(null);
importBundleNames.forEach(absolutePath => {
const bundlePath = path.relative(options.serverRoot, absolutePath);
// $FlowFixMe[prop-missing]
importBundleNamesObject[options.createModuleId(absolutePath)] =
bundlePath.slice(0, -path.extname(bundlePath).length);
});
const code = `(function(){var $$=${options.getRunModuleStatement(
options.createModuleId(options.asyncRequireModulePath),
)}$$.addImportBundleNames(${String(
JSON.stringify(importBundleNamesObject),
)})})();`;
output.push({
path: '$$importBundleNames',
dependencies: new Map(),
getSource: (): Buffer => Buffer.from(''),
inverseDependencies: new CountingSet(),
output: [
{
type: 'js/script/virtual',
data: {
code,
lineCount: countLines(code),
map: [],
},
},
],
});
}
if (options.runModule) {
const paths = [...options.runBeforeMainModule, entryPoint];
for (const path of paths) {
if (modules.some((module: Module<>) => module.path === path)) {
const code = options.getRunModuleStatement(
options.createModuleId(path),
);
output.push({
path: `require-${path}`,
dependencies: new Map(),
getSource: (): Buffer => Buffer.from(''),
inverseDependencies: new CountingSet(),
output: [
{
type: 'js/script/virtual',
data: {
code,
lineCount: countLines(code),
map: [],
},
},
],
});
}
}
}
if (options.inlineSourceMap || options.sourceMapUrl) {
const sourceMappingURL = options.inlineSourceMap
? getInlineSourceMappingURL(
sourceMapString(modules, {
processModuleFilter: (): boolean => true,
excludeSource: false,
}),
)
: nullthrows(options.sourceMapUrl);
const code = `//# sourceMappingURL=${sourceMappingURL}`;
output.push({
path: 'source-map',
dependencies: new Map(),
getSource: (): Buffer => Buffer.from(''),
inverseDependencies: new CountingSet(),
output: [
{
type: 'js/script/virtual',
data: {
code,
lineCount: countLines(code),
map: [],
},
},
],
});
}
if (options.sourceUrl) {
const code = `//# sourceURL=${options.sourceUrl}`;
output.push({
path: 'source-url',
dependencies: new Map(),
getSource: (): Buffer => Buffer.from(''),
inverseDependencies: new CountingSet(),
output: [
{
type: 'js/script/virtual',
data: {
code,
lineCount: countLines(code),
map: [],
},
},
],
});
}
return output;
}
module.exports = getAppendScripts;