Skip to content

Commit 7f3f9d5

Browse files
committed
apply AppMonet#5
1 parent ace5f21 commit 7f3f9d5

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

src/fix-output.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
/**
22
* Fix some common issues with the tsickle output
3-
* @param code {string} the transformed typescript code
3+
* @param {string} code the transformed typescript code
44
* @return {string} transformed code
55
*/
66
export const fixCode = (code: string): string => {
77
return code
8-
.replace(/^\s*var\s*tsickle.+=.*goog\.requireType.*$/g, "")
8+
.replace(
9+
/(?:const|var)\s*.*tsickle_.*\s*=\s*goog\.requireType.*/g,
10+
""
11+
)
912
.replace(
1013
/(?:const|var)\s*.*tsickle_forward_declare_.*\s*=\s*goog\.forwardDeclare.*/g,
1114
""
1215
)
13-
.replace(/!\.(\w)/gm, "!$1")
14-
.replace(/goog\.require.*/gm, "")
15-
.replace(/tsickle_forward_declare_\d\./gm, "")
16-
.replace(/var\s* tsickle_.+=\s+/g, "");
16+
.replace(/goog\.require.*/g, "")
17+
.replace(/tsickle_forward_declare_\d\./g, "")
18+
.replace(/\/\/# sourceMappingURL=.*\.js\.map/g, "");
1719
};
1820

1921
/**
2022
* Fix some issues with the tsickle extern file definition specific
2123
* to typescript-in-webpack
22-
* @param extern {string} the extern definition file content
24+
* @param {string} extern the extern definition file content
2325
* @return {string} transformed code
2426
*/
2527
export const fixExtern = (extern: string | null): string => {

src/index.ts

+37-26
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import ts from "typescript";
66
import { EOL } from "os";
77
import webpack from "webpack";
88
import { fixCode, fixExtern } from "./fix-output";
9-
import { jsToTS, tsToJS } from "./path-utils";
109
import { JSONSchema7 } from "json-schema";
10+
import { RawSourceMap } from "source-map";
1111

1212
const LOADER_NAME = "tsickle-loader";
1313
const DEFAULT_EXTERN_DIR = "dist/externs";
@@ -64,7 +64,7 @@ const setup = (loaderCTX: LoaderCTX): RealOptions => {
6464
const compilerConfig = ts.parseJsonConfigFileContent(
6565
compilerConfigFile.config,
6666
ts.sys,
67-
".",
67+
path.dirname(tsconfig || ''),
6868
{},
6969
tsconfig
7070
);
@@ -142,38 +142,49 @@ const tsickleLoader = function (
142142
rootDirsRelative: (f: string) => f,
143143
};
144144

145-
const jsFiles = new Map<string, string>();
145+
let transpiledSources: string[] = [];
146+
let transpiledSourceMaps: string[] = [];
146147

147-
const output = tsickle.emitWithTsickle(
148+
const output = tsickle.emit(
148149
program,
149150
tsickleHost,
150-
compilerHost,
151-
options,
152-
undefined,
153-
(path: string, contents: string) => jsFiles.set(path, contents)
151+
(jsFileName: string, contents: string, _writeByteOrderMark: boolean, _onError, tsSourceFiles) => {
152+
for (const source of tsSourceFiles ?? []) {
153+
if (source.fileName === sourceFileName) {
154+
if (jsFileName.endsWith('.map')) {
155+
transpiledSourceMaps.push(contents);
156+
} else {
157+
transpiledSources.push(contents);
158+
}
159+
}
160+
}
161+
},
162+
program.getSourceFile(sourceFileName)
154163
);
155164

156-
const sourceFileAsJs = tsToJS(sourceFileName);
157-
for (const [path, source] of jsFiles) {
158-
if (sourceFileAsJs.indexOf(path) === -1) {
159-
continue;
160-
}
161-
162-
const tsPathName = jsToTS(path);
163-
const extern = output.externs[tsPathName];
164-
if (extern != null) {
165-
// console.info(`appending extern for ${path} to (${externFile}) ::\n${extern}\n`);
166-
fs.appendFileSync(externFile, fixExtern(extern));
167-
}
165+
if (transpiledSources.length !== 1) {
166+
this.emitError(
167+
Error(`missing compiled result for source file: ${sourceFileName}`)
168+
);
169+
return;
170+
}
171+
if (this.sourceMap && transpiledSourceMaps.length !== 1) {
172+
this.emitError(
173+
Error(`tsconfig must specify sourceMap: "true" when sourcemaps are enabled!`)
174+
);
175+
return;
176+
}
168177

169-
const fixed = fixCode(source);
170-
// console.info("FIXED CODE:: \n", fixed);
171-
return fixed;
178+
const extern = output.externs[sourceFileName];
179+
if (extern != null) {
180+
fs.appendFileSync(externFile, fixExtern(extern));
172181
}
173182

174-
this.emitError(
175-
Error(`missing compiled result for source file: ${sourceFileName}`)
176-
);
183+
let sourceMap: RawSourceMap | undefined = undefined;
184+
if (this.sourceMap) {
185+
sourceMap = JSON.parse(transpiledSourceMaps[0]);
186+
}
187+
this.callback(null, fixCode(transpiledSources[0]), sourceMap);
177188
};
178189

179190
export default tsickleLoader;

src/path-utils.ts

-8
This file was deleted.

test/tsconfig.explicit.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
2-
"include": [
3-
"./examples/**/*.ts"
4-
],
2+
"include": ["./examples/**/*.ts"],
53
"compilerOptions": {
64
"module": "es2015",
75
"moduleResolution": "node",
86
"target": "es5",
9-
"rootDir": "."
7+
"rootDir": ".",
8+
"sourceMap": true
109
}
11-
}
10+
}

0 commit comments

Comments
 (0)