Skip to content

Commit

Permalink
fix #433, ensure deps not cleared for HMR
Browse files Browse the repository at this point in the history
further fix

accept new baseline

fix error

fix issue441

always show errors

fix issue372

fix json

fix

fix

ignore built

new baseline
  • Loading branch information
HerringtonDarkholme committed Feb 21, 2017
1 parent cc95c0b commit d16f6bd
Show file tree
Hide file tree
Showing 88 changed files with 233 additions and 410 deletions.
66 changes: 43 additions & 23 deletions src/after-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function makeAfterCompile(
}

removeTSLoaderErrors(compilation.errors);
addWatchFileList(instance, compilation);

provideCompilerOptionDiagnosticErrorsToWebpack(getCompilerOptionDiagnostics, compilation, instance, configFilePath);
getCompilerOptionDiagnostics = false;
Expand All @@ -43,6 +44,13 @@ interface Modules {
[modulePath: string]: interfaces.WebpackModule[];
}

// for modules without JS emission, we need to add it to watch list manually
function addWatchFileList(instance: interfaces.TSInstance, compilation: interfaces.WebpackCompilation) {
var program = instance.languageService.getProgram()
var files = program.getSourceFiles().map(f => f.fileName)
Array.prototype.push.apply(compilation.fileDependencies, files.map(path.normalize));
}

/**
* handle compiler option errors after the first compile
*/
Expand Down Expand Up @@ -125,32 +133,44 @@ function provideErrorsToWebpack(
instance: interfaces.TSInstance
) {
const { compiler, languageService, files, loaderOptions } = instance;
Object.keys(filesToCheckForErrors)
.filter(filePath => !!filePath.match(constants.dtsTsTsxRegex))
.forEach(filePath => {
const errors = languageService.getSyntacticDiagnostics(filePath).concat(languageService.getSemanticDiagnostics(filePath));
if (errors.length > 0) {
filesWithErrors[filePath] = files[filePath];
}

// if we have access to a webpack module, use that
if (utils.hasOwnProperty(modules, filePath)) {
const associatedModules = modules[filePath];
const fileNames = Object.keys(filesToCheckForErrors);

associatedModules.forEach(module => {
// remove any existing errors
removeTSLoaderErrors(module.errors);
if (fileNames.some(fn => constants.dtsDtsxRegex.test(fn))) {
Object.keys(instance.files)
.filter(filePath => !!filePath.match(constants.dtsTsTsxRegex))
.forEach(addErrorToFile);
return;
}

// append errors
const formattedErrors = utils.formatErrors(errors, loaderOptions, compiler, { module });
utils.registerWebpackErrors(module.errors, formattedErrors);
utils.registerWebpackErrors(compilation.errors, formattedErrors);
});
} else {
// otherwise it's a more generic error
utils.registerWebpackErrors(compilation.errors, utils.formatErrors(errors, loaderOptions, compiler, { file: filePath }));
}
});
fileNames
.filter(filePath => !!filePath.match(constants.dtsTsTsxRegex))
.forEach(addErrorToFile);

function addErrorToFile(filePath: string) {
const errors = languageService.getSyntacticDiagnostics(filePath).concat(languageService.getSemanticDiagnostics(filePath));
if (errors.length > 0) {
filesWithErrors[filePath] = files[filePath];
}

// if we have access to a webpack module, use that
if (utils.hasOwnProperty(modules, filePath)) {
const associatedModules = modules[filePath];

associatedModules.forEach(module => {
// remove any existing errors
removeTSLoaderErrors(module.errors);

// append errors
const formattedErrors = utils.formatErrors(errors, loaderOptions, compiler, { module });
utils.registerWebpackErrors(module.errors, formattedErrors);
utils.registerWebpackErrors(compilation.errors, formattedErrors);
});
} else {
// otherwise it's a more generic error
utils.registerWebpackErrors(compilation.errors, utils.formatErrors(errors, loaderOptions, compiler, { file: filePath }));
}
}
}

/**
Expand Down
20 changes: 0 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,10 @@ function getEmit(
) {
// Emit Javascript
const output = instance.languageService.getEmitOutput(filePath);

loader.clearDependencies();
loader.addDependency(filePath);

const allDefinitionFiles = Object.keys(instance.files).filter(defFilePath => !!defFilePath.match(constants.dtsDtsxRegex));

// Make this file dependent on *all* definition files in the program
const addDependency = loader.addDependency.bind(loader);
allDefinitionFiles.forEach(addDependency);

/* - alternative approach to the below which is more correct but has a heavy performance cost
see https://github.com/TypeStrong/ts-loader/issues/393
with this approach constEnumReExportWatch test will pass; without it, not.
// Additionally make this file dependent on all imported files as well
// as any deeper recursive dependencies
const additionalDependencies = utils.collectAllDependencies(instance.dependencyGraph, filePath);
*/

// Additionally make this file dependent on all imported files
const additionalDependencies = instance.dependencyGraph[filePath];
if (additionalDependencies) {
additionalDependencies.forEach(addDependency);
}

loader._module.meta.tsLoaderDefinitionFileVersions = allDefinitionFiles
.concat(additionalDependencies)
Expand Down
2 changes: 1 addition & 1 deletion src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function getTypeScriptInstance(
instance.languageService = compiler.createLanguageService(servicesHost, compiler.createDocumentRegistry());

loader._compiler.plugin("after-compile", afterCompile(instance, configFilePath));
loader._compiler.plugin("watch-run", watchRun(instance));
loader._compiler.plugin("watch-run", watchRun(instance, loader));

return { instance };
}
8 changes: 8 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ export interface Webpack {
ts: {},
resolve: Resolve;
};
/**
* The output options
*/
outputOptions?: {
json?: boolean
}

}

export interface Compiler {
Expand All @@ -112,6 +119,7 @@ export interface WebpackCompilation {
compiler: WebpackCompiler;
errors: WebpackError[];
modules: WebpackModule[];
fileDependencies: string[];
assets: {
[index: string]: {
size: () => number;
Expand Down
13 changes: 12 additions & 1 deletion src/watch-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ import constants = require('./constants');
* Make function which will manually update changed files
*/
function makeWatchRun(
instance: interfaces.TSInstance
instance: interfaces.TSInstance,
loader: interfaces.Webpack
) {
// always output stats
var lastHash: string;
const outputOptions = loader.outputOptions || {};
loader._compiler.plugin('done', (stats: any) => {
if (lastHash === stats.hash && !outputOptions.json) {
process.stdout.write(stats.toString(outputOptions) + '\n');
}
lastHash = stats.hash;
});

return (watching: interfaces.WebpackWatching, cb: () => void) => {
const watcher = watching.compiler.watchFileSystem.watcher ||
watching.compiler.watchFileSystem.wfs.watcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 2.74 kB 0 [emitted] main
chunk {0} bundle.js (main) 139 bytes [entry] [rendered]
[0] ./.test/aliasResolution/common/components/myComponent.ts 46 bytes {0} [built]
[1] ./.test/aliasResolution/app.ts 93 bytes {0} [built] [1 error]

ERROR in ./.test/aliasResolution/app.ts
(2,31): error TS2307: Cannot find module 'components/myComponent2'.
[0] ./.test/aliasResolution/common/components/myComponent.ts 46 bytes {0}
[1] ./.test/aliasResolution/app.ts 93 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 2.74 kB 0 [emitted] main
chunk {0} bundle.js (main) 138 bytes [entry] [rendered]
[0] ./.test/aliasResolution/common/components/myComponent.ts 45 bytes {0} [built]
[0] ./.test/aliasResolution/common/components/myComponent.ts 45 bytes {0}
[1] ./.test/aliasResolution/app.ts 93 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Asset Size Chunks Chunk Names
bundle.js 2.74 kB 0 [emitted] main
chunk {0} bundle.js (main) 138 bytes [entry] [rendered]
[0] ./.test/aliasResolution/common/components/myComponent.ts 45 bytes {0} [built]
[1] ./.test/aliasResolution/app.ts 93 bytes {0} [built] [1 error]
[0] ./.test/aliasResolution/common/components/myComponent.ts 45 bytes {0}
[1] ./.test/aliasResolution/app.ts 93 bytes {0} [1 error]

ERROR in ./.test/aliasResolution/app.ts
(2,31): error TS2307: Cannot find module 'components/myComponent2'.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 2.57 kB 0 [emitted] main
chunk {0} bundle.js (main) 38 bytes [entry] [rendered]
[0] ./.test/allowJs-entryFileIsJs/src/index.js 38 bytes {0} [built]
[0] ./.test/allowJs-entryFileIsJs/src/index.js 38 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 2.53 kB 0 [emitted] main
chunk {0} bundle.js (main) 24 bytes [entry] [rendered]
[0] ./.test/allowJs-entryFileIsJs/src/index.js 24 bytes {0} [built]
[0] ./.test/allowJs-entryFileIsJs/src/index.js 24 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Asset Size Chunks Chunk Names
bundle.js 3.29 kB 0 [emitted] main
chunk {0} bundle.js (main) 606 bytes [entry] [rendered]
[0] ./.test/appendSuffixTo/component.vue 154 bytes {0} [built]
[1] ./.test/appendSuffixTo/helper.ts 100 bytes {0} [built]
[2] ./.test/appendSuffixTo/index.vue 352 bytes {0} [built]
[0] ./.test/appendSuffixTo/component.vue 154 bytes {0}
[1] ./.test/appendSuffixTo/helper.ts 100 bytes {0}
[2] ./.test/appendSuffixTo/index.vue 352 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 3 kB 0 [emitted] main
chunk {0} bundle.js (main) 251 bytes [entry] [rendered]
[0] ./.test/babel-es6resolveParent/index.tsx 251 bytes {0} [built]
[0] ./.test/babel-es6resolveParent/index.tsx 251 bytes {0}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
bundle.js 3.87 kB 0 [emitted] main
bundle.js.map 2.83 kB 0 [emitted] main
chunk {0} bundle.js, bundle.js.map (main) 1.2 kB [entry] [rendered]
[0] ./.test/babel-issue81/a.ts 1.2 kB {0} [built]
[0] ./.test/babel-issue81/a.ts 1.2 kB {0}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 3.05 kB 0 [emitted] main
chunk {0} bundle.js (main) 110 bytes [entry] [rendered]
[0] ./.test/babel-issue92/submodule/submodule.tsx 38 bytes {0} [built]
[1] ./.test/babel-issue92/app.ts 72 bytes {0} [built]
[0] ./.test/babel-issue92/submodule/submodule.tsx 38 bytes {0}
[1] ./.test/babel-issue92/app.ts 72 bytes {0}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
bundle.js 3.42 kB 0 [emitted] main
chunk {0} bundle.js (main) 842 bytes [entry] [rendered]
[0] external "react" 42 bytes {0} [not cacheable]
[1] ./.test/babel-jsxPreserve/app.tsx 800 bytes {0} [built]
[1] ./.test/babel-jsxPreserve/app.tsx 800 bytes {0}
6 changes: 3 additions & 3 deletions test/comparison-tests/basic/expectedOutput-2.1/output.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Asset Size Chunks Chunk Names
bundle.js 3 kB 0 [emitted] main
chunk {0} bundle.js (main) 346 bytes [entry] [rendered]
[0] ./.test/basic/lib/externalLib.js 55 bytes {0} [built]
[1] ./.test/basic/submodule/submodule.ts 149 bytes {0} [built]
[2] ./.test/basic/app.ts 142 bytes {0} [built]
[0] ./.test/basic/lib/externalLib.js 55 bytes {0}
[1] ./.test/basic/submodule/submodule.ts 149 bytes {0}
[2] ./.test/basic/app.ts 142 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Asset Size Chunks Chunk Names
bundle.js 3 kB 0 [emitted] main
chunk {0} bundle.js (main) 347 bytes [entry] [rendered]
[0] ./.test/basic/lib/externalLib.js 55 bytes {0}
[1] ./.test/basic/submodule/submodule.ts 149 bytes {0}
[0] ./.test/basic/lib/externalLib.js 55 bytes {0} [built]
[1] ./.test/basic/submodule/submodule.ts 149 bytes {0} [built]
[2] ./.test/basic/app.ts 143 bytes {0} [built]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ bundle.js 3 kB 0 [emitted] main
chunk {0} bundle.js (main) 347 bytes [entry] [rendered]
[0] ./.test/basic/lib/externalLib.js 55 bytes {0}
[1] ./.test/basic/submodule/submodule.ts 149 bytes {0}
[2] ./.test/basic/app.ts 143 bytes {0} [built] [1 error]

ERROR in ./.test/basic/app.ts
(3,13): error TS2339: Property 'doSomething2' does not exist on type 'typeof externalLib'.
[2] ./.test/basic/app.ts 143 bytes {0}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ bundle.js 3 kB 0 [emitted] main
chunk {0} bundle.js (main) 346 bytes [entry] [rendered]
[0] ./.test/basic/lib/externalLib.js 55 bytes {0}
[1] ./.test/basic/submodule/submodule.ts 149 bytes {0}
[2] ./.test/basic/app.ts 142 bytes {0} [built]
[2] ./.test/basic/app.ts 142 bytes {0}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
0.bundle.js 249 bytes 0 [emitted]
bundle.js 6.46 kB 1 [emitted] main
chunk {0} 0.bundle.js 72 bytes {1} [rendered]
[0] ./.test/codeSplitting/c.ts 36 bytes {0} [built]
[1] ./.test/codeSplitting/d.ts 36 bytes {0} [built]
[0] ./.test/codeSplitting/c.ts 36 bytes {0}
[1] ./.test/codeSplitting/d.ts 36 bytes {0}
chunk {1} bundle.js (main) 634 bytes [entry] [rendered]
[2] ./.test/codeSplitting/a.ts 36 bytes {1} [built]
[3] ./.test/codeSplitting/b.ts 36 bytes {1} [built]
[4] ./.test/codeSplitting/app.ts 562 bytes {1} [built]
[2] ./.test/codeSplitting/a.ts 36 bytes {1}
[3] ./.test/codeSplitting/b.ts 36 bytes {1}
[4] ./.test/codeSplitting/app.ts 562 bytes {1}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Asset Size Chunks Chunk Names
bundle.js 1.5 kB 0 [emitted] main
chunk {0} bundle.js (main) 285 bytes [entry] [rendered]
[0] ./.test/conditionalRequire/app.ts 285 bytes {0} [built]
[0] ./.test/conditionalRequire/app.ts 285 bytes {0}

WARNING in bundle.js from UglifyJs
Condition always false [bundle.js:78,4]
Expand Down
3 changes: 3 additions & 0 deletions test/comparison-tests/create-and-execute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ function storeStats(stats, testState, paths, outputs, patch, options) {
.replace(new RegExp(regexEscape(rootPath + path.sep), 'g'), '')
.replace(new RegExp(regexEscape(rootPath), 'g'), '')
.replace(new RegExp(regexEscape(rootPathWithIncorrectWindowsSeparator), 'g'), '')
.replace(/\[built\]/g, '')
.replace(/\.transpile/g, '');

console.log(statsString)
fs.writeFileSync(path.join(paths.actualOutput, statsFileName), statsString);
if (saveOutputMode) {
var patchedStatsFileName = patch + '/' + statsFileName;
Expand Down Expand Up @@ -390,6 +392,7 @@ function normaliseString(platformSpecificContent) {
// Convert '/' to '\' and back to '/' so slashes are treated the same
// whether running / generated on windows or *nix
.replace(new RegExp(regexEscape('/'), 'g'), '\\')
.replace(/\[built\]/g, '')
.replace(new RegExp(regexEscape('\\'), 'g'), '/');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 2.59 kB 0 [emitted] main
chunk {0} bundle.js (main) 55 bytes [entry] [rendered]
[0] ./.test/declarationDeps/app.ts 55 bytes {0} [built]
[0] ./.test/declarationDeps/app.ts 55 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 2.55 kB 0 [emitted] main
chunk {0} bundle.js (main) 41 bytes [entry] [rendered]
[0] ./.test/declarationDeps/app.ts 41 bytes {0} [built] [1 error]

ERROR in ./.test/declarationDeps/app.ts
(2,7): error TS2339: Property 'sayHi' does not exist on type 'typeof Hello'.
[0] ./.test/declarationDeps/app.ts 41 bytes {0}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
app.d.ts 110 bytes [emitted]
sub/dep.d.ts 63 bytes [emitted]
chunk {0} bundle.js (main) 737 bytes [entry] [rendered]
[0] ./.test/declarationOutput/sub/dep.ts 168 bytes {0} [built]
[1] ./.test/declarationOutput/app.ts 569 bytes {0} [built]
[0] ./.test/declarationOutput/sub/dep.ts 168 bytes {0}
[1] ./.test/declarationOutput/app.ts 569 bytes {0}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 2.75 kB 0 [emitted] main
chunk {0} bundle.js (main) 140 bytes [entry] [rendered]
[0] ./.test/declarationWatch/dep.ts 59 bytes {0} [built]
[1] ./.test/declarationWatch/app.ts 81 bytes {0} [built]
[0] ./.test/declarationWatch/dep.ts 59 bytes {0}
[1] ./.test/declarationWatch/app.ts 81 bytes {0}
Loading

0 comments on commit d16f6bd

Please sign in to comment.