diff --git a/packages/typescript/package.json b/packages/typescript/package.json index 1163073ba..fa8b20f45 100644 --- a/packages/typescript/package.json +++ b/packages/typescript/package.json @@ -74,7 +74,7 @@ "@types/resolve": "^1.20.2", "buble": "^0.20.0", "rollup": "^4.0.0-24", - "typescript": "^4.8.3" + "typescript": "^5.9.3" }, "types": "./types/index.d.ts" } diff --git a/packages/typescript/src/index.ts b/packages/typescript/src/index.ts index 5caaae3c7..9bf140a37 100644 --- a/packages/typescript/src/index.ts +++ b/packages/typescript/src/index.ts @@ -224,7 +224,6 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi const mode = typeof ts.getImpliedNodeFormatForFile === 'function' ? ts.getImpliedNodeFormatForFile( - // @ts-expect-error containingFile, undefined, // eslint-disable-line no-undefined { ...ts.sys, ...formatHost }, @@ -236,7 +235,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi const resolved = resolveModule(importee, containingFile, undefined, mode); if (resolved) { - if (/\.d\.[cm]?ts/.test(resolved.extension)) return null; + if (isDeclarationOutputFile(resolved.extension)) return null; if (!filter(resolved.resolvedFileName)) return null; return path.normalize(resolved.resolvedFileName); } diff --git a/packages/typescript/src/options/tsconfig.ts b/packages/typescript/src/options/tsconfig.ts index 4ebc12675..36845f822 100644 --- a/packages/typescript/src/options/tsconfig.ts +++ b/packages/typescript/src/options/tsconfig.ts @@ -122,7 +122,7 @@ function setModuleResolutionKind(parsedConfig: ParsedCommandLine): ParsedCommand }; } -const configCache = new Map() as typescript.ESMap; +const configCache = new Map() as Map; /** * Parse the Typescript config to use with the plugin. diff --git a/packages/typescript/src/outputFile.ts b/packages/typescript/src/outputFile.ts index d7b080ab1..fed80f1ae 100644 --- a/packages/typescript/src/outputFile.ts +++ b/packages/typescript/src/outputFile.ts @@ -38,7 +38,7 @@ export function isTypeScriptMapOutputFile(name: string): boolean { * Checks if the given OutputFile represents some declaration */ export function isDeclarationOutputFile(name: string): boolean { - return /\.d\.[cm]?ts$/.test(name); + return /\.d(\..+)?\.[cm]?ts$/.test(name); } /** diff --git a/packages/typescript/src/watchProgram.ts b/packages/typescript/src/watchProgram.ts index f4b47c7cd..5a0a548a7 100644 --- a/packages/typescript/src/watchProgram.ts +++ b/packages/typescript/src/watchProgram.ts @@ -210,12 +210,12 @@ function createWatchHost( containingFile, _reusedNames, redirectedReference, - _optionsOnlyWithNewerTsVersions, + compilerOptions, containingSourceFile ) { return moduleNames.map((moduleName, i) => { const mode = containingSourceFile - ? ts.getModeForResolutionAtIndex?.(containingSourceFile, i) + ? ts.getModeForResolutionAtIndex?.(containingSourceFile, i, compilerOptions) : undefined; // eslint-disable-line no-undefined return resolveModule(moduleName, containingFile, redirectedReference, mode); diff --git a/packages/typescript/test/fixtures/arbitrary-dts/main.ts b/packages/typescript/test/fixtures/arbitrary-dts/main.ts new file mode 100644 index 000000000..080787f75 --- /dev/null +++ b/packages/typescript/test/fixtures/arbitrary-dts/main.ts @@ -0,0 +1,3 @@ +import module from './module.custom'; + +export default module.x; diff --git a/packages/typescript/test/fixtures/arbitrary-dts/module.custom b/packages/typescript/test/fixtures/arbitrary-dts/module.custom new file mode 100644 index 000000000..0c074148c --- /dev/null +++ b/packages/typescript/test/fixtures/arbitrary-dts/module.custom @@ -0,0 +1,3 @@ +export default { + x: 5, +}; \ No newline at end of file diff --git a/packages/typescript/test/fixtures/arbitrary-dts/module.d.custom.ts b/packages/typescript/test/fixtures/arbitrary-dts/module.d.custom.ts new file mode 100644 index 000000000..9ddd1ce3e --- /dev/null +++ b/packages/typescript/test/fixtures/arbitrary-dts/module.d.custom.ts @@ -0,0 +1,4 @@ +declare const CustomModule: { + x: Number; +} +export default CustomModule; \ No newline at end of file diff --git a/packages/typescript/test/fixtures/arbitrary-dts/tsconfig.json b/packages/typescript/test/fixtures/arbitrary-dts/tsconfig.json new file mode 100644 index 000000000..5f534221f --- /dev/null +++ b/packages/typescript/test/fixtures/arbitrary-dts/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "allowArbitraryExtensions": true + }, +} \ No newline at end of file diff --git a/packages/typescript/test/test.js b/packages/typescript/test/test.js index 8eb039799..2e4e8e6df 100644 --- a/packages/typescript/test/test.js +++ b/packages/typescript/test/test.js @@ -316,7 +316,7 @@ test.serial('warns for invalid module types', async (t) => { code: 'PLUGIN_WARNING', plugin: 'typescript', pluginCode: 'TS6046', - message: `@rollup/plugin-typescript TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'es2020', 'es2022', 'esnext', 'node16', 'nodenext'.` + message: `@rollup/plugin-typescript TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'es2020', 'es2022', 'esnext', 'node16', 'node18', 'node20', 'nodenext', 'preserve'.` } ]); }); @@ -496,6 +496,18 @@ test.serial('should not resolve .d.ts files', async (t) => { t.deepEqual(imports, ['an-import']); }); +test.serial('should not resolve arbitrary .d..ts files', async (t) => { + const bundle = await rollup({ + input: 'fixtures/arbitrary-dts/main.ts', + plugins: [typescript({ tsconfig: 'fixtures/arbitrary-dts/tsconfig.json' })], + onwarn + }); + const arbitraryDeclarationModules = bundle.cache.modules.filter((module) => + module.id.includes('.d.custom.ts') + ); + t.is(arbitraryDeclarationModules.length, 0); +}); + test.serial('should transpile JSX if enabled', async (t) => { process.chdir('fixtures/jsx'); @@ -1538,8 +1550,9 @@ test.serial('correctly resolves types with nodenext moduleResolution', async (t) const code = await getCode(bundle, outputOptions); t.true(code.includes('var bar = foo'), code); - t.is(warnings.length, 1); - t.is(warnings[0].code, 'UNRESOLVED_IMPORT'); + t.is(warnings.length, 2); + t.is(warnings[0].pluginCode, 'TS5110'); + t.is(warnings[1].code, 'UNRESOLVED_IMPORT'); }); test.serial('noForceEmit option defers to tsconfig.json for emitDeclarationOnly', async (t) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e5908b66b..d9ee70118 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -740,8 +740,8 @@ importers: specifier: ^4.0.0-24 version: 4.0.0-24 typescript: - specifier: ^4.8.3 - version: 4.8.4 + specifier: ^5.9.3 + version: 5.9.3 packages/url: dependencies: