@atlaspack/transformer-typescript-types 2.12.1-canary.3354
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 +33 -0
- package/src/TSModule.js +55 -0
- package/src/TSModuleGraph.js +277 -0
- package/src/TSTypesTransformer.js +185 -0
- package/src/collect.js +157 -0
- package/src/shake.js +318 -0
- package/src/utils.js +28 -0
- package/src/wrappers.js +202 -0
|
@@ -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 atlaspackDiagnostics = 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: atlaspackDiagnostics
|
|
185
|
+
});
|
|
186
|
+
} else {
|
|
187
|
+
for (let d of atlaspackDiagnostics) {
|
|
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 = `$$atlaspack$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
|
+
})();
|