WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 1a7d5b0

Browse files
committed
feat: add .js import extensions in .d.ts files
1 parent a874b81 commit 1a7d5b0

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@
6363
"prettier-plugin-jsdoc": "^1.3.3",
6464
"size-limit": "^11.2.0",
6565
"standard-version": "^9.5.0",
66-
"typescript": "^5.8.3",
6766
"typescript-eslint": "^8.38.0",
6867
"vite": "^6.0.2",
6968
"vitest": "^3.2.4"
7069
},
7170
"peerDependencies": {
71+
"tslib": "^2",
72+
"typescript": "^5",
7273
"vite": "^4 || ^5 || ^6 || ^7"
7374
},
7475
"engines": {

src/plugins/extendConfig.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "node:path"
33
import typescript from "@rollup/plugin-typescript"
44
import { defuFn } from "defu"
55
import renameNodeModules from "rollup-plugin-rename-node-modules"
6+
import ts from "typescript"
67
import type { Plugin, UserConfig } from "vite"
78
import { defineConfig } from "vite"
89

@@ -60,6 +61,9 @@ export const extendConfigPlugin = (options: Options): Plugin => {
6061
declaration: options.dts,
6162
outDir: userConfig.build?.outDir || "dist",
6263
include: [path.posix.join(options.srcDir, "**/*")],
64+
transformers: {
65+
afterDeclarations: [addImportExtensionsTransformer],
66+
},
6367
}) as Plugin,
6468
// Ensure bundled dependencies are published to npm.
6569
// `npm pack` ignores directories named `node_modules`.
@@ -84,3 +88,37 @@ export const extendConfigPlugin = (options: Options): Plugin => {
8488
},
8589
}
8690
}
91+
92+
/**
93+
* Adds a `.js` extension to all import declarations that do not contain an
94+
* extension.
95+
*/
96+
function addImportExtensionsTransformer(context: ts.TransformationContext) {
97+
return (source: ts.Bundle | ts.SourceFile) => {
98+
function visitor(node: ts.Node) {
99+
if (ts.isImportDeclaration(node)) {
100+
const moduleSpecifier = node.moduleSpecifier
101+
if (moduleSpecifier && ts.isStringLiteral(moduleSpecifier)) {
102+
const text = moduleSpecifier.text
103+
if (text.startsWith(".") && !/\.[a-z0-9]+$/i.test(text)) {
104+
const newModuleSpecifier = ts.factory.createStringLiteral(
105+
text + ".js",
106+
)
107+
108+
return ts.factory.updateImportDeclaration(
109+
node,
110+
node.modifiers,
111+
node.importClause,
112+
newModuleSpecifier,
113+
node.attributes,
114+
)
115+
}
116+
}
117+
}
118+
119+
return ts.visitEachChild(node, visitor, context)
120+
}
121+
122+
return ts.visitEachChild(source, visitor, context)
123+
}
124+
}

0 commit comments

Comments
 (0)