@gravity-ui/app-builder 0.28.1-beta.6 → 0.28.1-beta.7

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.
package/README.md CHANGED
@@ -143,6 +143,8 @@ All server settings are used only in dev mode:
143
143
  - `watchThrottle` (`number`) — use to add an extra throttle, or delay restarting.
144
144
  - `inspect/inspectBrk` (`number | true`) — listen for a debugging client on specified port.
145
145
  If specified `true`, try to listen on `9229`.
146
+ - `compiler` (`'typescript' | 'swc'`) — choose TypeScript compiler for server code compilation.
147
+ Default is `'typescript'`. Set to `'swc'` for faster compilation with SWC.
146
148
 
147
149
  ### Client
148
150
 
@@ -8,25 +8,52 @@ const signal_exit_1 = require("signal-exit");
8
8
  const controllable_script_1 = require("../../../common/child-process/controllable-script");
9
9
  const paths_1 = __importDefault(require("../../../common/paths"));
10
10
  const utils_1 = require("../../../common/utils");
11
+ function createSWCBuildScript(config) {
12
+ return `
13
+ let swcCli;
14
+ try {
15
+ swcCli = require('@swc/cli');
16
+ } catch (e) {
17
+ if (e.code !== 'MODULE_NOT_FOUND') {
18
+ throw e;
19
+ }
20
+ swcCli = require(${JSON.stringify(require.resolve('@swc/cli'))});
21
+ }
22
+ const {swcDir} = swcCli;
23
+ const {Logger} = require(${JSON.stringify(require.resolve('../../../common/logger'))});
24
+ const {compile} = require(${JSON.stringify(require.resolve('../../../common/swc/compile'))});
25
+
26
+ const logger = new Logger('server', ${config.verbose});
27
+ compile(swcDir, {
28
+ logger,
29
+ outputPath: ${JSON.stringify(paths_1.default.appDist)},
30
+ projectPath: ${JSON.stringify(paths_1.default.appServer)},
31
+ });`;
32
+ }
33
+ function createTypescriptBuildScript(config) {
34
+ return `
35
+ let ts;
36
+ try {
37
+ ts = require('typescript');
38
+ } catch (e) {
39
+ if (e.code !== 'MODULE_NOT_FOUND') {
40
+ throw e;
41
+ }
42
+ ts = require(${JSON.stringify(require.resolve('typescript'))});
43
+ }
44
+ const {Logger} = require(${JSON.stringify(require.resolve('../../../common/logger'))});
45
+ const {compile} = require(${JSON.stringify(require.resolve('../../../common/typescript/compile'))});
46
+
47
+ const logger = new Logger('server', ${config.verbose});
48
+ compile(ts, {logger, projectPath: ${JSON.stringify(paths_1.default.appServer)}});`;
49
+ }
11
50
  function buildServer(config) {
12
51
  (0, utils_1.createRunFolder)();
52
+ // Используем TypeScript для компиляции (по умолчанию)
13
53
  return new Promise((resolve, reject) => {
14
- const build = new controllable_script_1.ControllableScript(`
15
- let ts;
16
- try {
17
- ts = require('typescript');
18
- } catch (e) {
19
- if (e.code !== 'MODULE_NOT_FOUND') {
20
- throw e;
21
- }
22
- ts = require(${JSON.stringify(require.resolve('typescript'))});
23
- }
24
- const {Logger} = require(${JSON.stringify(require.resolve('../../../common/logger'))});
25
- const {compile} = require(${JSON.stringify(require.resolve('../../../common/typescript/compile'))});
26
-
27
- const logger = new Logger('server', ${config.verbose});
28
- compile(ts, {logger, projectPath: ${JSON.stringify(paths_1.default.appServer)}});
29
- `, null);
54
+ const build = new controllable_script_1.ControllableScript(config.server.compiler === 'swc'
55
+ ? createSWCBuildScript(config)
56
+ : createTypescriptBuildScript(config), null);
30
57
  build.start().then(() => {
31
58
  build.onExit((code) => {
32
59
  if (code) {
@@ -32,36 +32,69 @@ const rimraf_1 = require("rimraf");
32
32
  const controllable_script_1 = require("../../common/child-process/controllable-script");
33
33
  const utils_1 = require("../../common/utils");
34
34
  const paths_1 = __importDefault(require("../../common/paths"));
35
+ function createTypescriptBuildScript(config) {
36
+ return `
37
+ let ts;
38
+ try {
39
+ ts = require('typescript');
40
+ } catch (e) {
41
+ if (e.code !== 'MODULE_NOT_FOUND') {
42
+ throw e;
43
+ }
44
+ ts = require(${JSON.stringify(require.resolve('typescript'))});
45
+ }
46
+ const {Logger} = require(${JSON.stringify(require.resolve('../../common/logger'))});
47
+ const {watch} = require(${JSON.stringify(require.resolve('../../common/typescript/watch'))});
48
+
49
+ const logger = new Logger('server', ${config.verbose});
50
+ watch(
51
+ ts,
52
+ ${JSON.stringify(paths_1.default.appServer)},
53
+ {
54
+ logger,
55
+ onAfterFilesEmitted: () => {
56
+ process.send({type: 'Emitted'});
57
+ },
58
+ enableSourceMap: true
59
+ }
60
+ );`;
61
+ }
62
+ function createSWCBuildScript(config) {
63
+ return `
64
+ let swcCli;
65
+ try {
66
+ swcCli = require('@swc/cli');
67
+ } catch (e) {
68
+ if (e.code !== 'MODULE_NOT_FOUND') {
69
+ throw e;
70
+ }
71
+ swcCli = require(${JSON.stringify(require.resolve('@swc/cli'))});
72
+ }
73
+ const {swcDir} = swcCli;
74
+ const {Logger} = require(${JSON.stringify(require.resolve('../../common/logger'))});
75
+ const {watch} = require(${JSON.stringify(require.resolve('../../common/swc/watch'))});
76
+
77
+ const logger = new Logger('server', ${config.verbose});
78
+ watch(
79
+ swcDir,
80
+ ${JSON.stringify(paths_1.default.appServer)},
81
+ {
82
+ outputPath: ${JSON.stringify(paths_1.default.appDist)},
83
+ logger,
84
+ onAfterFilesEmitted: () => {
85
+ process.send({type: 'Emitted'});
86
+ },
87
+ enableSourceMap: true
88
+ }
89
+ );`;
90
+ }
35
91
  async function watchServerCompilation(config) {
36
92
  const serverPath = path.resolve(paths_1.default.appDist, 'server');
37
93
  rimraf_1.rimraf.sync(serverPath);
38
94
  (0, utils_1.createRunFolder)();
39
- const build = new controllable_script_1.ControllableScript(`
40
- let ts;
41
- try {
42
- ts = require('typescript');
43
- } catch (e) {
44
- if (e.code !== 'MODULE_NOT_FOUND') {
45
- throw e;
46
- }
47
- ts = require(${JSON.stringify(require.resolve('typescript'))});
48
- }
49
- const {Logger} = require(${JSON.stringify(require.resolve('../../common/logger'))});
50
- const {watch} = require(${JSON.stringify(require.resolve('../../common/typescript/watch'))});
51
-
52
- const logger = new Logger('server', ${config.verbose});
53
- watch(
54
- ts,
55
- ${JSON.stringify(paths_1.default.appServer)},
56
- {
57
- logger,
58
- onAfterFilesEmitted: () => {
59
- process.send({type: 'Emitted'});
60
- },
61
- enableSourceMap: true
62
- }
63
- );
64
- `, null);
95
+ const build = new controllable_script_1.ControllableScript(config.server.compiler === 'swc'
96
+ ? createSWCBuildScript(config)
97
+ : createTypescriptBuildScript(config), null);
65
98
  await build.start();
66
99
  return build;
67
100
  }
@@ -154,6 +154,7 @@ async function normalizeConfig(userConfig, mode) {
154
154
  port: undefined,
155
155
  inspect: undefined,
156
156
  inspectBrk: undefined,
157
+ compiler: serverConfig.compiler || 'typescript',
157
158
  };
158
159
  if (mode === 'dev') {
159
160
  if (serverConfig.port === true) {
@@ -18,6 +18,7 @@ import type { TerserOptions } from 'terser-webpack-plugin';
18
18
  import type { ReactRefreshPluginOptions } from '@pmmmwh/react-refresh-webpack-plugin/types/lib/types';
19
19
  type Bundler = 'webpack' | 'rspack';
20
20
  type JavaScriptLoader = 'babel' | 'swc';
21
+ type ServerCompiler = 'typescript' | 'swc';
21
22
  export type SwcConfig = Swc.Config & Pick<Swc.Options, 'isModule'>;
22
23
  export interface Entities<T> {
23
24
  data: Record<string, T>;
@@ -266,6 +267,11 @@ export interface ServerConfig {
266
267
  watchThrottle?: number;
267
268
  inspect?: number | true;
268
269
  inspectBrk?: number | true;
270
+ /**
271
+ * Compiler for server code compilation
272
+ * @default 'typescript'
273
+ */
274
+ compiler?: ServerCompiler;
269
275
  }
270
276
  export interface ServiceConfig {
271
277
  target?: 'client' | 'server';
@@ -306,11 +312,12 @@ export type NormalizedClientConfig = Omit<ClientConfig, 'publicPathPrefix' | 'pu
306
312
  }) => SwcConfig | Promise<SwcConfig>;
307
313
  reactRefresh: NonNullable<ClientConfig['reactRefresh']>;
308
314
  };
309
- export type NormalizedServerConfig = Omit<ServerConfig, 'port' | 'inspect' | 'inspectBrk'> & {
315
+ export type NormalizedServerConfig = Omit<ServerConfig, 'port' | 'inspect' | 'inspectBrk' | 'compiler'> & {
310
316
  port?: number;
311
317
  verbose?: boolean;
312
318
  inspect?: number;
313
319
  inspectBrk?: number;
320
+ compiler: ServerCompiler;
314
321
  };
315
322
  export type NormalizedServiceConfig = Omit<ServiceConfig, 'client' | 'server'> & {
316
323
  client: NormalizedClientConfig;
@@ -0,0 +1,9 @@
1
+ import type { Logger } from '../logger';
2
+ interface SwcCompileOptions {
3
+ projectPath: string;
4
+ outputPath: string;
5
+ logger: Logger;
6
+ enableSourceMap?: boolean;
7
+ }
8
+ export declare function compile(swcDir: any, { projectPath, outputPath, logger, enableSourceMap }: SwcCompileOptions): Promise<void>;
9
+ export {};
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compile = compile;
4
+ const pretty_time_1 = require("../logger/pretty-time");
5
+ const getSwcConfig = (enableSourceMap = false) => {
6
+ return {
7
+ module: {
8
+ type: 'commonjs',
9
+ },
10
+ jsc: {
11
+ target: 'es2020',
12
+ parser: {
13
+ syntax: 'typescript',
14
+ },
15
+ },
16
+ sourceMaps: enableSourceMap,
17
+ };
18
+ };
19
+ async function compile(
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ swcDir, { projectPath, outputPath, logger, enableSourceMap = false }) {
22
+ const start = process.hrtime.bigint();
23
+ logger.message('Start compilation');
24
+ const swcConfig = getSwcConfig(enableSourceMap);
25
+ const cliOptions = {
26
+ filenames: [projectPath],
27
+ outDir: outputPath,
28
+ watch: false,
29
+ sourceMaps: enableSourceMap,
30
+ extensions: ['.js', '.ts', '.mjs', '.cjs'],
31
+ stripLeadingPaths: true,
32
+ sync: false,
33
+ };
34
+ return new Promise((resolve, reject) => {
35
+ const callbacks = {
36
+ onSuccess: (_result) => {
37
+ logger.success(`Compiled successfully in ${(0, pretty_time_1.elapsedTime)(start)}`);
38
+ resolve();
39
+ },
40
+ onFail: (result) => {
41
+ logger.error(`Compilation failed in ${result.duration}ms`);
42
+ if (result.reasons) {
43
+ for (const [filename, error] of result.reasons) {
44
+ logger.error(`${filename}: ${error}`);
45
+ }
46
+ }
47
+ logger.error(`Error compile, elapsed time ${(0, pretty_time_1.elapsedTime)(start)}`);
48
+ reject(new Error('Compilation failed'));
49
+ },
50
+ };
51
+ try {
52
+ swcDir({
53
+ cliOptions,
54
+ swcOptions: swcConfig,
55
+ callbacks,
56
+ });
57
+ }
58
+ catch (error) {
59
+ logger.error(`Failed to start compilation: ${error}`);
60
+ reject(error);
61
+ }
62
+ });
63
+ }
@@ -0,0 +1,2 @@
1
+ export { compile } from './compile';
2
+ export { watch } from './watch';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.watch = exports.compile = void 0;
4
+ var compile_1 = require("./compile");
5
+ Object.defineProperty(exports, "compile", { enumerable: true, get: function () { return compile_1.compile; } });
6
+ var watch_1 = require("./watch");
7
+ Object.defineProperty(exports, "watch", { enumerable: true, get: function () { return watch_1.watch; } });
@@ -0,0 +1,9 @@
1
+ import type { Logger } from '../logger';
2
+ interface SwcWatchOptions {
3
+ outputPath: string;
4
+ logger: Logger;
5
+ onAfterFilesEmitted?: () => void;
6
+ enableSourceMap?: boolean;
7
+ }
8
+ export declare function watch(swcDir: any, projectPath: string, { outputPath, logger, onAfterFilesEmitted, enableSourceMap }: SwcWatchOptions): Promise<void>;
9
+ export {};
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.watch = watch;
4
+ const getSwcConfig = (enableSourceMap = false) => {
5
+ return {
6
+ module: {
7
+ type: 'commonjs',
8
+ },
9
+ jsc: {
10
+ target: 'es2020',
11
+ parser: {
12
+ syntax: 'typescript',
13
+ },
14
+ },
15
+ sourceMaps: enableSourceMap,
16
+ };
17
+ };
18
+ async function watch(
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ swcDir, projectPath, { outputPath, logger, onAfterFilesEmitted, enableSourceMap = false }) {
21
+ logger.message('Start compilation in watch mode');
22
+ const swcConfig = getSwcConfig(enableSourceMap);
23
+ const cliOptions = {
24
+ filenames: [projectPath],
25
+ outDir: outputPath,
26
+ watch: true,
27
+ sourceMaps: enableSourceMap,
28
+ extensions: ['.js', '.ts', '.mjs', '.cjs'],
29
+ stripLeadingPaths: true,
30
+ sync: false,
31
+ logWatchCompilation: true,
32
+ };
33
+ const callbacks = {
34
+ onSuccess: (result) => {
35
+ if (result.filename) {
36
+ logger.message(`Successfully compiled ${result.filename} in ${result.duration}ms`);
37
+ }
38
+ else {
39
+ logger.message(`Successfully compiled ${result.compiled || 0} files in ${result.duration}ms`);
40
+ }
41
+ onAfterFilesEmitted?.();
42
+ },
43
+ onFail: (result) => {
44
+ logger.error(`Compilation failed in ${result.duration}ms`);
45
+ if (result.reasons) {
46
+ for (const [filename, error] of result.reasons) {
47
+ logger.error(`${filename}: ${error}`);
48
+ }
49
+ }
50
+ },
51
+ onWatchReady: () => {
52
+ logger.message('Watching for file changes');
53
+ },
54
+ };
55
+ swcDir({
56
+ cliOptions,
57
+ swcOptions: swcConfig,
58
+ callbacks,
59
+ });
60
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/app-builder",
3
- "version": "0.28.1-beta.6",
3
+ "version": "0.28.1-beta.7",
4
4
  "description": "Develop and build your React client-server projects, powered by typescript and webpack",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",
@@ -75,13 +75,14 @@
75
75
  "@rspack/core": "1.3.9",
76
76
  "@rspack/dev-server": "^1.1.1",
77
77
  "@rspack/plugin-react-refresh": "^1.4.1",
78
- "@statoscope/webpack-plugin": "^5.29.0",
79
78
  "@statoscope/stats": "^5.28.1",
80
79
  "@statoscope/stats-extension-compressed": "^5.28.1",
81
80
  "@statoscope/webpack-model": "^5.29.0",
81
+ "@statoscope/webpack-plugin": "^5.29.0",
82
82
  "@svgr/core": "^8.1.0",
83
83
  "@svgr/plugin-jsx": "^8.1.0",
84
84
  "@svgr/webpack": "^8.1.0",
85
+ "@swc/cli": "^0.7.8",
85
86
  "@swc/core": "1.11.24",
86
87
  "@swc/plugin-transform-imports": "7.0.3",
87
88
  "babel-loader": "^9.2.1",