@simplysm/sd-cli 12.5.21 → 12.5.23

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.
Files changed (195) hide show
  1. package/dist/{build-tools → entry}/SdCliCordova.d.ts +5 -6
  2. package/dist/{build-tools → entry}/SdCliCordova.js +117 -99
  3. package/dist/entry/SdCliCordova.js.map +1 -0
  4. package/dist/entry/SdCliElectron.d.ts +2 -2
  5. package/dist/entry/SdCliElectron.js +50 -40
  6. package/dist/entry/SdCliElectron.js.map +1 -1
  7. package/dist/entry/SdCliLocalUpdate.d.ts +1 -2
  8. package/dist/entry/SdCliLocalUpdate.js +26 -27
  9. package/dist/entry/SdCliLocalUpdate.js.map +1 -1
  10. package/dist/entry/SdCliProject.d.ts +1 -5
  11. package/dist/entry/SdCliProject.js +62 -288
  12. package/dist/entry/SdCliProject.js.map +1 -1
  13. package/dist/index.d.ts +23 -20
  14. package/dist/index.js +23 -20
  15. package/dist/index.js.map +1 -1
  16. package/dist/pkg-builders/SdMultiBuildRunner.d.ts +21 -0
  17. package/dist/pkg-builders/SdMultiBuildRunner.js +174 -0
  18. package/dist/pkg-builders/SdMultiBuildRunner.js.map +1 -0
  19. package/dist/pkg-builders/client/SdCliNgRoutesFileGenerator.d.ts +4 -0
  20. package/dist/{build-tools → pkg-builders/client}/SdCliNgRoutesFileGenerator.js +7 -7
  21. package/dist/pkg-builders/client/SdCliNgRoutesFileGenerator.js.map +1 -0
  22. package/dist/pkg-builders/client/SdClientBuildRunner.d.ts +20 -0
  23. package/dist/pkg-builders/client/SdClientBuildRunner.js +135 -0
  24. package/dist/pkg-builders/client/SdClientBuildRunner.js.map +1 -0
  25. package/dist/pkg-builders/client/SdNgBundler.d.ts +29 -0
  26. package/dist/{build-tools → pkg-builders/client}/SdNgBundler.js +107 -151
  27. package/dist/pkg-builders/client/SdNgBundler.js.map +1 -0
  28. package/dist/{build-tools → pkg-builders/client}/SdNgBundlerContext.d.ts +1 -2
  29. package/dist/{build-tools → pkg-builders/client}/SdNgBundlerContext.js +19 -45
  30. package/dist/pkg-builders/client/SdNgBundlerContext.js.map +1 -0
  31. package/dist/pkg-builders/client/createSdNgPlugin.d.ts +10 -0
  32. package/dist/{bundle-plugins/sdNgPlugin.js → pkg-builders/client/createSdNgPlugin.js} +32 -33
  33. package/dist/pkg-builders/client/createSdNgPlugin.js.map +1 -0
  34. package/dist/pkg-builders/lib/SdCliIndexFileGenerator.d.ts +5 -0
  35. package/dist/{build-tools → pkg-builders/lib}/SdCliIndexFileGenerator.js +11 -11
  36. package/dist/pkg-builders/lib/SdCliIndexFileGenerator.js.map +1 -0
  37. package/dist/pkg-builders/lib/SdJsLibBuildRunner.d.ts +16 -0
  38. package/dist/pkg-builders/lib/SdJsLibBuildRunner.js +81 -0
  39. package/dist/pkg-builders/lib/SdJsLibBuildRunner.js.map +1 -0
  40. package/dist/pkg-builders/lib/SdTsLibBuildRunner.d.ts +13 -0
  41. package/dist/pkg-builders/lib/SdTsLibBuildRunner.js +89 -0
  42. package/dist/pkg-builders/lib/SdTsLibBuildRunner.js.map +1 -0
  43. package/dist/pkg-builders/lib/SdTsLibBuilder.d.ts +14 -0
  44. package/dist/pkg-builders/lib/SdTsLibBuilder.js +54 -0
  45. package/dist/pkg-builders/lib/SdTsLibBuilder.js.map +1 -0
  46. package/dist/pkg-builders/server/SdServerBuildRunner.d.ts +17 -0
  47. package/dist/{builders/SdCliServerBuilder.js → pkg-builders/server/SdServerBuildRunner.js} +48 -53
  48. package/dist/pkg-builders/server/SdServerBuildRunner.js.map +1 -0
  49. package/dist/pkg-builders/server/SdServerBundler.d.ts +19 -0
  50. package/dist/{build-tools → pkg-builders/server}/SdServerBundler.js +25 -30
  51. package/dist/pkg-builders/server/SdServerBundler.js.map +1 -0
  52. package/dist/pkg-builders/server/createSdServerPlugin.d.ts +10 -0
  53. package/dist/pkg-builders/server/createSdServerPlugin.js +53 -0
  54. package/dist/pkg-builders/server/createSdServerPlugin.js.map +1 -0
  55. package/dist/sd-cli.js +61 -54
  56. package/dist/sd-cli.js.map +1 -1
  57. package/dist/ts-builder/SdTsCompileWorker.d.ts +9 -0
  58. package/dist/ts-builder/SdTsCompileWorker.js +16 -0
  59. package/dist/ts-builder/SdTsCompileWorker.js.map +1 -0
  60. package/dist/ts-builder/SdTsCompiler.d.ts +7 -0
  61. package/dist/ts-builder/SdTsCompiler.js +557 -0
  62. package/dist/ts-builder/SdTsCompiler.js.map +1 -0
  63. package/dist/types/build-plugin.type.d.ts +14 -0
  64. package/dist/types/build-plugin.type.js +2 -0
  65. package/dist/types/build-plugin.type.js.map +1 -0
  66. package/dist/types/build.type.d.ts +15 -0
  67. package/dist/types/build.type.js +2 -0
  68. package/dist/types/build.type.js.map +1 -0
  69. package/dist/types/common-configs.type.d.ts +22 -0
  70. package/dist/types/common-configs.type.js +2 -0
  71. package/dist/types/common-configs.type.js.map +1 -0
  72. package/dist/types/sd-configs.type.d.ts +99 -0
  73. package/dist/types/sd-configs.type.js +2 -0
  74. package/dist/types/sd-configs.type.js.map +1 -0
  75. package/dist/types/ts-compiler.type.d.ts +29 -0
  76. package/dist/types/ts-compiler.type.js +2 -0
  77. package/dist/types/ts-compiler.type.js.map +1 -0
  78. package/dist/types/workers.type.d.ts +49 -0
  79. package/dist/types/workers.type.js +2 -0
  80. package/dist/types/workers.type.js.map +1 -0
  81. package/dist/utils/SdCliConvertMessageUtil.d.ts +21 -0
  82. package/dist/utils/SdCliConvertMessageUtil.js +137 -0
  83. package/dist/utils/SdCliConvertMessageUtil.js.map +1 -0
  84. package/dist/workers/compile-worker.js +27 -0
  85. package/dist/workers/compile-worker.js.map +1 -0
  86. package/dist/workers/lint-worker.js +36 -0
  87. package/dist/workers/lint-worker.js.map +1 -0
  88. package/dist/workers/server-worker.d.ts +1 -0
  89. package/dist/workers/server-worker.js +49 -0
  90. package/dist/workers/server-worker.js.map +1 -0
  91. package/package.json +8 -11
  92. package/src/entry/SdCliCordova.ts +363 -0
  93. package/src/entry/SdCliElectron.ts +96 -69
  94. package/src/entry/SdCliLocalUpdate.ts +43 -44
  95. package/src/entry/SdCliProject.ts +72 -417
  96. package/src/index.ts +23 -20
  97. package/src/pkg-builders/SdMultiBuildRunner.ts +250 -0
  98. package/src/{build-tools → pkg-builders/client}/SdCliNgRoutesFileGenerator.ts +7 -7
  99. package/src/pkg-builders/client/SdClientBuildRunner.ts +191 -0
  100. package/src/{build-tools → pkg-builders/client}/SdNgBundler.ts +135 -190
  101. package/src/pkg-builders/client/SdNgBundlerContext.ts +111 -0
  102. package/src/{bundle-plugins/sdNgPlugin.ts → pkg-builders/client/createSdNgPlugin.ts} +43 -46
  103. package/src/{build-tools → pkg-builders/lib}/SdCliIndexFileGenerator.ts +11 -11
  104. package/src/pkg-builders/lib/SdJsLibBuildRunner.ts +105 -0
  105. package/src/pkg-builders/lib/SdTsLibBuildRunner.ts +128 -0
  106. package/src/pkg-builders/lib/SdTsLibBuilder.ts +69 -0
  107. package/src/{builders/SdCliServerBuilder.ts → pkg-builders/server/SdServerBuildRunner.ts} +66 -78
  108. package/src/{build-tools → pkg-builders/server}/SdServerBundler.ts +40 -41
  109. package/src/pkg-builders/server/createSdServerPlugin.ts +77 -0
  110. package/src/sd-cli.ts +197 -211
  111. package/src/ts-builder/SdTsCompileWorker.ts +21 -0
  112. package/src/ts-builder/SdTsCompiler.ts +775 -0
  113. package/src/types/build-plugin.type.ts +16 -0
  114. package/src/types/build.type.ts +17 -0
  115. package/src/types/common-configs.type.ts +27 -0
  116. package/src/types/sd-configs.type.ts +114 -0
  117. package/src/types/ts-compiler.type.ts +29 -0
  118. package/src/types/workers.type.ts +27 -0
  119. package/src/utils/SdCliConvertMessageUtil.ts +177 -0
  120. package/src/workers/compile-worker.ts +31 -0
  121. package/src/workers/lint-worker.ts +44 -0
  122. package/src/workers/server-worker.ts +52 -0
  123. package/tsconfig.json +7 -1
  124. package/dist/build-cluster.js +0 -201
  125. package/dist/build-cluster.js.map +0 -1
  126. package/dist/build-tools/SdCliCordova.js.map +0 -1
  127. package/dist/build-tools/SdCliIndexFileGenerator.d.ts +0 -5
  128. package/dist/build-tools/SdCliIndexFileGenerator.js.map +0 -1
  129. package/dist/build-tools/SdCliNgRoutesFileGenerator.d.ts +0 -4
  130. package/dist/build-tools/SdCliNgRoutesFileGenerator.js.map +0 -1
  131. package/dist/build-tools/SdLinter.d.ts +0 -5
  132. package/dist/build-tools/SdLinter.js +0 -49
  133. package/dist/build-tools/SdLinter.js.map +0 -1
  134. package/dist/build-tools/SdNgBundler.d.ts +0 -29
  135. package/dist/build-tools/SdNgBundler.js.map +0 -1
  136. package/dist/build-tools/SdNgBundlerContext.js.map +0 -1
  137. package/dist/build-tools/SdReactBundler.d.ts +0 -25
  138. package/dist/build-tools/SdReactBundler.js +0 -295
  139. package/dist/build-tools/SdReactBundler.js.map +0 -1
  140. package/dist/build-tools/SdReactBundlerContext.d.ts +0 -14
  141. package/dist/build-tools/SdReactBundlerContext.js +0 -59
  142. package/dist/build-tools/SdReactBundlerContext.js.map +0 -1
  143. package/dist/build-tools/SdServerBundler.d.ts +0 -20
  144. package/dist/build-tools/SdServerBundler.js.map +0 -1
  145. package/dist/build-tools/SdTsCompiler.d.ts +0 -34
  146. package/dist/build-tools/SdTsCompiler.js +0 -534
  147. package/dist/build-tools/SdTsCompiler.js.map +0 -1
  148. package/dist/build-tools/SdTsLibBundler.d.ts +0 -13
  149. package/dist/build-tools/SdTsLibBundler.js +0 -58
  150. package/dist/build-tools/SdTsLibBundler.js.map +0 -1
  151. package/dist/builders/SdCliClientBuilder.d.ts +0 -18
  152. package/dist/builders/SdCliClientBuilder.js +0 -172
  153. package/dist/builders/SdCliClientBuilder.js.map +0 -1
  154. package/dist/builders/SdCliJsLibLinter.d.ts +0 -13
  155. package/dist/builders/SdCliJsLibLinter.js +0 -60
  156. package/dist/builders/SdCliJsLibLinter.js.map +0 -1
  157. package/dist/builders/SdCliServerBuilder.d.ts +0 -15
  158. package/dist/builders/SdCliServerBuilder.js.map +0 -1
  159. package/dist/builders/SdCliTsLibBuilder.d.ts +0 -12
  160. package/dist/builders/SdCliTsLibBuilder.js +0 -90
  161. package/dist/builders/SdCliTsLibBuilder.js.map +0 -1
  162. package/dist/bundle-plugins/sdNgPlugin.d.ts +0 -16
  163. package/dist/bundle-plugins/sdNgPlugin.js.map +0 -1
  164. package/dist/bundle-plugins/sdReactPlugin.d.ts +0 -16
  165. package/dist/bundle-plugins/sdReactPlugin.js +0 -117
  166. package/dist/bundle-plugins/sdReactPlugin.js.map +0 -1
  167. package/dist/bundle-plugins/sdServerPlugin.d.ts +0 -16
  168. package/dist/bundle-plugins/sdServerPlugin.js +0 -63
  169. package/dist/bundle-plugins/sdServerPlugin.js.map +0 -1
  170. package/dist/commons.d.ts +0 -145
  171. package/dist/commons.js +0 -2
  172. package/dist/commons.js.map +0 -1
  173. package/dist/server-worker.js +0 -57
  174. package/dist/server-worker.js.map +0 -1
  175. package/dist/utils/SdCliBuildResultUtil.d.ts +0 -16
  176. package/dist/utils/SdCliBuildResultUtil.js +0 -54
  177. package/dist/utils/SdCliBuildResultUtil.js.map +0 -1
  178. package/src/build-cluster.ts +0 -212
  179. package/src/build-tools/SdCliCordova.ts +0 -340
  180. package/src/build-tools/SdLinter.ts +0 -65
  181. package/src/build-tools/SdNgBundlerContext.ts +0 -137
  182. package/src/build-tools/SdReactBundler.ts +0 -372
  183. package/src/build-tools/SdReactBundlerContext.ts +0 -71
  184. package/src/build-tools/SdTsCompiler.ts +0 -754
  185. package/src/build-tools/SdTsLibBundler.ts +0 -72
  186. package/src/builders/SdCliClientBuilder.ts +0 -233
  187. package/src/builders/SdCliJsLibLinter.ts +0 -72
  188. package/src/builders/SdCliTsLibBuilder.ts +0 -126
  189. package/src/bundle-plugins/sdReactPlugin.ts +0 -164
  190. package/src/bundle-plugins/sdServerPlugin.ts +0 -94
  191. package/src/commons.ts +0 -171
  192. package/src/server-worker.ts +0 -65
  193. package/src/utils/SdCliBuildResultUtil.ts +0 -63
  194. /package/dist/{build-cluster.d.ts → workers/compile-worker.d.ts} +0 -0
  195. /package/dist/{server-worker.d.ts → workers/lint-worker.d.ts} +0 -0
@@ -1,754 +0,0 @@
1
- import ts, { CompilerOptions, DiagnosticCategory } from "typescript";
2
- import path from "path";
3
- import { FsUtil, Logger, PathUtil } from "@simplysm/sd-core-node";
4
- import { StringUtil } from "@simplysm/sd-core-common";
5
- import esbuild from "esbuild";
6
- import { NgtscProgram, OptimizeFor } from "@angular/compiler-cli";
7
- import { ComponentStylesheetBundler } from "@angular/build/src/tools/esbuild/angular/component-stylesheets";
8
- import { AngularCompilerHost } from "@angular/build/src/tools/angular/angular-host";
9
- import { transformSupportedBrowsersToTargets } from "@angular/build/src/tools/esbuild/utils";
10
- import browserslist from "browserslist";
11
- import { replaceBootstrap } from "@angular/build/src/tools/angular/transformers/jit-bootstrap-transformer";
12
- import { SdCliPerformanceTimer } from "../utils/SdCliPerformanceTime";
13
-
14
- export class SdTsCompiler {
15
- readonly #logger = Logger.get(["simplysm", "sd-cli", "SdTsCompiler"]);
16
-
17
- readonly #parsedTsconfig: ts.ParsedCommandLine;
18
- readonly #isForAngular: boolean;
19
-
20
- readonly #revDependencyCacheMap = new Map<string, Set<string>>();
21
- readonly #resourceDependencyCacheMap = new Map<string, Set<string>>();
22
- readonly #sourceFileCacheMap = new Map<string, ts.SourceFile>();
23
- readonly #emittedFilesCacheMap = new Map<
24
- string,
25
- {
26
- outAbsPath?: string;
27
- text: string;
28
- }[]
29
- >();
30
-
31
- readonly #stylesheetBundler: ComponentStylesheetBundler | undefined;
32
- readonly #compilerHost: ts.CompilerHost | AngularCompilerHost;
33
-
34
- #ngProgram: NgtscProgram | undefined;
35
- #program: ts.Program | undefined;
36
-
37
- readonly #modifiedFileSet = new Set<string>();
38
-
39
- readonly #watchFileSet = new Set<string>();
40
- readonly #stylesheetBundlingResultMap = new Map<string, IStylesheetBundlingResult>();
41
-
42
- readonly #pkgPath: string;
43
- readonly #distPath: string;
44
- readonly #globalStyleFilePath?: string;
45
- readonly #watchScopePaths: string[];
46
-
47
- readonly #isForBundle: boolean;
48
-
49
- constructor(opt: {
50
- pkgPath: string;
51
- additionalOptions: CompilerOptions;
52
- isForBundle: boolean;
53
- isDevMode: boolean;
54
- watchScopePaths: string[];
55
- globalStyleFilePath?: string;
56
- }) {
57
- this.#pkgPath = opt.pkgPath;
58
- this.#globalStyleFilePath = opt.globalStyleFilePath != null ? path.normalize(opt.globalStyleFilePath) : undefined;
59
- this.#isForBundle = opt.isForBundle;
60
- this.#watchScopePaths = opt.watchScopePaths;
61
-
62
- this.#debug("초기화...");
63
-
64
- //-- isForAngular / parsedTsConfig
65
-
66
- const tsconfigPath = path.resolve(opt.pkgPath, "tsconfig.json");
67
- const tsconfig = FsUtil.readJson(tsconfigPath);
68
- this.#isForAngular = Boolean(tsconfig.angularCompilerOptions);
69
- this.#parsedTsconfig = ts.parseJsonConfigFileContent(tsconfig, ts.sys, opt.pkgPath, {
70
- ...tsconfig.angularCompilerOptions,
71
- ...opt.additionalOptions,
72
- });
73
-
74
- this.#distPath = this.#parsedTsconfig.options.outDir ?? path.resolve(opt.pkgPath, "dist");
75
-
76
- //-- compilerHost
77
-
78
- this.#compilerHost = ts.createCompilerHost(this.#parsedTsconfig.options);
79
-
80
- const baseGetSourceFile = this.#compilerHost.getSourceFile;
81
- this.#compilerHost.getSourceFile = (
82
- fileName,
83
- languageVersionOrOptions,
84
- onError,
85
- shouldCreateNewSourceFile,
86
- ...args
87
- ) => {
88
- if (!shouldCreateNewSourceFile && this.#sourceFileCacheMap.has(path.normalize(fileName))) {
89
- return this.#sourceFileCacheMap.get(path.normalize(fileName));
90
- }
91
-
92
- const sf = baseGetSourceFile.call(this.#compilerHost, fileName, languageVersionOrOptions, onError, true, ...args);
93
-
94
- if (sf) {
95
- this.#sourceFileCacheMap.set(path.normalize(fileName), sf);
96
- } else {
97
- this.#sourceFileCacheMap.delete(path.normalize(fileName));
98
- }
99
-
100
- return sf;
101
- };
102
-
103
- const baseReadFile = this.#compilerHost.readFile;
104
- this.#compilerHost.readFile = (fileName) => {
105
- this.#watchFileSet.add(path.normalize(fileName));
106
- return baseReadFile.call(this.#compilerHost, fileName);
107
- };
108
-
109
- if (this.#isForAngular) {
110
- //-- stylesheetBundler
111
- this.#stylesheetBundler = new ComponentStylesheetBundler(
112
- {
113
- workspaceRoot: opt.pkgPath,
114
- optimization: !opt.isDevMode,
115
- inlineFonts: true,
116
- preserveSymlinks: false,
117
- sourcemap: "inline", //conf.dev ? 'inline' : false,
118
- outputNames: { bundles: "[name]", media: "media/[name]" },
119
- includePaths: [],
120
- externalDependencies: [],
121
- target: transformSupportedBrowsersToTargets(browserslist(["Chrome > 78"])),
122
- postcssConfiguration: {
123
- plugins: [["css-has-pseudo"]],
124
- },
125
- tailwindConfiguration: undefined,
126
- cacheOptions: {
127
- enabled: true,
128
- path: ".cache/angular",
129
- basePath: ".cache",
130
- },
131
- },
132
- opt.isDevMode,
133
- );
134
-
135
- //-- compilerHost
136
-
137
- (this.#compilerHost as AngularCompilerHost).readResource = (fileName: string) => {
138
- return this.#compilerHost.readFile(fileName) ?? "";
139
- };
140
-
141
- (this.#compilerHost as AngularCompilerHost).transformResource = async (
142
- data: string,
143
- context: {
144
- type: string;
145
- containingFile: string;
146
- resourceFile: string | null;
147
- },
148
- ) => {
149
- if (context.type !== "style") {
150
- return null;
151
- }
152
-
153
- const contents = await this.#bundleStylesheetAsync(data, context.containingFile, context.resourceFile);
154
-
155
- return StringUtil.isNullOrEmpty(contents) ? null : { content: contents };
156
- };
157
-
158
- (this.#compilerHost as AngularCompilerHost).getModifiedResourceFiles = () => {
159
- return this.#modifiedFileSet;
160
- };
161
- }
162
- }
163
-
164
- async #bundleStylesheetAsync(data: string, containingFile: string, resourceFile: string | null = null) {
165
- // containingFile: 포함된 파일 (.ts)
166
- // resourceFile: 외부 리소스 파일 (styleUrls로 입력하지 않고 styles에 직접 입력한 경우 null)
167
- // referencedFiles: import한 외부 scss 파일 혹은 woff파일등 외부 파일
168
-
169
- this.#debug(`스타일시트 번들링...(${containingFile}, ${resourceFile})`);
170
-
171
- const stylesheetResult =
172
- resourceFile != null
173
- ? await this.#stylesheetBundler!.bundleFile(resourceFile)
174
- : await this.#stylesheetBundler!.bundleInline(data, containingFile, "scss");
175
-
176
- if (stylesheetResult.referencedFiles) {
177
- for (const referencedFile of stylesheetResult.referencedFiles) {
178
- const depCacheSet = this.#resourceDependencyCacheMap.getOrCreate(
179
- path.normalize(referencedFile),
180
- new Set<string>(),
181
- );
182
- depCacheSet.add(path.normalize(resourceFile ?? containingFile));
183
- }
184
-
185
- this.#watchFileSet.adds(
186
- ...Array.from(stylesheetResult.referencedFiles.values()).map((item) => path.normalize(item)),
187
- );
188
- }
189
-
190
- this.#stylesheetBundlingResultMap.set(path.normalize(resourceFile ?? containingFile), {
191
- outputFiles: stylesheetResult.outputFiles,
192
- metafile: stylesheetResult.metafile,
193
- errors: stylesheetResult.errors,
194
- warnings: stylesheetResult.warnings,
195
- });
196
-
197
- return stylesheetResult.contents;
198
- }
199
-
200
- invalidate(modifiedFileSet: Set<string>) {
201
- this.#modifiedFileSet.adds(...Array.from(modifiedFileSet).map((item) => path.normalize(item)));
202
- }
203
-
204
- async buildAsync(): Promise<ISdTsCompilerResult> {
205
- let perf = new SdCliPerformanceTimer("esbuild");
206
-
207
- const affectedFileSet = new Set<string>();
208
- const emitFileSet = new Set<string>();
209
-
210
- this.#debug(`get affected (old deps & old res deps)...`);
211
-
212
- perf.run("get affected", () => {
213
- for (const modifiedFile of this.#modifiedFileSet) {
214
- affectedFileSet.add(modifiedFile);
215
- affectedFileSet.adds(...(this.#revDependencyCacheMap.get(modifiedFile) ?? []));
216
- affectedFileSet.adds(...(this.#resourceDependencyCacheMap.get(modifiedFile) ?? []));
217
-
218
- this.#emittedFilesCacheMap.delete(path.normalize(modifiedFile));
219
- }
220
- });
221
-
222
- this.#debug(`invalidate & clear cache...`);
223
-
224
- perf.run("invalidate & clear cache", () => {
225
- this.#stylesheetBundler?.invalidate(this.#modifiedFileSet);
226
-
227
- for (const affectedFile of affectedFileSet) {
228
- this.#sourceFileCacheMap.delete(path.normalize(affectedFile));
229
- this.#stylesheetBundlingResultMap.delete(path.normalize(affectedFile));
230
- this.#watchFileSet.delete(path.normalize(affectedFile));
231
- }
232
-
233
- this.#revDependencyCacheMap.clear();
234
- this.#resourceDependencyCacheMap.clear();
235
- });
236
-
237
- this.#debug(`create program...`);
238
-
239
- perf.run("create program", () => {
240
- if (this.#isForAngular) {
241
- this.#ngProgram = new NgtscProgram(
242
- this.#parsedTsconfig.fileNames,
243
- this.#parsedTsconfig.options,
244
- this.#compilerHost,
245
- this.#ngProgram,
246
- );
247
- this.#program = this.#ngProgram.getTsProgram();
248
- } else {
249
- this.#program = ts.createProgram(
250
- this.#parsedTsconfig.fileNames,
251
- this.#parsedTsconfig.options,
252
- this.#compilerHost,
253
- this.#program,
254
- );
255
- }
256
- });
257
-
258
- /*const baseGetSourceFiles = this.#program.getSourceFiles;
259
- this.#program.getSourceFiles = function (...parameters) {
260
- const files: readonly (ts.SourceFile & { version?: string })[] = baseGetSourceFiles(...parameters);
261
-
262
- for (const file of files) {
263
- if (file.version === undefined) {
264
- file.version = createHash("sha256").update(file.text).digest("hex");
265
- }
266
- }
267
-
268
- return files;
269
- };*/
270
-
271
- if (this.#ngProgram) {
272
- await perf.run("ng analyze", async () => {
273
- await this.#ngProgram!.compiler.analyzeAsync();
274
- });
275
- }
276
-
277
- const getOrgSourceFile = (sf: ts.SourceFile) => {
278
- if (sf.fileName.endsWith(".ngtypecheck.ts")) {
279
- const orgFileName = sf.fileName.slice(0, -15) + ".ts";
280
- return this.#program!.getSourceFile(orgFileName);
281
- }
282
-
283
- return sf;
284
- };
285
-
286
- this.#debug(`get affected (new deps)...`);
287
-
288
- const diagnostics: ts.Diagnostic[] = [];
289
-
290
- perf.run("get affected (deps)", () => {
291
- const sourceFileSet = new Set(
292
- this.#program!.getSourceFiles()
293
- .map((sf) => getOrgSourceFile(sf))
294
- .filterExists(),
295
- );
296
-
297
- const depMap = new Map<
298
- string,
299
- {
300
- fileName: string;
301
- importName: string;
302
- exportName?: string;
303
- }[]
304
- >();
305
- for (const sf of sourceFileSet) {
306
- if (!PathUtil.isChildPath(sf.fileName, this.#pkgPath)) {
307
- continue;
308
- }
309
-
310
- const refs = this.#findDeps(sf);
311
- diagnostics.push(...refs.filter((item) => "category" in item));
312
- depMap.set(
313
- path.normalize(sf.fileName),
314
- refs
315
- .filter((item) => "fileName" in item)
316
- .filter((item) =>
317
- this.#watchScopePaths.some((scopePath) => PathUtil.isChildPath(item.fileName, scopePath)),
318
- ),
319
- );
320
- }
321
-
322
- const allDepMap = new Map<string, Set<string>>();
323
- const getAllDeps = (fileName: string, prevSet?: Set<string>) => {
324
- if (allDepMap.has(fileName)) {
325
- return allDepMap.get(fileName)!;
326
- }
327
-
328
- const result = new Set<string>();
329
-
330
- const deps = depMap.get(fileName) ?? [];
331
- result.adds(...deps.map((item) => item.fileName));
332
-
333
- for (const dep of deps) {
334
- const targetDeps = depMap.get(dep.fileName) ?? [];
335
-
336
- if (dep.importName === "*") {
337
- for (const targetRefItem of targetDeps.filter((item) => item.exportName != null)) {
338
- if (prevSet?.has(targetRefItem.fileName)) continue;
339
-
340
- result.add(targetRefItem.fileName);
341
- result.adds(...getAllDeps(targetRefItem.fileName, new Set<string>(prevSet).adds(...result)));
342
- }
343
- } else {
344
- for (const targetRefItem of targetDeps.filter((item) => item.exportName === dep.importName)) {
345
- if (prevSet?.has(targetRefItem.fileName)) continue;
346
-
347
- result.add(targetRefItem.fileName);
348
- result.adds(...getAllDeps(targetRefItem.fileName, new Set<string>(prevSet).adds(...result)));
349
- }
350
- }
351
- }
352
-
353
- return result;
354
- };
355
-
356
- for (const sf of sourceFileSet) {
357
- const deps = getAllDeps(path.normalize(sf.fileName));
358
- allDepMap.set(path.normalize(sf.fileName), deps);
359
-
360
- for (const dep of getAllDeps(path.normalize(sf.fileName))) {
361
- const depCache = this.#revDependencyCacheMap.getOrCreate(path.normalize(dep), new Set<string>());
362
- depCache.add(path.normalize(sf.fileName));
363
- if (this.#modifiedFileSet.has(path.normalize(dep))) {
364
- affectedFileSet.add(path.normalize(sf.fileName));
365
- }
366
- }
367
-
368
- if (this.#ngProgram) {
369
- if (this.#ngProgram.compiler.ignoreForEmit.has(sf)) {
370
- continue;
371
- }
372
-
373
- for (const dep of this.#ngProgram.compiler.getResourceDependencies(sf)) {
374
- const ref = this.#resourceDependencyCacheMap.getOrCreate(path.normalize(dep), new Set<string>());
375
- ref.add(path.normalize(sf.fileName));
376
- if (this.#modifiedFileSet.has(path.normalize(dep))) {
377
- affectedFileSet.add(path.normalize(sf.fileName));
378
- }
379
- }
380
- }
381
- }
382
- });
383
-
384
- if (affectedFileSet.size === 0) {
385
- this.#debug(`get affected (init)...`);
386
-
387
- perf.run("get affected (init)", () => {
388
- for (const sf of this.#program!.getSourceFiles()) {
389
- if (!this.#watchScopePaths.some((scopePath) => PathUtil.isChildPath(sf.fileName, scopePath))) {
390
- continue;
391
- }
392
-
393
- const orgSf = getOrgSourceFile(sf);
394
- if (!orgSf) continue;
395
-
396
- affectedFileSet.add(path.normalize(orgSf.fileName));
397
- }
398
- });
399
- }
400
-
401
- this.#debug(`get diagnostics...`);
402
-
403
- perf.run("get program diagnostics", () => {
404
- diagnostics.push(
405
- ...this.#program!.getConfigFileParsingDiagnostics(),
406
- ...this.#program!.getOptionsDiagnostics(),
407
- ...this.#program!.getGlobalDiagnostics(),
408
- );
409
-
410
- if (this.#ngProgram) {
411
- diagnostics.push(...this.#ngProgram.compiler.getOptionDiagnostics());
412
- }
413
- });
414
-
415
- this.#debug(`get diagnostics of files...`);
416
-
417
- perf.run("get file diagnostics", () => {
418
- for (const affectedFile of affectedFileSet) {
419
- if (!PathUtil.isChildPath(affectedFile, this.#pkgPath)) {
420
- continue;
421
- }
422
-
423
- const affectedSourceFile = this.#program!.getSourceFile(affectedFile);
424
-
425
- if (
426
- !affectedSourceFile ||
427
- (this.#ngProgram && this.#ngProgram.compiler.ignoreForDiagnostics.has(affectedSourceFile))
428
- ) {
429
- continue;
430
- }
431
-
432
- // this.#debug(`get diagnostics of file ${affectedFile}...`);
433
-
434
- diagnostics.push(
435
- ...this.#program!.getSyntacticDiagnostics(affectedSourceFile),
436
- ...this.#program!.getSemanticDiagnostics(affectedSourceFile),
437
- );
438
-
439
- if (this.#ngProgram) {
440
- perf.run("get file diagnostics: ng", () => {
441
- if (affectedSourceFile.isDeclarationFile) {
442
- return;
443
- }
444
-
445
- diagnostics.push(
446
- ...this.#ngProgram!.compiler.getDiagnosticsForFile(affectedSourceFile, OptimizeFor.WholeProgram),
447
- );
448
- });
449
- }
450
- }
451
- });
452
-
453
- perf.run("emit", () => {
454
- this.#debug(`prepare emit...`);
455
-
456
- let transformers: ts.CustomTransformers = {};
457
-
458
- if (this.#ngProgram) {
459
- transformers = {
460
- ...transformers,
461
- ...this.#ngProgram.compiler.prepareEmit().transformers,
462
- };
463
- (transformers.before ??= []).push(replaceBootstrap(() => this.#program!.getTypeChecker()));
464
- }
465
- // (transformers.before ??= []).push(transformKeys(this.#program));
466
-
467
- this.#debug(`emit for files...`);
468
-
469
- // affected에 새로 추가된 파일은 포함되지 않는 현상이 있어 getSourceFiles로 바꿈
470
- // 비교해보니, 딱히 getSourceFiles라서 더 느려지는것 같지는 않음
471
- // 그래도 affected로 다시 테스트
472
- for (const affectedFile of affectedFileSet) {
473
- if (this.#emittedFilesCacheMap.has(affectedFile)) {
474
- continue;
475
- }
476
-
477
- const sf = this.#program!.getSourceFile(affectedFile);
478
- if (!sf) {
479
- continue;
480
- }
481
-
482
- // for (const sf of sourceFileSet) {
483
- /*if (this.#emittedFilesCacheMap.has(path.normalize(sf.fileName))) {
484
- continue;
485
- }*/
486
-
487
- if (sf.isDeclarationFile) {
488
- continue;
489
- }
490
-
491
- if (this.#ngProgram?.compiler.ignoreForEmit.has(sf)) {
492
- continue;
493
- }
494
-
495
- if (
496
- this.#ngProgram?.compiler.incrementalCompilation.safeToSkipEmit(sf) &&
497
- !affectedFileSet.has(path.normalize(sf.fileName))
498
- ) {
499
- continue;
500
- }
501
-
502
- // esbuild를 통해 bundle로 묶어야 하는놈들은 모든 output이 있어야 함.
503
- if (!this.#isForBundle) {
504
- if (!PathUtil.isChildPath(sf.fileName, this.#pkgPath)) {
505
- continue;
506
- }
507
- }
508
-
509
- // this.#debug(`emit for`, sf.fileName);
510
- this.#program!.emit(
511
- sf,
512
- (fileName, text, writeByteOrderMark, onError, sourceFiles, data) => {
513
- if (!sourceFiles || sourceFiles.length === 0) {
514
- this.#compilerHost.writeFile(fileName, text, writeByteOrderMark, onError, sourceFiles, data);
515
- return;
516
- }
517
-
518
- const sourceFile = ts.getOriginalNode(sourceFiles[0], ts.isSourceFile);
519
- if (this.#ngProgram) {
520
- if (this.#ngProgram.compiler.ignoreForEmit.has(sourceFile)) {
521
- return;
522
- }
523
- this.#ngProgram.compiler.incrementalCompilation.recordSuccessfulEmit(sourceFile);
524
- }
525
-
526
- const emitFileInfoCaches = this.#emittedFilesCacheMap.getOrCreate(path.normalize(sourceFile.fileName), []);
527
-
528
- if (PathUtil.isChildPath(sourceFile.fileName, this.#pkgPath)) {
529
- let realFilePath = fileName;
530
- let realText = text;
531
- if (
532
- PathUtil.isChildPath(realFilePath, path.resolve(this.#distPath, path.basename(this.#pkgPath), "src"))
533
- ) {
534
- realFilePath = path.resolve(
535
- this.#distPath,
536
- path.relative(path.resolve(this.#distPath, path.basename(this.#pkgPath), "src"), realFilePath),
537
- );
538
-
539
- if (fileName.endsWith(".js.map")) {
540
- const sourceMapContents = JSON.parse(realText);
541
- // remove "../../"
542
- sourceMapContents.sources[0] = sourceMapContents.sources[0].slice(6);
543
- realText = JSON.stringify(sourceMapContents);
544
- }
545
- }
546
-
547
- emitFileInfoCaches.push({
548
- outAbsPath: realFilePath,
549
- text: realText,
550
- });
551
- } else {
552
- emitFileInfoCaches.push({ text });
553
- }
554
-
555
- emitFileSet.add(path.normalize(sourceFile.fileName));
556
- },
557
- undefined,
558
- undefined,
559
- transformers,
560
- );
561
- }
562
- });
563
-
564
- //-- global style
565
- if (
566
- this.#globalStyleFilePath != null &&
567
- FsUtil.exists(this.#globalStyleFilePath) &&
568
- !this.#emittedFilesCacheMap.has(this.#globalStyleFilePath)
569
- ) {
570
- this.#debug(`bundle global style...`);
571
-
572
- await perf.run("bundle global style", async () => {
573
- const data = await FsUtil.readFileAsync(this.#globalStyleFilePath!);
574
- const contents = await this.#bundleStylesheetAsync(data, this.#globalStyleFilePath!, this.#globalStyleFilePath);
575
- const emitFileInfos = this.#emittedFilesCacheMap.getOrCreate(this.#globalStyleFilePath!, []);
576
- emitFileInfos.push({
577
- outAbsPath: path.resolve(
578
- this.#pkgPath,
579
- path.relative(path.resolve(this.#pkgPath, "src"), this.#globalStyleFilePath!).replace(/\.scss$/, ".css"),
580
- ),
581
- text: contents,
582
- });
583
- emitFileSet.add(this.#globalStyleFilePath!);
584
- });
585
- }
586
-
587
- //-- init
588
-
589
- this.#modifiedFileSet.clear();
590
-
591
- this.#debug(`build completed`, perf.toString());
592
-
593
- //-- result
594
-
595
- return {
596
- program: this.#program!,
597
- typescriptDiagnostics: diagnostics,
598
- stylesheetBundlingResultMap: this.#stylesheetBundlingResultMap,
599
- emittedFilesCacheMap: this.#emittedFilesCacheMap,
600
- watchFileSet: this.#watchFileSet,
601
- affectedFileSet,
602
- emitFileSet,
603
- };
604
- }
605
-
606
- #debug(...msg: any[]): void {
607
- this.#logger.debug(`[${path.basename(this.#pkgPath)}]`, ...msg);
608
- }
609
-
610
- #findDeps(sf: ts.SourceFile) {
611
- const deps: (
612
- | {
613
- fileName: string;
614
- importName: string;
615
- exportName?: string;
616
- }
617
- | ts.Diagnostic
618
- )[] = [];
619
-
620
- const tc = this.#program!.getTypeChecker();
621
-
622
- sf.forEachChild((node) => {
623
- if (ts.isExportDeclaration(node)) {
624
- if (node.moduleSpecifier) {
625
- const moduleSymbol = tc.getSymbolAtLocation(node.moduleSpecifier);
626
- if (!moduleSymbol) {
627
- deps.push({
628
- category: DiagnosticCategory.Error,
629
- code: -1,
630
- file: sf,
631
- start: node.getStart(),
632
- length: node.getEnd() - node.getStart(),
633
- messageText: `export moduleSymbol not found`,
634
- });
635
- return;
636
- }
637
-
638
- const decls = moduleSymbol.getDeclarations();
639
- if (!decls) {
640
- deps.push({
641
- category: DiagnosticCategory.Error,
642
- code: -1,
643
- file: sf,
644
- start: node.getStart(),
645
- length: node.getEnd() - node.getStart(),
646
- messageText: `export decls not found`,
647
- });
648
- return;
649
- }
650
-
651
- const namedBindings = node.exportClause;
652
- if (namedBindings && ts.isNamedExports(namedBindings)) {
653
- for (const el of namedBindings.elements) {
654
- for (const decl of decls) {
655
- deps.push({
656
- fileName: path.normalize(decl.getSourceFile().fileName),
657
- importName: el.name.text,
658
- exportName: el.propertyName?.text ?? el.name.text,
659
- });
660
- }
661
- }
662
- } else {
663
- if (!moduleSymbol.exports) {
664
- deps.push({
665
- category: DiagnosticCategory.Error,
666
- code: -1,
667
- file: sf,
668
- start: node.getStart(),
669
- length: node.getEnd() - node.getStart(),
670
- messageText: `moduleSymbol exports not found`,
671
- });
672
- return;
673
- }
674
-
675
- for (const decl of decls) {
676
- for (const key of moduleSymbol.exports.keys()) {
677
- deps.push({
678
- fileName: path.normalize(decl.getSourceFile().fileName),
679
- importName: key.toString(),
680
- exportName: key.toString(),
681
- });
682
- }
683
- }
684
- }
685
- }
686
- } else if (ts.isImportDeclaration(node)) {
687
- const moduleSymbol = tc.getSymbolAtLocation(node.moduleSpecifier);
688
- if (!moduleSymbol) {
689
- if (ts.isStringLiteral(node.moduleSpecifier) && node.moduleSpecifier.text.startsWith("./")) {
690
- deps.push({
691
- fileName: path.normalize(path.resolve(path.dirname(sf.fileName), node.moduleSpecifier.text)),
692
- importName: "*",
693
- });
694
- }
695
- /*else {
696
- throw new NeverEntryError(`import moduleSymbol: ${sf.fileName} ${node.moduleSpecifier["text"]}`);
697
- }*/
698
- } else {
699
- const decls = moduleSymbol.getDeclarations();
700
- if (!decls) {
701
- deps.push({
702
- category: DiagnosticCategory.Error,
703
- code: -1,
704
- file: sf,
705
- start: node.getStart(),
706
- length: node.getEnd() - node.getStart(),
707
- messageText: `import decls not found`,
708
- });
709
- return;
710
- }
711
-
712
- const namedBindings = node.importClause?.namedBindings;
713
- if (namedBindings && ts.isNamedImports(namedBindings)) {
714
- for (const el of namedBindings.elements) {
715
- for (const decl of decls) {
716
- deps.push({
717
- fileName: path.normalize(decl.getSourceFile().fileName),
718
- importName: el.name.text,
719
- });
720
- }
721
- }
722
- } else {
723
- for (const decl of decls) {
724
- deps.push({
725
- fileName: path.normalize(decl.getSourceFile().fileName),
726
- importName: "*",
727
- });
728
- }
729
- }
730
- }
731
- }
732
- });
733
-
734
- return deps;
735
- }
736
- }
737
-
738
- export interface ISdTsCompilerResult {
739
- program: ts.Program;
740
- typescriptDiagnostics: ts.Diagnostic[];
741
- stylesheetBundlingResultMap: Map<string, IStylesheetBundlingResult>;
742
- emittedFilesCacheMap: Map<string, { outAbsPath?: string; text: string }[]>;
743
- watchFileSet: Set<string>;
744
-
745
- affectedFileSet: Set<string>;
746
- emitFileSet: Set<string>;
747
- }
748
-
749
- interface IStylesheetBundlingResult {
750
- outputFiles: esbuild.OutputFile[];
751
- metafile?: esbuild.Metafile;
752
- errors?: esbuild.PartialMessage[];
753
- warnings?: esbuild.PartialMessage[];
754
- }