Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 066de6d

Browse files
committed
fix(sourcemaps): fix source maps for all files
1 parent 2affdc0 commit 066de6d

File tree

5 files changed

+22
-70
lines changed

5 files changed

+22
-70
lines changed

src/bundle.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ function runBundle(context: BuildContext, options: BuildOptions, rollupConfig: R
5757
// ngc does full production builds itself and the bundler
5858
// will already have receive transpiled and AoT templates
5959

60-
// dev mode auto-adds the ion-inline-template and
61-
// ion-compiler plugins, which will inline templates
62-
// and transpile source typescript code to JS before bundling
60+
// dev mode auto-adds the ion-compiler plugin, which will inline
61+
// templates and transpile source typescript code to JS before bundling
6362
rollupConfig.plugins.unshift(
64-
inlineTemplate({}),
6563
ionCompiler({
6664
rootDir: context.rootDir,
6765
sourceMap: rollupConfig.sourceMap

src/plugins/ion-compiler.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// https://github.com/rollup/rollup-plugin-typescript
33
// MIT Licenced
44

5-
import { helperFns, helpersId } from '../util/typescript-helpers';
65
import { getCompilerOptions, resolveId, transpile } from '../transpile';
6+
import { helperFns, helpersId } from '../util/typescript-helpers';
7+
import { inlineTemplate } from '../template';
78
import * as pluginutils from 'rollup-pluginutils';
89

910

@@ -30,6 +31,7 @@ export default function ionCompiler(options: IonCompilerOptions) {
3031

3132
transform(sourceText: string, sourcePath: string): any {
3233
if (filter(sourcePath)) {
34+
sourceText = inlineTemplate(sourceText, sourcePath);
3335
return transpile(sourceText, sourcePath, compilerOptions, true);
3436
}
3537
}

src/plugins/ion-inline-template.ts

-25
This file was deleted.

src/spec/template.spec.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('template', () => {
1313
const sourcePath = 'somefile.ts';
1414
const output = inlineTemplate(sourceText, sourcePath);
1515

16-
expect(output).toEqual(null);
16+
expect(output).toEqual(sourceText);
1717
});
1818

1919
it('should do nothing for files with incomplete @Component', () => {
@@ -23,7 +23,7 @@ describe('template', () => {
2323
const sourcePath = 'somefile.ts';
2424
const output = inlineTemplate(sourceText, sourcePath);
2525

26-
expect(output).toEqual(null);
26+
expect(output).toEqual(sourceText);
2727
});
2828

2929
it('should do nothing for files without @Component', () => {
@@ -33,31 +33,26 @@ describe('template', () => {
3333
const sourcePath = 'somefile.ts';
3434
const output = inlineTemplate(sourceText, sourcePath);
3535

36-
expect(output).toEqual(null);
36+
expect(output).toEqual(sourceText);
3737
});
3838

3939
describe('replaceTemplateUrl', () => {
4040

4141
it('should turn the template into one line', () => {
4242
const str = `
4343
@Component({
44-
templateUrl: "somepage.html"
45-
})
46-
`;
44+
templateUrl: "somepage.html"})`;
4745
const templateContent = `
48-
<div>
49-
this is multiline content
50-
</div>
46+
<div>\t
47+
this is "multiline" 'content'
48+
</div>\r
5149
`;
5250
const match = getTemplateMatch(str);
5351
const result = replaceTemplateUrl(match, templateContent);
5452

55-
expect(result).toEqual(`@Component({template: /* ion-inline-template */ \`
56-
<div>
57-
this is multiline content
58-
</div>
59-
\`
60-
})`);
53+
const expected = `@Component({template: /* ion-inline-template */ '\\n <div>\t\\n this is "multiline" \\'content\\'\\n </div>\\n\\n '})`;
54+
55+
expect(result).toEqual(expected);
6156
});
6257

6358
});

src/template.ts

+7-25
Original file line numberDiff line numberDiff line change
@@ -80,45 +80,33 @@ function getSourceComponentFile(htmlFilePath: string, context: BuildContext) {
8080
}
8181

8282

83-
export function inlineTemplate(sourceText: string, sourcePath: string): InlineTemplateOutput {
83+
export function inlineTemplate(sourceText: string, sourcePath: string): string {
8484
const magicString = new MagicString(sourceText);
8585
const componentDir = parse(sourcePath).dir;
8686
let match: TemplateUrlMatch;
87-
let hasReplacements = false;
8887
let replacement: string;
8988
let lastStart = -1;
9089

9190
while (match = getTemplateMatch(magicString.toString())) {
9291
if (match.start === lastStart) {
9392
// panic! we don't want to melt any machines if there's a bug
9493
Logger.debug(`Error matching component: ${match.component}`);
95-
return null;
94+
return magicString.toString();
9695
}
9796
lastStart = match.start;
9897

9998
if (match.templateUrl === '') {
10099
Logger.error(`Error @Component templateUrl missing in: "${sourcePath}"`);
101-
return null;
100+
return magicString.toString();
102101
}
103102

104103
replacement = updateTemplate(componentDir, match);
105104
if (replacement) {
106105
magicString.overwrite(match.start, match.end, replacement);
107-
hasReplacements = true;
108106
}
109107
}
110108

111-
if (hasReplacements) {
112-
return {
113-
code: magicString.toString(),
114-
map: magicString.generateMap({
115-
source: sourcePath,
116-
hires: false
117-
})
118-
};
119-
}
120-
121-
return null;
109+
return magicString.toString();
122110
}
123111

124112

@@ -133,11 +121,11 @@ function updateTemplate(componentDir: string, match: TemplateUrlMatch): string {
133121

134122
export function replaceTemplateUrl(match: TemplateUrlMatch, templateContent: string): string {
135123
// turn the template into one line and espcape single quotes
136-
// templateContent = templateContent.replace(/\r|\n/g, '\\n');
137-
// templateContent = templateContent.replace(/\'/g, '\\\'');
124+
templateContent = templateContent.replace(/\r|\n/g, '\\n');
125+
templateContent = templateContent.replace(/\'/g, '\\\'');
138126

139127
const orgTemplateProperty = match.templateProperty;
140-
const newTemplateProperty = 'template: /* ion-inline-template */ `' + templateContent + '`';
128+
const newTemplateProperty = 'template: /* ion-inline-template */ \'' + templateContent + '\'';
141129

142130
return match.component.replace(orgTemplateProperty, newTemplateProperty);
143131
}
@@ -173,12 +161,6 @@ export function getTemplateMatch(str: string): TemplateUrlMatch {
173161

174162
const COMPONENT_REGEX = /@Component\s*?\(\s*?(\{([\s\S]*?)(\s*templateUrl\s*:\s*(['"`])(.*?)(['"`])\s*?)([\s\S]*?)}\s*?)\)/m;
175163

176-
177-
export interface InlineTemplateOutput {
178-
code: string;
179-
map: any;
180-
}
181-
182164
export interface TemplateUrlMatch {
183165
start: number;
184166
end: number;

0 commit comments

Comments
 (0)