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

Using Solution builder to build project references #935

Merged
merged 13 commits into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -9,8 +9,9 @@
"lint": "tslint --project \"./src\"",
"comparison-tests": "tsc --project \"./test/comparison-tests\" && npm link ./test/comparison-tests/testLib && node test/comparison-tests/run-tests.js",
"comparison-tests-generate": "node test/comparison-tests/stub-new-version.js",
"execution-tests": "node test/execution-tests/run-tests.js",
"test": "node test/run-tests.js",
"execution-tests": "git clean -xfd test/execution-tests && node test/execution-tests/run-tests.js",
"test": "git clean -xfd test/execution-tests && node test/run-tests.js",
"clean": "git clean -xfd test/execution-tests",
"docker:build": "docker build -t ts-loader .",
"postdocker:build": "docker run -it ts-loader yarn test"
},
Expand Down Expand Up @@ -88,7 +89,7 @@
"rimraf": "^2.6.2",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"typescript": "^3.1.1",
"typescript": "^3.6.0-dev.20190606",
"webpack": "^4.5.0",
"webpack-cli": "^3.1.1"
},
Expand Down
29 changes: 29 additions & 0 deletions src/config.ts
Expand Up @@ -4,6 +4,7 @@ import * as semver from 'semver';
import * as typescript from 'typescript';
import * as webpack from 'webpack';

import { getCompilerOptions } from './compilerSetup';
import { LoaderOptions, WebpackError } from './interfaces';
import * as logger from './logger';
import { formatErrors } from './utils';
Expand Down Expand Up @@ -144,3 +145,31 @@ export function getConfigParseResult(

return configParseResult;
}

const extendedConfigCache = new Map() as typescript.Map<
typescript.ExtendedConfigCacheEntry
>;
export function getParsedCommandLine(
compiler: typeof typescript,
loaderOptions: LoaderOptions,
configFilePath: string
): typescript.ParsedCommandLine | undefined {
const result = compiler.getParsedCommandLineOfConfigFile(
configFilePath,
loaderOptions.compilerOptions,
{
...compiler.sys,
onUnRecoverableConfigFileDiagnostic: () => {} // tslint:disable-line no-empty
},
extendedConfigCache
);
if (result) {
const options = getCompilerOptions(result);
(compiler as any).setConfigFileInOptions(
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
options,
(result.options as any).configFile
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
);
result.options = options;
}
return result;
}
26 changes: 24 additions & 2 deletions src/instances.ts
Expand Up @@ -17,13 +17,18 @@ import {
WebpackError
} from './interfaces';
import * as logger from './logger';
import { makeServicesHost, makeWatchHost } from './servicesHost';
import {
makeServicesHost,
makeSolutionBuilderHost,
makeWatchHost
} from './servicesHost';
import {
appendSuffixesIfMatch,
ensureProgram,
formatErrors,
isUsingProjectReferences,
makeError
makeError,
supportsSolutionBuild
} from './utils';
import { makeWatchRun } from './watch-run';

Expand Down Expand Up @@ -265,6 +270,23 @@ function successfulTypeScriptInstance(
);
}

if (configFilePath && supportsSolutionBuild(loaderOptions, compiler)) {
// Use solution builder
log.logInfo('Using SolutionBuilder api');
instance.configFilePath = configFilePath;
instance.solutionBuilderHost = makeSolutionBuilderHost(
scriptRegex,
log,
loader,
instance
);
instance.solutionBuilder = compiler.createSolutionBuilderWithWatch(
instance.solutionBuilderHost,
[configFilePath],
{ verbose: true, watch: true }
);
}

if (loaderOptions.experimentalWatchApi && compiler.createWatchProgram) {
log.logInfo('Using watch api');

Expand Down
10 changes: 9 additions & 1 deletion src/interfaces.ts
@@ -1,4 +1,4 @@
export { ModuleResolutionHost } from 'typescript';
export { ModuleResolutionHost, FormatDiagnosticsHost } from 'typescript';
import * as typescript from 'typescript';

import { Chalk } from 'chalk';
Expand Down Expand Up @@ -85,6 +85,14 @@ export interface TSInstance {
program?: typescript.Program;
hasUnaccountedModifiedFiles?: boolean;
changedFilesList?: boolean;

solutionBuilderHost?: typescript.SolutionBuilderWithWatchHost<
typescript.EmitAndSemanticDiagnosticsBuilderProgram
>;
solutionBuilder?: typescript.SolutionBuilder<
typescript.EmitAndSemanticDiagnosticsBuilderProgram
>;
configFilePath?: string;
}

export interface LoaderOptionsCache {
Expand Down
83 changes: 80 additions & 3 deletions src/servicesHost.ts
Expand Up @@ -2,10 +2,12 @@ import * as path from 'path';
import * as typescript from 'typescript';
import * as webpack from 'webpack';

import { getParsedCommandLine } from './config';
import * as constants from './constants';
import {
CustomResolveModuleName,
CustomResolveTypeReferenceDirective,
FormatDiagnosticsHost,
ModuleResolutionHost,
ResolvedModule,
ResolveSync,
Expand Down Expand Up @@ -503,6 +505,83 @@ export function makeWatchHost(
}
}

/**
* Create the TypeScript Watch host
*/
export function makeSolutionBuilderHost(
scriptRegex: RegExp,
log: logger.Logger,
loader: webpack.loader.LoaderContext,
instance: TSInstance
) {
const {
compiler,
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions: {
resolveModuleName: customResolveModuleName,
resolveTypeReferenceDirective: customResolveTypeReferenceDirective
}
} = instance;

// loader.context seems to work fine on Linux / Mac regardless causes problems for @types resolution on Windows for TypeScript < 2.3
const getCurrentDirectory = () => loader.context;
const formatDiagnosticHost: FormatDiagnosticsHost = {
getCurrentDirectory: compiler.sys.getCurrentDirectory,
getCanonicalFileName: compiler.sys.useCaseSensitiveFileNames
? s => s
: s => s.toLowerCase(),
getNewLine: () => compiler.sys.newLine
};

const reportDiagnostic = (d: typescript.Diagnostic) =>
log.logError(compiler.formatDiagnostic(d, formatDiagnosticHost));
const reportSolutionBuilderStatus = (d: typescript.Diagnostic) =>
log.logInfo(compiler.formatDiagnostic(d, formatDiagnosticHost));
const reportWatchStatus = (
d: typescript.Diagnostic,
newLine: string,
_options: typescript.CompilerOptions
) =>
log.logInfo(
`${compiler.flattenDiagnosticMessageText(
d.messageText,
compiler.sys.newLine
)}${newLine + newLine}`
);
const solutionBuilderHost = compiler.createSolutionBuilderWithWatchHost(
compiler.sys,
compiler.createEmitAndSemanticDiagnosticsBuilderProgram,
reportDiagnostic,
reportSolutionBuilderStatus,
reportWatchStatus
);
solutionBuilderHost.getCurrentDirectory = getCurrentDirectory;
solutionBuilderHost.trace = logData => log.logInfo(logData);
solutionBuilderHost.getParsedCommandLine = file =>
getParsedCommandLine(compiler, instance.loaderOptions, file);

// make a (sync) resolver that follows webpack's rules
const resolveSync = makeResolver(loader._compiler.options);
const resolvers = makeResolvers(
compiler,
compilerOptions,
solutionBuilderHost,
customResolveTypeReferenceDirective,
customResolveModuleName,
resolveSync,
appendTsTsxSuffixesIfRequired,
scriptRegex,
instance
);
// used for (/// <reference types="...">) see https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/250#issuecomment-485061329
solutionBuilderHost.resolveTypeReferenceDirectives =
resolvers.resolveTypeReferenceDirectives;
solutionBuilderHost.resolveModuleNames = resolvers.resolveModuleNames;

return solutionBuilderHost;
}

type ResolveTypeReferenceDirective = (
directive: string,
containingFile: string,
Expand Down Expand Up @@ -670,9 +749,7 @@ function addCache(servicesHost: typescript.ModuleResolutionHost) {
const cache = createCache<ReturnType<typeof originalFunction>>(
originalFunction
);
servicesHost[
functionToCache
] = cache.getCached as typescript.ModuleResolutionHost[CacheableFunction];
servicesHost[functionToCache] = cache.getCached as any;
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
clearCacheFunctions.push(cache.clear);
}
});
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Expand Up @@ -235,6 +235,9 @@ export function arrify<T>(val: T | T[]) {
}

export function ensureProgram(instance: TSInstance) {
if (instance.solutionBuilder) {
instance.solutionBuilder.buildReferences(instance.configFilePath!);
}
if (instance && instance.watchHost) {
if (instance.hasUnaccountedModifiedFiles) {
if (instance.changedFilesList) {
Expand Down Expand Up @@ -351,6 +354,13 @@ export function validateSourceMapOncePerProject(
}
}

export function supportsSolutionBuild(
loaderOptions: LoaderOptions,
compiler: typeof typescript
) {
return !!loaderOptions.projectReferences && !!compiler.InvalidatedProjectKind;
}

/**
* Gets the output JS file path for an input file governed by a composite project.
* Pulls from the cache if it exists; computes and caches the result otherwise.
Expand Down
@@ -0,0 +1 @@
{}
@@ -0,0 +1,17 @@
/* eslint-disable no-var, strict */
'use strict';
var webpackConfig = require('./webpack.config.js');
var makeKarmaConfig = require('../../karmaConfig');

module.exports = function(config) {
config.set(
makeKarmaConfig({
config,
webpackConfig,
files: [
// This ensures we have the es6 shims in place from babel and then loads all the tests
'main.js'
]
})
);
};
@@ -0,0 +1,4 @@
*.d.ts
*.js
*.js.map
*.tsbuildinfo
@@ -0,0 +1,5 @@
export const lib = {
one: 1,
two: 2,
three: 3
};
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"sourceMap": true
},
"files": [
"./index.ts"
]
}
2 changes: 2 additions & 0 deletions test/execution-tests/3.6.0_projectReferencesToBeBuilt/main.js
@@ -0,0 +1,2 @@
const testsContext = require.context('./', true, /\.tests\.ts(x?)$/);
testsContext.keys().forEach(testsContext);
10 changes: 10 additions & 0 deletions test/execution-tests/3.6.0_projectReferencesToBeBuilt/package.json
@@ -0,0 +1,10 @@
{
"name": "basic",
"license": "MIT",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {
"@types/jasmine": "^2.5.35",
"jasmine-core": "^2.3.4"
}
}
@@ -0,0 +1,5 @@
import { lib } from '../lib';

export function whatNumbersDoYouHave() {
return [lib.one, lib.two, lib.three];
}
@@ -0,0 +1,7 @@
import { whatNumbersDoYouHave } from "../src/app";

describe("app", () => {
it("code compiles referenced project", () => {
expect(whatNumbersDoYouHave()).toEqual([1, 2, 3]);
});
});
@@ -0,0 +1,11 @@
{
"files": [
"./src/app.ts"
],
"references": [
{ "path": "./lib" }
],
"compilerOptions": {
"noEmitOnError": true
}
}
@@ -0,0 +1,18 @@
module.exports = {
mode: 'development',
entry: './src/app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{ test: /\.ts$/, loader: 'ts-loader', options: { projectReferences: true } }
]
}
}

// for test harness purposes only, you would not need this in a normal project
module.exports.resolveLoader = { alias: { 'ts-loader': require('path').join(__dirname, "../../../index.js") } }
11 changes: 11 additions & 0 deletions test/execution-tests/3.6.0_projectReferencesToBeBuilt/yarn.lock
@@ -0,0 +1,11 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/jasmine@^2.5.35":
version "2.8.5"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.5.tgz#96e58872583fa80c7ea0dd29024b180d5e133678"

jasmine-core@^2.3.4:
version "2.9.1"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.9.1.tgz#b6bbc1d8e65250d56f5888461705ebeeeb88f22f"