Skip to content

Commit d73225d

Browse files
committed
slight fix in codemode
1 parent 4357a70 commit d73225d

File tree

6 files changed

+153
-145
lines changed

6 files changed

+153
-145
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@appmonet/tsickle-loader",
3-
"version": "0.5.0",
2+
"name": "@monet/tsickle-loader",
3+
"version": "0.5.2",
44
"description": "tsickle webpack loader",
55
"main": "lib/index.js",
66
"repository": "github.com/AppMonet/tickle-loader",
@@ -50,4 +50,4 @@
5050
"fs-extra": "^7.0.1",
5151
"tsickle": "^0.34.3"
5252
}
53-
}
53+
}

src/fix-output.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export const fixCode = (code: string): string => {
99
/(?:const|var)\s*.*tsickle_forward_declare_.*\s*=\s*goog\.forwardDeclare.*/g,
1010
""
1111
)
12-
.replace(/goog\.require.*/g, "")
13-
.replace(/tsickle_forward_declare_\d\./g, "");
12+
.replace(/!\.(\w)/gm, "!$1")
13+
.replace(/goog\.require.*/gm, "")
14+
.replace(/tsickle_forward_declare_\d\./gm, "");
1415
};
1516

1617
/**
@@ -26,6 +27,7 @@ export const fixExtern = (extern: string | null): string => {
2627

2728
const fixed = extern
2829
.replace(/var\s*=\s*{};\s*$/gm, "")
30+
.replace(/!\.(\w)/gm, "!$1")
2931
.replace(/^\.(\w+\s+=\s+function.+$)/gm, "var $1")
3032
.replace(/^\./gm, "");
3133

src/index.ts

+132-131
Original file line numberDiff line numberDiff line change
@@ -15,160 +15,161 @@ const EXTERNS_FILE_NAME = "externs.js";
1515
const DEFAULT_CONFIG_FILE = "tsconfig.json";
1616

1717
const optionsSchema = {
18-
type: "object",
19-
properties: {
20-
tsconfig: {
21-
anyOf: [
22-
{
23-
type: "string"
18+
type: "object",
19+
properties: {
20+
tsconfig: {
21+
anyOf: [
22+
{
23+
type: "string"
24+
},
25+
{
26+
type: "boolean"
27+
}
28+
]
2429
},
25-
{
26-
type: "boolean"
30+
externDir: {
31+
type: "string"
2732
}
28-
]
29-
},
30-
externDir: {
31-
type: "string"
3233
}
33-
}
3434
};
3535

3636
interface RealOptions extends OptionObject {
37-
externDir: string;
38-
tsconfig: string;
39-
externFile: string;
40-
compilerConfig: ReturnType<typeof ts.parseJsonConfigFileContent>;
37+
externDir: string;
38+
tsconfig: string;
39+
externFile: string;
40+
compilerConfig: ReturnType<typeof ts.parseJsonConfigFileContent>;
4141
}
4242

4343
const setup = (loaderCTX: webpack.loader.LoaderContext): RealOptions => {
44-
const options = getOptions(loaderCTX);
45-
validateOptions(optionsSchema, options, LOADER_NAME);
46-
47-
const externDir =
48-
options.externDir != null ? options.externDir : DEFAULT_EXTERN_DIR;
49-
const externFile = path.resolve(externDir, EXTERNS_FILE_NAME);
50-
51-
fs.ensureDirSync(externDir);
52-
const tsconfig =
53-
typeof options.tsconfig === "string"
54-
? options.tsconfig
55-
: DEFAULT_CONFIG_FILE;
56-
57-
const compilerConfigFile = ts.readConfigFile(
58-
tsconfig,
59-
(configPath: string) => {
60-
return fs.readFileSync(configPath, "utf-8");
61-
}
62-
);
63-
64-
const compilerConfig = ts.parseJsonConfigFileContent(
65-
compilerConfigFile.config,
66-
ts.sys,
67-
".",
68-
{},
69-
tsconfig
70-
);
71-
72-
return {
73-
tsconfig,
74-
externDir,
75-
externFile,
76-
compilerConfig
77-
};
44+
const options = getOptions(loaderCTX);
45+
validateOptions(optionsSchema, options, LOADER_NAME);
46+
47+
const externDir =
48+
options.externDir != null ? options.externDir : DEFAULT_EXTERN_DIR;
49+
const externFile = path.resolve(externDir, EXTERNS_FILE_NAME);
50+
51+
fs.ensureDirSync(externDir);
52+
const tsconfig =
53+
typeof options.tsconfig === "string"
54+
? options.tsconfig
55+
: DEFAULT_CONFIG_FILE;
56+
57+
const compilerConfigFile = ts.readConfigFile(
58+
tsconfig,
59+
(configPath: string) => {
60+
return fs.readFileSync(configPath, "utf-8");
61+
}
62+
);
63+
64+
const compilerConfig = ts.parseJsonConfigFileContent(
65+
compilerConfigFile.config,
66+
ts.sys,
67+
".",
68+
{},
69+
tsconfig
70+
);
71+
72+
return {
73+
tsconfig,
74+
externDir,
75+
externFile,
76+
compilerConfig
77+
};
7878
};
7979

8080
type LoaderCTX = webpack.loader.LoaderContext;
8181

8282
const handleDiagnostics = (
83-
ctx: LoaderCTX,
84-
diagnostics: ReadonlyArray<ts.Diagnostic>,
85-
diagnosticHost: ts.FormatDiagnosticsHost,
86-
type: "error" | "warning"
83+
ctx: LoaderCTX,
84+
diagnostics: ReadonlyArray<ts.Diagnostic>,
85+
diagnosticHost: ts.FormatDiagnosticsHost,
86+
type: "error" | "warning"
8787
): void => {
88-
const formatted = ts.formatDiagnosticsWithColorAndContext(
89-
diagnostics,
90-
diagnosticHost
91-
);
92-
93-
if (type === "error") {
94-
ctx.emitError(Error(formatted));
95-
} else {
96-
ctx.emitWarning(formatted);
97-
}
88+
const formatted = ts.formatDiagnosticsWithColorAndContext(
89+
diagnostics,
90+
diagnosticHost
91+
);
92+
93+
if (type === "error") {
94+
ctx.emitError(Error(formatted));
95+
} else {
96+
ctx.emitWarning(formatted);
97+
}
9898
};
9999

100100
const tsickleLoader: webpack.loader.Loader = function(
101-
this: LoaderCTX,
102-
_source: string | Buffer
101+
this: LoaderCTX,
102+
_source: string | Buffer
103103
) {
104-
const {
105-
compilerConfig: { options },
106-
externFile
107-
} = setup(this);
108-
109-
// normalize the path to unix-style
110-
const sourceFileName = this.resourcePath.replace(/\\/g, "/");
111-
const compilerHost = ts.createCompilerHost(options);
112-
const program = ts.createProgram([sourceFileName], options, compilerHost);
113-
const diagnostics = ts.getPreEmitDiagnostics(program);
114-
115-
const diagnosticsHost: ts.FormatDiagnosticsHost = {
116-
getNewLine: () => EOL,
117-
getCanonicalFileName: fileName => fileName,
118-
getCurrentDirectory: () => path.dirname(sourceFileName)
119-
};
120-
121-
if (diagnostics.length > 0) {
122-
handleDiagnostics(this, diagnostics, diagnosticsHost, "error");
123-
return;
124-
}
125-
126-
const tsickleHost: tsickle.TsickleHost = {
127-
shouldSkipTsickleProcessing: filename => sourceFileName !== filename,
128-
shouldIgnoreWarningsForPath: () => false,
129-
pathToModuleName: name => name,
130-
fileNameToModuleId: name => name,
131-
options: {}, // TODO: set possible options here
132-
es5Mode: true,
133-
moduleResolutionHost: compilerHost,
134-
googmodule: false,
135-
transformDecorators: true,
136-
transformTypesToClosure: true,
137-
typeBlackListPaths: new Set(),
138-
untyped: false,
139-
logWarning: warning =>
140-
handleDiagnostics(this, [warning], diagnosticsHost, "warning")
141-
};
142-
143-
const jsFiles = new Map<string, string>();
144-
145-
const output = tsickle.emitWithTsickle(
146-
program,
147-
tsickleHost,
148-
compilerHost,
149-
options,
150-
undefined,
151-
(path: string, contents: string) => jsFiles.set(path, contents)
152-
);
153-
154-
const sourceFileAsJs = tsToJS(sourceFileName);
155-
for (const [path, source] of jsFiles) {
156-
if (sourceFileAsJs.indexOf(path) === -1) {
157-
continue;
104+
const {
105+
compilerConfig: { options },
106+
externFile
107+
} = setup(this);
108+
109+
// normalize the path to unix-style
110+
const sourceFileName = this.resourcePath.replace(/\\/g, "/");
111+
const compilerHost = ts.createCompilerHost(options);
112+
const program = ts.createProgram([sourceFileName], options, compilerHost);
113+
const diagnostics = ts.getPreEmitDiagnostics(program);
114+
115+
const diagnosticsHost: ts.FormatDiagnosticsHost = {
116+
getNewLine: () => EOL,
117+
getCanonicalFileName: fileName => fileName,
118+
getCurrentDirectory: () => path.dirname(sourceFileName)
119+
};
120+
121+
if (diagnostics.length > 0) {
122+
handleDiagnostics(this, diagnostics, diagnosticsHost, "error");
123+
return;
158124
}
159125

160-
const tsPathName = jsToTS(path);
161-
const extern = output.externs[tsPathName];
162-
if (extern != null) {
163-
fs.appendFileSync(externFile, fixExtern(extern));
164-
}
126+
const tsickleHost: tsickle.TsickleHost = {
127+
shouldSkipTsickleProcessing: filename => sourceFileName !== filename,
128+
shouldIgnoreWarningsForPath: () => false,
129+
pathToModuleName: name => name,
130+
fileNameToModuleId: name => name,
131+
options: {}, // TODO: set possible options here
132+
es5Mode: true,
133+
moduleResolutionHost: compilerHost,
134+
googmodule: false,
135+
transformDecorators: true,
136+
transformTypesToClosure: true,
137+
typeBlackListPaths: new Set(),
138+
untyped: false,
139+
logWarning: warning =>
140+
handleDiagnostics(this, [warning], diagnosticsHost, "warning")
141+
};
142+
143+
const jsFiles = new Map<string, string>();
144+
145+
const output = tsickle.emitWithTsickle(
146+
program,
147+
tsickleHost,
148+
compilerHost,
149+
options,
150+
undefined,
151+
(path: string, contents: string) => jsFiles.set(path, contents)
152+
);
153+
154+
const sourceFileAsJs = tsToJS(sourceFileName);
155+
for (const [path, source] of jsFiles) {
156+
if (sourceFileAsJs.indexOf(path) === -1) {
157+
continue;
158+
}
159+
160+
const tsPathName = jsToTS(path);
161+
const extern = output.externs[tsPathName];
162+
if (extern != null) {
163+
console.info(`appending extern for ${path} to::\n${extern}\n`);
164+
fs.appendFileSync(externFile, fixExtern(extern));
165+
}
165166

166-
return fixCode(source);
167-
}
167+
return fixCode(source);
168+
}
168169

169-
this.emitError(
170-
Error(`missing compiled result for source file: ${sourceFileName}`)
171-
);
170+
this.emitError(
171+
Error(`missing compiled result for source file: ${sourceFileName}`)
172+
);
172173
};
173174

174175
export default tsickleLoader;

test/compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = (fixture, options = {}) => {
4040
],
4141
optimization: {
4242
minimize: true,
43-
minimizer: options.minimizer,
43+
// minimizer: options.minimizer,
4444
usedExports: true,
4545
splitChunks: {
4646
minSize: 0

test/examples/complex-example.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ export declare interface PublicAPI {
66
class Timer {
77
private intervalHandle: number | null;
88

9-
constructor(private interval: number, private callback: () => void) {}
9+
constructor(private timeInterval: number, private callback: () => void) {}
1010

11-
public cancel() {
11+
cancelHandle() {
1212
if (this.intervalHandle != null) {
1313
clearInterval(this.intervalHandle);
1414
}
1515
}
1616

17-
public start() {
18-
this.cancel();
19-
this.intervalHandle = window.setInterval(() => this.run(), this.interval);
17+
startHandle() {
18+
this.cancelHandle();
19+
this.intervalHandle = window.setInterval(
20+
() => this.run(),
21+
this.timeInterval
22+
);
2023
}
2124

2225
private run() {
@@ -32,9 +35,9 @@ const instance = new Timer(1e3, () => console.info("doing something"));
3235

3336
(window as any)["mySDK"] = {
3437
start() {
35-
instance.start();
38+
instance.startHandle();
3639
},
3740
stop() {
38-
instance.cancel();
41+
instance.cancelHandle();
3942
}
4043
};

test/loader.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ test('will work with closure compiler plugin', async () => {
6767
compilation_level: 'ADVANCED'
6868
})]
6969

70-
const [output] = await compiler('examples/dual-import.ts', {
70+
const [output] = await compiler('examples/complex-example.ts', {
7171
tsconfig: path.resolve(__dirname, 'tsconfig.explicit.json'), // use es2015 modules
7272
mode: 'production',
7373
rules,
7474
minimizer,
7575
externDir
7676
})
77+
78+
fs.writeFileSync('./complex-example.js', output)
7779
expect(output).toBeTruthy()
7880
}, 15e3) // this can be *very* slow

0 commit comments

Comments
 (0)