@simplysm/sd-cli 12.5.11 → 12.5.13

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.
@@ -1,111 +0,0 @@
1
- import ts from "typescript";
2
- import path from "path";
3
- import { SdTsCompiler } from "../build-tools/SdTsCompiler";
4
- import { convertTypeScriptDiagnostic } from "@angular/build/src/tools/esbuild/angular/diagnostics";
5
- import postcss from "postcss";
6
- import { FsUtil } from "@simplysm/sd-core-node";
7
- import postcssUrl from "postcss-url";
8
- import postcssHas from "css-has-pseudo";
9
- import autoprefixer from "autoprefixer";
10
- export function sdReactPlugin(conf) {
11
- return {
12
- name: "sd-ng-compiler",
13
- setup: (build) => {
14
- const compiler = new SdTsCompiler(conf.pkgPath, { declaration: false }, conf.dev);
15
- let buildResult;
16
- const outputContentsCacheMap = new Map();
17
- //---------------------------
18
- build.onStart(async () => {
19
- compiler.invalidate(conf.modifiedFileSet);
20
- for (const modifiedFile of conf.modifiedFileSet) {
21
- outputContentsCacheMap.delete(modifiedFile);
22
- }
23
- buildResult = await compiler.buildAsync();
24
- conf.result.watchFileSet = buildResult.watchFileSet;
25
- conf.result.affectedFileSet = buildResult.affectedFileSet;
26
- conf.result.program = buildResult.program;
27
- //-- return err/warn
28
- return {
29
- errors: [
30
- ...buildResult.typescriptDiagnostics
31
- .filter((item) => item.category === ts.DiagnosticCategory.Error)
32
- .map((item) => convertTypeScriptDiagnostic(ts, item)),
33
- ...Array.from(buildResult.stylesheetBundlingResultMap.values()).flatMap((item) => item.errors),
34
- ].filterExists(),
35
- warnings: [
36
- ...buildResult.typescriptDiagnostics
37
- .filter((item) => item.category !== ts.DiagnosticCategory.Error)
38
- .map((item) => convertTypeScriptDiagnostic(ts, item)),
39
- // ...Array.from(buildResult.stylesheetResultMap.values()).flatMap(item => item.warnings)
40
- ],
41
- };
42
- });
43
- build.onLoad({ filter: /\.tsx?$/ }, (args) => {
44
- const output = outputContentsCacheMap.get(path.normalize(args.path));
45
- if (output != null) {
46
- return { contents: output, loader: "js" };
47
- }
48
- const emittedJsFile = buildResult.emittedFilesCacheMap.get(path.normalize(args.path))?.last();
49
- if (!emittedJsFile) {
50
- throw new Error(`ts 빌더 결과 emit 파일이 존재하지 않습니다. ${args.path}`);
51
- }
52
- const contents = emittedJsFile.text;
53
- outputContentsCacheMap.set(path.normalize(args.path), contents);
54
- return { contents, loader: "js" };
55
- });
56
- build.onLoad({ filter: /\.[cm]?jsx?$/ }, (args) => {
57
- conf.result.watchFileSet.add(path.normalize(args.path));
58
- return null;
59
- });
60
- build.onLoad({ filter: /\.css$/ }, async (args) => {
61
- conf.result.watchFileSet.add(path.normalize(args.path));
62
- const output = outputContentsCacheMap.get(path.normalize(args.path));
63
- if (output != null) {
64
- return {
65
- contents: output,
66
- loader: "file",
67
- };
68
- }
69
- const css = await FsUtil.readFileAsync(path.normalize(args.path));
70
- const result = await postcss()
71
- .use(autoprefixer({
72
- overrideBrowserslist: ["Chrome > 78"]
73
- }))
74
- .use(postcssUrl({
75
- url: "copy",
76
- }))
77
- .use(postcssHas())
78
- .process(css, {
79
- from: path.normalize(args.path),
80
- to: path.resolve(conf.pkgPath, "dist", "media", path.basename(args.path)),
81
- });
82
- outputContentsCacheMap.set(path.normalize(args.path), result.css);
83
- return { contents: result.css, loader: "file" };
84
- });
85
- build.onLoad({
86
- filter: new RegExp("(" +
87
- Object.keys(build.initialOptions.loader)
88
- .map((item) => "\\" + item)
89
- .join("|") +
90
- ")$"),
91
- }, (args) => {
92
- conf.result.watchFileSet.add(path.normalize(args.path));
93
- return null;
94
- });
95
- build.onEnd((result) => {
96
- for (const { outputFiles, metafile } of buildResult.stylesheetBundlingResultMap.values()) {
97
- result.outputFiles = result.outputFiles ?? [];
98
- result.outputFiles.push(...outputFiles);
99
- if (result.metafile && metafile) {
100
- result.metafile.inputs = { ...result.metafile.inputs, ...metafile.inputs };
101
- result.metafile.outputs = { ...result.metafile.outputs, ...metafile.outputs };
102
- }
103
- }
104
- conf.result.outputFiles = result.outputFiles;
105
- conf.result.metafile = result.metafile;
106
- conf.modifiedFileSet.clear();
107
- });
108
- },
109
- };
110
- }
111
- //# sourceMappingURL=sdReactPlugin.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sdReactPlugin.js","sourceRoot":"","sources":["../../src/bundle-plugins/sdReactPlugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAuB,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChF,OAAO,EAAE,2BAA2B,EAAE,MAAM,sDAAsD,CAAC;AACnG,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,UAAU,MAAM,gBAAgB,CAAC;AACxC,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,MAAM,UAAU,aAAa,CAAC,IAK7B;IACC,OAAO;QACL,IAAI,EAAE,gBAAgB;QACtB,KAAK,EAAE,CAAC,KAA0B,EAAE,EAAE;YACpC,MAAM,QAAQ,GAAG,IAAI,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;YAElF,IAAI,WAAgC,CAAC;YACrC,MAAM,sBAAsB,GAAG,IAAI,GAAG,EAA2C,CAAC;YAElF,6BAA6B;YAE7B,KAAK,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE;gBACvB,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;gBAC1C,KAAK,MAAM,YAAY,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;oBAChD,sBAAsB,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;gBAC9C,CAAC;gBAED,WAAW,GAAG,MAAM,QAAQ,CAAC,UAAU,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EAAE,EAAE;gBACP,IAAI,CAAC,MAAM,CAAC,YAAa,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;gBACzD,OAAO,IAAI,CAAC;YACd,CAAC,CACF,CAAC;YAEF,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE;gBACrB,KAAK,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,IAAI,WAAW,CAAC,2BAA2B,CAAC,MAAM,EAAE,EAAE,CAAC;oBACzF,MAAM,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,IAAI,EAAE,CAAC;oBAC9C,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;oBAExC,IAAI,MAAM,CAAC,QAAQ,IAAI,QAAQ,EAAE,CAAC;wBAChC,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;wBAC3E,MAAM,CAAC,QAAQ,CAAC,OAAO,GAAG,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;oBAChF,CAAC;gBACH,CAAC;gBAED,IAAI,CAAC,MAAM,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;gBAC7C,IAAI,CAAC,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;gBAEvC,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;YAC/B,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -1,339 +0,0 @@
1
- import path from "path";
2
- import { FsUtil, Logger, PathUtil } from "@simplysm/sd-core-node";
3
- import { fileURLToPath } from "url";
4
- import nodeStdLibBrowser from "node-stdlib-browser";
5
- import nodeStdLibBrowserPlugin from "node-stdlib-browser/helpers/esbuild/plugin";
6
- import { INpmConfig, ISdCliClientBuilderCordovaConfig, ISdCliPackageBuildResult } from "../commons";
7
- import ts from "typescript";
8
- import { SdReactBundlerContext } from "./SdReactBundlerContext";
9
- import { IReactPluginResultCache, sdReactPlugin } from "../bundle-plugins/sdReactPlugin";
10
- import { transformSupportedBrowsersToTargets } from "@angular/build/src/tools/esbuild/utils";
11
- import browserslist from "browserslist";
12
- import { glob } from "glob";
13
- import { generateSW } from "workbox-build";
14
-
15
- export class SdReactBundler {
16
- readonly #logger = Logger.get(["simplysm", "sd-cli", "SdNgBundler"]);
17
-
18
- readonly #modifiedFileSet = new Set<string>();
19
- readonly #compileResultCache: IReactPluginResultCache = {
20
- affectedFileSet: new Set<string>(),
21
- watchFileSet: new Set<string>(),
22
- };
23
- #contexts: SdReactBundlerContext[] | undefined;
24
-
25
- readonly #outputCache = new Map<string, string | number>();
26
-
27
- readonly #opt: IOptions;
28
-
29
- readonly #pkgNpmConf: INpmConfig;
30
- readonly #indexFilePath: string;
31
- readonly #tsConfigFilePath: string;
32
- readonly #browserTarget: string[];
33
-
34
- public constructor(opt: IOptions) {
35
- this.#opt = opt;
36
- this.#pkgNpmConf = FsUtil.readJson(path.resolve(opt.pkgPath, "package.json"));
37
- this.#indexFilePath = path.resolve(opt.pkgPath, "src/index.tsx");
38
- this.#tsConfigFilePath = path.resolve(opt.pkgPath, "tsconfig.json");
39
- this.#browserTarget = transformSupportedBrowsersToTargets(browserslist(["Chrome > 78"]));
40
- }
41
-
42
- public markForChanges(filePaths: string[]): void {
43
- for (const filePath of filePaths) {
44
- this.#modifiedFileSet.add(path.normalize(filePath));
45
- }
46
- }
47
-
48
- public async bundleAsync(): Promise<{
49
- program?: ts.Program;
50
- watchFileSet: Set<string>;
51
- affectedFileSet: Set<string>;
52
- results: ISdCliPackageBuildResult[];
53
- }> {
54
- this.#debug(`get contexts...`);
55
-
56
- if (!this.#contexts) {
57
- this.#contexts = [
58
- this._getAppContext(),
59
- ...(this.#opt.builderType === "electron" ? [this._getElectronMainContext()] : []),
60
- ];
61
- }
62
-
63
- this.#debug(`build...`);
64
-
65
- const bundlingResults = await this.#contexts.mapAsync(async (ctx, i) => await ctx.bundleAsync());
66
-
67
- //-- results
68
- const results = bundlingResults.mapMany((bundlingResult) => bundlingResult.results);
69
-
70
- this.#debug(`convert result...`);
71
-
72
- const outputFiles: IReactOutputFile[] = bundlingResults
73
- .mapMany((item) => item.outputFiles ?? [])
74
- .map((item) => ({
75
- relPath: path.relative(this.#opt.pkgPath, item.path),
76
- contents: item.contents,
77
- }));
78
-
79
- // cordova empty
80
- if (this.#opt.builderType === "cordova" && this.#opt.cordovaConfig?.plugins) {
81
- outputFiles.push({ relPath: "cordova-empty.js", contents: "export default {};" });
82
- }
83
-
84
- // index.html
85
- let indexHtml = await FsUtil.readFileAsync(path.resolve(this.#opt.pkgPath, "src/index.html"));
86
- indexHtml = indexHtml.replace(/<\/head>/, '<script type="module" src="index.js"></script></head>');
87
- if (this.#opt.builderType === "cordova") {
88
- indexHtml = indexHtml.replace(
89
- /(.*)<\/head>/,
90
- '<script type="module" src="cordova-entry.js"></script>\n$1</head>',
91
- );
92
- }
93
- if (outputFiles.some((item) => item.relPath === "index.css")) {
94
- indexHtml = indexHtml.replace(/(.*)<\/head>/, '<link rel="stylesheet" href="index.css">\n$1</head>');
95
- }
96
-
97
- outputFiles.push({
98
- relPath: "index.html",
99
- contents: indexHtml,
100
- });
101
-
102
- // copy assets
103
- outputFiles.push(
104
- ...this.#copyAssets("src", "favicon.ico"),
105
- ...this.#copyAssets("src", "manifest.webmanifest"),
106
- ...this.#copyAssets("src/assets", "**/*", "assets"),
107
- ...(this.#opt.dev && this.#opt.builderType === "cordova"
108
- ? Object.keys(this.#opt.cordovaConfig?.platform ?? { browser: {} }).mapMany((platform) => [
109
- ...this.#copyAssets(
110
- `.cordova/platforms/${platform}/platform_www/plugins`,
111
- "**/*",
112
- `cordova-${platform}/plugins`,
113
- ),
114
- ...this.#copyAssets(`.cordova/platforms/${platform}/platform_www`, "cordova.js", `cordova-${platform}`),
115
- ...this.#copyAssets(
116
- `.cordova/platforms/${platform}/platform_www`,
117
- "cordova_plugins.js",
118
- `cordova-${platform}`,
119
- ),
120
- ...this.#copyAssets(`.cordova/platforms/${platform}/www`, "config.xml", `cordova-${platform}`),
121
- ])
122
- : []),
123
- );
124
-
125
- // TODO: extract 3rdpartylicenses
126
-
127
- //-- write
128
- this.#debug(`write output files...(${outputFiles.length})`);
129
-
130
- for (const outputFile of outputFiles) {
131
- const distFilePath = path.resolve(this.#opt.outputPath, outputFile.relPath);
132
- const prev = this.#outputCache.get(distFilePath);
133
- if (prev !== Buffer.from(outputFile.contents).toString("base64")) {
134
- await FsUtil.writeFileAsync(distFilePath, outputFile.contents);
135
- this.#outputCache.set(distFilePath, Buffer.from(outputFile.contents).toString("base64"));
136
- }
137
- }
138
-
139
- // TODO: service worker
140
- const swResult = await generateSW({
141
- globDirectory: path.resolve(this.#opt.pkgPath, "dist"),
142
- swDest: path.resolve(this.#opt.pkgPath, "dist", "sw.js"),
143
- });
144
- swResult.warnings.map((msg) => {
145
- results.push({
146
- filePath: undefined,
147
- line: undefined,
148
- char: undefined,
149
- code: undefined,
150
- severity: "warning",
151
- message: `(gen-sw) ${msg}`,
152
- type: "build",
153
- });
154
- });
155
-
156
- this.#debug(`번들링중 영향받은 파일`, Array.from(this.#compileResultCache.affectedFileSet!));
157
-
158
- return {
159
- program: this.#compileResultCache.program,
160
- watchFileSet: new Set(this.#compileResultCache.watchFileSet),
161
- affectedFileSet: this.#compileResultCache.affectedFileSet!,
162
- results,
163
- };
164
- }
165
-
166
- #copyAssets(srcDir: string, globPath: string, distDir?: string): IReactOutputFile[] {
167
- const result: IReactOutputFile[] = [];
168
- const srcGlobPath = path.resolve(this.#opt.pkgPath, srcDir, globPath);
169
- const srcPaths = glob.sync(srcGlobPath);
170
- for (const srcPath of srcPaths) {
171
- if (!FsUtil.exists(srcPath)) continue;
172
- result.push({
173
- relPath: path.join(
174
- ...[distDir, path.relative(path.resolve(this.#opt.pkgPath, srcDir), srcPath)].filterExists(),
175
- ),
176
- contents: FsUtil.readFile(srcPath),
177
- });
178
- }
179
-
180
- return result;
181
- }
182
-
183
- private _getAppContext() {
184
- return new SdReactBundlerContext(this.#opt.pkgPath, {
185
- absWorkingDir: this.#opt.pkgPath,
186
- bundle: true,
187
- keepNames: true,
188
- format: "esm",
189
- assetNames: "media/[name]",
190
- conditions: ["es2020", "es2015", "module"],
191
- resolveExtensions: [".js", ".mjs", ".cjs", ".ts", ".tsx"],
192
- metafile: true,
193
- legalComments: this.#opt.dev ? "eof" : "none",
194
- logLevel: "silent",
195
- minifyIdentifiers: !this.#opt.dev,
196
- minifySyntax: !this.#opt.dev,
197
- minifyWhitespace: !this.#opt.dev,
198
- pure: ["forwardRef"],
199
- outdir: this.#opt.pkgPath,
200
- outExtension: undefined,
201
- sourcemap: true, //this.#opt.dev,
202
- splitting: true,
203
- chunkNames: "[name]-[hash]",
204
- tsconfig: this.#tsConfigFilePath,
205
- write: false,
206
- preserveSymlinks: false,
207
- define: {
208
- ...(!this.#opt.dev ? { ngDevMode: "false" } : {}),
209
- "ngJitMode": "false",
210
- "global": "global",
211
- "process": "process",
212
- "Buffer": "Buffer",
213
- "process.env.SD_VERSION": JSON.stringify(this.#pkgNpmConf.version),
214
- "process.env.NODE_ENV": JSON.stringify(this.#opt.dev ? "development" : "production"),
215
- ...(this.#opt.env
216
- ? Object.keys(this.#opt.env).toObject(
217
- (key) => `process.env.${key}`,
218
- (key) => JSON.stringify(this.#opt.env![key]),
219
- )
220
- : {}),
221
- },
222
- platform: "browser",
223
- mainFields: ["es2020", "es2015", "browser", "module", "main"],
224
- entryNames: "[name]",
225
- entryPoints: {
226
- index: this.#indexFilePath,
227
- },
228
- external: ["electron"],
229
- target: this.#browserTarget,
230
- supported: { "async-await": false, "object-rest-spread": false },
231
- loader: {
232
- ".png": "file",
233
- ".jpeg": "file",
234
- ".jpg": "file",
235
- ".jfif": "file",
236
- ".gif": "file",
237
- ".svg": "file",
238
- ".woff": "file",
239
- ".woff2": "file",
240
- ".ttf": "file",
241
- ".ttc": "file",
242
- ".eot": "file",
243
- ".ico": "file",
244
- ".otf": "file",
245
- ".csv": "file",
246
- ".xlsx": "file",
247
- ".xls": "file",
248
- ".pptx": "file",
249
- ".ppt": "file",
250
- ".docx": "file",
251
- ".doc": "file",
252
- ".zip": "file",
253
- ".pfx": "file",
254
- ".pkl": "file",
255
- ".mp3": "file",
256
- ".ogg": "file",
257
- },
258
- inject: [PathUtil.posix(fileURLToPath(import.meta.resolve("node-stdlib-browser/helpers/esbuild/shim")))],
259
- plugins: [
260
- ...(this.#opt.builderType === "cordova" && this.#opt.cordovaConfig?.plugins
261
- ? [
262
- {
263
- name: "cordova:plugin-empty",
264
- setup: ({ onResolve }) => {
265
- onResolve({ filter: new RegExp("(" + this.#opt.cordovaConfig!.plugins!.join("|") + ")") }, () => {
266
- return {
267
- path: `./cordova-empty.js`,
268
- external: true,
269
- };
270
- });
271
- },
272
- },
273
- ]
274
- : []),
275
- sdReactPlugin({
276
- modifiedFileSet: this.#modifiedFileSet,
277
- dev: this.#opt.dev,
278
- pkgPath: this.#opt.pkgPath,
279
- result: this.#compileResultCache,
280
- }),
281
- nodeStdLibBrowserPlugin(nodeStdLibBrowser),
282
- ],
283
- });
284
- }
285
-
286
- private _getElectronMainContext() {
287
- return new SdReactBundlerContext(this.#opt.pkgPath, {
288
- absWorkingDir: this.#opt.pkgPath,
289
- bundle: true,
290
- entryNames: "[name]",
291
- assetNames: "media/[name]",
292
- conditions: ["es2020", "es2015", "module"],
293
- resolveExtensions: [".js", ".mjs", ".cjs", ".ts"],
294
- metafile: true,
295
- legalComments: this.#opt.dev ? "eof" : "none",
296
- logLevel: "silent",
297
- minify: !this.#opt.dev,
298
- outdir: this.#opt.pkgPath,
299
- sourcemap: true, //this.#opt.dev,
300
- tsconfig: this.#tsConfigFilePath,
301
- write: false,
302
- preserveSymlinks: false,
303
- external: ["electron"],
304
- define: {
305
- ...(!this.#opt.dev ? { ngDevMode: "false" } : {}),
306
- "process.env.SD_VERSION": JSON.stringify(this.#pkgNpmConf.version),
307
- "process.env.NODE_ENV": JSON.stringify(this.#opt.dev ? "development" : "production"),
308
- ...(this.#opt.env
309
- ? Object.keys(this.#opt.env).toObject(
310
- (key) => `process.env.${key}`,
311
- (key) => JSON.stringify(this.#opt.env![key]),
312
- )
313
- : {}),
314
- },
315
- platform: "node",
316
- entryPoints: {
317
- "electron-main": path.resolve(this.#opt.pkgPath, "src/electron-main.ts"),
318
- },
319
- });
320
- }
321
-
322
- #debug(...msg: any[]): void {
323
- this.#logger.debug(`[${path.basename(this.#opt.pkgPath)}]`, ...msg);
324
- }
325
- }
326
-
327
- interface IOptions {
328
- dev: boolean;
329
- outputPath: string;
330
- pkgPath: string;
331
- builderType: string;
332
- env: Record<string, string> | undefined;
333
- cordovaConfig: ISdCliClientBuilderCordovaConfig | undefined;
334
- }
335
-
336
- interface IReactOutputFile {
337
- relPath: string;
338
- contents: Uint8Array | string;
339
- }
@@ -1,71 +0,0 @@
1
- import esbuild from "esbuild";
2
- import path from "path";
3
- import { ISdCliPackageBuildResult } from "../commons";
4
- import { Logger } from "@simplysm/sd-core-node";
5
-
6
- export class SdReactBundlerContext {
7
- readonly #logger = Logger.get(["simplysm", "sd-cli", "SdReactBundlerContext"]);
8
-
9
- private _context?: esbuild.BuildContext;
10
-
11
- public constructor(
12
- private readonly _pkgPath: string,
13
- private readonly _esbuildOptions: esbuild.BuildOptions,
14
- ) {}
15
-
16
- public async bundleAsync() {
17
- if (this._context == null) {
18
- this._context = await esbuild.context(this._esbuildOptions);
19
- }
20
-
21
- let buildResult: esbuild.BuildResult;
22
-
23
- try {
24
- this.#debug(`rebuild...`);
25
- buildResult = await this._context.rebuild();
26
- this.#debug(`rebuild completed`);
27
- } catch (err) {
28
- if ("warnings" in err || "errors" in err) {
29
- buildResult = err;
30
- } else {
31
- throw err;
32
- }
33
- }
34
-
35
- this.#debug(`convert results...`);
36
-
37
- const results = [
38
- ...buildResult.warnings.map((warn) => ({
39
- filePath: warn.location?.file !== undefined ? path.resolve(this._pkgPath, warn.location.file) : undefined,
40
- line: warn.location?.line,
41
- char: warn.location?.column,
42
- code: warn.text.slice(0, warn.text.indexOf(":")),
43
- severity: "warning",
44
- message: `${warn.pluginName ? `(${warn.pluginName}) ` : ""} ${warn.text.slice(warn.text.indexOf(":") + 1)}`,
45
- type: "build",
46
- })),
47
- ...buildResult.errors.map((err) => ({
48
- filePath: err.location?.file !== undefined ? path.resolve(this._pkgPath, err.location.file) : undefined,
49
- line: err.location?.line,
50
- char: err.location?.column !== undefined ? err.location.column + 1 : undefined,
51
- code: err.text.slice(0, err.text.indexOf(":")),
52
- severity: "error",
53
- message: `${err.pluginName ? `(${err.pluginName}) ` : ""} ${err.text.slice(err.text.indexOf(":") + 1)}`,
54
- type: "build",
55
- })),
56
- ] as ISdCliPackageBuildResult[];
57
-
58
- return {
59
- results,
60
- outputFiles: buildResult.outputFiles,
61
- metafile: buildResult.metafile,
62
- };
63
- }
64
-
65
- #debug(...msg: any[]): void {
66
- this.#logger.debug(
67
- `[${path.basename(this._pkgPath)}] (${Object.keys(this._esbuildOptions.entryPoints as Record<string, any>).join(", ")})`,
68
- ...msg,
69
- );
70
- }
71
- }
@@ -1,157 +0,0 @@
1
- import esbuild from "esbuild";
2
- import ts from "typescript";
3
- import path from "path";
4
- import { ISdTsCompilerResult, SdTsCompiler } from "../build-tools/SdTsCompiler";
5
- import { convertTypeScriptDiagnostic } from "@angular/build/src/tools/esbuild/angular/diagnostics";
6
- import postcss from "postcss";
7
- import { FsUtil } from "@simplysm/sd-core-node";
8
- import postcssUrl from "postcss-url";
9
- import postcssHas from "css-has-pseudo";
10
- import autoprefixer from "autoprefixer";
11
-
12
- export function sdReactPlugin(conf: {
13
- pkgPath: string;
14
- dev: boolean;
15
- modifiedFileSet: Set<string>;
16
- result: IReactPluginResultCache;
17
- }): esbuild.Plugin {
18
- return {
19
- name: "sd-ng-compiler",
20
- setup: (build: esbuild.PluginBuild) => {
21
- const compiler = new SdTsCompiler(conf.pkgPath, { declaration: false }, conf.dev);
22
-
23
- let buildResult: ISdTsCompilerResult;
24
- const outputContentsCacheMap = new Map<string, string | Uint8Array | undefined>();
25
-
26
- //---------------------------
27
-
28
- build.onStart(async () => {
29
- compiler.invalidate(conf.modifiedFileSet);
30
- for (const modifiedFile of conf.modifiedFileSet) {
31
- outputContentsCacheMap.delete(modifiedFile);
32
- }
33
-
34
- buildResult = await compiler.buildAsync();
35
-
36
- conf.result.watchFileSet = buildResult.watchFileSet;
37
- conf.result.affectedFileSet = buildResult.affectedFileSet;
38
- conf.result.program = buildResult.program;
39
-
40
- //-- return err/warn
41
- return {
42
- errors: [
43
- ...buildResult.typescriptDiagnostics
44
- .filter((item) => item.category === ts.DiagnosticCategory.Error)
45
- .map((item) => convertTypeScriptDiagnostic(ts, item)),
46
- ...Array.from(buildResult.stylesheetBundlingResultMap.values()).flatMap((item) => item.errors),
47
- ].filterExists(),
48
- warnings: [
49
- ...buildResult.typescriptDiagnostics
50
- .filter((item) => item.category !== ts.DiagnosticCategory.Error)
51
- .map((item) => convertTypeScriptDiagnostic(ts, item)),
52
- // ...Array.from(buildResult.stylesheetResultMap.values()).flatMap(item => item.warnings)
53
- ],
54
- };
55
- });
56
-
57
- build.onLoad({ filter: /\.tsx?$/ }, (args) => {
58
- const output = outputContentsCacheMap.get(path.normalize(args.path));
59
- if (output != null) {
60
- return { contents: output, loader: "js" };
61
- }
62
-
63
- const emittedJsFile = buildResult.emittedFilesCacheMap.get(path.normalize(args.path))?.last();
64
- if (!emittedJsFile) {
65
- throw new Error(`ts 빌더 결과 emit 파일이 존재하지 않습니다. ${args.path}`);
66
- }
67
-
68
- const contents = emittedJsFile.text;
69
-
70
- outputContentsCacheMap.set(path.normalize(args.path), contents);
71
-
72
- return { contents, loader: "js" };
73
- });
74
-
75
- build.onLoad({ filter: /\.[cm]?jsx?$/ }, (args) => {
76
- conf.result.watchFileSet!.add(path.normalize(args.path));
77
- return null;
78
- });
79
-
80
- build.onLoad({ filter: /\.css$/ }, async (args) => {
81
- conf.result.watchFileSet!.add(path.normalize(args.path));
82
- const output = outputContentsCacheMap.get(path.normalize(args.path));
83
- if (output != null) {
84
- return {
85
- contents: output,
86
- loader: "file",
87
- };
88
- }
89
-
90
- const css = await FsUtil.readFileAsync(path.normalize(args.path));
91
- const result = await postcss()
92
- .use(
93
- autoprefixer({
94
- overrideBrowserslist: ["Chrome > 78"]
95
- })
96
- )
97
- .use(
98
- postcssUrl({
99
- url: "copy",
100
- }),
101
- )
102
- .use(
103
- postcssHas()
104
- )
105
- .process(css, {
106
- from: path.normalize(args.path),
107
- to: path.resolve(conf.pkgPath, "dist", "media", path.basename(args.path)),
108
- });
109
-
110
- outputContentsCacheMap.set(path.normalize(args.path), result.css);
111
-
112
- return { contents: result.css, loader: "file" };
113
- });
114
-
115
- build.onLoad(
116
- {
117
- filter: new RegExp(
118
- "(" +
119
- Object.keys(build.initialOptions.loader!)
120
- .map((item) => "\\" + item)
121
- .join("|") +
122
- ")$",
123
- ),
124
- },
125
- (args) => {
126
- conf.result.watchFileSet!.add(path.normalize(args.path));
127
- return null;
128
- },
129
- );
130
-
131
- build.onEnd((result) => {
132
- for (const { outputFiles, metafile } of buildResult.stylesheetBundlingResultMap.values()) {
133
- result.outputFiles = result.outputFiles ?? [];
134
- result.outputFiles.push(...outputFiles);
135
-
136
- if (result.metafile && metafile) {
137
- result.metafile.inputs = { ...result.metafile.inputs, ...metafile.inputs };
138
- result.metafile.outputs = { ...result.metafile.outputs, ...metafile.outputs };
139
- }
140
- }
141
-
142
- conf.result.outputFiles = result.outputFiles;
143
- conf.result.metafile = result.metafile;
144
-
145
- conf.modifiedFileSet.clear();
146
- });
147
- },
148
- };
149
- }
150
-
151
- export interface IReactPluginResultCache {
152
- watchFileSet?: Set<string>;
153
- affectedFileSet?: Set<string>;
154
- program?: ts.Program;
155
- outputFiles?: esbuild.OutputFile[];
156
- metafile?: esbuild.Metafile;
157
- }