@atlaspack/transformer-typescript-types 2.14.0 → 2.14.1-canary.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/lib/TSModule.js +53 -0
- package/lib/TSModuleGraph.js +252 -0
- package/lib/TSTypesTransformer.js +205 -0
- package/lib/collect.js +128 -0
- package/lib/shake.js +226 -0
- package/lib/utils.js +30 -0
- package/lib/wrappers.js +72 -0
- package/package.json +8 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright (c) 2024 Atlassian US., Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/lib/TSModule.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TSModule = void 0;
|
|
7
|
+
class TSModule {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.imports = new Map();
|
|
10
|
+
this.exports = [];
|
|
11
|
+
this.bindings = new Map();
|
|
12
|
+
this.names = new Map();
|
|
13
|
+
this.used = new Set();
|
|
14
|
+
}
|
|
15
|
+
addImport(local, specifier, imported) {
|
|
16
|
+
this.imports.set(local, {
|
|
17
|
+
specifier,
|
|
18
|
+
imported
|
|
19
|
+
});
|
|
20
|
+
if (imported !== '*' && imported !== 'default') {
|
|
21
|
+
this.names.set(local, local);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// if not a reexport: imported = local, name = exported
|
|
26
|
+
addExport(name, imported, specifier) {
|
|
27
|
+
this.exports.push({
|
|
28
|
+
name,
|
|
29
|
+
specifier,
|
|
30
|
+
imported
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
addWildcardExport(specifier) {
|
|
34
|
+
this.exports.push({
|
|
35
|
+
specifier
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
addLocal(name, node) {
|
|
39
|
+
const bindings = this.bindings.get(name) ?? new Set();
|
|
40
|
+
bindings.add(node);
|
|
41
|
+
this.bindings.set(name, bindings);
|
|
42
|
+
if (name !== 'default') {
|
|
43
|
+
this.names.set(name, name);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
getName(name) {
|
|
47
|
+
return this.names.get(name) || name;
|
|
48
|
+
}
|
|
49
|
+
hasBinding(name) {
|
|
50
|
+
return this.bindings.has(name);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.TSModule = TSModule;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TSModuleGraph = void 0;
|
|
7
|
+
function _nullthrows() {
|
|
8
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
9
|
+
_nullthrows = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _assert() {
|
|
15
|
+
const data = _interopRequireDefault(require("assert"));
|
|
16
|
+
_assert = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _typescript() {
|
|
22
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
23
|
+
_typescript = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
+
class TSModuleGraph {
|
|
30
|
+
constructor(mainModuleName) {
|
|
31
|
+
this.modules = new Map();
|
|
32
|
+
this.mainModuleName = mainModuleName;
|
|
33
|
+
this.mainModule = null;
|
|
34
|
+
this.syntheticImportCount = 0;
|
|
35
|
+
}
|
|
36
|
+
addModule(name, module) {
|
|
37
|
+
this.modules.set(name, module);
|
|
38
|
+
if (name === this.mainModuleName) {
|
|
39
|
+
this.mainModule = module;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getModule(name) {
|
|
43
|
+
return this.modules.get(name);
|
|
44
|
+
}
|
|
45
|
+
markUsed(module, name, context) {
|
|
46
|
+
// If name is imported, mark used in the original module
|
|
47
|
+
if (module.imports.has(name)) {
|
|
48
|
+
module.used.add(name);
|
|
49
|
+
let resolved = this.resolveImport(module, name);
|
|
50
|
+
// Missing or external
|
|
51
|
+
if (!resolved || resolved.module === module) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
return this.markUsed(resolved.module, resolved.imported, context);
|
|
55
|
+
}
|
|
56
|
+
if (module.used.has(name)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
module.used.add(name);
|
|
60
|
+
|
|
61
|
+
// Visit all child nodes of the original binding and mark any referenced types as used.
|
|
62
|
+
let visit = node => {
|
|
63
|
+
if (_typescript().default.isQualifiedName(node) && _typescript().default.isIdentifier(node.left)) {
|
|
64
|
+
let resolved = this.resolveImport(module, node.left.text, node.right.text);
|
|
65
|
+
if (resolved) {
|
|
66
|
+
this.markUsed(resolved.module, resolved.imported, context);
|
|
67
|
+
}
|
|
68
|
+
} else if (_typescript().default.isIdentifier(node)) {
|
|
69
|
+
this.markUsed(module, node.text, context);
|
|
70
|
+
}
|
|
71
|
+
return _typescript().default.visitEachChild(node, visit, context);
|
|
72
|
+
};
|
|
73
|
+
let bindings = module.bindings.get(name);
|
|
74
|
+
if (bindings) {
|
|
75
|
+
for (let node of bindings) {
|
|
76
|
+
_typescript().default.visitEachChild(node, visit, context);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
getExport(m, e) {
|
|
81
|
+
(0, _assert().default)(e.name != null);
|
|
82
|
+
let exportName = e.name;
|
|
83
|
+
|
|
84
|
+
// Re-export
|
|
85
|
+
if (e.specifier && e.imported) {
|
|
86
|
+
let m = this.getModule(e.specifier);
|
|
87
|
+
if (!m) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
let exp = this.resolveExport(m, e.imported);
|
|
91
|
+
if (!exp) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
module: exp.module,
|
|
96
|
+
imported: exp.imported || exp.name,
|
|
97
|
+
name: exportName
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Import and then export
|
|
102
|
+
if (m.imports.has(exportName)) {
|
|
103
|
+
let imp = this.resolveImport(m, exportName);
|
|
104
|
+
if (!imp) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
module: imp.module,
|
|
109
|
+
imported: imp.name,
|
|
110
|
+
name: exportName
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Named export
|
|
115
|
+
return {
|
|
116
|
+
module: m,
|
|
117
|
+
name: exportName,
|
|
118
|
+
imported: e.imported != null ? m.getName(e.imported) : exportName
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
resolveImport(module, local, imported) {
|
|
122
|
+
let i = module.imports.get(local);
|
|
123
|
+
if (!i) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
let m = this.getModule(i.specifier);
|
|
127
|
+
if (!m) {
|
|
128
|
+
// External module. pass through the import.
|
|
129
|
+
return {
|
|
130
|
+
module,
|
|
131
|
+
name: local,
|
|
132
|
+
imported: imported || i.imported
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return this.resolveExport(m, imported || i.imported);
|
|
136
|
+
}
|
|
137
|
+
resolveExport(module, name) {
|
|
138
|
+
for (let e of module.exports) {
|
|
139
|
+
if (e.name === name) {
|
|
140
|
+
return this.getExport(module, e);
|
|
141
|
+
} else if (e.specifier) {
|
|
142
|
+
const m = this.resolveExport((0, _nullthrows().default)(this.getModule(e.specifier)), name);
|
|
143
|
+
if (m) {
|
|
144
|
+
return m;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
getAllExports(module = (0, _nullthrows().default)(this.mainModule), excludeDefault = false) {
|
|
150
|
+
let res = [];
|
|
151
|
+
for (let e of module.exports) {
|
|
152
|
+
if (e.name && (!excludeDefault || e.name !== 'default')) {
|
|
153
|
+
let exp = this.getExport(module, e);
|
|
154
|
+
if (exp) {
|
|
155
|
+
res.push(exp);
|
|
156
|
+
}
|
|
157
|
+
} else if (e.specifier) {
|
|
158
|
+
let m = this.getModule(e.specifier);
|
|
159
|
+
if (m) {
|
|
160
|
+
res.push(...this.getAllExports(m, true));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return res;
|
|
165
|
+
}
|
|
166
|
+
getAllImports() {
|
|
167
|
+
// Build a map of all imports for external modules
|
|
168
|
+
let importsBySpecifier = new Map();
|
|
169
|
+
for (let module of this.modules.values()) {
|
|
170
|
+
for (let [name, imp] of module.imports) {
|
|
171
|
+
if (module.used.has(name) && !this.modules.has(imp.specifier)) {
|
|
172
|
+
let importMap = importsBySpecifier.get(imp.specifier);
|
|
173
|
+
if (!importMap) {
|
|
174
|
+
importMap = new Map();
|
|
175
|
+
importsBySpecifier.set(imp.specifier, importMap);
|
|
176
|
+
}
|
|
177
|
+
name = module.getName(name);
|
|
178
|
+
importMap.set(name, imp.imported);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return importsBySpecifier;
|
|
183
|
+
}
|
|
184
|
+
propagate(context) {
|
|
185
|
+
// Resolve all exported values, and mark them as used.
|
|
186
|
+
let names = Object.create(null);
|
|
187
|
+
let exportedNames = new Map();
|
|
188
|
+
for (let e of this.getAllExports()) {
|
|
189
|
+
this.markUsed(e.module, e.imported, context);
|
|
190
|
+
e.module.names.set(e.imported, e.name);
|
|
191
|
+
names[e.name] = 1;
|
|
192
|
+
exportedNames.set(e.name, e.module);
|
|
193
|
+
}
|
|
194
|
+
let importedSymbolsToUpdate = [];
|
|
195
|
+
|
|
196
|
+
// Assign unique names across all modules
|
|
197
|
+
for (let m of this.modules.values()) {
|
|
198
|
+
for (let [orig, name] of m.names) {
|
|
199
|
+
if (exportedNames.has(name) && exportedNames.get(name) === m) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (!m.used.has(orig)) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (m.imports.has(orig)) {
|
|
206
|
+
// Update imports after all modules's local variables have been renamed
|
|
207
|
+
importedSymbolsToUpdate.push([m, orig]);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (names[name]) {
|
|
211
|
+
m.names.set(name, `_${name}${names[name]++}`);
|
|
212
|
+
} else {
|
|
213
|
+
names[name] = 1;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Map of imported specifiers -> map of imported names to local names
|
|
219
|
+
let imports = new Map();
|
|
220
|
+
for (let [m, orig] of importedSymbolsToUpdate) {
|
|
221
|
+
let imp = (0, _nullthrows().default)(m.imports.get(orig));
|
|
222
|
+
let imported = (0, _nullthrows().default)(this.resolveImport(m, orig));
|
|
223
|
+
|
|
224
|
+
// If the module is bundled, map the local name to the original exported name.
|
|
225
|
+
if (this.modules.has(imp.specifier)) {
|
|
226
|
+
m.names.set(orig, imported.imported);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// If it's external, then we need to dedup duplicate imported names, and ensure
|
|
231
|
+
// that they do not conflict with any exported or local names.
|
|
232
|
+
let importedNames = imports.get(imp.specifier);
|
|
233
|
+
if (!importedNames) {
|
|
234
|
+
importedNames = new Map();
|
|
235
|
+
imports.set(imp.specifier, importedNames);
|
|
236
|
+
}
|
|
237
|
+
let name = importedNames.get(imported.imported);
|
|
238
|
+
if (!name) {
|
|
239
|
+
if (names[imported.imported]) {
|
|
240
|
+
name = `_${imported.imported}${names[imported.imported]++}`;
|
|
241
|
+
} else {
|
|
242
|
+
name = imported.imported;
|
|
243
|
+
names[imported.imported] = 1;
|
|
244
|
+
}
|
|
245
|
+
importedNames.set(imported.imported, name);
|
|
246
|
+
}
|
|
247
|
+
m.names.set(orig, name);
|
|
248
|
+
}
|
|
249
|
+
return exportedNames;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
exports.TSModuleGraph = TSModuleGraph;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
function _plugin() {
|
|
8
|
+
const data = require("@atlaspack/plugin");
|
|
9
|
+
_plugin = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _path() {
|
|
15
|
+
const data = _interopRequireDefault(require("path"));
|
|
16
|
+
_path = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _sourceMap() {
|
|
22
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
23
|
+
_sourceMap = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _typescript() {
|
|
29
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
30
|
+
_typescript = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _tsUtils() {
|
|
36
|
+
const data = require("@atlaspack/ts-utils");
|
|
37
|
+
_tsUtils = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function _utils() {
|
|
43
|
+
const data = require("@atlaspack/utils");
|
|
44
|
+
_utils = function () {
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
function _diagnostic() {
|
|
50
|
+
const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
|
|
51
|
+
_diagnostic = function () {
|
|
52
|
+
return data;
|
|
53
|
+
};
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
var _TSModuleGraph = require("./TSModuleGraph");
|
|
57
|
+
function _nullthrows() {
|
|
58
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
59
|
+
_nullthrows = function () {
|
|
60
|
+
return data;
|
|
61
|
+
};
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
var _collect = require("./collect");
|
|
65
|
+
var _shake = require("./shake");
|
|
66
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
67
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
68
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
69
|
+
var _default = exports.default = new (_plugin().Transformer)({
|
|
70
|
+
loadConfig({
|
|
71
|
+
config,
|
|
72
|
+
options
|
|
73
|
+
}) {
|
|
74
|
+
return (0, _tsUtils().loadTSConfig)(config, options);
|
|
75
|
+
},
|
|
76
|
+
transform({
|
|
77
|
+
asset,
|
|
78
|
+
config,
|
|
79
|
+
options,
|
|
80
|
+
logger
|
|
81
|
+
}) {
|
|
82
|
+
let opts = {
|
|
83
|
+
// React is the default. Users can override this by supplying their own tsconfig,
|
|
84
|
+
// which many TypeScript users will already have for typechecking, etc.
|
|
85
|
+
jsx: _typescript().default.JsxEmit.React,
|
|
86
|
+
moduleResolution: _typescript().default.ModuleResolutionKind.NodeJs,
|
|
87
|
+
...config,
|
|
88
|
+
// Always emit output
|
|
89
|
+
noEmit: false,
|
|
90
|
+
noEmitOnError: false,
|
|
91
|
+
declaration: true,
|
|
92
|
+
declarationMap: true,
|
|
93
|
+
isolatedModules: false,
|
|
94
|
+
emitDeclarationOnly: true,
|
|
95
|
+
outFile: 'index.d.ts',
|
|
96
|
+
// createProgram doesn't support incremental mode
|
|
97
|
+
composite: false,
|
|
98
|
+
incremental: false
|
|
99
|
+
};
|
|
100
|
+
let host = new (_tsUtils().CompilerHost)(options.inputFS, _typescript().default, logger);
|
|
101
|
+
// $FlowFixMe
|
|
102
|
+
let program = _typescript().default.createProgram([asset.filePath], opts, host);
|
|
103
|
+
for (let file of program.getSourceFiles()) {
|
|
104
|
+
if (_path().default.normalize(file.fileName) !== asset.filePath) {
|
|
105
|
+
asset.invalidateOnFileChange(host.redirectTypes.get(file.fileName) ?? file.fileName);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
let mainModuleName = (0, _utils().normalizeSeparators)(_path().default.relative(program.getCommonSourceDirectory(), asset.filePath).slice(0, -_path().default.extname(asset.filePath).length));
|
|
109
|
+
let moduleGraph = new _TSModuleGraph.TSModuleGraph(mainModuleName);
|
|
110
|
+
let emitResult = program.emit(undefined, undefined, undefined, true, {
|
|
111
|
+
afterDeclarations: [
|
|
112
|
+
// 1. Build module graph
|
|
113
|
+
context => sourceFile => {
|
|
114
|
+
return (0, _collect.collect)(moduleGraph, context, sourceFile);
|
|
115
|
+
},
|
|
116
|
+
// 2. Tree shake and rename types
|
|
117
|
+
context => sourceFile => {
|
|
118
|
+
return (0, _shake.shake)(moduleGraph, context, sourceFile);
|
|
119
|
+
}]
|
|
120
|
+
});
|
|
121
|
+
let diagnostics = _typescript().default.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
122
|
+
let diagnosticIds = new Set();
|
|
123
|
+
let deduplicatedDiagnostics = [];
|
|
124
|
+
for (let d of diagnostics) {
|
|
125
|
+
if (d.start != null && d.length != null && d.messageText != null) {
|
|
126
|
+
let id = `${d.start}:${d.length}:${_typescript().default.flattenDiagnosticMessageText(d.messageText, '\n')}`;
|
|
127
|
+
if (!diagnosticIds.has(id)) {
|
|
128
|
+
deduplicatedDiagnostics.push(d);
|
|
129
|
+
}
|
|
130
|
+
diagnosticIds.add(id);
|
|
131
|
+
} else {
|
|
132
|
+
deduplicatedDiagnostics.push(d);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
let parcelDiagnostics = deduplicatedDiagnostics.map(diagnostic => {
|
|
136
|
+
let filename = asset.filePath;
|
|
137
|
+
let {
|
|
138
|
+
file
|
|
139
|
+
} = diagnostic;
|
|
140
|
+
let diagnosticMessage = _typescript().default.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
|
141
|
+
let codeframe;
|
|
142
|
+
if (file != null && diagnostic.start != null) {
|
|
143
|
+
let source = file.text || diagnostic.source;
|
|
144
|
+
if (file.fileName) {
|
|
145
|
+
filename = file.fileName;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// $FlowFixMe
|
|
149
|
+
if (source) {
|
|
150
|
+
let lineChar = file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
151
|
+
let start = {
|
|
152
|
+
line: lineChar.line + 1,
|
|
153
|
+
column: lineChar.character + 1
|
|
154
|
+
};
|
|
155
|
+
let end = {
|
|
156
|
+
line: start.line,
|
|
157
|
+
column: start.column + 1
|
|
158
|
+
};
|
|
159
|
+
if (typeof diagnostic.start === 'number' && typeof diagnostic.length === 'number') {
|
|
160
|
+
let endCharPosition = file.getLineAndCharacterOfPosition(diagnostic.start + diagnostic.length);
|
|
161
|
+
end = {
|
|
162
|
+
line: endCharPosition.line + 1,
|
|
163
|
+
column: endCharPosition.character
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
codeframe = {
|
|
167
|
+
filePath: filename,
|
|
168
|
+
code: source,
|
|
169
|
+
codeHighlights: [{
|
|
170
|
+
start,
|
|
171
|
+
end,
|
|
172
|
+
message: (0, _diagnostic().escapeMarkdown)(diagnosticMessage)
|
|
173
|
+
}]
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
message: (0, _diagnostic().escapeMarkdown)(diagnosticMessage),
|
|
179
|
+
codeFrames: codeframe ? [codeframe] : undefined
|
|
180
|
+
};
|
|
181
|
+
});
|
|
182
|
+
if (host.outputCode == null) {
|
|
183
|
+
throw new (_diagnostic().default)({
|
|
184
|
+
diagnostic: parcelDiagnostics
|
|
185
|
+
});
|
|
186
|
+
} else {
|
|
187
|
+
for (let d of parcelDiagnostics) {
|
|
188
|
+
logger.warn(d);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
let code = (0, _nullthrows().default)(host.outputCode);
|
|
192
|
+
code = code.substring(0, code.lastIndexOf('//# sourceMappingURL'));
|
|
193
|
+
let map = JSON.parse((0, _nullthrows().default)(host.outputMap));
|
|
194
|
+
map.sources = map.sources.map(source => _path().default.join(_path().default.dirname(asset.filePath), source));
|
|
195
|
+
let sourceMap = null;
|
|
196
|
+
if (map.mappings) {
|
|
197
|
+
sourceMap = new (_sourceMap().default)(options.projectRoot);
|
|
198
|
+
sourceMap.addVLQMap(map);
|
|
199
|
+
}
|
|
200
|
+
asset.type = 'ts';
|
|
201
|
+
asset.setCode(code);
|
|
202
|
+
asset.setMap(sourceMap);
|
|
203
|
+
return [asset];
|
|
204
|
+
}
|
|
205
|
+
});
|
package/lib/collect.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.collect = collect;
|
|
7
|
+
function _nullthrows() {
|
|
8
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
9
|
+
_nullthrows = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _typescript() {
|
|
15
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
16
|
+
_typescript = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
var _TSModule = require("./TSModule");
|
|
22
|
+
var _utils = require("./utils");
|
|
23
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
|
+
function collect(moduleGraph, context, sourceFile) {
|
|
25
|
+
// Factory only exists on TS >= 4.0
|
|
26
|
+
const {
|
|
27
|
+
factory = _typescript().default
|
|
28
|
+
} = context;
|
|
29
|
+
|
|
30
|
+
// When module definitions are nested inside each other (e.g with module augmentation),
|
|
31
|
+
// we want to keep track of the hierarchy so we can associated nodes with the right module.
|
|
32
|
+
const moduleStack = [];
|
|
33
|
+
let _currentModule;
|
|
34
|
+
let visit = node => {
|
|
35
|
+
if (_typescript().default.isBundle(node)) {
|
|
36
|
+
return factory.updateBundle(node, _typescript().default.visitNodes(node.sourceFiles, visit));
|
|
37
|
+
}
|
|
38
|
+
if (_typescript().default.isModuleDeclaration(node)) {
|
|
39
|
+
moduleStack.push(_currentModule);
|
|
40
|
+
_currentModule = new _TSModule.TSModule();
|
|
41
|
+
moduleGraph.addModule(node.name.text, _currentModule);
|
|
42
|
+
}
|
|
43
|
+
if (!_currentModule) {
|
|
44
|
+
return _typescript().default.visitEachChild(node, visit, context);
|
|
45
|
+
}
|
|
46
|
+
let currentModule = (0, _nullthrows().default)(_currentModule);
|
|
47
|
+
if (_typescript().default.isImportDeclaration(node) && node.importClause) {
|
|
48
|
+
if (node.importClause.namedBindings) {
|
|
49
|
+
if (node.importClause.namedBindings.elements) {
|
|
50
|
+
for (let element of node.importClause.namedBindings.elements) {
|
|
51
|
+
currentModule.addImport(element.name.text, node.moduleSpecifier.text, (element.propertyName ?? element.name).text);
|
|
52
|
+
}
|
|
53
|
+
} else if (node.importClause.namedBindings.name) {
|
|
54
|
+
currentModule.addImport(node.importClause.namedBindings.name.text, node.moduleSpecifier.text, '*');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (node.importClause.name) {
|
|
58
|
+
currentModule.addImport(node.importClause.name.text, node.moduleSpecifier.text, 'default');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (_typescript().default.isExportDeclaration(node)) {
|
|
62
|
+
if (node.exportClause) {
|
|
63
|
+
for (let element of node.exportClause.elements) {
|
|
64
|
+
if (node.moduleSpecifier) {
|
|
65
|
+
currentModule.addExport(element.name.text, (element.propertyName ?? element.name).text, node.moduleSpecifier.text);
|
|
66
|
+
} else {
|
|
67
|
+
currentModule.addExport(element.name.text, (element.propertyName ?? element.name).text);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
currentModule.addWildcardExport(node.moduleSpecifier.text);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
node = _typescript().default.visitEachChild(node, visit, context);
|
|
75
|
+
if (_typescript().default.isImportTypeNode(node) && _typescript().default.isLiteralTypeNode(node.argument) && _typescript().default.isStringLiteral(node.argument.literal)) {
|
|
76
|
+
let local = `$$parcel$import$${moduleGraph.syntheticImportCount++}`;
|
|
77
|
+
let [specifier, entity] = getImportName(node.qualifier, local, factory);
|
|
78
|
+
currentModule.addImport(local, node.argument.literal.text, specifier);
|
|
79
|
+
return factory.createTypeReferenceNode(entity, node.typeArguments);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Handle `export default name;`
|
|
83
|
+
if (_typescript().default.isExportAssignment(node) && _typescript().default.isIdentifier(node.expression)) {
|
|
84
|
+
currentModule.addExport('default', node.expression.text);
|
|
85
|
+
}
|
|
86
|
+
if ((0, _utils.isDeclaration)(node)) {
|
|
87
|
+
if (node.name) {
|
|
88
|
+
currentModule.addLocal(node.name.text, node);
|
|
89
|
+
}
|
|
90
|
+
let name = (0, _utils.getExportedName)(node);
|
|
91
|
+
if (name) {
|
|
92
|
+
currentModule.addLocal(name, node);
|
|
93
|
+
currentModule.addExport(name, name);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (_typescript().default.isVariableStatement(node) && node.modifiers) {
|
|
97
|
+
let isExported = node.modifiers.some(m => m.kind === _typescript().default.SyntaxKind.ExportKeyword);
|
|
98
|
+
for (let v of node.declarationList.declarations) {
|
|
99
|
+
currentModule.addLocal(v.name.text, v);
|
|
100
|
+
if (isExported) {
|
|
101
|
+
currentModule.addExport(v.name.text, v.name.text);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// After we finish traversing the children of a module definition,
|
|
107
|
+
// we need to make sure that subsequent nodes get associated with the next-highest level module.
|
|
108
|
+
if (_typescript().default.isModuleDeclaration(node)) {
|
|
109
|
+
_currentModule = moduleStack.pop();
|
|
110
|
+
}
|
|
111
|
+
return node;
|
|
112
|
+
};
|
|
113
|
+
return _typescript().default.visitNode(sourceFile, visit);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Traverse down an EntityName to the root identifier. Return that to use as the named import specifier,
|
|
117
|
+
// and collect the remaining parts into a new QualifiedName with the local replacement at the root.
|
|
118
|
+
// import('react').JSX.Element => import {JSX} from 'react'; JSX.Element
|
|
119
|
+
function getImportName(qualifier, local, factory) {
|
|
120
|
+
if (!qualifier) {
|
|
121
|
+
return ['*', factory.createIdentifier(local)];
|
|
122
|
+
}
|
|
123
|
+
if (qualifier.kind === _typescript().default.SyntaxKind.Identifier) {
|
|
124
|
+
return [qualifier.text, factory.createIdentifier(local)];
|
|
125
|
+
}
|
|
126
|
+
let [name, entity] = getImportName(qualifier.left, local, factory);
|
|
127
|
+
return [name, factory.createQualifiedName(entity, qualifier.right)];
|
|
128
|
+
}
|
package/lib/shake.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.shake = shake;
|
|
7
|
+
var _TSModule = require("./TSModule");
|
|
8
|
+
function _typescript() {
|
|
9
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
10
|
+
_typescript = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
function _nullthrows() {
|
|
16
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
17
|
+
_nullthrows = function () {
|
|
18
|
+
return data;
|
|
19
|
+
};
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
var _utils = require("./utils");
|
|
23
|
+
var _wrappers = require("./wrappers");
|
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
+
function shake(moduleGraph, context, sourceFile) {
|
|
26
|
+
// Factory only exists on TS >= 4.0
|
|
27
|
+
const {
|
|
28
|
+
factory = _typescript().default
|
|
29
|
+
} = context;
|
|
30
|
+
|
|
31
|
+
// We traverse things out of order which messes with typescript's internal state.
|
|
32
|
+
// We don't rely on the lexical environment, so just overwrite with noops to avoid errors.
|
|
33
|
+
context.suspendLexicalEnvironment = () => {};
|
|
34
|
+
context.resumeLexicalEnvironment = () => {};
|
|
35
|
+
|
|
36
|
+
// Propagate exports from the main module to determine what types should be included
|
|
37
|
+
let exportedNames = moduleGraph.propagate(context);
|
|
38
|
+
|
|
39
|
+
// When module definitions are nested inside each other (e.g with module augmentation),
|
|
40
|
+
// we want to keep track of the hierarchy so we can associated nodes with the right module.
|
|
41
|
+
const moduleStack = [];
|
|
42
|
+
let addedGeneratedImports = false;
|
|
43
|
+
let _currentModule;
|
|
44
|
+
let visit = node => {
|
|
45
|
+
if (_typescript().default.isBundle(node)) {
|
|
46
|
+
return factory.updateBundle(node, _typescript().default.visitNodes(node.sourceFiles, visit));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Flatten all module declarations into the top-level scope
|
|
50
|
+
if (_typescript().default.isModuleDeclaration(node)) {
|
|
51
|
+
// Deeply nested module declarations are assumed to be module augmentations and left alone.
|
|
52
|
+
if (moduleStack.length >= 1) {
|
|
53
|
+
var _node$modifiers$;
|
|
54
|
+
// Since we are hoisting them to the top-level scope, we need to add a "declare" keyword to make them ambient.
|
|
55
|
+
// we also want the declare keyword to come after the export keyword to guarantee a valid typings file.
|
|
56
|
+
node.modifiers ??= [];
|
|
57
|
+
const index = ((_node$modifiers$ = node.modifiers[0]) === null || _node$modifiers$ === void 0 ? void 0 : _node$modifiers$.kind) === _typescript().default.SyntaxKind.ExportKeyword ? 1 : 0;
|
|
58
|
+
node.modifiers.splice(index, 0, factory.createModifier(_typescript().default.SyntaxKind.DeclareKeyword));
|
|
59
|
+
return node;
|
|
60
|
+
}
|
|
61
|
+
moduleStack.push(_currentModule);
|
|
62
|
+
let isFirstModule = !_currentModule;
|
|
63
|
+
_currentModule = moduleGraph.getModule(node.name.text);
|
|
64
|
+
let statements = _typescript().default.visitEachChild(node, visit, context).body.statements;
|
|
65
|
+
_currentModule = moduleStack.pop();
|
|
66
|
+
if (isFirstModule && !addedGeneratedImports) {
|
|
67
|
+
statements.unshift(...generateImports(factory, moduleGraph));
|
|
68
|
+
addedGeneratedImports = true;
|
|
69
|
+
}
|
|
70
|
+
return statements;
|
|
71
|
+
}
|
|
72
|
+
if (!_currentModule) {
|
|
73
|
+
return _typescript().default.visitEachChild(node, visit, context);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Remove inline imports. They are hoisted to the top of the output.
|
|
77
|
+
if (_typescript().default.isImportDeclaration(node)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
let currentModule = (0, _nullthrows().default)(_currentModule);
|
|
81
|
+
// Remove exports from flattened modules
|
|
82
|
+
if (_typescript().default.isExportDeclaration(node)) {
|
|
83
|
+
if (!node.moduleSpecifier || moduleGraph.getModule(node.moduleSpecifier.text)) {
|
|
84
|
+
if (!node.moduleSpecifier && node.exportClause) {
|
|
85
|
+
// Filter exported elements to only external re-exports
|
|
86
|
+
let exported = [];
|
|
87
|
+
for (let element of node.exportClause.elements) {
|
|
88
|
+
let name = (element.propertyName ?? element.name).text;
|
|
89
|
+
if (exportedNames.get(name) === currentModule && !currentModule.hasBinding(name)) {
|
|
90
|
+
exported.push(element);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (exported.length > 0) {
|
|
94
|
+
return (0, _wrappers.updateExportDeclaration)(factory, node, undefined,
|
|
95
|
+
// modifiers
|
|
96
|
+
false,
|
|
97
|
+
// isTypeOnly
|
|
98
|
+
factory.updateNamedExports(node.exportClause, exported), undefined,
|
|
99
|
+
// moduleSpecifier
|
|
100
|
+
undefined // assertClause
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Remove export assignment if unused.
|
|
110
|
+
if (_typescript().default.isExportAssignment(node)) {
|
|
111
|
+
let name = currentModule.getName('default');
|
|
112
|
+
if (exportedNames.get(name) !== currentModule) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if ((0, _utils.isDeclaration)(node)) {
|
|
117
|
+
let name = (0, _utils.getExportedName)(node) || node.name.text;
|
|
118
|
+
|
|
119
|
+
// Remove unused declarations
|
|
120
|
+
if (!currentModule.used.has(name)) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Remove original export modifiers
|
|
125
|
+
node.modifiers = (node.modifiers || []).filter(m => m.kind !== _typescript().default.SyntaxKind.ExportKeyword && m.kind !== _typescript().default.SyntaxKind.DefaultKeyword);
|
|
126
|
+
|
|
127
|
+
// Rename declarations
|
|
128
|
+
let newName = currentModule.getName(name);
|
|
129
|
+
if (newName !== name && newName !== 'default') {
|
|
130
|
+
node.name = factory.createIdentifier(newName);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Export declarations that should be exported
|
|
134
|
+
if (exportedNames.get(newName) === currentModule) {
|
|
135
|
+
if (newName === 'default') {
|
|
136
|
+
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.DefaultKeyword));
|
|
137
|
+
}
|
|
138
|
+
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.ExportKeyword));
|
|
139
|
+
} else if (_typescript().default.isFunctionDeclaration(node) || _typescript().default.isClassDeclaration(node)) {
|
|
140
|
+
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.DeclareKeyword));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (_typescript().default.isVariableStatement(node)) {
|
|
144
|
+
node = _typescript().default.visitEachChild(node, visit, context);
|
|
145
|
+
|
|
146
|
+
// Remove empty variable statements
|
|
147
|
+
if (node.declarationList.declarations.length === 0) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Remove original export modifiers
|
|
152
|
+
node.modifiers = (node.modifiers || []).filter(m => m.kind !== _typescript().default.SyntaxKind.ExportKeyword && m.kind !== _typescript().default.SyntaxKind.DeclareKeyword);
|
|
153
|
+
|
|
154
|
+
// Add export modifier if all declarations are exported.
|
|
155
|
+
let isExported = node.declarationList.declarations.every(d => exportedNames.get(d.name.text) === currentModule);
|
|
156
|
+
if (isExported) {
|
|
157
|
+
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.ExportKeyword));
|
|
158
|
+
} else {
|
|
159
|
+
// Otherwise, add `declare` modifier (required for top-level declarations in d.ts files).
|
|
160
|
+
node.modifiers.unshift(factory.createModifier(_typescript().default.SyntaxKind.DeclareKeyword));
|
|
161
|
+
}
|
|
162
|
+
return node;
|
|
163
|
+
}
|
|
164
|
+
if (_typescript().default.isVariableDeclaration(node)) {
|
|
165
|
+
// Remove unused variables
|
|
166
|
+
if (!currentModule.used.has(node.name.text)) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Rename references
|
|
172
|
+
if (_typescript().default.isIdentifier(node) && currentModule.names.has(node.text)) {
|
|
173
|
+
let newName = (0, _nullthrows().default)(currentModule.getName(node.text));
|
|
174
|
+
if (newName !== 'default') {
|
|
175
|
+
return factory.createIdentifier(newName);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Replace namespace references with final names
|
|
180
|
+
if (_typescript().default.isQualifiedName(node) && _typescript().default.isIdentifier(node.left)) {
|
|
181
|
+
let resolved = moduleGraph.resolveImport(currentModule, node.left.text, node.right.text);
|
|
182
|
+
if (resolved && resolved.module.hasBinding(resolved.name)) {
|
|
183
|
+
return factory.createIdentifier(resolved.name);
|
|
184
|
+
} else {
|
|
185
|
+
return factory.updateQualifiedName(node, factory.createIdentifier(currentModule.getName(node.left.text)), node.right);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Remove private properties
|
|
190
|
+
if (_typescript().default.isPropertyDeclaration(node)) {
|
|
191
|
+
let isPrivate = node.modifiers && node.modifiers.some(m => m.kind === _typescript().default.SyntaxKind.PrivateKeyword);
|
|
192
|
+
if (isPrivate) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return _typescript().default.visitEachChild(node, visit, context);
|
|
197
|
+
};
|
|
198
|
+
return _typescript().default.visitNode(sourceFile, visit);
|
|
199
|
+
}
|
|
200
|
+
function generateImports(factory, moduleGraph) {
|
|
201
|
+
let importStatements = [];
|
|
202
|
+
for (let [specifier, names] of moduleGraph.getAllImports()) {
|
|
203
|
+
let defaultSpecifier;
|
|
204
|
+
let namespaceSpecifier;
|
|
205
|
+
let namedSpecifiers = [];
|
|
206
|
+
for (let [name, imported] of names) {
|
|
207
|
+
if (imported === 'default') {
|
|
208
|
+
defaultSpecifier = factory.createIdentifier(name);
|
|
209
|
+
} else if (imported === '*') {
|
|
210
|
+
namespaceSpecifier = factory.createNamespaceImport(factory.createIdentifier(name));
|
|
211
|
+
} else {
|
|
212
|
+
namedSpecifiers.push((0, _wrappers.createImportSpecifier)(factory, false, name === imported ? undefined : factory.createIdentifier(imported), factory.createIdentifier(name)));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (namespaceSpecifier) {
|
|
216
|
+
let importClause = (0, _wrappers.createImportClause)(factory, false, defaultSpecifier, namespaceSpecifier);
|
|
217
|
+
importStatements.push((0, _wrappers.createImportDeclaration)(factory, undefined, importClause, factory.createStringLiteral(specifier), undefined));
|
|
218
|
+
defaultSpecifier = undefined;
|
|
219
|
+
}
|
|
220
|
+
if (defaultSpecifier || namedSpecifiers.length > 0) {
|
|
221
|
+
let importClause = (0, _wrappers.createImportClause)(factory, false, defaultSpecifier, namedSpecifiers.length > 0 ? factory.createNamedImports(namedSpecifiers) : undefined);
|
|
222
|
+
importStatements.push((0, _wrappers.createImportDeclaration)(factory, undefined, importClause, factory.createStringLiteral(specifier), undefined));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return importStatements;
|
|
226
|
+
}
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getExportedName = getExportedName;
|
|
7
|
+
exports.isDeclaration = isDeclaration;
|
|
8
|
+
function _typescript() {
|
|
9
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
10
|
+
_typescript = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
+
function getExportedName(node) {
|
|
17
|
+
if (!node.modifiers) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
if (!node.modifiers.some(m => m.kind === _typescript().default.SyntaxKind.ExportKeyword)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
if (node.modifiers.some(m => m.kind === _typescript().default.SyntaxKind.DefaultKeyword)) {
|
|
24
|
+
return 'default';
|
|
25
|
+
}
|
|
26
|
+
return node.name.text;
|
|
27
|
+
}
|
|
28
|
+
function isDeclaration(node) {
|
|
29
|
+
return _typescript().default.isFunctionDeclaration(node) || _typescript().default.isClassDeclaration(node) || _typescript().default.isInterfaceDeclaration(node) || _typescript().default.isEnumDeclaration(node) || _typescript().default.isTypeAliasDeclaration(node);
|
|
30
|
+
}
|
package/lib/wrappers.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.updateExportDeclaration = exports.createImportSpecifier = exports.createImportDeclaration = exports.createImportClause = void 0;
|
|
7
|
+
function _typescript() {
|
|
8
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
9
|
+
_typescript = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _assert() {
|
|
15
|
+
const data = _interopRequireDefault(require("assert"));
|
|
16
|
+
_assert = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
/* eslint-disable no-unused-vars */
|
|
23
|
+
const [majorVersion, minorVersion] = _typescript().default.versionMajorMinor.split('.').map(num => parseInt(num, 10));
|
|
24
|
+
|
|
25
|
+
// Everything below was generated using https://github.com/mischnic/tsc-version-wrapper
|
|
26
|
+
|
|
27
|
+
const createImportClause = exports.createImportClause = (() => {
|
|
28
|
+
if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 0) {
|
|
29
|
+
return (factory, isTypeOnly, name, namedBindings) => factory.createImportClause(isTypeOnly, name, namedBindings);
|
|
30
|
+
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 8) {
|
|
31
|
+
return (factory, isTypeOnly, name, namedBindings) => factory.createImportClause(name, namedBindings, isTypeOnly);
|
|
32
|
+
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 0) {
|
|
33
|
+
return (factory, isTypeOnly, name, namedBindings) => factory.createImportClause(name, namedBindings);
|
|
34
|
+
} else {
|
|
35
|
+
(0, _assert().default)(false);
|
|
36
|
+
}
|
|
37
|
+
})();
|
|
38
|
+
const createImportDeclaration = exports.createImportDeclaration = (() => {
|
|
39
|
+
if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 8) {
|
|
40
|
+
return (factory, modifiers, importClause, moduleSpecifier, assertClause) => factory.createImportDeclaration(modifiers, importClause, moduleSpecifier, assertClause);
|
|
41
|
+
} else if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 5) {
|
|
42
|
+
return (factory, modifiers, importClause, moduleSpecifier, assertClause) => factory.createImportDeclaration(undefined /* decorators */, modifiers, importClause, moduleSpecifier, assertClause);
|
|
43
|
+
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 0) {
|
|
44
|
+
return (factory, modifiers, importClause, moduleSpecifier) => factory.createImportDeclaration(undefined /* decorators */, modifiers, importClause, moduleSpecifier);
|
|
45
|
+
} else {
|
|
46
|
+
(0, _assert().default)(false);
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
49
|
+
const createImportSpecifier = exports.createImportSpecifier = (() => {
|
|
50
|
+
if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 5) {
|
|
51
|
+
return (factory, isTypeOnly, propertyName, name) => factory.createImportSpecifier(isTypeOnly, propertyName, name);
|
|
52
|
+
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 0) {
|
|
53
|
+
return (factory, isTypeOnly, propertyName, name) => factory.createImportSpecifier(propertyName, name);
|
|
54
|
+
} else {
|
|
55
|
+
(0, _assert().default)(false);
|
|
56
|
+
}
|
|
57
|
+
})();
|
|
58
|
+
const updateExportDeclaration = exports.updateExportDeclaration = (() => {
|
|
59
|
+
if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 8) {
|
|
60
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause);
|
|
61
|
+
} else if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 5) {
|
|
62
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, isTypeOnly, exportClause, moduleSpecifier, assertClause);
|
|
63
|
+
} else if (majorVersion > 4 || majorVersion === 4 && minorVersion >= 0) {
|
|
64
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, isTypeOnly, exportClause, moduleSpecifier);
|
|
65
|
+
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 8) {
|
|
66
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier, isTypeOnly);
|
|
67
|
+
} else if (majorVersion > 3 || majorVersion === 3 && minorVersion >= 0) {
|
|
68
|
+
return (factory, node, modifiers, isTypeOnly, exportClause, moduleSpecifier) => factory.updateExportDeclaration(node, undefined /* decorators */, modifiers, exportClause, moduleSpecifier);
|
|
69
|
+
} else {
|
|
70
|
+
(0, _assert().default)(false);
|
|
71
|
+
}
|
|
72
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/transformer-typescript-types",
|
|
3
|
-
"version": "2.14.
|
|
3
|
+
"version": "2.14.1-canary.6+57e29c592",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -12,15 +12,15 @@
|
|
|
12
12
|
"main": "lib/TSTypesTransformer.js",
|
|
13
13
|
"source": "src/TSTypesTransformer.js",
|
|
14
14
|
"engines": {
|
|
15
|
-
"atlaspack": "
|
|
15
|
+
"atlaspack": "2.14.1-canary.6+57e29c592",
|
|
16
16
|
"node": ">= 16.0.0"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@atlaspack/diagnostic": "2.14.
|
|
20
|
-
"@atlaspack/plugin": "2.14.
|
|
19
|
+
"@atlaspack/diagnostic": "2.14.1-canary.6+57e29c592",
|
|
20
|
+
"@atlaspack/plugin": "2.14.1-canary.6+57e29c592",
|
|
21
|
+
"@atlaspack/ts-utils": "2.14.1-canary.6+57e29c592",
|
|
22
|
+
"@atlaspack/utils": "2.14.1-canary.6+57e29c592",
|
|
21
23
|
"@parcel/source-map": "^2.1.1",
|
|
22
|
-
"@atlaspack/ts-utils": "2.14.0",
|
|
23
|
-
"@atlaspack/utils": "2.14.0",
|
|
24
24
|
"nullthrows": "^1.1.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
@@ -29,5 +29,6 @@
|
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"typescript": ">=3.0.0"
|
|
31
31
|
},
|
|
32
|
-
"type": "commonjs"
|
|
32
|
+
"type": "commonjs",
|
|
33
|
+
"gitHead": "57e29c592cff406d084b3c676a1c899f0bb6e78f"
|
|
33
34
|
}
|