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(typescript): Move to BuilderProgram API #217

Merged
merged 23 commits into from
Feb 28, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Work on supporting project references
NotWoods committed Feb 20, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit afddf1f5c7bc9b42e081065c6e2ec9c51fbcf000
15 changes: 7 additions & 8 deletions packages/typescript/src/host.ts
Original file line number Diff line number Diff line change
@@ -23,10 +23,8 @@ export interface WatchCompilerHost extends BaseHost {
interface CreateWatchHostOptions {
/** Formatting host used to get some system functions and emit type errors. */
formatHost: DiagnosticsHost;
/** Typescript compiler options. */
compilerOptions: import('typescript').CompilerOptions;
/** List of Typescript files that should be compiled. */
fileNames: string[];
/** Parsed Typescript compiler options. */
parsedOptions: import('typescript').ParsedCommandLine;
/** Callback to save compiled files in memory. */
writeFile: import('typescript').WriteFileCallback;
}
@@ -40,19 +38,20 @@ interface CreateWatchHostOptions {
export default function createWatchHost(
ts: typeof import('typescript'),
context: PluginContext,
{ formatHost, compilerOptions, fileNames, writeFile }: CreateWatchHostOptions
{ formatHost, parsedOptions, writeFile }: CreateWatchHostOptions
): WatchCompilerHost {
const createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;

const baseHost = ts.createWatchCompilerHost(
fileNames,
compilerOptions,
parsedOptions.fileNames,
parsedOptions.options,
ts.sys,
createProgram,
// Use Rollup to report diagnostics from Typescript
(diagnostic) => emitDiagnostics(ts, context, formatHost, [diagnostic]),
// Ignore watch status changes
() => {}
() => {},
parsedOptions.projectReferences
);

const resolver = createModuleResolver(ts, { ...formatHost, ...baseHost });
9 changes: 6 additions & 3 deletions packages/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
const emittedFiles = new Map<string, string>();

const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions);
parsedOptions.fileNames = parsedOptions.fileNames.filter(filter);

const formatHost = createFormattingHost(ts, parsedOptions.options);
let host: WatchCompilerHost;

@@ -22,12 +24,13 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi

buildStart() {
emitParsedOptionsErrors(ts, this, parsedOptions);
console.log(parsedOptions.projectReferences)

host = createWatchHost(ts, this, {
formatHost,
compilerOptions: parsedOptions.options,
fileNames: parsedOptions.fileNames.filter(filter),
parsedOptions,
writeFile(fileName, data) {
console.log(fileName);
emittedFiles.set(fileName, data);
}
});
@@ -57,7 +60,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi

load(id) {
if (!filter(id)) return null;

console.log('Load: ', id)
return findTypescriptOutput(id, emittedFiles);
}
};
17 changes: 7 additions & 10 deletions packages/typescript/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { dirname, resolve } from 'path';

import { createFilter } from '@rollup/pluginutils';
import { PluginContext } from 'rollup';
@@ -34,15 +34,6 @@ const DEFAULT_COMPILER_OPTIONS: PartialCustomOptions = {
};

const FORCED_COMPILER_OPTIONS: Partial<CompilerOptions> = {
// See: https://github.com/rollup/rollup-plugin-typescript/issues/45
// See: https://github.com/rollup/rollup-plugin-typescript/issues/142
declaration: false,
// Delete the `declarationMap` option, as it will cause an error, because we have
// deleted the `declaration` option.
declarationMap: false,
incremental: false,
// eslint-disable-next-line no-undefined
tsBuildInfoFile: undefined,
// Always use tslib
noEmitHelpers: true,
importHelpers: true,
@@ -239,6 +230,12 @@ export function parseTypescriptConfig(
// Normal script files are handled by Rollup.
// parsedConfig.fileNames = parsedConfig.fileNames.filter((file) => file.endsWith('.d.ts'));
normalizeCompilerOptions(ts, parsedConfig.options);
parsedConfig.projectReferences = parsedConfig.projectReferences?.map((projectReference) => {
return {
...projectReference,
path: resolve(dirname(tsConfigPath || cwd), projectReference.originalPath!)
}
})

return parsedConfig;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type Size = 'small' | 'medium' | 'large';
export default interface Animal {
size: Size;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { makeRandomName } from '../core/utilities';

import Animal from '.';

export interface Dog extends Animal {
name: string;
woof(): void;
}

export function createDog(): Dog {
return {
size: 'medium',
woof(this: Dog) {
// eslint-disable-next-line no-console
console.log(`${this.name} says "Woof"!`);
},
name: makeRandomName()
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Animal from './animal';
import { createDog, Dog } from './dog';

export default Animal;
export { createDog, Dog };
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"outDir": "../lib/animals",
"rootDir": ".",
},
"references": [
{ "path": "../core" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"outDir": "../lib/core",
"rootDir": "."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function makeRandomName() {
return 'Bob!?! ';
}

export function lastElementOf<T>(arr: T[]): T | null {
if (arr.length === 0) return null;
return arr[arr.length - 1];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"declaration": true,
"target": "es5",
"module": "commonjs",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"composite": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"files": [],
"references": [
{
"path": "./core"
},
{
"path": "./animals"
},
{
"path": "./zoo"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"outDir": "../lib/zoo",
"rootDir": "."
},
"references": [
{
"path": "../animals"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// import Animal from '../animals/index';
import { Dog, createDog } from '../animals/index';

export default function createZoo(): Array<Dog> {
return [createDog()];
}
18 changes: 17 additions & 1 deletion packages/typescript/test/test.js
Original file line number Diff line number Diff line change
@@ -279,7 +279,7 @@ test('supports overriding tslib with a custom path in a promise', async (t) => {
test('should not resolve .d.ts files', async (t) => {
const bundle = await rollup({
input: 'fixtures/dts/main.ts',
plugins: [typescript({ tsconfig: 'fixtures/dts/main.ts' })],
plugins: [typescript({ tsconfig: 'fixtures/dts/tsconfig.json' })],
onwarn,
external: ['an-import']
});
@@ -538,6 +538,22 @@ test('supports optional chaining', async (t) => {
t.is(output, 'NOT FOUND');
});

test.serial.only('supports project references', async (t) => {
process.chdir('fixtures/project-references');

const bundle = await rollup({
input: 'zoo/zoo.ts',
plugins: [typescript({ tsconfig: 'zoo/tsconfig.json' })],
onwarn
});
const createZoo = await evaluateBundle(bundle);
const zoo = createZoo();

t.is(zoo.length, 1);
t.is(zoo[0].size, 'medium');
t.is(zoo[0].name, 'Bob!?! ');
});

function fakeTypescript(custom) {
return Object.assign(
{