Skip to content

Commit

Permalink
fix(typescript): change the value of isExternalLibraryImport to false…
Browse files Browse the repository at this point in the history
… if the resolvedModule should be compiled (#1521)
  • Loading branch information
TrickyPi committed Jun 28, 2023
1 parent d875c5f commit 6b134ed
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/typescript/src/index.ts
Expand Up @@ -42,7 +42,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
parsedOptions.fileNames = parsedOptions.fileNames.filter(filter);

const formatHost = createFormattingHost(ts, parsedOptions.options);
const resolveModule = createModuleResolver(ts, formatHost);
const resolveModule = createModuleResolver(ts, formatHost, filter);

let program: Watch<unknown> | null = null;

Expand Down
15 changes: 12 additions & 3 deletions packages/typescript/src/moduleResolution.ts
Expand Up @@ -5,6 +5,7 @@ import type {
ResolvedProjectReference,
ModuleKind
} from 'typescript';
import type { CreateFilter } from '@rollup/pluginutils';

import type { DiagnosticsHost } from './diagnostics/host';

Expand All @@ -24,7 +25,8 @@ export type Resolver = (
*/
export default function createModuleResolver(
ts: typeof typescript,
host: ModuleResolverHost
host: ModuleResolverHost,
filter: ReturnType<CreateFilter>
): Resolver {
const compilerOptions = host.getCompilationSettings();
const cache = ts.createModuleResolutionCache(
Expand All @@ -35,7 +37,7 @@ export default function createModuleResolver(
const moduleHost = { ...ts.sys, ...host };

return (moduleName, containingFile, redirectedReference, mode) => {
const resolved = ts.resolveModuleName(
const { resolvedModule } = ts.resolveModuleName(
moduleName,
containingFile,
compilerOptions,
Expand All @@ -44,6 +46,13 @@ export default function createModuleResolver(
redirectedReference,
mode
);
return resolved.resolvedModule;
/**
* If the module's path contains 'node_modules', ts considers it an external library and refuses to compile it,
* so we have to change the value of `isExternalLibraryImport` to false if it's true
* */
if (resolvedModule?.isExternalLibraryImport && filter(resolvedModule?.resolvedFileName)) {
resolvedModule.isExternalLibraryImport = false;
}
return resolvedModule;
};
}
@@ -0,0 +1 @@
export { dep } from './node_modules/dep';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions packages/typescript/test/test.js
Expand Up @@ -1393,3 +1393,12 @@ test.serial('noForceEmit option defers to tsconfig.json for noEmit', async (t) =
const originalCode = fs.readFileSync(path.join(__dirname, input), 'utf8');
t.is(output[0].code, originalCode);
});

test.serial('compiled external library', async (t) => {
const input = 'fixtures/external-library-import/main.ts';
await rollup({
input,
plugins: [typescript({ tsconfig: 'fixtures/external-library-import/tsconfig.json' })]
});
t.pass();
});

0 comments on commit 6b134ed

Please sign in to comment.