Skip to content

Commit f895d78

Browse files
committed
fix: fix TypeScript declarations to match that we distribute as CommonJS
The plugin itself is now the export object. A "default" property is provided for backwards compatibility. The built types are transformed by fix-types-for-back-compat.js, which adds an exported declaration of ResolveTypeScriptPluginOptions for backwards compatibility. fix #27
1 parent a340ec1 commit f895d78

File tree

6 files changed

+64
-6
lines changed

6 files changed

+64
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ npm-debug.log*
99
*.d.ts
1010
*.js.map
1111
!/types/*.d.ts
12+
!/fix-types-for-back-compat.js
1213

1314
# macOS
1415
.DS_Store
@@ -31,4 +32,4 @@ Temporary Items
3132
Thumbs.db
3233
ehthumbs.db
3334
Desktop.ini
34-
$RECYCLE.BIN/
35+
$RECYCLE.BIN/

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/.snyk
99
*.ts
1010
!*.d.ts
11+
/fix-types-for-back-compat.js
1112

1213
# yarn and npm
1314
package-lock.json
@@ -36,4 +37,4 @@ Temporary Items
3637
Thumbs.db
3738
ehthumbs.db
3839
Desktop.ini
39-
$RECYCLE.BIN/
40+
$RECYCLE.BIN/

.prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules
33
*.js
44
*.d.ts
55
*.js.map
6-
!/types/*.d.ts
6+
!/types/*.d.ts
7+
!/fix-types-for-back-compat.js

fix-types-for-back-compat.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
3+
const typescript = require("typescript");
4+
const fs = require("fs");
5+
6+
const program = typescript.createProgram(["index.d.ts"], {});
7+
const source = program.getSourceFile("index.d.ts");
8+
const printer = typescript.createPrinter();
9+
10+
const fix = context => node => {
11+
const visit = node => {
12+
if (
13+
typescript.isIdentifier(node) &&
14+
node.escapedText === "ResolveTypescriptPluginOptions"
15+
) {
16+
return typescript.factory.createQualifiedName(
17+
typescript.factory.createIdentifier("ResolveTypescriptPlugin"),
18+
typescript.factory.createIdentifier("ResolveTypescriptPluginOptions")
19+
);
20+
} else {
21+
return typescript.visitEachChild(node, visit, context);
22+
}
23+
};
24+
const visitTopLevel = node => {
25+
if (
26+
typescript.isInterfaceDeclaration(node) &&
27+
node.name.escapedText === "ResolveTypescriptPluginOptions"
28+
) {
29+
return typescript.factory.createModuleDeclaration(
30+
[],
31+
[typescript.factory.createModifier(typescript.SyntaxKind.DeclareKeyword)],
32+
typescript.factory.createIdentifier("ResolveTypescriptPlugin"),
33+
typescript.factory.createModuleBlock([node]),
34+
typescript.NodeFlags.Namespace
35+
);
36+
} else {
37+
return typescript.visitEachChild(node, visit, context);
38+
}
39+
};
40+
return typescript.visitEachChild(node, visitTopLevel, context);
41+
};
42+
43+
if (source != null) {
44+
typescript.transform(source, [fix]).transformed.forEach(node => {
45+
if (typescript.isSourceFile(node)) {
46+
fs.writeFileSync("index.d.ts", printer.printFile(node));
47+
}
48+
});
49+
}

index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ type Resolver = NonNullable<ResolveOptions["resolver"]>;
44

55
const pluginName = "ResolveTypescriptPlugin";
66

7-
export interface ResolveTypescriptPluginOptions {
7+
interface ResolveTypescriptPluginOptions {
88
includeNodeModules?: boolean;
99
}
1010

11-
export default class ResolveTypescriptPlugin {
11+
class ResolveTypescriptPlugin {
12+
/** @deprecated For backwards compatibility with versions < v1.2.
13+
* Will be removed in v2.0. */
14+
public static default = ResolveTypescriptPlugin;
15+
1216
private static defaultOptions: ResolveTypescriptPluginOptions = {
1317
includeNodeModules: false
1418
};
@@ -55,3 +59,5 @@ export default class ResolveTypescriptPlugin {
5559
}
5660
}
5761
}
62+
63+
export = ResolveTypescriptPlugin;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"scripts": {
1818
"fix": "tsc --noEmit && eslint . --fix && prettier --write .",
1919
"lint": "tsc --noEmit && eslint . && prettier --check .",
20-
"prepare": "tsc",
20+
"prepare": "tsc && node ./fix-types-for-back-compat.js",
2121
"semantic-release": "semantic-release",
2222
"test": "ava"
2323
},

0 commit comments

Comments
 (0)