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

Commit f6dcc22

Browse files
committed
feat: rework path normalization for new TS
1 parent 22048a0 commit f6dcc22

File tree

4 files changed

+22
-36
lines changed

4 files changed

+22
-36
lines changed

src/checker-runtime.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ICompilerInfo, IFile } from './host';
22
import { LoaderPlugin, LoaderPluginDef, LoaderConfig } from './instance';
3-
import * as path from 'path';
43

54
let colors = require('colors/safe');
65

@@ -77,26 +76,20 @@ export class Host implements ts.LanguageServiceHost {
7776
this.moduleResolutionHost = new ModuleResolutionHost(this);
7877
}
7978

80-
normalizePath(filePath: string): string {
81-
return path.normalize(filePath);
82-
}
83-
8479
getScriptFileNames() {
8580
return Object.keys(env.files);
8681
}
8782

8883
getScriptVersion(fileName: string) {
89-
let fileName_ = path.normalize(fileName);
90-
if (env.files[fileName_]) {
91-
return env.files[fileName_].version.toString();
84+
if (env.files[fileName]) {
85+
return env.files[fileName].version.toString();
9286
}
9387
}
9488

9589
getScriptSnapshot(fileName: string) {
96-
let fileName_ = path.normalize(fileName);
97-
let file = env.files[fileName_];
90+
let file = env.files[fileName];
9891
if (!file) {
99-
throw new Error(`Requested file is unknown: ${ fileName_ }`);
92+
throw new Error(`Requested file is unknown: ${ fileName }`);
10093
}
10194
return env.compiler.ScriptSnapshot.fromString(file.text);
10295
}

src/helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export function findResultFor(output: host.IEmitOutput, fileName: string) {
2727
let text;
2828
let sourceMap;
2929
let declaration: host.IOutputFile;
30-
fileName = withoutExt(path.normalize(fileName));
30+
fileName = withoutExt(fileName);
3131

3232
for (let i = 0; i < output.outputFiles.length; i++) {
3333
let o = output.outputFiles[i];
34-
let outputFileName = path.normalize(o.name);
35-
let sourceFileName = withoutExt(path.normalize(o.sourceName));
34+
let outputFileName = o.name;
35+
let sourceFileName = withoutExt(o.sourceName);
3636
if (isFileEmit(fileName, outputFileName, sourceFileName)) {
3737
text = o.text;
3838
}

src/host.ts

+4-21
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@ export class Host implements ts.LanguageServiceHost {
4040
}
4141

4242
getScriptVersion(fileName: string) {
43-
let fileName_ = path.normalize(fileName);
44-
if (this.state.getFile(fileName_)) {
45-
return this.state.getFile(fileName_).version.toString();
43+
if (this.state.getFile(fileName)) {
44+
return this.state.getFile(fileName).version.toString();
4645
}
4746
}
4847

4948
getScriptSnapshot(fileName) {
50-
let fileName_ = path.normalize(fileName);
51-
let file = this.state.getFile(fileName_);
49+
let file = this.state.getFile(fileName);
5250
if (!file) {
53-
throw new Error(`Requested file is unknown: ${ fileName_ }`);
51+
throw new Error(`Requested file is unknown: ${ fileName }`);
5452
}
5553
return this.state.ts.ScriptSnapshot.fromString(file.text);
5654
}
@@ -175,8 +173,6 @@ export class State {
175173
}
176174

177175
emit(fileName: string): IEmitOutput {
178-
fileName = this.normalizePath(fileName);
179-
180176
if (!this.program) {
181177
this.program = this.services.getProgram();
182178
}
@@ -216,8 +212,6 @@ export class State {
216212
}
217213

218214
fastEmit(fileName: string) {
219-
fileName = this.normalizePath(fileName);
220-
221215
let file = this.getFile(fileName);
222216
if (!file) {
223217
throw new Error(`Unknown file ${ fileName }`);
@@ -236,7 +230,6 @@ export class State {
236230
}
237231

238232
updateFile(fileName: string, text: string, checked: boolean = false): boolean {
239-
fileName = this.normalizePath(fileName);
240233
let prevFile = this.files[fileName];
241234
let version = 0;
242235
let changed = true;
@@ -261,7 +254,6 @@ export class State {
261254
}
262255

263256
addFile(fileName: string, text: string, isDefaultLib = false): IFile {
264-
fileName = this.normalizePath(fileName);
265257
return this.files[fileName] = {
266258
text,
267259
isDefaultLib,
@@ -270,36 +262,27 @@ export class State {
270262
}
271263

272264
getFile(fileName: string): IFile {
273-
fileName = this.normalizePath(fileName);
274265
return this.files[fileName];
275266
}
276267

277268
hasFile(fileName: string): boolean {
278-
fileName = this.normalizePath(fileName);
279269
return this.files.hasOwnProperty(fileName);
280270
}
281271

282272
readFile(fileName: string): string {
283-
fileName = this.normalizePath(fileName);
284273
// Use global fs here, because local doesn't contain `readFileSync`
285274
return fs.readFileSync(fileName, {encoding: 'utf-8'});
286275
}
287276

288277
readFileAndAdd(fileName: string, isDefaultLib = false) {
289-
fileName = this.normalizePath(fileName);
290278
let text = this.readFile(fileName);
291279
this.addFile(fileName, text, isDefaultLib);
292280
}
293281

294282
readFileAndUpdate(fileName: string, checked: boolean = false): boolean {
295-
fileName = this.normalizePath(fileName);
296283
let text = this.readFile(fileName);
297284
return this.updateFile(fileName, text, checked);
298285
}
299-
300-
normalizePath(filePath: string): string {
301-
return path.normalize(filePath);
302-
}
303286
}
304287

305288
/**

src/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ interface Transformation {
2828
map: any;
2929
}
3030

31+
const double = /\/\//;
32+
function toUnix(fileName: string) {
33+
let res: string = fileName.replace(/\\/g, '/');
34+
while (res.match(double)) {
35+
res = res.replace(double, '/');
36+
}
37+
38+
return res;
39+
}
40+
3141
function compiler(webpack: IWebPack, text: string): void {
3242
if (webpack.cacheable) {
3343
webpack.cacheable();
@@ -38,7 +48,7 @@ function compiler(webpack: IWebPack, text: string): void {
3848
let instance = ensureInstance(webpack, options, instanceName);
3949
let state = instance.tsState;
4050
let callback = webpack.async();
41-
let fileName = state.normalizePath(webpack.resourcePath);
51+
let fileName = toUnix(webpack.resourcePath);
4252

4353
let depsInjector = {
4454
add: (depFileName) => webpack.addDependency(depFileName),

0 commit comments

Comments
 (0)