Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): change the value of isExternalLibraryImport to false if the resolvedModule should be compiled #1521

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/typescript/src/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
};
}
Original file line number Diff line number Diff line change
@@ -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.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions packages/typescript/test/test.js
Original file line number Diff line number Diff line change
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();
});