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

feat: support typescript format #51

Merged
merged 1 commit into from
Aug 1, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/generator.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ImportMap } from '@jspm/import-map';
import { NonRelativeModuleNameResolutionCache } from 'typescript';

export type LogStream = () => AsyncGenerator<{ type: string, message: string }, never, unknown>;

Expand Down Expand Up @@ -46,7 +45,7 @@ export declare class Generator {
}

export interface ModuleAnalysis {
format: 'commonjs' | 'esm' | 'system';
format: 'commonjs' | 'esm' | 'system' | 'json' | 'typescript';
staticDeps: string[];
dynamicDeps: string[];
cjsLazyDeps: string[] | null;
Expand Down
2 changes: 1 addition & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface GeneratorOptions {
}

export interface ModuleAnalysis {
format: 'commonjs' | 'esm' | 'system' | 'json';
format: 'commonjs' | 'esm' | 'system' | 'json' | 'typescript';
staticDeps: string[];
dynamicDeps: string[];
cjsLazyDeps: string[] | null;
Expand Down
2 changes: 1 addition & 1 deletion src/trace/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface Analysis {
deps: string[];
dynamicDeps: string[];
cjsLazyDeps: string[] | null;
format: 'esm' | 'commonjs' | 'system' | 'json';
format: 'esm' | 'commonjs' | 'system' | 'json' | 'typescript';
size: number;
}

Expand Down
13 changes: 10 additions & 3 deletions src/trace/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,24 @@ export class Resolver {
default: throw new JspmError(`Invalid status code ${res.status} loading ${resolvedUrl}. ${res.statusText}`);
}
let source = await res.text();
// TODO: headers over extensions for non-file
// TODO: headers over extensions for non-file URLs
try {
if (resolvedUrl.endsWith('.ts') || resolvedUrl.endsWith('.tsx') || resolvedUrl.endsWith('.jsx'))
if (resolvedUrl.endsWith('.ts') || resolvedUrl.endsWith('.tsx') || resolvedUrl.endsWith('.jsx')) {
source = await parseTs(source);
if (resolvedUrl.endsWith('.json')) {
const [imports] = await parse(source) as any as [any[], string[]];
const esmAnalysis = createEsmAnalysis(imports, source, resolvedUrl);
esmAnalysis.format = 'typescript';
return esmAnalysis;

}
else if (resolvedUrl.endsWith('.json')) {
try {
JSON.parse(source);
return { deps: [], dynamicDeps: [], cjsLazyDeps: null, size: source.length, format: 'json' };
}
catch {}
}

const [imports, exports] = await parse(source) as any as [any[], string[]];
if (imports.every(impt => impt.d > 0) && !exports.length && resolvedUrl.startsWith('file:')) {
// Support CommonJS package boundary checks for non-ESM on file: protocol only
Expand Down
2 changes: 1 addition & 1 deletion src/trace/tracemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface TraceEntry {
// For cjs modules, the list of hoisted deps
// this is needed for proper cycle handling
cjsLazyDeps: Record<string, string | string[]>;
format: 'esm' | 'commonjs' | 'system' | 'json';
format: 'esm' | 'commonjs' | 'system' | 'json' | 'typescript';
}

// The tracemap fully drives the installer
Expand Down
13 changes: 13 additions & 0 deletions test/resolve/ts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Generator } from '@jspm/generator';
import assert from 'assert';

if (typeof document === 'undefined') {
const generator = new Generator({
mapUrl: import.meta.url,
defaultProvider: 'nodemodules'
});

await generator.traceInstall('./tspkg/main.ts');

assert.strictEqual(generator.getAnalysis(new URL('./tspkg/dep.ts', import.meta.url)).format, 'typescript');
}
1 change: 1 addition & 0 deletions test/resolve/tspkg/dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var dep: string = 'dep';
2 changes: 2 additions & 0 deletions test/resolve/tspkg/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './dep.ts';
export var p: number = 5;