@esportsplus/typescript 0.28.4 → 0.28.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ type CoordinatorResult = {
6
6
  sourceFile: ts.SourceFile;
7
7
  };
8
8
  declare const _default: {
9
+ createPatchedProgram: (baseProgram: ts.Program, fileName: string, newContent: string) => ts.Program;
9
10
  transform: (plugins: Plugin[], code: string, file: ts.SourceFile, program: ts.Program, shared: SharedContext) => {
10
11
  changed: boolean;
11
12
  code: string;
@@ -43,20 +43,6 @@ function applyPrepend(code, file, prepend) {
43
43
  }
44
44
  return code.slice(0, position) + prepend.join('\n') + code.slice(position);
45
45
  }
46
- function createPatchedProgram(baseProgram, fileName, newContent) {
47
- let baseHost = ts.createCompilerHost(baseProgram.getCompilerOptions()), options = baseProgram.getCompilerOptions();
48
- return ts.createProgram(baseProgram.getRootFileNames(), options, {
49
- ...baseHost,
50
- getSourceFile: (name, languageVersion) => {
51
- if (name === fileName || name === fileName.replace(/\\/g, '/')) {
52
- return ts.createSourceFile(name, newContent, languageVersion, true);
53
- }
54
- return baseProgram.getSourceFile(name) ?? baseHost.getSourceFile(name, languageVersion);
55
- },
56
- fileExists: (name) => baseHost.fileExists(name),
57
- readFile: (name) => name === fileName ? newContent : baseHost.readFile(name)
58
- }, baseProgram);
59
- }
60
46
  function hasPattern(code, patterns) {
61
47
  for (let i = 0, n = patterns.length; i < n; i++) {
62
48
  if (code.indexOf(patterns[i]) !== -1) {
@@ -125,6 +111,20 @@ function replaceReverse(code, replacements) {
125
111
  }
126
112
  return result;
127
113
  }
114
+ const createPatchedProgram = (baseProgram, fileName, newContent) => {
115
+ let baseHost = ts.createCompilerHost(baseProgram.getCompilerOptions()), options = baseProgram.getCompilerOptions();
116
+ return ts.createProgram(baseProgram.getRootFileNames(), options, {
117
+ ...baseHost,
118
+ getSourceFile: (name, languageVersion) => {
119
+ if (name === fileName || name === fileName.replace(/\\/g, '/')) {
120
+ return ts.createSourceFile(name, newContent, languageVersion, true);
121
+ }
122
+ return baseProgram.getSourceFile(name) ?? baseHost.getSourceFile(name, languageVersion);
123
+ },
124
+ fileExists: (name) => baseHost.fileExists(name),
125
+ readFile: (name) => name === fileName ? newContent : baseHost.readFile(name)
126
+ }, baseProgram);
127
+ };
128
128
  const transform = (plugins, code, file, program, shared) => {
129
129
  if (plugins.length === 0) {
130
130
  return { changed: false, code, sourceFile: file };
@@ -168,4 +168,4 @@ const transform = (plugins, code, file, program, shared) => {
168
168
  }
169
169
  return { changed, code: currentCode, sourceFile: currentFile };
170
170
  };
171
- export default { transform };
171
+ export default { createPatchedProgram, transform };
@@ -18,10 +18,13 @@ export default ({ name, onWatchChange, plugins }) => {
18
18
  return null;
19
19
  }
20
20
  try {
21
- let prog = program.get(root || ''), sourceFile = prog.getSourceFile(id.replace(DIRECTORY_SEPARATOR_REGEX, '/')) || prog.getSourceFile(id);
22
- if (!sourceFile ||
23
- sourceFile.getText().replace(LINE_ENDINGS_REGEX, '\n') !== code.replace(LINE_ENDINGS_REGEX, '\n')) {
24
- sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
21
+ let normalizedId = id.replace(DIRECTORY_SEPARATOR_REGEX, '/'), prog = program.get(root || ''), sourceFile = prog.getSourceFile(normalizedId) || prog.getSourceFile(id);
22
+ if (sourceFile && sourceFile.getText().replace(LINE_ENDINGS_REGEX, '\n') !== code.replace(LINE_ENDINGS_REGEX, '\n')) {
23
+ sourceFile = undefined;
24
+ }
25
+ if (!sourceFile) {
26
+ prog = coordinator.createPatchedProgram(prog, normalizedId, code);
27
+ sourceFile = prog.getSourceFile(normalizedId) || ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
25
28
  }
26
29
  let result = coordinator.transform(plugins, code, sourceFile, prog, contexts.get(root || '') ?? contexts.set(root || '', new Map()).get(root || ''));
27
30
  if (!result.changed) {
package/package.json CHANGED
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "type": "module",
41
41
  "types": "build/index.d.ts",
42
- "version": "0.28.4",
42
+ "version": "0.28.5",
43
43
  "scripts": {
44
44
  "build": "tsc && tsc-alias",
45
45
  "-": "-"
@@ -73,28 +73,6 @@ function applyPrepend(code: string, file: ts.SourceFile, prepend: string[]): str
73
73
  return code.slice(0, position) + prepend.join('\n') + code.slice(position);
74
74
  }
75
75
 
76
- function createPatchedProgram(baseProgram: ts.Program, fileName: string, newContent: string) {
77
- let baseHost = ts.createCompilerHost(baseProgram.getCompilerOptions()),
78
- options = baseProgram.getCompilerOptions();
79
-
80
- return ts.createProgram(
81
- baseProgram.getRootFileNames(),
82
- options,
83
- {
84
- ...baseHost,
85
- getSourceFile: (name, languageVersion) => {
86
- if (name === fileName || name === fileName.replace(/\\/g, '/')) {
87
- return ts.createSourceFile(name, newContent, languageVersion, true);
88
- }
89
- return baseProgram.getSourceFile(name) ?? baseHost.getSourceFile(name, languageVersion);
90
- },
91
- fileExists: (name) => baseHost.fileExists(name),
92
- readFile: (name) => name === fileName ? newContent : baseHost.readFile(name)
93
- },
94
- baseProgram
95
- );
96
- }
97
-
98
76
  function hasPattern(code: string, patterns: string[]): boolean {
99
77
  for (let i = 0, n = patterns.length; i < n; i++) {
100
78
  if (code.indexOf(patterns[i]) !== -1) {
@@ -190,6 +168,29 @@ function replaceReverse(code: string, replacements: Replacement[]): string {
190
168
  return result;
191
169
  }
192
170
 
171
+
172
+ const createPatchedProgram = (baseProgram: ts.Program, fileName: string, newContent: string) => {
173
+ let baseHost = ts.createCompilerHost(baseProgram.getCompilerOptions()),
174
+ options = baseProgram.getCompilerOptions();
175
+
176
+ return ts.createProgram(
177
+ baseProgram.getRootFileNames(),
178
+ options,
179
+ {
180
+ ...baseHost,
181
+ getSourceFile: (name, languageVersion) => {
182
+ if (name === fileName || name === fileName.replace(/\\/g, '/')) {
183
+ return ts.createSourceFile(name, newContent, languageVersion, true);
184
+ }
185
+ return baseProgram.getSourceFile(name) ?? baseHost.getSourceFile(name, languageVersion);
186
+ },
187
+ fileExists: (name) => baseHost.fileExists(name),
188
+ readFile: (name) => name === fileName ? newContent : baseHost.readFile(name)
189
+ },
190
+ baseProgram
191
+ );
192
+ };
193
+
193
194
  const transform = (
194
195
  plugins: Plugin[],
195
196
  code: string,
@@ -256,5 +257,5 @@ const transform = (
256
257
  };
257
258
 
258
259
 
259
- export default { transform };
260
+ export default { createPatchedProgram, transform };
260
261
  export type { CoordinatorResult };
@@ -44,14 +44,18 @@ export default ({ name, onWatchChange, plugins }: VitePluginOptions) => {
44
44
  }
45
45
 
46
46
  try {
47
- let prog = program.get(root || ''),
48
- sourceFile = prog.getSourceFile(id.replace(DIRECTORY_SEPARATOR_REGEX, '/')) || prog.getSourceFile(id);
49
-
50
- if (
51
- !sourceFile ||
52
- sourceFile.getText().replace(LINE_ENDINGS_REGEX, '\n') !== code.replace(LINE_ENDINGS_REGEX, '\n')
53
- ) {
54
- sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
47
+ let normalizedId = id.replace(DIRECTORY_SEPARATOR_REGEX, '/'),
48
+ prog = program.get(root || ''),
49
+ sourceFile = prog.getSourceFile(normalizedId) || prog.getSourceFile(id);
50
+
51
+ // Check if file content matches (existing file may have changed)
52
+ if (sourceFile && sourceFile.getText().replace(LINE_ENDINGS_REGEX, '\n') !== code.replace(LINE_ENDINGS_REGEX, '\n')) {
53
+ sourceFile = undefined;
54
+ }
55
+
56
+ if (!sourceFile) {
57
+ prog = coordinator.createPatchedProgram(prog, normalizedId, code);
58
+ sourceFile = prog.getSourceFile(normalizedId) || ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
55
59
  }
56
60
 
57
61
  let result = coordinator.transform(