Skip to content

Commit 08c85bd

Browse files
committed
whitespace crap + handle tsx
1 parent d73225d commit 08c85bd

File tree

4 files changed

+137
-135
lines changed

4 files changed

+137
-135
lines changed

src/fix-output.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const fixExtern = (extern: string | null): string => {
2828
const fixed = extern
2929
.replace(/var\s*=\s*{};\s*$/gm, "")
3030
.replace(/!\.(\w)/gm, "!$1")
31+
.replace(/^\.(\w+\s+=\s+{}\s*;?\s*$)/gm, "var $1")
3132
.replace(/^\.(\w+\s+=\s+function.+$)/gm, "var $1")
3233
.replace(/^\./gm, "");
3334

src/index.ts

+133-132
Original file line numberDiff line numberDiff line change
@@ -15,161 +15,162 @@ 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"
24-
},
25-
{
26-
type: "boolean"
27-
}
28-
]
18+
type: "object",
19+
properties: {
20+
tsconfig: {
21+
anyOf: [
22+
{
23+
type: "string"
2924
},
30-
externDir: {
31-
type: "string"
25+
{
26+
type: "boolean"
3227
}
28+
]
29+
},
30+
externDir: {
31+
type: "string"
3332
}
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;
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: string) =>
128+
sourceFileName !== filename,
129+
shouldIgnoreWarningsForPath: () => false,
130+
pathToModuleName: (name: string) => name,
131+
fileNameToModuleId: (name: string) => name,
132+
options: {}, // TODO: set possible options here
133+
es5Mode: true,
134+
moduleResolutionHost: compilerHost,
135+
googmodule: false,
136+
transformDecorators: true,
137+
transformTypesToClosure: true,
138+
typeBlackListPaths: new Set(),
139+
untyped: false,
140+
logWarning: (warning: unknown) =>
141+
handleDiagnostics(this, [warning], diagnosticsHost, "warning")
142+
};
143+
144+
const jsFiles = new Map<string, string>();
145+
146+
const output = tsickle.emitWithTsickle(
147+
program,
148+
tsickleHost,
149+
compilerHost,
150+
options,
151+
undefined,
152+
(path: string, contents: string) => jsFiles.set(path, contents)
153+
);
154+
155+
const sourceFileAsJs = tsToJS(sourceFileName);
156+
for (const [path, source] of jsFiles) {
157+
if (sourceFileAsJs.indexOf(path) === -1) {
158+
continue;
124159
}
125160

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-
}
166-
167-
return fixCode(source);
161+
const tsPathName = jsToTS(path);
162+
const extern = output.externs[tsPathName];
163+
if (extern != null) {
164+
console.info(`appending extern for ${path} to::\n${extern}\n`);
165+
fs.appendFileSync(externFile, fixExtern(extern));
168166
}
169167

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

175176
export default tsickleLoader;

src/path-utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const JS_RXP = /\.js$/;
2-
const TS_RXP = /\.ts$/;
1+
const JS_RXP = /\.jsx?$/;
2+
const TS_RXP = /\.tsx?$/;
33

44
export const jsToTS = (path: string | null) =>
55
path != null ? path.replace(JS_RXP, ".ts") : "";

test/compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = (fixture, options = {}) => {
2424
rules: [
2525
...(options.prerules || []),
2626
{
27-
test: /\.ts$/,
27+
test: /\.tsx?$/,
2828
use: {
2929
loader: path.resolve(__dirname, '../lib/index.js'),
3030
options: {

0 commit comments

Comments
 (0)