Skip to content

Commit

Permalink
Fix remaining tests:
Browse files Browse the repository at this point in the history
- track which files have been renamed by appendSuffixTo and exclude them from needing to be in rootFileNames
- check for project references before rootFileNames
- fix up remaining test tsconfigs
  • Loading branch information
andrewbranch committed Jun 10, 2019
1 parent 8217d51 commit 948cb35
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 29 deletions.
60 changes: 38 additions & 22 deletions src/index.ts
Expand Up @@ -23,6 +23,7 @@ import {
formatErrors,
getAndCacheOutputJSFileName,
getAndCacheProjectReference,
isRootFileOrExempt,
validateSourceMapOncePerProject
} from './utils';

Expand Down Expand Up @@ -75,22 +76,11 @@ function successLoader(

const { version: fileVersion, changedFilesList } = updateFileInCache(
filePath,
loaderContext.resourcePath,
contents,
instance
);

if (changedFilesList && !instance.rootFileNames.has(filePath)) {
reloadRootFileNamesFromConfig(loaderContext, instance);
if (
!instance.rootFileNames.has(filePath) &&
!options.onlyCompileBundledFiles
) {
throw new Error(
`The file ${filePath} is not part of the project specified in tsconfig.json. Make sure it is covered by the fields 'include', 'exclude' and 'files'.`
);
}
}

const referencedProject = getAndCacheProjectReference(filePath, instance);
if (referencedProject !== undefined) {
const [relativeProjectConfigPath, relativeFilePath] = [
Expand Down Expand Up @@ -155,6 +145,18 @@ function successLoader(
callback
);
} else {
if (changedFilesList && !isRootFileOrExempt(filePath, instance)) {
reloadRootFileNamesFromConfig(loaderContext, instance);
if (
!instance.rootFileNames.has(filePath) &&
!options.onlyCompileBundledFiles
) {
throw new Error(
`The file ${filePath} is not part of the project specified in tsconfig.json. Make sure it is covered by the fields 'include', 'exclude' and 'files'.`
);
}
}

const { outputText, sourceMapText } = options.transpileOnly
? getTranspilationEmit(filePath, contents, instance, loaderContext)
: getEmit(rawFilePath, filePath, instance, loaderContext);
Expand Down Expand Up @@ -396,28 +398,32 @@ function makeLoaderOptions(instanceName: string, loaderOptions: LoaderOptions) {
/**
* Either add file to the overall files cache or update it in the cache when the file contents have changed
* Also add the file to the modified files
* @param internalFilePath The path ts-loader uses to track files and register them with the TypeScript compiler. Usually the same as `realFilePath`, but can be different if `loaderOptions.appendTsSuffixTo` or `loaderOptions.appendTsxSuffixTo` are set.
* @param realFilePath The absolute path of the input file on disk.
* @param contents The file contents as a string.
*/
function updateFileInCache(
filePath: string,
internalFilePath: string,
realFilePath: string,
contents: string,
instance: TSInstance
) {
let fileWatcherEventKind: typescript.FileWatcherEventKind | undefined;
let changedFilesList = false;

// Update file contents
let file = instance.files.get(filePath);
let file = instance.files.get(internalFilePath);
if (file === undefined) {
file = instance.otherFiles.get(filePath);
file = instance.otherFiles.get(internalFilePath);
if (file !== undefined) {
instance.otherFiles.delete(filePath);
instance.files.set(filePath, file);
instance.otherFiles.delete(internalFilePath);
instance.files.set(internalFilePath, file);
} else {
if (instance.watchHost !== undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Created;
}
file = { version: 0 };
instance.files.set(filePath, file);
instance.files.set(internalFilePath, file);
}
changedFilesList = true;
instance.changedFilesList = true;
Expand All @@ -430,7 +436,7 @@ function updateFileInCache(
//
// See https://github.com/TypeStrong/ts-loader/issues/943
//
if (!instance.rootFileNames.has(filePath)) {
if (!instance.rootFileNames.has(internalFilePath)) {
instance.version!++;
}

Expand All @@ -450,17 +456,27 @@ function updateFileInCache(
}
}

// Set the real file path if it’s different than the one ts-loader generally uses
file.realFileName =
internalFilePath === realFilePath ? undefined : realFilePath;

if (instance.watchHost !== undefined && fileWatcherEventKind !== undefined) {
instance.hasUnaccountedModifiedFiles = true;
instance.watchHost.invokeFileWatcher(filePath, fileWatcherEventKind);
instance.watchHost.invokeDirectoryWatcher(path.dirname(filePath), filePath);
instance.watchHost.invokeFileWatcher(
internalFilePath,
fileWatcherEventKind
);
instance.watchHost.invokeDirectoryWatcher(
path.dirname(internalFilePath),
internalFilePath
);
}

// push this file to modified files hash.
if (instance.modifiedFiles === null || instance.modifiedFiles === undefined) {
instance.modifiedFiles = new Map<string, TSFile>();
}
instance.modifiedFiles.set(filePath, file);
instance.modifiedFiles.set(internalFilePath, file);
return {
version: file.version,
changedFilesList
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Expand Up @@ -179,6 +179,8 @@ export interface LoaderOptions {
export interface TSFile {
text?: string;
version: number;
/** Only defined when ts-loader has renamed the file internally */
realFileName?: string;
projectReference?: {
/**
* Undefined here means we’ve already checked and confirmed there is no
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Expand Up @@ -255,6 +255,24 @@ export function ensureProgram(instance: TSInstance) {
return instance.program;
}

export function isRootFileOrExempt(
fileName: string,
instance: TSInstance
): boolean {
if (instance.rootFileNames.has(fileName)) {
return true;
}

const file = instance.files.get(fileName);
// Indicates that ts-loader is tracking this file by a different path,
// so should be exempt from being in tsconfig’s root files.
if (file && file.realFileName) {
return true;
}

return false;
}

export function supportsProjectReferences(instance: TSInstance) {
const program = ensureProgram(instance);
return program && !!program.getProjectReferences;
Expand Down
3 changes: 2 additions & 1 deletion test/execution-tests/3.0.1_projectReferences/tsconfig.json
@@ -1,6 +1,7 @@
{
"files": [
"./src/app.ts"
"./src/app.ts",
"./test/app.tests.ts"
],
"references": [
{ "path": "./lib" }
Expand Down
2 changes: 2 additions & 0 deletions test/execution-tests/3.0.1_projectReferences/yarn.lock
Expand Up @@ -5,7 +5,9 @@
"@types/jasmine@^2.5.35":
version "2.8.5"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.5.tgz#96e58872583fa80c7ea0dd29024b180d5e133678"
integrity sha512-mkrHFZTgOXkZhau36K628iKFkjbp11t/bHCkY4Mefu4R6McMg2FD9P3naBv/0Ygyn4sz8baColJp2gdmSekgiw==

jasmine-core@^2.3.4:
version "2.9.1"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.9.1.tgz#b6bbc1d8e65250d56f5888461705ebeeeb88f22f"
integrity sha1-trvB2OZSUNVvWIhGFwXr7uuI8i8=
7 changes: 3 additions & 4 deletions test/execution-tests/allowTsInNodeModules/tsconfig.json
Expand Up @@ -3,9 +3,8 @@
"noEmitOnError": true
},
"include": [
"./node_modules/whitelistedModule"
],
"files": [
"./node_modules/whitelistedFiles/file.ts"
"./node_modules/whitelistedModule",
"./src",
"./test"
]
}
2 changes: 2 additions & 0 deletions test/execution-tests/allowTsInNodeModules/yarn.lock
Expand Up @@ -5,10 +5,12 @@
"@types/jasmine@^2.5.35":
version "2.8.6"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.6.tgz#14445b6a1613cf4e05dd61c3c3256d0e95c0421e"
integrity sha512-clg9raJTY0EOo5pVZKX3ZlMjlYzVU73L71q5OV1jhE2Uezb7oF94jh4CvwrW6wInquQAdhOxJz5VDF2TLUGmmA==

jasmine-core@^2.3.4:
version "2.99.1"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.99.1.tgz#e6400df1e6b56e130b61c4bcd093daa7f6e8ca15"
integrity sha1-5kAN8ea1bhMLYcS80JPap/boyhU=

"whitelistedFiles@file:../../testPackages/whitelistedFiles":
version "1.0.0"
Expand Down
5 changes: 4 additions & 1 deletion test/execution-tests/optionsCaching/tsconfig.json
Expand Up @@ -3,5 +3,8 @@
"target": "esnext",
"jsx": "react",
"noEmitOnError": true
}
},
"include": [
"*"
]
}
2 changes: 1 addition & 1 deletion test/execution-tests/run-tests.js
Expand Up @@ -140,7 +140,7 @@ function runTests(testName) {
console.log('running webpack compilation');
var webpackPath = path.resolve(__dirname, '../../node_modules/webpack/bin/webpack.js');
var program = debug ? 'node --inspect-brk=5858 ' + webpackPath : 'webpack';
execSync(webpackPath + ' --bail', { cwd: testPath, stdio: 'inherit' });
execSync(program + ' --bail', { cwd: testPath, stdio: 'inherit' });
passingTests.push(testName);
}
}
Expand Down

0 comments on commit 948cb35

Please sign in to comment.