@granite-js/cli 0.1.7 → 0.1.8

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @granite-js/cli
2
2
 
3
+ ## 0.1.8
4
+
5
+ ### Patch Changes
6
+
7
+ - b69366c: improve config structure
8
+ - Updated dependencies [b69366c]
9
+ - @granite-js/plugin-core@0.1.8
10
+ - @granite-js/mpack@0.1.8
11
+ - @granite-js/utils@0.1.8
12
+
3
13
  ## 0.1.7
4
14
 
5
15
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -39,48 +39,9 @@ var import_clipanion4 = require("clipanion");
39
39
  var import_cosmiconfig2 = require("cosmiconfig");
40
40
 
41
41
  // src/commands/BuildCommand/BuildCommand.ts
42
- var import_clipanion = require("clipanion");
43
-
44
- // src/build.ts
45
42
  var import_mpack = require("@granite-js/mpack");
46
43
  var import_plugins = require("@granite-js/mpack/plugins");
47
- var import_plugin_core = require("@granite-js/plugin-core");
48
- async function build(config, {
49
- tag,
50
- cache
51
- }) {
52
- const resolvedPlugins = await (0, import_plugin_core.resolvePlugins)(config.plugins);
53
- const pluginContext = {
54
- meta: /* @__PURE__ */ Object.create(null)
55
- };
56
- for (const preHandler of resolvedPlugins.build.preHandlers) {
57
- await preHandler?.call(pluginContext, {
58
- cwd: config.cwd,
59
- entryFile: config.entryFile,
60
- appName: config.appName,
61
- outdir: config.outdir,
62
- buildResults: []
63
- });
64
- }
65
- const results = await (0, import_mpack.runBundle)({
66
- tag,
67
- rootDir: config.cwd,
68
- dev: false,
69
- cache,
70
- metafile: true,
71
- plugins: [import_plugins.statusPlugin],
72
- config: config.mpack.build.config
73
- });
74
- for (const postHandler of resolvedPlugins.build.postHandlers) {
75
- await postHandler?.call(pluginContext, {
76
- cwd: config.cwd,
77
- entryFile: config.entryFile,
78
- appName: config.appName,
79
- outdir: config.outdir,
80
- buildResults: results
81
- });
82
- }
83
- }
44
+ var import_clipanion = require("clipanion");
84
45
 
85
46
  // src/config/loadConfig.ts
86
47
  var import_utils = require("@granite-js/utils");
@@ -110,26 +71,30 @@ var BuildCommand = class extends import_clipanion.Command {
110
71
  static paths = [[`build`]];
111
72
  static usage = import_clipanion.Command.Usage({
112
73
  category: "Build",
113
- description: "Granite App \uBC88\uB4E4\uC744 \uC0DD\uC131\uD569\uB2C8\uB2E4",
114
- examples: [
115
- ["\uBE4C\uB4DC\uD558\uAE30", "granite build"],
116
- ["\uC9C0\uC815\uB41C \uB300\uC0C1\uB9CC \uBE4C\uB4DC\uD558\uAE30", "granite build --id service-ios"]
117
- ]
74
+ description: "Build Granite App",
75
+ examples: [["Build Granite App", "granite build"]]
118
76
  });
119
- tag = import_clipanion.Option.String("--tag", {
120
- description: "\uAD6C\uC131 \uD30C\uC77C\uC5D0\uC11C \uC9C0\uC815\uD55C \uD0DC\uADF8\uC5D0 \uD574\uB2F9\uD558\uB294 \uB300\uC0C1\uB9CC \uBC88\uB4E4\uB9C1 \uD569\uB2C8\uB2E4"
77
+ dev = import_clipanion.Option.Boolean("--dev", {
78
+ description: "Build in development mode"
121
79
  });
122
- disableCache = import_clipanion.Option.Boolean("--disable-cache", {
123
- description: "\uCE90\uC2DC\uB97C \uBE44\uD65C\uC131\uD654 \uD569\uB2C8\uB2E4"
80
+ metafile = import_clipanion.Option.Boolean("--metafile", {
81
+ description: "Generate metafile"
82
+ });
83
+ cache = import_clipanion.Option.Boolean("--cache", {
84
+ description: "Enable cache"
124
85
  });
125
86
  async execute() {
126
87
  try {
127
- const { tag, disableCache = false } = this;
88
+ const { cache = true, metafile = false, dev = false } = this;
128
89
  const config = await loadConfig();
129
- await build(config, {
130
- tag,
131
- cache: !disableCache
132
- });
90
+ const options = ["android", "ios"].map((platform) => ({
91
+ dev,
92
+ cache,
93
+ metafile,
94
+ platform,
95
+ outfile: `bundle.${platform}.js`
96
+ }));
97
+ await import_mpack.BuildUtils.buildAll(options, { config, plugins: [import_plugins.statusPlugin] });
133
98
  return 0;
134
99
  } catch (error) {
135
100
  console.error(error);
@@ -222,56 +187,16 @@ var HermesCommand = class extends import_clipanion2.Command {
222
187
  };
223
188
 
224
189
  // src/commands/DevCommand/DevCommand.ts
225
- var import_assert2 = __toESM(require("assert"), 1);
226
- var mpack = __toESM(require("@granite-js/mpack"), 1);
227
- var import_plugin_core2 = require("@granite-js/plugin-core");
190
+ var import_mpack2 = require("@granite-js/mpack");
228
191
  var import_clipanion3 = require("clipanion");
229
192
  var import_debug = __toESM(require("debug"), 1);
230
-
231
- // src/config/mergeTransformFromPlugins.ts
232
- async function mergeTransformFromPlugins(plugins) {
233
- const mergedTransformFunctions = plugins.reduce(
234
- (acc, plugin) => {
235
- if (plugin.transformSync) {
236
- acc.transformSync.push(plugin.transformSync);
237
- }
238
- if (plugin.transformAsync) {
239
- acc.transformAsync.push(plugin.transformAsync);
240
- }
241
- return acc;
242
- },
243
- {
244
- transformSync: [],
245
- transformAsync: []
246
- }
247
- );
248
- if (mergedTransformFunctions.transformSync.length === 0 && mergedTransformFunctions.transformAsync.length > 0) {
249
- console.warn(
250
- `Metro is only supported 'transformSync', but ${mergedTransformFunctions.transformAsync.length} 'transformAsync' are detected and it will be ignored.`
251
- );
252
- }
253
- return {
254
- transformSync: (id, code) => {
255
- return mergedTransformFunctions.transformSync.reduce((acc, transform) => transform?.(id, acc) ?? acc, code);
256
- },
257
- transformAsync: async (id, code) => {
258
- return mergedTransformFunctions.transformAsync.reduce(async (acc, transform) => {
259
- return await transform?.(id, await acc) ?? acc;
260
- }, Promise.resolve(code));
261
- }
262
- };
263
- }
264
-
265
- // src/commands/DevCommand/DevCommand.ts
266
193
  var debug = (0, import_debug.default)("cli");
267
- var DEFAULT_HOST = "0.0.0.0";
268
- var DEFAULT_LOCALHOST_PORT = 8081;
269
194
  var DevCommand = class extends import_clipanion3.Command {
270
195
  static paths = [[`dev`]];
271
196
  static usage = import_clipanion3.Command.Usage({
272
197
  category: "Development",
273
- description: "Granite \uAC1C\uBC1C \uC11C\uBC84\uB97C \uC2E4\uD589\uD569\uB2C8\uB2E4",
274
- examples: [["\uAC1C\uBC1C \uC11C\uBC84 \uC2E4\uD589\uD558\uAE30", "granite dev"]]
198
+ description: "Run Granite development server",
199
+ examples: [["Run Granite development server", "granite dev"]]
275
200
  });
276
201
  host = import_clipanion3.Option.String("--host");
277
202
  port = import_clipanion3.Option.String("--port");
@@ -286,62 +211,18 @@ var DevCommand = class extends import_clipanion3.Command {
286
211
  host: this.host,
287
212
  port: this.port ? parseInt(this.port, 10) : void 0
288
213
  };
289
- (0, import_assert2.default)(config?.appName, "\uC571 \uC774\uB984\uC744 \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4");
290
214
  debug("StartCommand", {
291
215
  ...serverOptions,
292
216
  disableEmbeddedReactDevTools: this.disableEmbeddedReactDevTools,
293
217
  experimentalMode: this.experimentalMode
294
218
  });
295
219
  if (this.experimentalMode) {
296
- const mpackConfig = config.mpack.devServer.config;
297
- const mpackDevServerConfig = mpackConfig?.devServer;
298
- (0, import_assert2.default)(mpackDevServerConfig, "mpack dev server \uC124\uC815\uC744 \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4");
299
- await mpack.experimental_runServer({
300
- appName: config.appName,
301
- scheme: config.scheme,
302
- devServerConfig: mpackDevServerConfig,
303
- host: serverOptions.host,
304
- port: serverOptions.port
305
- });
220
+ await (0, import_mpack2.EXPERIMENTAL__server)({ config, ...serverOptions });
306
221
  } else {
307
- const resolvedPlugins = await (0, import_plugin_core2.resolvePlugins)(config.plugins);
308
- const mergedTransform = await mergeTransformFromPlugins(resolvedPlugins.plugins);
309
- const additionalMetroConfig = {
310
- ...config.metro,
311
- transformSync: mergedTransform?.transformSync
312
- };
313
- for (const preHandler of resolvedPlugins.devServer.preHandlers) {
314
- debug("preHandler", preHandler);
315
- await preHandler?.({
316
- host: serverOptions.host || DEFAULT_HOST,
317
- port: serverOptions.port || DEFAULT_LOCALHOST_PORT,
318
- appName: config.appName,
319
- outdir: config.outdir,
320
- cwd: config.cwd,
321
- entryFile: config.entryFile
322
- });
323
- }
324
- await mpack.runServer({
325
- cwd: config.cwd,
326
- host: serverOptions.host,
327
- port: serverOptions.port,
328
- middlewares: config.mpack.devServer.middlewares,
329
- config: config.mpack.devServer.config,
330
- onServerReady: async () => {
331
- for (const postHandler of resolvedPlugins.devServer.postHandlers) {
332
- debug("postHandler", postHandler);
333
- await postHandler?.({
334
- host: serverOptions.host || DEFAULT_HOST,
335
- port: serverOptions.port || DEFAULT_LOCALHOST_PORT,
336
- appName: config.appName,
337
- outdir: config.outdir,
338
- cwd: config.cwd,
339
- entryFile: config.entryFile
340
- });
341
- }
342
- },
222
+ await (0, import_mpack2.runServer)({
223
+ config,
343
224
  enableEmbeddedReactDevTools: !this.disableEmbeddedReactDevTools,
344
- additionalConfig: additionalMetroConfig
225
+ ...serverOptions
345
226
  });
346
227
  }
347
228
  } catch (error) {
@@ -355,157 +236,78 @@ var DevCommand = class extends import_clipanion3.Command {
355
236
  };
356
237
 
357
238
  // src/config/defineConfig.ts
239
+ var import_path4 = __toESM(require("path"), 1);
240
+ var import_plugin_core = require("@granite-js/plugin-core");
241
+ var import_utils3 = require("@granite-js/utils");
242
+ var import_es_toolkit2 = require("es-toolkit");
243
+
244
+ // src/config/graniteGlobals.ts
245
+ var import_fs = __toESM(require("fs"), 1);
358
246
  var import_path3 = __toESM(require("path"), 1);
359
- var import_mpack2 = require("@granite-js/mpack");
360
- var import_plugin_core4 = require("@granite-js/plugin-core");
361
247
  var import_utils2 = require("@granite-js/utils");
362
- var import_es_toolkit3 = require("es-toolkit");
363
- var import_zod = require("zod");
364
-
365
- // src/config/mergeConfigFromPlugins.ts
366
- var import_plugin_core3 = require("@granite-js/plugin-core");
367
- var import_es_toolkit2 = require("es-toolkit");
368
- function concatArray(objValue, srcValue) {
369
- if (Array.isArray(objValue) && Array.isArray(srcValue)) {
370
- return objValue.concat(srcValue);
371
- }
372
- return;
373
- }
374
- function mergeMetroConfig(objValue, srcValue, key) {
375
- if (key === "getPolyfills" && typeof objValue === "function" && typeof srcValue === "function") {
376
- return () => [...objValue(), ...srcValue()];
377
- }
378
- return void 0;
379
- }
380
- function mergeEsbuildConfig(objValue, srcValue, key) {
381
- if (key === "banner" && typeof objValue === "object" && typeof srcValue === "object") {
382
- return Object.entries(srcValue ?? {}).reduce(
383
- (acc, [key2, value]) => ({
384
- ...acc,
385
- [key2]: [acc[key2], value].join("\n")
386
- }),
387
- objValue ?? {}
388
- );
389
- }
390
- if (key === "footer" && typeof objValue === "object" && typeof srcValue === "object") {
391
- return Object.entries(srcValue ?? {}).reduce(
392
- (acc, [key2, value]) => ({
393
- ...acc,
394
- [key2]: [acc[key2], value].join("\n")
395
- }),
396
- objValue ?? {}
397
- );
398
- }
399
- return void 0;
400
- }
401
- function mergeResolverConfig(objValue, srcValue, key) {
402
- if (key === "alias" && Array.isArray(objValue) && Array.isArray(srcValue)) {
403
- return [...objValue, ...srcValue];
404
- }
405
- if (key === "protocols" && typeof objValue === "object" && typeof srcValue === "object") {
406
- return { ...objValue, ...srcValue };
407
- }
408
- return void 0;
409
- }
410
- function combineComparers(...fns) {
411
- return (targetValue, sourceValue, key, target, source) => {
412
- for (const fn of fns) {
413
- const result = fn(targetValue, sourceValue, key, target, source);
414
- if (result !== void 0) {
415
- return result;
248
+ function prepareGraniteGlobalsScript(config) {
249
+ const filePath = writeGraniteGlobalsScript(config);
250
+ return {
251
+ esbuild: {
252
+ prelude: [filePath]
253
+ },
254
+ metro: {
255
+ serializer: {
256
+ getPolyfills: () => [filePath]
416
257
  }
417
258
  }
418
- return void 0;
419
259
  };
420
260
  }
421
- async function mergeConfigFromPlugins(plugins) {
422
- const pluginsResolved = await (0, import_plugin_core3.flattenPlugins)(plugins);
423
- return pluginsResolved.reduce(
424
- (acc, plugin) => (0, import_es_toolkit2.mergeWith)(
425
- acc,
426
- plugin.config ?? {},
427
- combineComparers(concatArray, mergeMetroConfig, mergeEsbuildConfig, mergeResolverConfig)
428
- ),
429
- {}
430
- );
261
+ function writeGraniteGlobalsScript(config) {
262
+ const script = getGraniteGlobalScript(config);
263
+ const filePath = import_path3.default.join((0, import_utils2.getLocalTempDirectoryPath)(config.rootDir), "granite-globals.js");
264
+ (0, import_utils2.prepareLocalDirectory)(config.rootDir);
265
+ import_fs.default.writeFileSync(filePath, script, "utf-8");
266
+ return filePath;
267
+ }
268
+ function getGraniteGlobalScript({ appName, scheme }) {
269
+ return [
270
+ "global.__granite = global.__granite || {};",
271
+ `global.__granite.app = { name: ${JSON.stringify(appName)}, scheme: ${JSON.stringify(scheme)} };`
272
+ ].join("\n");
431
273
  }
432
274
 
433
275
  // src/config/defineConfig.ts
434
- var graniteConfigSchema = import_zod.z.object({
435
- appName: import_zod.z.string(),
436
- scheme: import_zod.z.string(),
437
- plugins: import_zod.z.custom(),
438
- outdir: import_zod.z.string().default("dist"),
439
- entryFile: import_zod.z.string().default("./src/_app.tsx"),
440
- cwd: import_zod.z.string().default(process.cwd()),
441
- resolver: import_mpack2.resolverConfigSchema.optional(),
442
- mpack: import_plugin_core4.mpackConfigScheme.optional(),
443
- babel: import_mpack2.babelConfigSchema.optional(),
444
- esbuild: import_mpack2.esbuildConfigSchema.optional(),
445
- metro: import_zod.z.custom().optional()
446
- });
447
276
  var defineConfig = async (config) => {
448
- const parsedConfig = graniteConfigSchema.parse(config);
449
- const appName = parsedConfig.appName;
450
- const scheme = parsedConfig.scheme;
451
- const outdir = import_path3.default.join((0, import_utils2.getPackageRoot)(), parsedConfig.outdir);
452
- const entryFile = parsedConfig.entryFile;
453
- const plugins = await (0, import_plugin_core4.flattenPlugins)(parsedConfig.plugins);
454
- const mergedConfig = await mergeConfigFromPlugins(plugins);
455
- const mergedTransform = await mergeTransformFromPlugins(plugins);
456
- const resolver = mergedConfig?.resolver ? (0, import_es_toolkit3.merge)(mergedConfig.resolver, parsedConfig?.resolver ?? {}) : void 0;
457
- const esbuild = mergedConfig?.esbuild ? (0, import_es_toolkit3.merge)(mergedConfig.esbuild, parsedConfig?.esbuild ?? {}) : void 0;
458
- const metro = mergedConfig?.metro ? (0, import_es_toolkit3.merge)(mergedConfig.metro, parsedConfig?.metro ?? {}) : void 0;
459
- const babel = mergedConfig?.babel ? (0, import_es_toolkit3.merge)(mergedConfig.babel, parsedConfig?.babel ?? {}) : void 0;
460
- const mpackDevServer = mergedConfig?.mpack?.devServer ? (0, import_es_toolkit3.merge)(mergedConfig?.mpack?.devServer ?? {}, parsedConfig?.mpack?.devServer ?? {}) : void 0;
461
- const createTask = (platform) => ({
462
- tag: `${appName}-${platform}`,
463
- build: {
464
- resolver,
465
- esbuild,
466
- babel,
467
- platform,
468
- entry: entryFile,
469
- outfile: import_path3.default.join(outdir, `bundle.${platform}.js`),
470
- transformSync: mergedTransform?.transformSync,
471
- transformAsync: mergedTransform?.transformAsync
472
- }
473
- });
277
+ const parsed = import_plugin_core.pluginConfigSchema.parse(config);
278
+ const cwd = parsed.cwd ?? (0, import_utils3.getPackageRoot)();
279
+ const appName = parsed.appName;
280
+ const scheme = parsed.scheme;
281
+ const entryFile = import_path4.default.resolve(cwd, parsed.entryFile);
282
+ const outdir = import_path4.default.join(cwd, parsed.outdir);
283
+ const parsedBuildConfig = parsed.build;
284
+ const parsedDevServerConfig = parsed.devServer;
285
+ const parsedMetroConfig = parsed.metro;
286
+ const parsedConfig = {
287
+ ...parsedBuildConfig,
288
+ devServer: parsedDevServerConfig,
289
+ metro: parsedMetroConfig
290
+ };
291
+ const { configs, pluginHooks } = await (0, import_plugin_core.resolvePlugins)(parsed.plugins);
292
+ const globalsScriptConfig = prepareGraniteGlobalsScript({ rootDir: cwd, appName, scheme });
293
+ const mergedConfig = (0, import_plugin_core.mergeConfig)(parsedConfig, ...[globalsScriptConfig, ...configs].filter(import_es_toolkit2.isNotNil));
294
+ const { metro, devServer, ...buildConfig } = mergedConfig ?? {};
474
295
  return {
475
- ...parsedConfig,
296
+ cwd,
297
+ entryFile,
298
+ appName,
299
+ scheme,
476
300
  outdir,
477
- mpack: {
478
- devServer: {
479
- middlewares: mpackDevServer?.middlewares ?? [],
480
- config: {
481
- appName,
482
- scheme,
483
- devServer: {
484
- build: {
485
- entry: entryFile,
486
- resolver,
487
- esbuild,
488
- babel,
489
- transformSync: mergedTransform?.transformSync,
490
- transformAsync: mergedTransform?.transformAsync
491
- }
492
- },
493
- tasks: []
494
- }
495
- },
496
- build: {
497
- config: {
498
- appName,
499
- scheme,
500
- concurrency: 2,
501
- tasks: [createTask("ios"), createTask("android")]
502
- }
503
- }
301
+ devServer,
302
+ pluginHooks,
303
+ build: {
304
+ ...buildConfig,
305
+ entry: entryFile
504
306
  },
505
307
  metro: {
506
308
  ...metro,
507
- babelConfig: babel,
508
- transformSync: mergedTransform?.transformSync
309
+ babelConfig: mergedConfig?.babel,
310
+ transformSync: mergedConfig?.transformer?.transformSync
509
311
  }
510
312
  };
511
313
  };
package/dist/index.d.cts CHANGED
@@ -1,131 +1,5 @@
1
- import * as esbuild from 'esbuild';
2
- import * as _granite_js_mpack from '@granite-js/mpack';
3
- import { AdditionalMetroConfig, Config } from '@granite-js/mpack';
4
- import { PluginInput } from '@granite-js/plugin-core';
5
- import { HandleFunction } from 'connect';
6
- import { z } from 'zod';
1
+ import { GraniteConfig, CompleteGraniteConfig } from '@granite-js/plugin-core';
7
2
 
8
- declare const graniteConfigSchema: z.ZodObject<{
9
- appName: z.ZodString;
10
- scheme: z.ZodString;
11
- plugins: z.ZodType<PluginInput, z.ZodTypeDef, PluginInput>;
12
- outdir: z.ZodDefault<z.ZodString>;
13
- entryFile: z.ZodDefault<z.ZodString>;
14
- cwd: z.ZodDefault<z.ZodString>;
15
- resolver: z.ZodOptional<z.ZodObject<{
16
- alias: z.ZodOptional<z.ZodArray<z.ZodType<_granite_js_mpack.AliasConfig, z.ZodTypeDef, _granite_js_mpack.AliasConfig>, "many">>;
17
- protocols: z.ZodOptional<z.ZodType<_granite_js_mpack.ProtocolConfig, z.ZodTypeDef, _granite_js_mpack.ProtocolConfig>>;
18
- }, "strip", z.ZodTypeAny, {
19
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
20
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
21
- }, {
22
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
23
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
24
- }>>;
25
- mpack: z.ZodOptional<z.ZodOptional<z.ZodObject<{
26
- devServer: z.ZodDefault<z.ZodOptional<z.ZodObject<{
27
- middlewares: z.ZodDefault<z.ZodType<HandleFunction[], z.ZodTypeDef, HandleFunction[]>>;
28
- }, "strip", z.ZodTypeAny, {
29
- middlewares: HandleFunction[];
30
- }, {
31
- middlewares?: HandleFunction[] | undefined;
32
- }>>>;
33
- }, "strip", z.ZodTypeAny, {
34
- devServer: {
35
- middlewares: HandleFunction[];
36
- };
37
- }, {
38
- devServer?: {
39
- middlewares?: HandleFunction[] | undefined;
40
- } | undefined;
41
- }>>>;
42
- babel: z.ZodOptional<z.ZodObject<{
43
- configFile: z.ZodOptional<z.ZodString>;
44
- presets: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
45
- plugins: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
46
- conditions: z.ZodOptional<z.ZodArray<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodBoolean>, "many">>;
47
- }, "strip", z.ZodTypeAny, {
48
- plugins?: any[] | undefined;
49
- configFile?: string | undefined;
50
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
51
- presets?: any[] | undefined;
52
- }, {
53
- plugins?: any[] | undefined;
54
- configFile?: string | undefined;
55
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
56
- presets?: any[] | undefined;
57
- }>>;
58
- esbuild: z.ZodOptional<z.ZodType<esbuild.BuildOptions & {
59
- prelude?: string[];
60
- }, z.ZodTypeDef, esbuild.BuildOptions & {
61
- prelude?: string[];
62
- }>>;
63
- metro: z.ZodOptional<z.ZodType<Partial<AdditionalMetroConfig>, z.ZodTypeDef, Partial<AdditionalMetroConfig>>>;
64
- }, "strip", z.ZodTypeAny, {
65
- appName: string;
66
- scheme: string;
67
- plugins: PluginInput;
68
- outdir: string;
69
- entryFile: string;
70
- cwd: string;
71
- mpack?: {
72
- devServer: {
73
- middlewares: HandleFunction[];
74
- };
75
- } | undefined;
76
- resolver?: {
77
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
78
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
79
- } | undefined;
80
- babel?: {
81
- plugins?: any[] | undefined;
82
- configFile?: string | undefined;
83
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
84
- presets?: any[] | undefined;
85
- } | undefined;
86
- esbuild?: (esbuild.BuildOptions & {
87
- prelude?: string[];
88
- }) | undefined;
89
- metro?: Partial<AdditionalMetroConfig> | undefined;
90
- }, {
91
- appName: string;
92
- scheme: string;
93
- plugins: PluginInput;
94
- mpack?: {
95
- devServer?: {
96
- middlewares?: HandleFunction[] | undefined;
97
- } | undefined;
98
- } | undefined;
99
- outdir?: string | undefined;
100
- entryFile?: string | undefined;
101
- cwd?: string | undefined;
102
- resolver?: {
103
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
104
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
105
- } | undefined;
106
- babel?: {
107
- plugins?: any[] | undefined;
108
- configFile?: string | undefined;
109
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
110
- presets?: any[] | undefined;
111
- } | undefined;
112
- esbuild?: (esbuild.BuildOptions & {
113
- prelude?: string[];
114
- }) | undefined;
115
- metro?: Partial<AdditionalMetroConfig> | undefined;
116
- }>;
117
- type GraniteConfigInput = z.input<typeof graniteConfigSchema>;
118
- interface GraniteConfigResponse extends z.infer<typeof graniteConfigSchema> {
119
- mpack: {
120
- devServer: {
121
- middlewares: HandleFunction[];
122
- config: Config;
123
- };
124
- build: {
125
- config: Config;
126
- };
127
- };
128
- }
129
3
  /**
130
4
  * @public
131
5
  * @category Configuration
@@ -141,16 +15,15 @@ interface GraniteConfigResponse extends z.infer<typeof graniteConfigSchema> {
141
15
  * - Additional functionality through Granite plugins
142
16
  *
143
17
  * @param config - Configuration options
18
+ * @param config.cwd - Working directory for build process (defaults to process.cwd())
144
19
  * @param config.appName - Your app's unique identifier
145
20
  * @param config.scheme - URL scheme for launching your app (e.g. 'granite')
146
- * @param config.plugins - Granite plugins to enhance functionality
147
21
  * @param config.outdir - Where to output build files (defaults to 'dist')
148
22
  * @param config.entryFile - Your app's entry point (defaults to './src/_app.tsx')
149
- * @param config.cwd - Working directory for build process (defaults to process.cwd())
150
- * @param config.mpack - Fine-tune mpack bundler behavior
151
- * @param config.babel - Customize Babel transpilation
152
- * @param config.esbuild - Adjust ESBuild bundling
23
+ * @param config.build - Customize build settings
153
24
  * @param config.metro - Configure Metro bundler settings
25
+ * @param config.devServer - Configure Mpack dev server settings
26
+ * @param config.plugins - Granite plugins to enhance functionality
154
27
  * @returns The processed configuration
155
28
  *
156
29
  * @example
@@ -175,10 +48,10 @@ interface GraniteConfigResponse extends z.infer<typeof graniteConfigSchema> {
175
48
  * });
176
49
  * ```
177
50
  */
178
- declare const defineConfig: (config: GraniteConfigInput) => Promise<GraniteConfigResponse>;
51
+ declare const defineConfig: (config: GraniteConfig) => Promise<CompleteGraniteConfig>;
179
52
 
180
- declare const loadConfig: () => Promise<GraniteConfigResponse>;
53
+ declare const loadConfig: () => Promise<CompleteGraniteConfig>;
181
54
 
182
55
  declare function initialize(): Promise<void>;
183
56
 
184
- export { type GraniteConfigInput, type GraniteConfigResponse, defineConfig, initialize, loadConfig };
57
+ export { defineConfig, initialize, loadConfig };
package/dist/index.d.ts CHANGED
@@ -1,131 +1,5 @@
1
- import * as esbuild from 'esbuild';
2
- import * as _granite_js_mpack from '@granite-js/mpack';
3
- import { AdditionalMetroConfig, Config } from '@granite-js/mpack';
4
- import { PluginInput } from '@granite-js/plugin-core';
5
- import { HandleFunction } from 'connect';
6
- import { z } from 'zod';
1
+ import { GraniteConfig, CompleteGraniteConfig } from '@granite-js/plugin-core';
7
2
 
8
- declare const graniteConfigSchema: z.ZodObject<{
9
- appName: z.ZodString;
10
- scheme: z.ZodString;
11
- plugins: z.ZodType<PluginInput, z.ZodTypeDef, PluginInput>;
12
- outdir: z.ZodDefault<z.ZodString>;
13
- entryFile: z.ZodDefault<z.ZodString>;
14
- cwd: z.ZodDefault<z.ZodString>;
15
- resolver: z.ZodOptional<z.ZodObject<{
16
- alias: z.ZodOptional<z.ZodArray<z.ZodType<_granite_js_mpack.AliasConfig, z.ZodTypeDef, _granite_js_mpack.AliasConfig>, "many">>;
17
- protocols: z.ZodOptional<z.ZodType<_granite_js_mpack.ProtocolConfig, z.ZodTypeDef, _granite_js_mpack.ProtocolConfig>>;
18
- }, "strip", z.ZodTypeAny, {
19
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
20
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
21
- }, {
22
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
23
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
24
- }>>;
25
- mpack: z.ZodOptional<z.ZodOptional<z.ZodObject<{
26
- devServer: z.ZodDefault<z.ZodOptional<z.ZodObject<{
27
- middlewares: z.ZodDefault<z.ZodType<HandleFunction[], z.ZodTypeDef, HandleFunction[]>>;
28
- }, "strip", z.ZodTypeAny, {
29
- middlewares: HandleFunction[];
30
- }, {
31
- middlewares?: HandleFunction[] | undefined;
32
- }>>>;
33
- }, "strip", z.ZodTypeAny, {
34
- devServer: {
35
- middlewares: HandleFunction[];
36
- };
37
- }, {
38
- devServer?: {
39
- middlewares?: HandleFunction[] | undefined;
40
- } | undefined;
41
- }>>>;
42
- babel: z.ZodOptional<z.ZodObject<{
43
- configFile: z.ZodOptional<z.ZodString>;
44
- presets: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
45
- plugins: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
46
- conditions: z.ZodOptional<z.ZodArray<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodBoolean>, "many">>;
47
- }, "strip", z.ZodTypeAny, {
48
- plugins?: any[] | undefined;
49
- configFile?: string | undefined;
50
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
51
- presets?: any[] | undefined;
52
- }, {
53
- plugins?: any[] | undefined;
54
- configFile?: string | undefined;
55
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
56
- presets?: any[] | undefined;
57
- }>>;
58
- esbuild: z.ZodOptional<z.ZodType<esbuild.BuildOptions & {
59
- prelude?: string[];
60
- }, z.ZodTypeDef, esbuild.BuildOptions & {
61
- prelude?: string[];
62
- }>>;
63
- metro: z.ZodOptional<z.ZodType<Partial<AdditionalMetroConfig>, z.ZodTypeDef, Partial<AdditionalMetroConfig>>>;
64
- }, "strip", z.ZodTypeAny, {
65
- appName: string;
66
- scheme: string;
67
- plugins: PluginInput;
68
- outdir: string;
69
- entryFile: string;
70
- cwd: string;
71
- mpack?: {
72
- devServer: {
73
- middlewares: HandleFunction[];
74
- };
75
- } | undefined;
76
- resolver?: {
77
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
78
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
79
- } | undefined;
80
- babel?: {
81
- plugins?: any[] | undefined;
82
- configFile?: string | undefined;
83
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
84
- presets?: any[] | undefined;
85
- } | undefined;
86
- esbuild?: (esbuild.BuildOptions & {
87
- prelude?: string[];
88
- }) | undefined;
89
- metro?: Partial<AdditionalMetroConfig> | undefined;
90
- }, {
91
- appName: string;
92
- scheme: string;
93
- plugins: PluginInput;
94
- mpack?: {
95
- devServer?: {
96
- middlewares?: HandleFunction[] | undefined;
97
- } | undefined;
98
- } | undefined;
99
- outdir?: string | undefined;
100
- entryFile?: string | undefined;
101
- cwd?: string | undefined;
102
- resolver?: {
103
- alias?: _granite_js_mpack.AliasConfig[] | undefined;
104
- protocols?: _granite_js_mpack.ProtocolConfig | undefined;
105
- } | undefined;
106
- babel?: {
107
- plugins?: any[] | undefined;
108
- configFile?: string | undefined;
109
- conditions?: ((...args: unknown[]) => boolean)[] | undefined;
110
- presets?: any[] | undefined;
111
- } | undefined;
112
- esbuild?: (esbuild.BuildOptions & {
113
- prelude?: string[];
114
- }) | undefined;
115
- metro?: Partial<AdditionalMetroConfig> | undefined;
116
- }>;
117
- type GraniteConfigInput = z.input<typeof graniteConfigSchema>;
118
- interface GraniteConfigResponse extends z.infer<typeof graniteConfigSchema> {
119
- mpack: {
120
- devServer: {
121
- middlewares: HandleFunction[];
122
- config: Config;
123
- };
124
- build: {
125
- config: Config;
126
- };
127
- };
128
- }
129
3
  /**
130
4
  * @public
131
5
  * @category Configuration
@@ -141,16 +15,15 @@ interface GraniteConfigResponse extends z.infer<typeof graniteConfigSchema> {
141
15
  * - Additional functionality through Granite plugins
142
16
  *
143
17
  * @param config - Configuration options
18
+ * @param config.cwd - Working directory for build process (defaults to process.cwd())
144
19
  * @param config.appName - Your app's unique identifier
145
20
  * @param config.scheme - URL scheme for launching your app (e.g. 'granite')
146
- * @param config.plugins - Granite plugins to enhance functionality
147
21
  * @param config.outdir - Where to output build files (defaults to 'dist')
148
22
  * @param config.entryFile - Your app's entry point (defaults to './src/_app.tsx')
149
- * @param config.cwd - Working directory for build process (defaults to process.cwd())
150
- * @param config.mpack - Fine-tune mpack bundler behavior
151
- * @param config.babel - Customize Babel transpilation
152
- * @param config.esbuild - Adjust ESBuild bundling
23
+ * @param config.build - Customize build settings
153
24
  * @param config.metro - Configure Metro bundler settings
25
+ * @param config.devServer - Configure Mpack dev server settings
26
+ * @param config.plugins - Granite plugins to enhance functionality
154
27
  * @returns The processed configuration
155
28
  *
156
29
  * @example
@@ -175,10 +48,10 @@ interface GraniteConfigResponse extends z.infer<typeof graniteConfigSchema> {
175
48
  * });
176
49
  * ```
177
50
  */
178
- declare const defineConfig: (config: GraniteConfigInput) => Promise<GraniteConfigResponse>;
51
+ declare const defineConfig: (config: GraniteConfig) => Promise<CompleteGraniteConfig>;
179
52
 
180
- declare const loadConfig: () => Promise<GraniteConfigResponse>;
53
+ declare const loadConfig: () => Promise<CompleteGraniteConfig>;
181
54
 
182
55
  declare function initialize(): Promise<void>;
183
56
 
184
- export { type GraniteConfigInput, type GraniteConfigResponse, defineConfig, initialize, loadConfig };
57
+ export { defineConfig, initialize, loadConfig };
package/dist/index.js CHANGED
@@ -11,48 +11,9 @@ import { Builtins, Cli } from "clipanion";
11
11
  import { cosmiconfig as cosmiconfig2 } from "cosmiconfig";
12
12
 
13
13
  // src/commands/BuildCommand/BuildCommand.ts
14
- import { Command, Option } from "clipanion";
15
-
16
- // src/build.ts
17
- import { runBundle } from "@granite-js/mpack";
14
+ import { BuildUtils } from "@granite-js/mpack";
18
15
  import { statusPlugin } from "@granite-js/mpack/plugins";
19
- import { resolvePlugins } from "@granite-js/plugin-core";
20
- async function build(config, {
21
- tag,
22
- cache
23
- }) {
24
- const resolvedPlugins = await resolvePlugins(config.plugins);
25
- const pluginContext = {
26
- meta: /* @__PURE__ */ Object.create(null)
27
- };
28
- for (const preHandler of resolvedPlugins.build.preHandlers) {
29
- await preHandler?.call(pluginContext, {
30
- cwd: config.cwd,
31
- entryFile: config.entryFile,
32
- appName: config.appName,
33
- outdir: config.outdir,
34
- buildResults: []
35
- });
36
- }
37
- const results = await runBundle({
38
- tag,
39
- rootDir: config.cwd,
40
- dev: false,
41
- cache,
42
- metafile: true,
43
- plugins: [statusPlugin],
44
- config: config.mpack.build.config
45
- });
46
- for (const postHandler of resolvedPlugins.build.postHandlers) {
47
- await postHandler?.call(pluginContext, {
48
- cwd: config.cwd,
49
- entryFile: config.entryFile,
50
- appName: config.appName,
51
- outdir: config.outdir,
52
- buildResults: results
53
- });
54
- }
55
- }
16
+ import { Command, Option } from "clipanion";
56
17
 
57
18
  // src/config/loadConfig.ts
58
19
  import { getPackageRoot } from "@granite-js/utils";
@@ -82,26 +43,30 @@ var BuildCommand = class extends Command {
82
43
  static paths = [[`build`]];
83
44
  static usage = Command.Usage({
84
45
  category: "Build",
85
- description: "Granite App \uBC88\uB4E4\uC744 \uC0DD\uC131\uD569\uB2C8\uB2E4",
86
- examples: [
87
- ["\uBE4C\uB4DC\uD558\uAE30", "granite build"],
88
- ["\uC9C0\uC815\uB41C \uB300\uC0C1\uB9CC \uBE4C\uB4DC\uD558\uAE30", "granite build --id service-ios"]
89
- ]
46
+ description: "Build Granite App",
47
+ examples: [["Build Granite App", "granite build"]]
48
+ });
49
+ dev = Option.Boolean("--dev", {
50
+ description: "Build in development mode"
90
51
  });
91
- tag = Option.String("--tag", {
92
- description: "\uAD6C\uC131 \uD30C\uC77C\uC5D0\uC11C \uC9C0\uC815\uD55C \uD0DC\uADF8\uC5D0 \uD574\uB2F9\uD558\uB294 \uB300\uC0C1\uB9CC \uBC88\uB4E4\uB9C1 \uD569\uB2C8\uB2E4"
52
+ metafile = Option.Boolean("--metafile", {
53
+ description: "Generate metafile"
93
54
  });
94
- disableCache = Option.Boolean("--disable-cache", {
95
- description: "\uCE90\uC2DC\uB97C \uBE44\uD65C\uC131\uD654 \uD569\uB2C8\uB2E4"
55
+ cache = Option.Boolean("--cache", {
56
+ description: "Enable cache"
96
57
  });
97
58
  async execute() {
98
59
  try {
99
- const { tag, disableCache = false } = this;
60
+ const { cache = true, metafile = false, dev = false } = this;
100
61
  const config = await loadConfig();
101
- await build(config, {
102
- tag,
103
- cache: !disableCache
104
- });
62
+ const options = ["android", "ios"].map((platform) => ({
63
+ dev,
64
+ cache,
65
+ metafile,
66
+ platform,
67
+ outfile: `bundle.${platform}.js`
68
+ }));
69
+ await BuildUtils.buildAll(options, { config, plugins: [statusPlugin] });
105
70
  return 0;
106
71
  } catch (error) {
107
72
  console.error(error);
@@ -194,56 +159,16 @@ var HermesCommand = class extends Command2 {
194
159
  };
195
160
 
196
161
  // src/commands/DevCommand/DevCommand.ts
197
- import assert2 from "assert";
198
- import * as mpack from "@granite-js/mpack";
199
- import { resolvePlugins as resolvePlugins2 } from "@granite-js/plugin-core";
162
+ import { runServer, EXPERIMENTAL__server } from "@granite-js/mpack";
200
163
  import { Command as Command3, Option as Option3 } from "clipanion";
201
164
  import Debug from "debug";
202
-
203
- // src/config/mergeTransformFromPlugins.ts
204
- async function mergeTransformFromPlugins(plugins) {
205
- const mergedTransformFunctions = plugins.reduce(
206
- (acc, plugin) => {
207
- if (plugin.transformSync) {
208
- acc.transformSync.push(plugin.transformSync);
209
- }
210
- if (plugin.transformAsync) {
211
- acc.transformAsync.push(plugin.transformAsync);
212
- }
213
- return acc;
214
- },
215
- {
216
- transformSync: [],
217
- transformAsync: []
218
- }
219
- );
220
- if (mergedTransformFunctions.transformSync.length === 0 && mergedTransformFunctions.transformAsync.length > 0) {
221
- console.warn(
222
- `Metro is only supported 'transformSync', but ${mergedTransformFunctions.transformAsync.length} 'transformAsync' are detected and it will be ignored.`
223
- );
224
- }
225
- return {
226
- transformSync: (id, code) => {
227
- return mergedTransformFunctions.transformSync.reduce((acc, transform) => transform?.(id, acc) ?? acc, code);
228
- },
229
- transformAsync: async (id, code) => {
230
- return mergedTransformFunctions.transformAsync.reduce(async (acc, transform) => {
231
- return await transform?.(id, await acc) ?? acc;
232
- }, Promise.resolve(code));
233
- }
234
- };
235
- }
236
-
237
- // src/commands/DevCommand/DevCommand.ts
238
165
  var debug = Debug("cli");
239
- var DEFAULT_HOST = "0.0.0.0";
240
- var DEFAULT_LOCALHOST_PORT = 8081;
241
166
  var DevCommand = class extends Command3 {
242
167
  static paths = [[`dev`]];
243
168
  static usage = Command3.Usage({
244
169
  category: "Development",
245
- description: "Granite \uAC1C\uBC1C \uC11C\uBC84\uB97C \uC2E4\uD589\uD569\uB2C8\uB2E4",
246
- examples: [["\uAC1C\uBC1C \uC11C\uBC84 \uC2E4\uD589\uD558\uAE30", "granite dev"]]
170
+ description: "Run Granite development server",
171
+ examples: [["Run Granite development server", "granite dev"]]
247
172
  });
248
173
  host = Option3.String("--host");
249
174
  port = Option3.String("--port");
@@ -258,62 +183,18 @@ var DevCommand = class extends Command3 {
258
183
  host: this.host,
259
184
  port: this.port ? parseInt(this.port, 10) : void 0
260
185
  };
261
- assert2(config?.appName, "\uC571 \uC774\uB984\uC744 \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4");
262
186
  debug("StartCommand", {
263
187
  ...serverOptions,
264
188
  disableEmbeddedReactDevTools: this.disableEmbeddedReactDevTools,
265
189
  experimentalMode: this.experimentalMode
266
190
  });
267
191
  if (this.experimentalMode) {
268
- const mpackConfig = config.mpack.devServer.config;
269
- const mpackDevServerConfig = mpackConfig?.devServer;
270
- assert2(mpackDevServerConfig, "mpack dev server \uC124\uC815\uC744 \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4");
271
- await mpack.experimental_runServer({
272
- appName: config.appName,
273
- scheme: config.scheme,
274
- devServerConfig: mpackDevServerConfig,
275
- host: serverOptions.host,
276
- port: serverOptions.port
277
- });
192
+ await EXPERIMENTAL__server({ config, ...serverOptions });
278
193
  } else {
279
- const resolvedPlugins = await resolvePlugins2(config.plugins);
280
- const mergedTransform = await mergeTransformFromPlugins(resolvedPlugins.plugins);
281
- const additionalMetroConfig = {
282
- ...config.metro,
283
- transformSync: mergedTransform?.transformSync
284
- };
285
- for (const preHandler of resolvedPlugins.devServer.preHandlers) {
286
- debug("preHandler", preHandler);
287
- await preHandler?.({
288
- host: serverOptions.host || DEFAULT_HOST,
289
- port: serverOptions.port || DEFAULT_LOCALHOST_PORT,
290
- appName: config.appName,
291
- outdir: config.outdir,
292
- cwd: config.cwd,
293
- entryFile: config.entryFile
294
- });
295
- }
296
- await mpack.runServer({
297
- cwd: config.cwd,
298
- host: serverOptions.host,
299
- port: serverOptions.port,
300
- middlewares: config.mpack.devServer.middlewares,
301
- config: config.mpack.devServer.config,
302
- onServerReady: async () => {
303
- for (const postHandler of resolvedPlugins.devServer.postHandlers) {
304
- debug("postHandler", postHandler);
305
- await postHandler?.({
306
- host: serverOptions.host || DEFAULT_HOST,
307
- port: serverOptions.port || DEFAULT_LOCALHOST_PORT,
308
- appName: config.appName,
309
- outdir: config.outdir,
310
- cwd: config.cwd,
311
- entryFile: config.entryFile
312
- });
313
- }
314
- },
194
+ await runServer({
195
+ config,
315
196
  enableEmbeddedReactDevTools: !this.disableEmbeddedReactDevTools,
316
- additionalConfig: additionalMetroConfig
197
+ ...serverOptions
317
198
  });
318
199
  }
319
200
  } catch (error) {
@@ -327,161 +208,82 @@ var DevCommand = class extends Command3 {
327
208
  };
328
209
 
329
210
  // src/config/defineConfig.ts
330
- import path3 from "path";
211
+ import path4 from "path";
331
212
  import {
332
- babelConfigSchema,
333
- esbuildConfigSchema,
334
- resolverConfigSchema
335
- } from "@granite-js/mpack";
336
- import { flattenPlugins as flattenPlugins2, mpackConfigScheme } from "@granite-js/plugin-core";
213
+ resolvePlugins,
214
+ mergeConfig,
215
+ pluginConfigSchema
216
+ } from "@granite-js/plugin-core";
337
217
  import { getPackageRoot as getPackageRoot2 } from "@granite-js/utils";
338
- import { merge } from "es-toolkit";
339
- import { z } from "zod";
218
+ import { isNotNil as isNotNil2 } from "es-toolkit";
340
219
 
341
- // src/config/mergeConfigFromPlugins.ts
342
- import { flattenPlugins } from "@granite-js/plugin-core";
343
- import { mergeWith } from "es-toolkit";
344
- function concatArray(objValue, srcValue) {
345
- if (Array.isArray(objValue) && Array.isArray(srcValue)) {
346
- return objValue.concat(srcValue);
347
- }
348
- return;
349
- }
350
- function mergeMetroConfig(objValue, srcValue, key) {
351
- if (key === "getPolyfills" && typeof objValue === "function" && typeof srcValue === "function") {
352
- return () => [...objValue(), ...srcValue()];
353
- }
354
- return void 0;
355
- }
356
- function mergeEsbuildConfig(objValue, srcValue, key) {
357
- if (key === "banner" && typeof objValue === "object" && typeof srcValue === "object") {
358
- return Object.entries(srcValue ?? {}).reduce(
359
- (acc, [key2, value]) => ({
360
- ...acc,
361
- [key2]: [acc[key2], value].join("\n")
362
- }),
363
- objValue ?? {}
364
- );
365
- }
366
- if (key === "footer" && typeof objValue === "object" && typeof srcValue === "object") {
367
- return Object.entries(srcValue ?? {}).reduce(
368
- (acc, [key2, value]) => ({
369
- ...acc,
370
- [key2]: [acc[key2], value].join("\n")
371
- }),
372
- objValue ?? {}
373
- );
374
- }
375
- return void 0;
376
- }
377
- function mergeResolverConfig(objValue, srcValue, key) {
378
- if (key === "alias" && Array.isArray(objValue) && Array.isArray(srcValue)) {
379
- return [...objValue, ...srcValue];
380
- }
381
- if (key === "protocols" && typeof objValue === "object" && typeof srcValue === "object") {
382
- return { ...objValue, ...srcValue };
383
- }
384
- return void 0;
385
- }
386
- function combineComparers(...fns) {
387
- return (targetValue, sourceValue, key, target, source) => {
388
- for (const fn of fns) {
389
- const result = fn(targetValue, sourceValue, key, target, source);
390
- if (result !== void 0) {
391
- return result;
220
+ // src/config/graniteGlobals.ts
221
+ import fs from "fs";
222
+ import path3 from "path";
223
+ import { getLocalTempDirectoryPath, prepareLocalDirectory } from "@granite-js/utils";
224
+ function prepareGraniteGlobalsScript(config) {
225
+ const filePath = writeGraniteGlobalsScript(config);
226
+ return {
227
+ esbuild: {
228
+ prelude: [filePath]
229
+ },
230
+ metro: {
231
+ serializer: {
232
+ getPolyfills: () => [filePath]
392
233
  }
393
234
  }
394
- return void 0;
395
235
  };
396
236
  }
397
- async function mergeConfigFromPlugins(plugins) {
398
- const pluginsResolved = await flattenPlugins(plugins);
399
- return pluginsResolved.reduce(
400
- (acc, plugin) => mergeWith(
401
- acc,
402
- plugin.config ?? {},
403
- combineComparers(concatArray, mergeMetroConfig, mergeEsbuildConfig, mergeResolverConfig)
404
- ),
405
- {}
406
- );
237
+ function writeGraniteGlobalsScript(config) {
238
+ const script = getGraniteGlobalScript(config);
239
+ const filePath = path3.join(getLocalTempDirectoryPath(config.rootDir), "granite-globals.js");
240
+ prepareLocalDirectory(config.rootDir);
241
+ fs.writeFileSync(filePath, script, "utf-8");
242
+ return filePath;
243
+ }
244
+ function getGraniteGlobalScript({ appName, scheme }) {
245
+ return [
246
+ "global.__granite = global.__granite || {};",
247
+ `global.__granite.app = { name: ${JSON.stringify(appName)}, scheme: ${JSON.stringify(scheme)} };`
248
+ ].join("\n");
407
249
  }
408
250
 
409
251
  // src/config/defineConfig.ts
410
- var graniteConfigSchema = z.object({
411
- appName: z.string(),
412
- scheme: z.string(),
413
- plugins: z.custom(),
414
- outdir: z.string().default("dist"),
415
- entryFile: z.string().default("./src/_app.tsx"),
416
- cwd: z.string().default(process.cwd()),
417
- resolver: resolverConfigSchema.optional(),
418
- mpack: mpackConfigScheme.optional(),
419
- babel: babelConfigSchema.optional(),
420
- esbuild: esbuildConfigSchema.optional(),
421
- metro: z.custom().optional()
422
- });
423
252
  var defineConfig = async (config) => {
424
- const parsedConfig = graniteConfigSchema.parse(config);
425
- const appName = parsedConfig.appName;
426
- const scheme = parsedConfig.scheme;
427
- const outdir = path3.join(getPackageRoot2(), parsedConfig.outdir);
428
- const entryFile = parsedConfig.entryFile;
429
- const plugins = await flattenPlugins2(parsedConfig.plugins);
430
- const mergedConfig = await mergeConfigFromPlugins(plugins);
431
- const mergedTransform = await mergeTransformFromPlugins(plugins);
432
- const resolver = mergedConfig?.resolver ? merge(mergedConfig.resolver, parsedConfig?.resolver ?? {}) : void 0;
433
- const esbuild = mergedConfig?.esbuild ? merge(mergedConfig.esbuild, parsedConfig?.esbuild ?? {}) : void 0;
434
- const metro = mergedConfig?.metro ? merge(mergedConfig.metro, parsedConfig?.metro ?? {}) : void 0;
435
- const babel = mergedConfig?.babel ? merge(mergedConfig.babel, parsedConfig?.babel ?? {}) : void 0;
436
- const mpackDevServer = mergedConfig?.mpack?.devServer ? merge(mergedConfig?.mpack?.devServer ?? {}, parsedConfig?.mpack?.devServer ?? {}) : void 0;
437
- const createTask = (platform) => ({
438
- tag: `${appName}-${platform}`,
439
- build: {
440
- resolver,
441
- esbuild,
442
- babel,
443
- platform,
444
- entry: entryFile,
445
- outfile: path3.join(outdir, `bundle.${platform}.js`),
446
- transformSync: mergedTransform?.transformSync,
447
- transformAsync: mergedTransform?.transformAsync
448
- }
449
- });
253
+ const parsed = pluginConfigSchema.parse(config);
254
+ const cwd = parsed.cwd ?? getPackageRoot2();
255
+ const appName = parsed.appName;
256
+ const scheme = parsed.scheme;
257
+ const entryFile = path4.resolve(cwd, parsed.entryFile);
258
+ const outdir = path4.join(cwd, parsed.outdir);
259
+ const parsedBuildConfig = parsed.build;
260
+ const parsedDevServerConfig = parsed.devServer;
261
+ const parsedMetroConfig = parsed.metro;
262
+ const parsedConfig = {
263
+ ...parsedBuildConfig,
264
+ devServer: parsedDevServerConfig,
265
+ metro: parsedMetroConfig
266
+ };
267
+ const { configs, pluginHooks } = await resolvePlugins(parsed.plugins);
268
+ const globalsScriptConfig = prepareGraniteGlobalsScript({ rootDir: cwd, appName, scheme });
269
+ const mergedConfig = mergeConfig(parsedConfig, ...[globalsScriptConfig, ...configs].filter(isNotNil2));
270
+ const { metro, devServer, ...buildConfig } = mergedConfig ?? {};
450
271
  return {
451
- ...parsedConfig,
272
+ cwd,
273
+ entryFile,
274
+ appName,
275
+ scheme,
452
276
  outdir,
453
- mpack: {
454
- devServer: {
455
- middlewares: mpackDevServer?.middlewares ?? [],
456
- config: {
457
- appName,
458
- scheme,
459
- devServer: {
460
- build: {
461
- entry: entryFile,
462
- resolver,
463
- esbuild,
464
- babel,
465
- transformSync: mergedTransform?.transformSync,
466
- transformAsync: mergedTransform?.transformAsync
467
- }
468
- },
469
- tasks: []
470
- }
471
- },
472
- build: {
473
- config: {
474
- appName,
475
- scheme,
476
- concurrency: 2,
477
- tasks: [createTask("ios"), createTask("android")]
478
- }
479
- }
277
+ devServer,
278
+ pluginHooks,
279
+ build: {
280
+ ...buildConfig,
281
+ entry: entryFile
480
282
  },
481
283
  metro: {
482
284
  ...metro,
483
- babelConfig: babel,
484
- transformSync: mergedTransform?.transformSync
285
+ babelConfig: mergedConfig?.babel,
286
+ transformSync: mergedConfig?.transformer?.transformSync
485
287
  }
486
288
  };
487
289
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@granite-js/cli",
3
3
  "type": "module",
4
- "version": "0.1.7",
4
+ "version": "0.1.8",
5
5
  "description": "The Granite CLI",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -36,7 +36,7 @@
36
36
  "@types/debug": "^4",
37
37
  "@types/node": "^22.10.2",
38
38
  "@types/serve-static": "^1",
39
- "esbuild": "^0.25.4",
39
+ "esbuild": "0.25.8",
40
40
  "eslint": "^9.7.0",
41
41
  "ts-prune": "^0.10.3",
42
42
  "tsup": "^8.5.0",
@@ -44,9 +44,9 @@
44
44
  "vitest": "^3.0.7"
45
45
  },
46
46
  "dependencies": {
47
- "@granite-js/mpack": "0.1.7",
48
- "@granite-js/plugin-core": "0.1.7",
49
- "@granite-js/utils": "0.1.7",
47
+ "@granite-js/mpack": "0.1.8",
48
+ "@granite-js/plugin-core": "0.1.8",
49
+ "@granite-js/utils": "0.1.8",
50
50
  "@inquirer/prompts": "^7.2.3",
51
51
  "@shopify/semaphore": "^3.1.0",
52
52
  "chalk": "^4",