@rock-js/plugin-metro 0.8.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.
Files changed (92) hide show
  1. package/README.md +7 -0
  2. package/dist/src/index.d.ts +1 -0
  3. package/dist/src/index.d.ts.map +1 -0
  4. package/dist/src/index.js +2 -0
  5. package/dist/src/index.js.map +1 -0
  6. package/dist/src/lib/bundle/__tests__/filterPlatformAssetScales-test.d.ts +8 -0
  7. package/dist/src/lib/bundle/__tests__/filterPlatformAssetScales-test.js +29 -0
  8. package/dist/src/lib/bundle/__tests__/filterPlatformAssetScales-test.js.map +1 -0
  9. package/dist/src/lib/bundle/assetCatalogIOS.d.ts +21 -0
  10. package/dist/src/lib/bundle/assetCatalogIOS.js +54 -0
  11. package/dist/src/lib/bundle/assetCatalogIOS.js.map +1 -0
  12. package/dist/src/lib/bundle/assetPathUtils.d.ts +28 -0
  13. package/dist/src/lib/bundle/assetPathUtils.js +72 -0
  14. package/dist/src/lib/bundle/assetPathUtils.js.map +1 -0
  15. package/dist/src/lib/bundle/buildBundle.d.ts +57 -0
  16. package/dist/src/lib/bundle/buildBundle.js +99 -0
  17. package/dist/src/lib/bundle/buildBundle.js.map +1 -0
  18. package/dist/src/lib/bundle/command.d.ts +2 -0
  19. package/dist/src/lib/bundle/command.d.ts.map +1 -0
  20. package/dist/src/lib/bundle/command.js +135 -0
  21. package/dist/src/lib/bundle/command.js.map +1 -0
  22. package/dist/src/lib/bundle/createKeepFileAsync.d.ts +10 -0
  23. package/dist/src/lib/bundle/createKeepFileAsync.js +28 -0
  24. package/dist/src/lib/bundle/createKeepFileAsync.js.map +1 -0
  25. package/dist/src/lib/bundle/filterPlatformAssetScales.d.ts +9 -0
  26. package/dist/src/lib/bundle/filterPlatformAssetScales.js +36 -0
  27. package/dist/src/lib/bundle/filterPlatformAssetScales.js.map +1 -0
  28. package/dist/src/lib/bundle/getAssetDestPathAndroid.d.ts +10 -0
  29. package/dist/src/lib/bundle/getAssetDestPathAndroid.js +16 -0
  30. package/dist/src/lib/bundle/getAssetDestPathAndroid.js.map +1 -0
  31. package/dist/src/lib/bundle/getAssetDestPathIOS.d.ts +10 -0
  32. package/dist/src/lib/bundle/getAssetDestPathIOS.js +19 -0
  33. package/dist/src/lib/bundle/getAssetDestPathIOS.js.map +1 -0
  34. package/dist/src/lib/bundle/index.d.ts +11 -0
  35. package/dist/src/lib/bundle/index.js +106 -0
  36. package/dist/src/lib/bundle/index.js.map +1 -0
  37. package/dist/src/lib/bundle/saveAssets.d.ts +10 -0
  38. package/dist/src/lib/bundle/saveAssets.js +112 -0
  39. package/dist/src/lib/bundle/saveAssets.js.map +1 -0
  40. package/dist/src/lib/getDevMiddleware.d.ts +2 -0
  41. package/dist/src/lib/getDevMiddleware.js +15 -0
  42. package/dist/src/lib/getDevMiddleware.js.map +1 -0
  43. package/dist/src/lib/getReactNativeDeps.d.ts +3 -0
  44. package/dist/src/lib/getReactNativeDeps.js +15 -0
  45. package/dist/src/lib/getReactNativeDeps.js.map +1 -0
  46. package/dist/src/lib/loadMetroConfig.d.ts +24 -0
  47. package/dist/src/lib/loadMetroConfig.js +76 -0
  48. package/dist/src/lib/loadMetroConfig.js.map +1 -0
  49. package/dist/src/lib/pluginMetro.d.ts +3 -0
  50. package/dist/src/lib/pluginMetro.d.ts.map +1 -0
  51. package/dist/src/lib/pluginMetro.js +12 -0
  52. package/dist/src/lib/pluginMetro.js.map +1 -0
  53. package/dist/src/lib/start/OpenDebuggerKeyboardHandler.d.ts +35 -0
  54. package/dist/src/lib/start/OpenDebuggerKeyboardHandler.d.ts.map +1 -0
  55. package/dist/src/lib/start/OpenDebuggerKeyboardHandler.js +128 -0
  56. package/dist/src/lib/start/OpenDebuggerKeyboardHandler.js.map +1 -0
  57. package/dist/src/lib/start/attachKeyHandlers.d.ts +14 -0
  58. package/dist/src/lib/start/attachKeyHandlers.d.ts.map +1 -0
  59. package/dist/src/lib/start/attachKeyHandlers.js +107 -0
  60. package/dist/src/lib/start/attachKeyHandlers.js.map +1 -0
  61. package/dist/src/lib/start/command.d.ts +8 -0
  62. package/dist/src/lib/start/command.d.ts.map +1 -0
  63. package/dist/src/lib/start/command.js +110 -0
  64. package/dist/src/lib/start/command.js.map +1 -0
  65. package/dist/src/lib/start/createDevMiddlewareLogger.d.ts +18 -0
  66. package/dist/src/lib/start/createDevMiddlewareLogger.d.ts.map +1 -0
  67. package/dist/src/lib/start/createDevMiddlewareLogger.js +27 -0
  68. package/dist/src/lib/start/createDevMiddlewareLogger.js.map +1 -0
  69. package/dist/src/lib/start/loadMetroConfig.d.ts +24 -0
  70. package/dist/src/lib/start/loadMetroConfig.d.ts.map +1 -0
  71. package/dist/src/lib/start/loadMetroConfig.js +76 -0
  72. package/dist/src/lib/start/loadMetroConfig.js.map +1 -0
  73. package/dist/src/lib/start/metroPlatformResolver.d.ts +24 -0
  74. package/dist/src/lib/start/metroPlatformResolver.d.ts.map +1 -0
  75. package/dist/src/lib/start/metroPlatformResolver.js +38 -0
  76. package/dist/src/lib/start/metroPlatformResolver.js.map +1 -0
  77. package/dist/src/lib/start/runServer.d.ts +32 -0
  78. package/dist/src/lib/start/runServer.d.ts.map +1 -0
  79. package/dist/src/lib/start/runServer.js +133 -0
  80. package/dist/src/lib/start/runServer.js.map +1 -0
  81. package/dist/src/utils/loadMetroConfig.d.ts +24 -0
  82. package/dist/src/utils/loadMetroConfig.js +76 -0
  83. package/dist/src/utils/loadMetroConfig.js.map +1 -0
  84. package/dist/src/utils/metroPlatformResolver.d.ts +24 -0
  85. package/dist/src/utils/metroPlatformResolver.js +38 -0
  86. package/dist/src/utils/metroPlatformResolver.js.map +1 -0
  87. package/dist/src/utils/parseKeyValueParamArray.d.ts +8 -0
  88. package/dist/src/utils/parseKeyValueParamArray.js +21 -0
  89. package/dist/src/utils/parseKeyValueParamArray.js.map +1 -0
  90. package/package.json +41 -0
  91. package/template/metro.config.js +11 -0
  92. package/template/package.json +7 -0
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ export type StartCommandArgs = {
8
+ assetPlugins?: string[];
9
+ cert?: string;
10
+ customLogReporterPath?: string;
11
+ host?: string;
12
+ https?: boolean;
13
+ maxWorkers?: string;
14
+ key?: string;
15
+ platforms: string[];
16
+ port?: string;
17
+ resetCache?: boolean;
18
+ sourceExts?: string[];
19
+ transformer?: string;
20
+ watchFolders?: string[];
21
+ config?: string;
22
+ projectRoot?: string;
23
+ interactive: boolean;
24
+ clientLogs: boolean;
25
+ };
26
+ declare function runServer(options: {
27
+ platforms: Record<string, object>;
28
+ reactNativeVersion: string;
29
+ reactNativePath: string;
30
+ root: string;
31
+ }, args: StartCommandArgs): Promise<void>;
32
+ export default runServer;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runServer.d.ts","sourceRoot":"","sources":["../../../../src/lib/start/runServer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAiBH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,OAAO,CAAC;IACrB,UAAU,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,iBAAe,SAAS,CACtB,OAAO,EAAE;IACP,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,eAAe,EAAE,MAAM,CAAC;IACxB,IAAI,EAAE,MAAM,CAAC;CACd,EACD,IAAI,EAAE,gBAAgB,iBAqHvB;AAsBD,eAAe,SAAS,CAAC"}
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { createRequire } from 'node:module';
8
+ import path from 'node:path';
9
+ import url from 'node:url';
10
+ import { createDevServerMiddleware } from '@react-native-community/cli-server-api';
11
+ import { color } from '@rock-js/tools';
12
+ import Metro from 'metro';
13
+ import { Terminal } from 'metro-core';
14
+ import { getDevMiddleware } from '../getReactNativeDeps.js';
15
+ import attachKeyHandlers from './attachKeyHandlers.js';
16
+ import createDevMiddlewareLogger from './createDevMiddlewareLogger.js';
17
+ import loadMetroConfig from './loadMetroConfig.js';
18
+ async function runServer(options, args) {
19
+ const metroConfig = await loadMetroConfig({
20
+ platforms: options.platforms,
21
+ reactNativeVersion: options.reactNativeVersion,
22
+ reactNativePath: options.reactNativePath,
23
+ root: options.root,
24
+ }, {
25
+ config: args.config,
26
+ maxWorkers: args.maxWorkers,
27
+ port: args.port,
28
+ resetCache: args.resetCache,
29
+ watchFolders: args.watchFolders,
30
+ projectRoot: args.projectRoot,
31
+ sourceExts: args.sourceExts,
32
+ });
33
+ const hostname = args.host?.length ? args.host : 'localhost';
34
+ const { projectRoot, server: { port }, watchFolders, } = metroConfig;
35
+ const protocol = args.https === true ? 'https' : 'http';
36
+ const devServerUrl = url.format({ protocol, hostname, port });
37
+ console.info(`Starting dev server on ${devServerUrl}\n`);
38
+ if (args.assetPlugins) {
39
+ // @ts-expect-error Assigning to readonly property
40
+ metroConfig.transformer.assetPlugins = args.assetPlugins.map((plugin) => require.resolve(plugin));
41
+ }
42
+ // TODO(T214991636): Remove legacy Metro log forwarding
43
+ if (!args.clientLogs) {
44
+ // @ts-expect-error Assigning to readonly property
45
+ metroConfig.server.forwardClientLogs = false;
46
+ }
47
+ let reportEvent = () => {
48
+ // do nothing
49
+ };
50
+ const terminal = new Terminal(process.stdout);
51
+ const ReporterImpl = getReporterImpl(args.customLogReporterPath);
52
+ // @ts-expect-error - metro types are not updated
53
+ const terminalReporter = new ReporterImpl(terminal);
54
+ const { middleware: communityMiddleware, websocketEndpoints: communityWebsocketEndpoints, messageSocketEndpoint, eventsSocketEndpoint, } = createDevServerMiddleware({
55
+ host: hostname,
56
+ port,
57
+ watchFolders,
58
+ });
59
+ const { createDevMiddleware } = await getDevMiddleware(options.reactNativePath);
60
+ const { middleware, websocketEndpoints } = createDevMiddleware({
61
+ projectRoot,
62
+ serverBaseUrl: devServerUrl,
63
+ logger: createDevMiddlewareLogger(terminalReporter),
64
+ });
65
+ const reporter = {
66
+ update(event) {
67
+ terminalReporter.update(event);
68
+ if (reportEvent) {
69
+ reportEvent(event);
70
+ }
71
+ // @ts-expect-error - metro types are not updated
72
+ if (args.interactive && event.type === 'initialize_done') {
73
+ terminalReporter.update({
74
+ type: 'unstable_server_log',
75
+ level: 'info',
76
+ data: `Dev server ready. ${color.dim('Press Ctrl+C to exit.')}`,
77
+ });
78
+ attachKeyHandlers({
79
+ devServerUrl,
80
+ // @ts-expect-error - TBD
81
+ messageSocket: messageSocketEndpoint,
82
+ reporter: terminalReporter,
83
+ });
84
+ }
85
+ },
86
+ };
87
+ // @ts-expect-error Assigning to readonly property
88
+ metroConfig.reporter = reporter;
89
+ const serverInstance = await Metro.runServer(metroConfig, {
90
+ host: args.host,
91
+ secure: args.https,
92
+ secureCert: args.cert,
93
+ secureKey: args.key,
94
+ unstable_extraMiddleware: [communityMiddleware, middleware],
95
+ websocketEndpoints: {
96
+ ...communityWebsocketEndpoints,
97
+ ...websocketEndpoints,
98
+ },
99
+ });
100
+ reportEvent = eventsSocketEndpoint.reportEvent;
101
+ // In Node 8, the default keep-alive for an HTTP connection is 5 seconds. In
102
+ // early versions of Node 8, this was implemented in a buggy way which caused
103
+ // some HTTP responses (like those containing large JS bundles) to be
104
+ // terminated early.
105
+ //
106
+ // As a workaround, arbitrarily increase the keep-alive from 5 to 30 seconds,
107
+ // which should be enough to send even the largest of JS bundles.
108
+ //
109
+ // For more info: https://github.com/nodejs/node/issues/13391
110
+ //
111
+ serverInstance.keepAliveTimeout = 30000;
112
+ }
113
+ const require = createRequire(import.meta.url);
114
+ function getReporterImpl(customLogReporterPath) {
115
+ if (customLogReporterPath == null) {
116
+ return require('metro/src/lib/TerminalReporter');
117
+ }
118
+ try {
119
+ // First we let require resolve it, so we can require packages in node_modules
120
+ // as expected. eg: require('my-package/reporter');
121
+ return require(customLogReporterPath);
122
+ }
123
+ catch (e) {
124
+ if (e instanceof Error && 'code' in e && e.code !== 'MODULE_NOT_FOUND') {
125
+ throw e;
126
+ }
127
+ // If that doesn't work, then we next try relative to the cwd, eg:
128
+ // require('./reporter');
129
+ return require(path.resolve(customLogReporterPath));
130
+ }
131
+ }
132
+ export default runServer;
133
+ //# sourceMappingURL=runServer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runServer.js","sourceRoot":"","sources":["../../../../src/lib/start/runServer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,EAAE,yBAAyB,EAAE,MAAM,wCAAwC,CAAC;AACnF,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,KAAK,MAAM,OAAO,CAAC;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/C,SAAS,eAAe,CAAC,qBAA8B;IACrD,IAAI,qBAAqB,IAAI,IAAI,EAAE,CAAC;QAClC,OAAO,OAAO,CAAC,gCAAgC,CAAC,CAAC;IACnD,CAAC;IACD,IAAI,CAAC;QACH,8EAA8E;QAC9E,mDAAmD;QACnD,OAAO,OAAO,CAAC,qBAAqB,CAAC,CAAC;IACxC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,YAAY,KAAK,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,kBAAkB,EAAE,CAAC;YACvE,MAAM,CAAC,CAAC;QACV,CAAC;QACD,kEAAkE;QAClE,yBAAyB;QACzB,OAAO,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC,CAAC;IACtD,CAAC;AACH,CAAC;AAED,eAAe,SAAS,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { ConfigT, YargArguments } from 'metro-config';
8
+ export type ConfigLoadingContext = Readonly<{
9
+ root: string;
10
+ reactNativePath: string;
11
+ platforms: Record<string, object>;
12
+ }>;
13
+ /**
14
+ * Load Metro config.
15
+ *
16
+ * Allows the CLI to override select values in `metro.config.js` based on
17
+ * dynamic user options in `ctx`.
18
+ */
19
+ export default function loadMetroConfig(ctx: {
20
+ platforms: Record<string, object>;
21
+ reactNativeVersion: string;
22
+ reactNativePath: string;
23
+ root: string;
24
+ }, options?: YargArguments): Promise<ConfigT>;
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { createRequire } from 'node:module';
8
+ import path from 'node:path';
9
+ import { logger, RnefError } from '@rnef/tools';
10
+ import { loadConfig, mergeConfig, resolveConfig } from 'metro-config';
11
+ import { reactNativePlatformResolver } from './metroPlatformResolver.js';
12
+ /**
13
+ * Get the config options to override based on RN CLI inputs.
14
+ */
15
+ function getOverrideConfig(ctx, config) {
16
+ const outOfTreePlatforms = Object.keys(ctx.platforms).filter(
17
+ // @ts-expect-error - TBD
18
+ (platform) => ctx.platforms[platform].npmPackageName);
19
+ const resolver = {
20
+ platforms: [...Object.keys(ctx.platforms), 'native'],
21
+ };
22
+ if (outOfTreePlatforms.length) {
23
+ // @ts-expect-error - TBD
24
+ resolver.resolveRequest = reactNativePlatformResolver(outOfTreePlatforms.reduce((result, platform) => {
25
+ // @ts-expect-error - TBD
26
+ result[platform] = ctx.platforms[platform].npmPackageName;
27
+ return result;
28
+ }, {}), config.resolver?.resolveRequest);
29
+ }
30
+ const require = createRequire(import.meta.url);
31
+ return {
32
+ resolver,
33
+ serializer: {
34
+ // We can include multiple copies of InitializeCore here because metro will
35
+ // only add ones that are already part of the bundle
36
+ getModulesRunBeforeMainModule: () => [
37
+ require.resolve(path.join(ctx.reactNativePath, 'Libraries/Core/InitializeCore'), { paths: [ctx.root] }),
38
+ ...outOfTreePlatforms.map((platform) => require.resolve(
39
+ // @ts-expect-error - TBD
40
+ `${ctx.platforms[platform].npmPackageName}/Libraries/Core/InitializeCore`, { paths: [ctx.root] })),
41
+ ],
42
+ },
43
+ };
44
+ }
45
+ /**
46
+ * Load Metro config.
47
+ *
48
+ * Allows the CLI to override select values in `metro.config.js` based on
49
+ * dynamic user options in `ctx`.
50
+ */
51
+ export default async function loadMetroConfig(ctx, options = {}) {
52
+ const cwd = ctx.root;
53
+ const projectConfig = await resolveConfig(options.config, cwd);
54
+ if (projectConfig.isEmpty) {
55
+ throw new RnefError(`No Metro config found in ${cwd}`);
56
+ }
57
+ logger.debug(`Reading Metro config from ${projectConfig.filepath}`);
58
+ // @ts-expect-error - we're fine with this
59
+ if (!global.__REACT_NATIVE_METRO_CONFIG_LOADED) {
60
+ const warning = `
61
+ =================================================================================================
62
+ From React Native 0.73, your project's Metro config should extend '@react-native/metro-config'
63
+ or it will fail to build. Please copy the template at:
64
+ https://github.com/react-native-community/template/blob/main/template/metro.config.js
65
+ This warning will be removed in future (https://github.com/facebook/metro/issues/1018).
66
+ =================================================================================================
67
+ `;
68
+ for (const line of warning.trim().split('\n')) {
69
+ console.warn(line);
70
+ }
71
+ }
72
+ const config = await loadConfig({ cwd, ...options });
73
+ const overrideConfig = getOverrideConfig(ctx, config);
74
+ return mergeConfig(config, overrideConfig);
75
+ }
76
+ //# sourceMappingURL=loadMetroConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadMetroConfig.js","sourceRoot":"","sources":["../../../src/utils/loadMetroConfig.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACtE,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC;AAQzE;;GAEG;AACH,SAAS,iBAAiB,CACxB,GAAyB,EACzB,MAAe;IAEf,MAAM,kBAAkB,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,MAAM;IAC1D,yBAAyB;IACzB,CAAC,QAAQ,EAAE,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,cAAc,CACrD,CAAC;IACF,MAAM,QAAQ,GAAiC;QAC7C,SAAS,EAAE,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,QAAQ,CAAC;KACrD,CAAC;IAEF,IAAI,kBAAkB,CAAC,MAAM,EAAE,CAAC;QAC9B,yBAAyB;QAC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M,IAAI,SAAS,CAAC,4BAA4B,GAAG,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,6BAA6B,aAAa,CAAC,QAAQ,EAAE,CAAC,CAAC;IAEpE,0CAA0C;IAC1C,IAAI,CAAC,MAAM,CAAC,kCAAkC,EAAE,CAAC;QAC/C,MAAM,OAAO,GAAG;;;;;;;KAOf,CAAC;QAEF,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9C,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;IACH,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IACrD,MAAM,cAAc,GAAG,iBAAiB,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAEtD,OAAO,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { CustomResolver } from 'metro-resolver';
8
+ /**
9
+ * This is an implementation of a metro resolveRequest option which will remap react-native imports
10
+ * to different npm packages based on the platform requested. This allows a single metro instance/config
11
+ * to produce bundles for multiple out of tree platforms at a time.
12
+ *
13
+ * @param platformImplementations
14
+ * A map of platform to npm package that implements that platform
15
+ *
16
+ * Ex:
17
+ * {
18
+ * windows: 'react-native-windows'
19
+ * macos: 'react-native-macos'
20
+ * }
21
+ */
22
+ export declare function reactNativePlatformResolver(platformImplementations: {
23
+ [platform: string]: string;
24
+ }, customResolver: CustomResolver | null | undefined): CustomResolver;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ /**
8
+ * This is an implementation of a metro resolveRequest option which will remap react-native imports
9
+ * to different npm packages based on the platform requested. This allows a single metro instance/config
10
+ * to produce bundles for multiple out of tree platforms at a time.
11
+ *
12
+ * @param platformImplementations
13
+ * A map of platform to npm package that implements that platform
14
+ *
15
+ * Ex:
16
+ * {
17
+ * windows: 'react-native-windows'
18
+ * macos: 'react-native-macos'
19
+ * }
20
+ */
21
+ export function reactNativePlatformResolver(platformImplementations, customResolver) {
22
+ return (context, moduleName, platform) => {
23
+ let modifiedModuleName = moduleName;
24
+ if (platform != null && platformImplementations[platform]) {
25
+ if (moduleName === 'react-native') {
26
+ modifiedModuleName = platformImplementations[platform];
27
+ }
28
+ else if (moduleName.startsWith('react-native/')) {
29
+ modifiedModuleName = `${platformImplementations[platform]}/${modifiedModuleName.slice('react-native/'.length)}`;
30
+ }
31
+ }
32
+ if (customResolver) {
33
+ return customResolver(context, modifiedModuleName, platform);
34
+ }
35
+ return context.resolveRequest(context, modifiedModuleName, platform);
36
+ };
37
+ }
38
+ //# sourceMappingURL=metroPlatformResolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metroPlatformResolver.js","sourceRoot":"","sources":["../../../src/utils/metroPlatformResolver.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,2BAA2B,CACzC,uBAEC,EACD,cAAiD;IAEjD,OAAO,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE;QACvC,IAAI,kBAAkB,GAAG,UAAU,CAAC;QACpC,IAAI,QAAQ,IAAI,IAAI,IAAI,uBAAuB,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1D,IAAI,UAAU,KAAK,cAAc,EAAE,CAAC;gBAClC,kBAAkB,GAAG,uBAAuB,CAAC,QAAQ,CAAC,CAAC;YACzD,CAAC;iBAAM,IAAI,UAAU,CAAC,UAAU,CAAC,eAAe,CAAC,EAAE,CAAC;gBAClD,kBAAkB,GAAG,GACnB,uBAAuB,CAAC,QAAQ,CAClC,IAAI,kBAAkB,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;YACzD,CAAC;QACH,CAAC;QACD,IAAI,cAAc,EAAE,CAAC;YACnB,OAAO,cAAc,CAAC,OAAO,EAAE,kBAAkB,EAAE,QAAQ,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,OAAO,CAAC,cAAc,CAAC,OAAO,EAAE,kBAAkB,EAAE,QAAQ,CAAC,CAAC;IACvE,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ export default function parseKeyValueParamArray(paramArray: Array<string>): Record<string, string>;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ export default function parseKeyValueParamArray(paramArray) {
9
+ const result = {};
10
+ for (const param of paramArray) {
11
+ const equalIndex = param.indexOf('=');
12
+ if (equalIndex === -1) {
13
+ throw new Error(`Invalid key-value param: ${param}. Expected format: key=value`);
14
+ }
15
+ const key = decodeURIComponent(param.slice(0, equalIndex));
16
+ const value = decodeURIComponent(param.slice(equalIndex + 1));
17
+ result[key] = value;
18
+ }
19
+ return result;
20
+ }
21
+ //# sourceMappingURL=parseKeyValueParamArray.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseKeyValueParamArray.js","sourceRoot":"","sources":["../../../src/utils/parseKeyValueParamArray.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,CAAC,OAAO,UAAU,uBAAuB,CAC7C,UAAyB;IAEzB,MAAM,MAAM,GAA2B,EAAE,CAAC;IAE1C,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE,CAAC;QAC/B,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACtC,IAAI,UAAU,KAAK,CAAC,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,4BAA4B,KAAK,8BAA8B,CAAC,CAAC;QACnF,CAAC;QAED,MAAM,GAAG,GAAG,kBAAkB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC;QAC3D,MAAM,KAAK,GAAG,kBAAkB,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC,CAAC;QAE9D,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@rock-js/plugin-metro",
3
+ "version": "0.8.13",
4
+ "type": "module",
5
+ "types": "./dist/src/index.d.ts",
6
+ "exports": {
7
+ "types": "./dist/src/index.d.ts",
8
+ "default": "./dist/src/index.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "template"
13
+ ],
14
+ "scripts": {
15
+ "build": "tsc -p tsconfig.lib.json",
16
+ "dev": "tsc -p tsconfig.lib.json --watch",
17
+ "publish:npm": "npm publish --access public",
18
+ "publish:verdaccio": "npm publish --registry http://localhost:4873 --userconfig ../../.npmrc"
19
+ },
20
+ "dependencies": {
21
+ "@react-native-community/cli-server-api": "^19.1.0",
22
+ "@react-native/dev-middleware": "^0.80.1",
23
+ "@rock-js/tools": "^0.8.13",
24
+ "metro": "^0.82.2",
25
+ "metro-config": "^0.82.2",
26
+ "metro-core": "^0.82.2",
27
+ "metro-resolver": "^0.82.2",
28
+ "tslib": "^2.3.0"
29
+ },
30
+ "devDependencies": {
31
+ "@react-native/dev-middleware": "^0.80.1",
32
+ "@react-native/community-cli-plugin": "0.80.1",
33
+ "@rock-js/config": "^0.8.13"
34
+ },
35
+ "peerDependencies": {
36
+ "@react-native/community-cli-plugin": "*"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ }
41
+ }
@@ -0,0 +1,11 @@
1
+ const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
2
+
3
+ /**
4
+ * Metro configuration
5
+ * https://reactnative.dev/docs/metro
6
+ *
7
+ * @type {import('@react-native/metro-config').MetroConfig}
8
+ */
9
+ const config = {};
10
+
11
+ module.exports = mergeConfig(getDefaultConfig(__dirname), config);
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "rock-plugin-metro-template",
3
+ "devDependencies": {
4
+ "@rock-js/plugin-metro": "^0.8.13",
5
+ "@react-native/metro-config": "0.80.1"
6
+ }
7
+ }