@granite-js/plugin-core 0.1.9 → 0.1.10
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 +7 -0
- package/dist/index.cjs +142 -0
- package/dist/index.d.cts +72 -1
- package/dist/index.d.ts +72 -1
- package/dist/index.js +142 -2
- package/package.json +4 -1
package/CHANGELOG.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -22,7 +22,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
|
|
23
23
|
//#endregion
|
|
24
24
|
const es_toolkit = __toESM(require("es-toolkit"));
|
|
25
|
+
const path = __toESM(require("path"));
|
|
26
|
+
const __granite_js_utils = __toESM(require("@granite-js/utils"));
|
|
27
|
+
const fs = __toESM(require("fs"));
|
|
25
28
|
const zod = __toESM(require("zod"));
|
|
29
|
+
const cosmiconfig = __toESM(require("cosmiconfig"));
|
|
30
|
+
const cosmiconfig_typescript_loader = __toESM(require("cosmiconfig-typescript-loader"));
|
|
26
31
|
|
|
27
32
|
//#region src/createContext.ts
|
|
28
33
|
function createContext() {
|
|
@@ -319,6 +324,26 @@ function createPluginContext() {
|
|
|
319
324
|
return context;
|
|
320
325
|
}
|
|
321
326
|
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region src/config/graniteGlobals.ts
|
|
329
|
+
function prepareGraniteGlobalsScript(config) {
|
|
330
|
+
const filePath = writeGraniteGlobalsScript(config);
|
|
331
|
+
return {
|
|
332
|
+
esbuild: { prelude: [filePath] },
|
|
333
|
+
metro: { serializer: { getPolyfills: () => [filePath] } }
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
function writeGraniteGlobalsScript(config) {
|
|
337
|
+
const script = getGraniteGlobalScript(config);
|
|
338
|
+
const filePath = path.default.join((0, __granite_js_utils.getLocalTempDirectoryPath)(config.rootDir), "granite-globals.js");
|
|
339
|
+
(0, __granite_js_utils.prepareLocalDirectory)(config.rootDir);
|
|
340
|
+
fs.default.writeFileSync(filePath, script, "utf-8");
|
|
341
|
+
return filePath;
|
|
342
|
+
}
|
|
343
|
+
function getGraniteGlobalScript({ appName, scheme }) {
|
|
344
|
+
return ["global.__granite = global.__granite || {};", `global.__granite.app = { name: ${JSON.stringify(appName)}, scheme: ${JSON.stringify(scheme)} };`].join("\n");
|
|
345
|
+
}
|
|
346
|
+
|
|
322
347
|
//#endregion
|
|
323
348
|
//#region src/schema/pluginConfig.ts
|
|
324
349
|
const pluginConfigSchema = zod.object({
|
|
@@ -333,11 +358,128 @@ const pluginConfigSchema = zod.object({
|
|
|
333
358
|
plugins: zod.custom()
|
|
334
359
|
});
|
|
335
360
|
|
|
361
|
+
//#endregion
|
|
362
|
+
//#region src/config/defineConfig.ts
|
|
363
|
+
/**
|
|
364
|
+
* @public
|
|
365
|
+
* @category Configuration
|
|
366
|
+
* @name defineConfig
|
|
367
|
+
* @description
|
|
368
|
+
* Configures your Granite application by defining key settings in `granite.config.ts`.
|
|
369
|
+
*
|
|
370
|
+
* The configuration lets you specify:
|
|
371
|
+
* - How users will access your app through a URL scheme (e.g. `granite://`)
|
|
372
|
+
* - Your app's unique name that appears in the URL (e.g. `granite://my-service`)
|
|
373
|
+
* - Build settings for bundlers like ESBuild and Metro
|
|
374
|
+
* - Code transformation settings through Babel
|
|
375
|
+
* - Additional functionality through Granite plugins
|
|
376
|
+
*
|
|
377
|
+
* @param config - Configuration options
|
|
378
|
+
* @param config.cwd - Working directory for build process (defaults to process.cwd())
|
|
379
|
+
* @param config.appName - Your app's unique identifier
|
|
380
|
+
* @param config.scheme - URL scheme for launching your app (e.g. 'granite')
|
|
381
|
+
* @param config.outdir - Where to output build files (defaults to 'dist')
|
|
382
|
+
* @param config.entryFile - Your app's entry point (defaults to './src/_app.tsx')
|
|
383
|
+
* @param config.build - Customize build settings
|
|
384
|
+
* @param config.metro - Configure Metro bundler settings
|
|
385
|
+
* @param config.devServer - Configure Mpack dev server settings
|
|
386
|
+
* @param config.plugins - Granite plugins to enhance functionality
|
|
387
|
+
* @returns The processed configuration
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* Here's a basic configuration that:
|
|
391
|
+
* - Makes your app accessible via the `granite://` scheme
|
|
392
|
+
* - Names your service "my-app" so it's reachable at `granite://my-app`
|
|
393
|
+
* - Uses the Hermes plugin to optimize JavaScript bundles into bytecode
|
|
394
|
+
*
|
|
395
|
+
* ```ts
|
|
396
|
+
* import { defineConfig } from '@granite-js/react-native/config';
|
|
397
|
+
* import { hermes } from '@granite-js/plugin-hermes';
|
|
398
|
+
*
|
|
399
|
+
* export default defineConfig({
|
|
400
|
+
* // The name of your microservice
|
|
401
|
+
* appName: 'my-app',
|
|
402
|
+
* // The URL scheme for deep linking
|
|
403
|
+
* scheme: 'granite',
|
|
404
|
+
* // Entry file path
|
|
405
|
+
* entryFile: 'index.ts',
|
|
406
|
+
* // Array of plugins to use
|
|
407
|
+
* plugins: [hermes()],
|
|
408
|
+
* });
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
const defineConfig = async (config) => {
|
|
412
|
+
const parsed = pluginConfigSchema.parse(config);
|
|
413
|
+
const cwd = parsed.cwd ?? (0, __granite_js_utils.getPackageRoot)();
|
|
414
|
+
const appName = parsed.appName;
|
|
415
|
+
const scheme = parsed.scheme;
|
|
416
|
+
const entryFile = path.default.resolve(cwd, parsed.entryFile);
|
|
417
|
+
const outdir = path.default.join(cwd, parsed.outdir);
|
|
418
|
+
const parsedBuildConfig = parsed.build;
|
|
419
|
+
const parsedDevServerConfig = parsed.devServer;
|
|
420
|
+
const parsedMetroConfig = parsed.metro;
|
|
421
|
+
const parsedConfig = {
|
|
422
|
+
...parsedBuildConfig,
|
|
423
|
+
devServer: parsedDevServerConfig,
|
|
424
|
+
metro: parsedMetroConfig
|
|
425
|
+
};
|
|
426
|
+
const { configs, pluginHooks } = await resolvePlugins(parsed.plugins);
|
|
427
|
+
const globalsScriptConfig = prepareGraniteGlobalsScript({
|
|
428
|
+
rootDir: cwd,
|
|
429
|
+
appName,
|
|
430
|
+
scheme
|
|
431
|
+
});
|
|
432
|
+
const mergedConfig = mergeConfig(parsedConfig, ...[globalsScriptConfig, ...configs].filter(es_toolkit.isNotNil));
|
|
433
|
+
const { metro, devServer,...build } = mergedConfig ?? {};
|
|
434
|
+
return {
|
|
435
|
+
cwd,
|
|
436
|
+
appName,
|
|
437
|
+
entryFile,
|
|
438
|
+
outdir,
|
|
439
|
+
build,
|
|
440
|
+
devServer,
|
|
441
|
+
pluginHooks,
|
|
442
|
+
metro: {
|
|
443
|
+
...metro,
|
|
444
|
+
babelConfig: mergedConfig?.babel,
|
|
445
|
+
transformSync: mergedConfig?.transformer?.transformSync
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
//#endregion
|
|
451
|
+
//#region src/config/loadConfig.ts
|
|
452
|
+
const MODULE_NAME = "granite";
|
|
453
|
+
const loadConfig = async (options = {}) => {
|
|
454
|
+
let result;
|
|
455
|
+
if (options.configFile) result = await getConfigExplorer().load(path.default.resolve(options.root ?? (0, __granite_js_utils.getPackageRoot)(), options.configFile));
|
|
456
|
+
else result = await getConfigExplorer({ searchPlaces: [
|
|
457
|
+
`${MODULE_NAME}.config.ts`,
|
|
458
|
+
`${MODULE_NAME}.config.mts`,
|
|
459
|
+
`${MODULE_NAME}.config.js`,
|
|
460
|
+
`${MODULE_NAME}.config.cjs`
|
|
461
|
+
] }).search(options.root);
|
|
462
|
+
(0, es_toolkit.assert)(result, "Config file not found");
|
|
463
|
+
const config = await result.config;
|
|
464
|
+
return config;
|
|
465
|
+
};
|
|
466
|
+
function getConfigExplorer(options) {
|
|
467
|
+
return (0, cosmiconfig.cosmiconfig)(MODULE_NAME, {
|
|
468
|
+
loaders: {
|
|
469
|
+
".ts": (0, cosmiconfig_typescript_loader.TypeScriptLoader)(),
|
|
470
|
+
".mts": (0, cosmiconfig_typescript_loader.TypeScriptLoader)()
|
|
471
|
+
},
|
|
472
|
+
...options
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
|
|
336
476
|
//#endregion
|
|
337
477
|
exports.createContext = createContext;
|
|
338
478
|
exports.createPluginContext = createPluginContext;
|
|
339
479
|
exports.createPluginHooksDriver = createPluginHooksDriver;
|
|
480
|
+
exports.defineConfig = defineConfig;
|
|
340
481
|
exports.flattenPlugins = flattenPlugins;
|
|
482
|
+
exports.loadConfig = loadConfig;
|
|
341
483
|
exports.mergeBuildConfigs = mergeBuildConfigs;
|
|
342
484
|
exports.mergeConfig = mergeConfig;
|
|
343
485
|
exports.pluginConfigSchema = pluginConfigSchema;
|
package/dist/index.d.cts
CHANGED
|
@@ -592,4 +592,75 @@ declare function createPluginHooksDriver(config: CompleteGraniteConfig): {
|
|
|
592
592
|
declare function createPluginContext(): PluginContext;
|
|
593
593
|
|
|
594
594
|
//#endregion
|
|
595
|
-
|
|
595
|
+
//#region src/config/defineConfig.d.ts
|
|
596
|
+
/**
|
|
597
|
+
* @public
|
|
598
|
+
* @category Configuration
|
|
599
|
+
* @name defineConfig
|
|
600
|
+
* @description
|
|
601
|
+
* Configures your Granite application by defining key settings in `granite.config.ts`.
|
|
602
|
+
*
|
|
603
|
+
* The configuration lets you specify:
|
|
604
|
+
* - How users will access your app through a URL scheme (e.g. `granite://`)
|
|
605
|
+
* - Your app's unique name that appears in the URL (e.g. `granite://my-service`)
|
|
606
|
+
* - Build settings for bundlers like ESBuild and Metro
|
|
607
|
+
* - Code transformation settings through Babel
|
|
608
|
+
* - Additional functionality through Granite plugins
|
|
609
|
+
*
|
|
610
|
+
* @param config - Configuration options
|
|
611
|
+
* @param config.cwd - Working directory for build process (defaults to process.cwd())
|
|
612
|
+
* @param config.appName - Your app's unique identifier
|
|
613
|
+
* @param config.scheme - URL scheme for launching your app (e.g. 'granite')
|
|
614
|
+
* @param config.outdir - Where to output build files (defaults to 'dist')
|
|
615
|
+
* @param config.entryFile - Your app's entry point (defaults to './src/_app.tsx')
|
|
616
|
+
* @param config.build - Customize build settings
|
|
617
|
+
* @param config.metro - Configure Metro bundler settings
|
|
618
|
+
* @param config.devServer - Configure Mpack dev server settings
|
|
619
|
+
* @param config.plugins - Granite plugins to enhance functionality
|
|
620
|
+
* @returns The processed configuration
|
|
621
|
+
*
|
|
622
|
+
* @example
|
|
623
|
+
* Here's a basic configuration that:
|
|
624
|
+
* - Makes your app accessible via the `granite://` scheme
|
|
625
|
+
* - Names your service "my-app" so it's reachable at `granite://my-app`
|
|
626
|
+
* - Uses the Hermes plugin to optimize JavaScript bundles into bytecode
|
|
627
|
+
*
|
|
628
|
+
* ```ts
|
|
629
|
+
* import { defineConfig } from '@granite-js/react-native/config';
|
|
630
|
+
* import { hermes } from '@granite-js/plugin-hermes';
|
|
631
|
+
*
|
|
632
|
+
* export default defineConfig({
|
|
633
|
+
* // The name of your microservice
|
|
634
|
+
* appName: 'my-app',
|
|
635
|
+
* // The URL scheme for deep linking
|
|
636
|
+
* scheme: 'granite',
|
|
637
|
+
* // Entry file path
|
|
638
|
+
* entryFile: 'index.ts',
|
|
639
|
+
* // Array of plugins to use
|
|
640
|
+
* plugins: [hermes()],
|
|
641
|
+
* });
|
|
642
|
+
* ```
|
|
643
|
+
*/
|
|
644
|
+
declare const defineConfig: (config: GraniteConfig) => Promise<CompleteGraniteConfig>;
|
|
645
|
+
|
|
646
|
+
//#endregion
|
|
647
|
+
//#region src/config/loadConfig.d.ts
|
|
648
|
+
interface LoadConfigOptions {
|
|
649
|
+
/**
|
|
650
|
+
* Root directory to search for the config file.
|
|
651
|
+
*
|
|
652
|
+
* Defaults to project root
|
|
653
|
+
*/
|
|
654
|
+
root?: string;
|
|
655
|
+
/**
|
|
656
|
+
* Exact path to the config file.
|
|
657
|
+
*
|
|
658
|
+
* If provided, the config file will be loaded from the given path.
|
|
659
|
+
* Otherwise, the config file will be searched for in the root directory.
|
|
660
|
+
*/
|
|
661
|
+
configFile?: string;
|
|
662
|
+
}
|
|
663
|
+
declare const loadConfig: (options?: LoadConfigOptions) => Promise<CompleteGraniteConfig>;
|
|
664
|
+
|
|
665
|
+
//#endregion
|
|
666
|
+
export { AdditionalMetroConfig, AliasConfig, BabelConfig, BuildConfig, BuildResult, BundleData, CompleteGraniteConfig, DevServerConfig, EsbuildConfig, GraniteConfig, GranitePlugin, GranitePluginBuildPostHandler, GranitePluginBuildPreHandler, GranitePluginConfig, GranitePluginCore, GranitePluginDevHandlerArgs, GranitePluginDevPostHandler, GranitePluginDevPreHandler, GranitePluginHooks, GranitePluginPostHandlerArgs, GranitePluginPreHandlerArgs, MetroDevServerConfig, MetroMiddleware, Middleware, ParsedGraniteConfig, PluginBuildConfig, PluginConfig, PluginContext, PluginInput, PluginMetroConfig, PluginResolvable, ProtocolConfig, ResolverConfig, SwcConfig, TransformAsync, TransformSync, TransformerConfig, createContext, createPluginContext, createPluginHooksDriver, defineConfig, flattenPlugins, loadConfig, mergeBuildConfigs, mergeConfig, pluginConfigSchema, resolvePlugins };
|
package/dist/index.d.ts
CHANGED
|
@@ -592,4 +592,75 @@ declare function createPluginHooksDriver(config: CompleteGraniteConfig): {
|
|
|
592
592
|
declare function createPluginContext(): PluginContext;
|
|
593
593
|
|
|
594
594
|
//#endregion
|
|
595
|
-
|
|
595
|
+
//#region src/config/defineConfig.d.ts
|
|
596
|
+
/**
|
|
597
|
+
* @public
|
|
598
|
+
* @category Configuration
|
|
599
|
+
* @name defineConfig
|
|
600
|
+
* @description
|
|
601
|
+
* Configures your Granite application by defining key settings in `granite.config.ts`.
|
|
602
|
+
*
|
|
603
|
+
* The configuration lets you specify:
|
|
604
|
+
* - How users will access your app through a URL scheme (e.g. `granite://`)
|
|
605
|
+
* - Your app's unique name that appears in the URL (e.g. `granite://my-service`)
|
|
606
|
+
* - Build settings for bundlers like ESBuild and Metro
|
|
607
|
+
* - Code transformation settings through Babel
|
|
608
|
+
* - Additional functionality through Granite plugins
|
|
609
|
+
*
|
|
610
|
+
* @param config - Configuration options
|
|
611
|
+
* @param config.cwd - Working directory for build process (defaults to process.cwd())
|
|
612
|
+
* @param config.appName - Your app's unique identifier
|
|
613
|
+
* @param config.scheme - URL scheme for launching your app (e.g. 'granite')
|
|
614
|
+
* @param config.outdir - Where to output build files (defaults to 'dist')
|
|
615
|
+
* @param config.entryFile - Your app's entry point (defaults to './src/_app.tsx')
|
|
616
|
+
* @param config.build - Customize build settings
|
|
617
|
+
* @param config.metro - Configure Metro bundler settings
|
|
618
|
+
* @param config.devServer - Configure Mpack dev server settings
|
|
619
|
+
* @param config.plugins - Granite plugins to enhance functionality
|
|
620
|
+
* @returns The processed configuration
|
|
621
|
+
*
|
|
622
|
+
* @example
|
|
623
|
+
* Here's a basic configuration that:
|
|
624
|
+
* - Makes your app accessible via the `granite://` scheme
|
|
625
|
+
* - Names your service "my-app" so it's reachable at `granite://my-app`
|
|
626
|
+
* - Uses the Hermes plugin to optimize JavaScript bundles into bytecode
|
|
627
|
+
*
|
|
628
|
+
* ```ts
|
|
629
|
+
* import { defineConfig } from '@granite-js/react-native/config';
|
|
630
|
+
* import { hermes } from '@granite-js/plugin-hermes';
|
|
631
|
+
*
|
|
632
|
+
* export default defineConfig({
|
|
633
|
+
* // The name of your microservice
|
|
634
|
+
* appName: 'my-app',
|
|
635
|
+
* // The URL scheme for deep linking
|
|
636
|
+
* scheme: 'granite',
|
|
637
|
+
* // Entry file path
|
|
638
|
+
* entryFile: 'index.ts',
|
|
639
|
+
* // Array of plugins to use
|
|
640
|
+
* plugins: [hermes()],
|
|
641
|
+
* });
|
|
642
|
+
* ```
|
|
643
|
+
*/
|
|
644
|
+
declare const defineConfig: (config: GraniteConfig) => Promise<CompleteGraniteConfig>;
|
|
645
|
+
|
|
646
|
+
//#endregion
|
|
647
|
+
//#region src/config/loadConfig.d.ts
|
|
648
|
+
interface LoadConfigOptions {
|
|
649
|
+
/**
|
|
650
|
+
* Root directory to search for the config file.
|
|
651
|
+
*
|
|
652
|
+
* Defaults to project root
|
|
653
|
+
*/
|
|
654
|
+
root?: string;
|
|
655
|
+
/**
|
|
656
|
+
* Exact path to the config file.
|
|
657
|
+
*
|
|
658
|
+
* If provided, the config file will be loaded from the given path.
|
|
659
|
+
* Otherwise, the config file will be searched for in the root directory.
|
|
660
|
+
*/
|
|
661
|
+
configFile?: string;
|
|
662
|
+
}
|
|
663
|
+
declare const loadConfig: (options?: LoadConfigOptions) => Promise<CompleteGraniteConfig>;
|
|
664
|
+
|
|
665
|
+
//#endregion
|
|
666
|
+
export { AdditionalMetroConfig, AliasConfig, BabelConfig, BuildConfig, BuildResult, BundleData, CompleteGraniteConfig, DevServerConfig, EsbuildConfig, GraniteConfig, GranitePlugin, GranitePluginBuildPostHandler, GranitePluginBuildPreHandler, GranitePluginConfig, GranitePluginCore, GranitePluginDevHandlerArgs, GranitePluginDevPostHandler, GranitePluginDevPreHandler, GranitePluginHooks, GranitePluginPostHandlerArgs, GranitePluginPreHandlerArgs, MetroDevServerConfig, MetroMiddleware, Middleware, ParsedGraniteConfig, PluginBuildConfig, PluginConfig, PluginContext, PluginInput, PluginMetroConfig, PluginResolvable, ProtocolConfig, ResolverConfig, SwcConfig, TransformAsync, TransformSync, TransformerConfig, createContext, createPluginContext, createPluginHooksDriver, defineConfig, flattenPlugins, loadConfig, mergeBuildConfigs, mergeConfig, pluginConfigSchema, resolvePlugins };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import { isNotNil } from "es-toolkit";
|
|
1
|
+
import { assert, isNotNil } from "es-toolkit";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { getLocalTempDirectoryPath, getPackageRoot, prepareLocalDirectory } from "@granite-js/utils";
|
|
4
|
+
import fs from "fs";
|
|
2
5
|
import * as z from "zod";
|
|
6
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
7
|
+
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";
|
|
3
8
|
|
|
4
9
|
//#region src/createContext.ts
|
|
5
10
|
function createContext() {
|
|
@@ -296,6 +301,26 @@ function createPluginContext() {
|
|
|
296
301
|
return context;
|
|
297
302
|
}
|
|
298
303
|
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/config/graniteGlobals.ts
|
|
306
|
+
function prepareGraniteGlobalsScript(config) {
|
|
307
|
+
const filePath = writeGraniteGlobalsScript(config);
|
|
308
|
+
return {
|
|
309
|
+
esbuild: { prelude: [filePath] },
|
|
310
|
+
metro: { serializer: { getPolyfills: () => [filePath] } }
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
function writeGraniteGlobalsScript(config) {
|
|
314
|
+
const script = getGraniteGlobalScript(config);
|
|
315
|
+
const filePath = path.join(getLocalTempDirectoryPath(config.rootDir), "granite-globals.js");
|
|
316
|
+
prepareLocalDirectory(config.rootDir);
|
|
317
|
+
fs.writeFileSync(filePath, script, "utf-8");
|
|
318
|
+
return filePath;
|
|
319
|
+
}
|
|
320
|
+
function getGraniteGlobalScript({ appName, scheme }) {
|
|
321
|
+
return ["global.__granite = global.__granite || {};", `global.__granite.app = { name: ${JSON.stringify(appName)}, scheme: ${JSON.stringify(scheme)} };`].join("\n");
|
|
322
|
+
}
|
|
323
|
+
|
|
299
324
|
//#endregion
|
|
300
325
|
//#region src/schema/pluginConfig.ts
|
|
301
326
|
const pluginConfigSchema = z.object({
|
|
@@ -311,4 +336,119 @@ const pluginConfigSchema = z.object({
|
|
|
311
336
|
});
|
|
312
337
|
|
|
313
338
|
//#endregion
|
|
314
|
-
|
|
339
|
+
//#region src/config/defineConfig.ts
|
|
340
|
+
/**
|
|
341
|
+
* @public
|
|
342
|
+
* @category Configuration
|
|
343
|
+
* @name defineConfig
|
|
344
|
+
* @description
|
|
345
|
+
* Configures your Granite application by defining key settings in `granite.config.ts`.
|
|
346
|
+
*
|
|
347
|
+
* The configuration lets you specify:
|
|
348
|
+
* - How users will access your app through a URL scheme (e.g. `granite://`)
|
|
349
|
+
* - Your app's unique name that appears in the URL (e.g. `granite://my-service`)
|
|
350
|
+
* - Build settings for bundlers like ESBuild and Metro
|
|
351
|
+
* - Code transformation settings through Babel
|
|
352
|
+
* - Additional functionality through Granite plugins
|
|
353
|
+
*
|
|
354
|
+
* @param config - Configuration options
|
|
355
|
+
* @param config.cwd - Working directory for build process (defaults to process.cwd())
|
|
356
|
+
* @param config.appName - Your app's unique identifier
|
|
357
|
+
* @param config.scheme - URL scheme for launching your app (e.g. 'granite')
|
|
358
|
+
* @param config.outdir - Where to output build files (defaults to 'dist')
|
|
359
|
+
* @param config.entryFile - Your app's entry point (defaults to './src/_app.tsx')
|
|
360
|
+
* @param config.build - Customize build settings
|
|
361
|
+
* @param config.metro - Configure Metro bundler settings
|
|
362
|
+
* @param config.devServer - Configure Mpack dev server settings
|
|
363
|
+
* @param config.plugins - Granite plugins to enhance functionality
|
|
364
|
+
* @returns The processed configuration
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* Here's a basic configuration that:
|
|
368
|
+
* - Makes your app accessible via the `granite://` scheme
|
|
369
|
+
* - Names your service "my-app" so it's reachable at `granite://my-app`
|
|
370
|
+
* - Uses the Hermes plugin to optimize JavaScript bundles into bytecode
|
|
371
|
+
*
|
|
372
|
+
* ```ts
|
|
373
|
+
* import { defineConfig } from '@granite-js/react-native/config';
|
|
374
|
+
* import { hermes } from '@granite-js/plugin-hermes';
|
|
375
|
+
*
|
|
376
|
+
* export default defineConfig({
|
|
377
|
+
* // The name of your microservice
|
|
378
|
+
* appName: 'my-app',
|
|
379
|
+
* // The URL scheme for deep linking
|
|
380
|
+
* scheme: 'granite',
|
|
381
|
+
* // Entry file path
|
|
382
|
+
* entryFile: 'index.ts',
|
|
383
|
+
* // Array of plugins to use
|
|
384
|
+
* plugins: [hermes()],
|
|
385
|
+
* });
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
const defineConfig = async (config) => {
|
|
389
|
+
const parsed = pluginConfigSchema.parse(config);
|
|
390
|
+
const cwd = parsed.cwd ?? getPackageRoot();
|
|
391
|
+
const appName = parsed.appName;
|
|
392
|
+
const scheme = parsed.scheme;
|
|
393
|
+
const entryFile = path.resolve(cwd, parsed.entryFile);
|
|
394
|
+
const outdir = path.join(cwd, parsed.outdir);
|
|
395
|
+
const parsedBuildConfig = parsed.build;
|
|
396
|
+
const parsedDevServerConfig = parsed.devServer;
|
|
397
|
+
const parsedMetroConfig = parsed.metro;
|
|
398
|
+
const parsedConfig = {
|
|
399
|
+
...parsedBuildConfig,
|
|
400
|
+
devServer: parsedDevServerConfig,
|
|
401
|
+
metro: parsedMetroConfig
|
|
402
|
+
};
|
|
403
|
+
const { configs, pluginHooks } = await resolvePlugins(parsed.plugins);
|
|
404
|
+
const globalsScriptConfig = prepareGraniteGlobalsScript({
|
|
405
|
+
rootDir: cwd,
|
|
406
|
+
appName,
|
|
407
|
+
scheme
|
|
408
|
+
});
|
|
409
|
+
const mergedConfig = mergeConfig(parsedConfig, ...[globalsScriptConfig, ...configs].filter(isNotNil));
|
|
410
|
+
const { metro, devServer,...build } = mergedConfig ?? {};
|
|
411
|
+
return {
|
|
412
|
+
cwd,
|
|
413
|
+
appName,
|
|
414
|
+
entryFile,
|
|
415
|
+
outdir,
|
|
416
|
+
build,
|
|
417
|
+
devServer,
|
|
418
|
+
pluginHooks,
|
|
419
|
+
metro: {
|
|
420
|
+
...metro,
|
|
421
|
+
babelConfig: mergedConfig?.babel,
|
|
422
|
+
transformSync: mergedConfig?.transformer?.transformSync
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
//#endregion
|
|
428
|
+
//#region src/config/loadConfig.ts
|
|
429
|
+
const MODULE_NAME = "granite";
|
|
430
|
+
const loadConfig = async (options = {}) => {
|
|
431
|
+
let result;
|
|
432
|
+
if (options.configFile) result = await getConfigExplorer().load(path.resolve(options.root ?? getPackageRoot(), options.configFile));
|
|
433
|
+
else result = await getConfigExplorer({ searchPlaces: [
|
|
434
|
+
`${MODULE_NAME}.config.ts`,
|
|
435
|
+
`${MODULE_NAME}.config.mts`,
|
|
436
|
+
`${MODULE_NAME}.config.js`,
|
|
437
|
+
`${MODULE_NAME}.config.cjs`
|
|
438
|
+
] }).search(options.root);
|
|
439
|
+
assert(result, "Config file not found");
|
|
440
|
+
const config = await result.config;
|
|
441
|
+
return config;
|
|
442
|
+
};
|
|
443
|
+
function getConfigExplorer(options) {
|
|
444
|
+
return cosmiconfig(MODULE_NAME, {
|
|
445
|
+
loaders: {
|
|
446
|
+
".ts": TypeScriptLoader(),
|
|
447
|
+
".mts": TypeScriptLoader()
|
|
448
|
+
},
|
|
449
|
+
...options
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
//#endregion
|
|
454
|
+
export { createContext, createPluginContext, createPluginHooksDriver, defineConfig, flattenPlugins, loadConfig, mergeBuildConfigs, mergeConfig, pluginConfigSchema, resolvePlugins };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@granite-js/plugin-core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.10",
|
|
5
5
|
"description": "The core plugin module for Granite",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prepack": "yarn build",
|
|
@@ -31,9 +31,12 @@
|
|
|
31
31
|
"vitest": "^3.0.9"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
+
"@granite-js/utils": "0.1.10",
|
|
34
35
|
"@swc/core": "1.5.24",
|
|
35
36
|
"@types/babel__core": "^7",
|
|
36
37
|
"@types/connect": "^3",
|
|
38
|
+
"cosmiconfig": "^9.0.0",
|
|
39
|
+
"cosmiconfig-typescript-loader": "^5.1.0",
|
|
37
40
|
"es-toolkit": "^1.39.8",
|
|
38
41
|
"esbuild": "^0.25.8",
|
|
39
42
|
"fastify": "4.14.0",
|