From 1df9908579bf4b71ac0247dbd8076e2da860d22b Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 27 Mar 2019 22:10:31 +0000 Subject: [PATCH] fix(ivy): ngcc - ensure generated source map paths are correct (#29556) Previously we were using absolute paths, but since at rendering time we do not know exactly where the file will be written it is more correct to change to using relative paths. This is actually better all round since it allows the folders to be portable to different machines, etc. PR Close #29556 --- .../ngcc/src/rendering/renderer.ts | 9 ++++++--- .../ngcc/test/rendering/renderer_spec.ts | 18 +++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/compiler-cli/ngcc/src/rendering/renderer.ts b/packages/compiler-cli/ngcc/src/rendering/renderer.ts index 964c27ec47..9f4d05f3ae 100644 --- a/packages/compiler-cli/ngcc/src/rendering/renderer.ts +++ b/packages/compiler-cli/ngcc/src/rendering/renderer.ts @@ -329,8 +329,11 @@ export abstract class Renderer { */ protected renderSourceAndMap( sourceFile: ts.SourceFile, input: SourceMapInfo, output: MagicString): FileInfo[] { - const outputPath = resolve(this.targetPath, relative(this.sourcePath, sourceFile.fileName)); + const outputPath = sourceFile.fileName; const outputMapPath = `${outputPath}.map`; + const relativeSourcePath = basename(outputPath); + const relativeMapPath = `${relativeSourcePath}.map`; + const outputMap = output.generateMap({ source: outputPath, includeContent: true, @@ -339,7 +342,7 @@ export abstract class Renderer { }); // we must set this after generation as magic string does "manipulation" on the path - outputMap.file = outputPath; + outputMap.file = relativeSourcePath; const mergedMap = mergeSourceMaps(input.map && input.map.toObject(), JSON.parse(outputMap.toString())); @@ -350,7 +353,7 @@ export abstract class Renderer { } else { result.push({ path: outputPath, - contents: `${output.toString()}\n${generateMapFileComment(outputMapPath)}` + contents: `${output.toString()}\n${generateMapFileComment(relativeMapPath)}` }); result.push({path: outputMapPath, contents: mergedMap.toJSON()}); } diff --git a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts index c933a04894..69efa4bcb9 100644 --- a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts @@ -107,7 +107,7 @@ describe('Renderer', () => { const OUTPUT_PROGRAM_MAP = fromObject({ 'version': 3, - 'file': '/dist/file.js', + 'file': 'file.js', 'sources': ['/src/file.js'], 'sourcesContent': [INPUT_PROGRAM.contents], 'names': [], @@ -119,7 +119,7 @@ describe('Renderer', () => { 'sources': ['/src/file.ts'], 'names': [], 'mappings': ';;;;;;;;;;AAAA', - 'file': '/dist/file.js', + 'file': 'file.js', 'sourcesContent': [INPUT_PROGRAM.contents] }); @@ -131,10 +131,10 @@ describe('Renderer', () => { const result = renderer.renderProgram( decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses, moduleWithProvidersAnalyses); - expect(result[0].path).toEqual('/dist/file.js'); + expect(result[0].path).toEqual('/src/file.js'); expect(result[0].contents) - .toEqual(RENDERED_CONTENTS + '\n' + generateMapFileComment('/dist/file.js.map')); - expect(result[1].path).toEqual('/dist/file.js.map'); + .toEqual(RENDERED_CONTENTS + '\n' + generateMapFileComment('file.js.map')); + expect(result[1].path).toEqual('/src/file.js.map'); expect(result[1].contents).toEqual(OUTPUT_PROGRAM_MAP.toJSON()); }); @@ -237,7 +237,7 @@ A.ngDirectiveDef = ɵngcc0.ɵdefineDirective({ type: A, selectors: [["", "a", "" const result = renderer.renderProgram( decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses, moduleWithProvidersAnalyses); - expect(result[0].path).toEqual('/dist/file.js'); + expect(result[0].path).toEqual('/src/file.js'); expect(result[0].contents) .toEqual(RENDERED_CONTENTS + '\n' + MERGED_OUTPUT_PROGRAM_MAP.toComment()); expect(result[1]).toBeUndefined(); @@ -257,10 +257,10 @@ A.ngDirectiveDef = ɵngcc0.ɵdefineDirective({ type: A, selectors: [["", "a", "" const result = renderer.renderProgram( decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses, moduleWithProvidersAnalyses); - expect(result[0].path).toEqual('/dist/file.js'); + expect(result[0].path).toEqual('/src/file.js'); expect(result[0].contents) - .toEqual(RENDERED_CONTENTS + '\n' + generateMapFileComment('/dist/file.js.map')); - expect(result[1].path).toEqual('/dist/file.js.map'); + .toEqual(RENDERED_CONTENTS + '\n' + generateMapFileComment('file.js.map')); + expect(result[1].path).toEqual('/src/file.js.map'); expect(result[1].contents).toEqual(MERGED_OUTPUT_PROGRAM_MAP.toJSON()); }); });