@granite-js/cli 0.0.1
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 +54 -0
- package/LICENSE +202 -0
- package/README.md +31 -0
- package/dist/index.cjs +695 -0
- package/dist/index.d.cts +161 -0
- package/dist/index.d.ts +161 -0
- package/dist/index.js +666 -0
- package/package.json +65 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,695 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
commonResolver: () => commonResolver,
|
|
34
|
+
defineConfig: () => defineConfig,
|
|
35
|
+
getGlobalVariables: () => getGlobalVariables,
|
|
36
|
+
initialize: () => initialize,
|
|
37
|
+
loadConfig: () => loadConfig,
|
|
38
|
+
service: () => service
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(index_exports);
|
|
41
|
+
var import_clipanion4 = require("clipanion");
|
|
42
|
+
var import_cosmiconfig2 = require("cosmiconfig");
|
|
43
|
+
|
|
44
|
+
// src/commands/BuildCommand/BuildCommand.ts
|
|
45
|
+
var import_clipanion = require("clipanion");
|
|
46
|
+
|
|
47
|
+
// src/build.ts
|
|
48
|
+
var import_mpack = require("@granite-js/mpack");
|
|
49
|
+
var import_plugins = require("@granite-js/mpack/plugins");
|
|
50
|
+
var import_plugin_core = require("@granite-js/plugin-core");
|
|
51
|
+
async function build(config, {
|
|
52
|
+
tag,
|
|
53
|
+
cache
|
|
54
|
+
}) {
|
|
55
|
+
const resolvedPlugins = await (0, import_plugin_core.resolvePlugins)(config.plugins);
|
|
56
|
+
const pluginContext = {
|
|
57
|
+
meta: /* @__PURE__ */ Object.create(null)
|
|
58
|
+
};
|
|
59
|
+
for (const preHandler of resolvedPlugins.build.preHandlers) {
|
|
60
|
+
await preHandler?.call(pluginContext, {
|
|
61
|
+
cwd: config.cwd,
|
|
62
|
+
entryFile: config.entryFile,
|
|
63
|
+
appName: config.appName,
|
|
64
|
+
outdir: config.outdir,
|
|
65
|
+
buildResults: []
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const results = await (0, import_mpack.runBundle)({
|
|
69
|
+
tag,
|
|
70
|
+
rootDir: config.cwd,
|
|
71
|
+
dev: false,
|
|
72
|
+
cache,
|
|
73
|
+
metafile: true,
|
|
74
|
+
plugins: [import_plugins.statusPlugin],
|
|
75
|
+
config: config.mpack.build.config
|
|
76
|
+
});
|
|
77
|
+
for (const postHandler of resolvedPlugins.build.postHandlers) {
|
|
78
|
+
await postHandler?.call(pluginContext, {
|
|
79
|
+
cwd: config.cwd,
|
|
80
|
+
entryFile: config.entryFile,
|
|
81
|
+
appName: config.appName,
|
|
82
|
+
outdir: config.outdir,
|
|
83
|
+
buildResults: results
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/config/loadConfig.ts
|
|
89
|
+
var import_utils = require("@granite-js/utils");
|
|
90
|
+
var import_cosmiconfig = require("cosmiconfig");
|
|
91
|
+
var import_cosmiconfig_typescript_loader = require("cosmiconfig-typescript-loader");
|
|
92
|
+
var MODULE_NAME = "granite";
|
|
93
|
+
var loadConfig = async () => {
|
|
94
|
+
const explorer = (0, import_cosmiconfig.cosmiconfig)(MODULE_NAME, {
|
|
95
|
+
searchPlaces: [
|
|
96
|
+
`${MODULE_NAME}.config.ts`,
|
|
97
|
+
`${MODULE_NAME}.config.mts`,
|
|
98
|
+
`${MODULE_NAME}.config.js`,
|
|
99
|
+
`${MODULE_NAME}.config.cjs`
|
|
100
|
+
],
|
|
101
|
+
loaders: {
|
|
102
|
+
".ts": (0, import_cosmiconfig_typescript_loader.TypeScriptLoader)(),
|
|
103
|
+
".mts": (0, import_cosmiconfig_typescript_loader.TypeScriptLoader)()
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
const result = await explorer.search((0, import_utils.getPackageRoot)());
|
|
107
|
+
const config = await result?.config;
|
|
108
|
+
return config;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// src/commands/BuildCommand/BuildCommand.ts
|
|
112
|
+
var BuildCommand = class extends import_clipanion.Command {
|
|
113
|
+
static paths = [[`build`]];
|
|
114
|
+
static usage = import_clipanion.Command.Usage({
|
|
115
|
+
category: "Build",
|
|
116
|
+
description: "Granite App \uBC88\uB4E4\uC744 \uC0DD\uC131\uD569\uB2C8\uB2E4",
|
|
117
|
+
examples: [
|
|
118
|
+
["\uBE4C\uB4DC\uD558\uAE30", "granite build"],
|
|
119
|
+
["\uC9C0\uC815\uB41C \uB300\uC0C1\uB9CC \uBE4C\uB4DC\uD558\uAE30", "granite build --id service-ios"]
|
|
120
|
+
]
|
|
121
|
+
});
|
|
122
|
+
tag = import_clipanion.Option.String("--tag", {
|
|
123
|
+
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"
|
|
124
|
+
});
|
|
125
|
+
disableCache = import_clipanion.Option.Boolean("--disable-cache", {
|
|
126
|
+
description: "\uCE90\uC2DC\uB97C \uBE44\uD65C\uC131\uD654 \uD569\uB2C8\uB2E4"
|
|
127
|
+
});
|
|
128
|
+
async execute() {
|
|
129
|
+
try {
|
|
130
|
+
const { tag, disableCache = false } = this;
|
|
131
|
+
const config = await loadConfig();
|
|
132
|
+
await build(config, {
|
|
133
|
+
tag,
|
|
134
|
+
cache: !disableCache
|
|
135
|
+
});
|
|
136
|
+
return 0;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error(error);
|
|
139
|
+
return 1;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// src/commands/HermesCommand/HermesCommand.ts
|
|
145
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
146
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
147
|
+
var import_clipanion2 = require("clipanion");
|
|
148
|
+
|
|
149
|
+
// src/utils/compileHbc.ts
|
|
150
|
+
var import_assert = __toESM(require("assert"), 1);
|
|
151
|
+
var import_os = __toESM(require("os"), 1);
|
|
152
|
+
var import_path = __toESM(require("path"), 1);
|
|
153
|
+
var import_es_toolkit = require("es-toolkit");
|
|
154
|
+
var import_execa = __toESM(require("execa"), 1);
|
|
155
|
+
var binary = {
|
|
156
|
+
Darwin: "react-native/sdks/hermesc/osx-bin/hermesc",
|
|
157
|
+
Linux: "react-native/sdks/hermesc/linux64-bin/hermesc",
|
|
158
|
+
Windows_NT: "react-native/sdks/hermesc/win64-bin/hermesc.exe"
|
|
159
|
+
};
|
|
160
|
+
async function compileHbc({ rootDir, filePath, sourcemap }) {
|
|
161
|
+
const binary2 = getHermesc(rootDir);
|
|
162
|
+
const outfile = import_path.default.resolve(rootDir, filePath.replace(new RegExp(`${import_path.default.extname(filePath)}$`), ".hbc"));
|
|
163
|
+
await (0, import_execa.default)(
|
|
164
|
+
binary2,
|
|
165
|
+
[
|
|
166
|
+
// Disable warnings
|
|
167
|
+
"-w",
|
|
168
|
+
// Expensive optimizations
|
|
169
|
+
"-O",
|
|
170
|
+
// Emit binary
|
|
171
|
+
"-emit-binary",
|
|
172
|
+
// Emit source map
|
|
173
|
+
sourcemap ? "-output-source-map" : null,
|
|
174
|
+
// Output path
|
|
175
|
+
"-out",
|
|
176
|
+
outfile,
|
|
177
|
+
filePath
|
|
178
|
+
].filter(import_es_toolkit.isNotNil)
|
|
179
|
+
);
|
|
180
|
+
return { outfile, sourcemapOutfile: sourcemap ? `${outfile}.map` : null };
|
|
181
|
+
}
|
|
182
|
+
function getHermesc(rootDir) {
|
|
183
|
+
const os2 = getOs();
|
|
184
|
+
const binarySource = binary[os2];
|
|
185
|
+
(0, import_assert.default)(binarySource, `\uC9C0\uC6D0\uD558\uC9C0 \uC54A\uB294 OS \uC785\uB2C8\uB2E4: ${os2}`);
|
|
186
|
+
return resolveFromRoot(rootDir, binarySource);
|
|
187
|
+
}
|
|
188
|
+
function getOs() {
|
|
189
|
+
return import_os.default.type();
|
|
190
|
+
}
|
|
191
|
+
function resolveFromRoot(rootDir, request) {
|
|
192
|
+
return require.resolve(request, { paths: [rootDir] });
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/commands/HermesCommand/HermesCommand.ts
|
|
196
|
+
var HermesCommand = class extends import_clipanion2.Command {
|
|
197
|
+
static paths = [[`hermes`]];
|
|
198
|
+
static usage = import_clipanion2.Command.Usage({
|
|
199
|
+
category: "Hermes",
|
|
200
|
+
description: "\uC9C0\uC815\uD55C \uBC88\uB4E4\uC744 Hermes \uBC14\uC774\uD2B8 \uCF54\uB4DC\uB85C \uCEF4\uD30C\uC77C \uD569\uB2C8\uB2E4",
|
|
201
|
+
examples: [["\uCEF4\uD30C\uC77C\uD558\uAE30", "granite hermes --jsbundle dist/bundle.js"]]
|
|
202
|
+
});
|
|
203
|
+
jsBundleFile = import_clipanion2.Option.String("--jsbundle", {
|
|
204
|
+
required: true,
|
|
205
|
+
description: "Hermes \uBC14\uC774\uD2B8 \uCF54\uB4DC\uB85C \uCEF4\uD30C\uC77C \uD560 Javascript \uD30C\uC77C \uACBD\uB85C \uC785\uB2C8\uB2E4"
|
|
206
|
+
});
|
|
207
|
+
sourcemap = import_clipanion2.Option.Boolean("--sourcemap", true, {
|
|
208
|
+
description: "\uC18C\uC2A4\uB9F5 \uD30C\uC77C\uC744 \uC0DD\uC131\uD569\uB2C8\uB2E4"
|
|
209
|
+
});
|
|
210
|
+
async execute() {
|
|
211
|
+
try {
|
|
212
|
+
const rootDir = process.cwd();
|
|
213
|
+
const filePath = import_path2.default.resolve(rootDir, this.jsBundleFile);
|
|
214
|
+
const { outfile, sourcemapOutfile } = await compileHbc({ rootDir, filePath, sourcemap: this.sourcemap });
|
|
215
|
+
console.log(`\u2705 \uCEF4\uD30C\uC77C \uC644\uB8CC: ${import_chalk.default.gray(outfile)}`);
|
|
216
|
+
if (sourcemapOutfile) {
|
|
217
|
+
console.log(`\u2705 \uC18C\uC2A4\uB9F5 \uC0DD\uC131 \uC644\uB8CC: ${import_chalk.default.gray(sourcemapOutfile)}`);
|
|
218
|
+
}
|
|
219
|
+
return 0;
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.error(error.stack);
|
|
222
|
+
return 1;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// src/commands/DevCommand/DevCommand.ts
|
|
228
|
+
var import_assert2 = __toESM(require("assert"), 1);
|
|
229
|
+
var mpack = __toESM(require("@granite-js/mpack"), 1);
|
|
230
|
+
var import_plugin_core2 = require("@granite-js/plugin-core");
|
|
231
|
+
var import_clipanion3 = require("clipanion");
|
|
232
|
+
var import_debug = __toESM(require("debug"), 1);
|
|
233
|
+
|
|
234
|
+
// src/config/mergeTransformFromPlugins.ts
|
|
235
|
+
async function mergeTransformFromPlugins(plugins) {
|
|
236
|
+
const mergedTransformFunctions = plugins.reduce(
|
|
237
|
+
(acc, plugin) => {
|
|
238
|
+
if (plugin.transformSync) {
|
|
239
|
+
acc.transformSync.push(plugin.transformSync);
|
|
240
|
+
}
|
|
241
|
+
if (plugin.transformAsync) {
|
|
242
|
+
acc.transformAsync.push(plugin.transformAsync);
|
|
243
|
+
}
|
|
244
|
+
return acc;
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
transformSync: [],
|
|
248
|
+
transformAsync: []
|
|
249
|
+
}
|
|
250
|
+
);
|
|
251
|
+
if (mergedTransformFunctions.transformSync.length === 0 && mergedTransformFunctions.transformAsync.length > 0) {
|
|
252
|
+
console.warn(
|
|
253
|
+
`Metro is only supported 'transformSync', but ${mergedTransformFunctions.transformAsync.length} 'transformAsync' are detected and it will be ignored.`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
transformSync: (id, code) => {
|
|
258
|
+
return mergedTransformFunctions.transformSync.reduce((acc, transform) => transform?.(id, acc) ?? acc, code);
|
|
259
|
+
},
|
|
260
|
+
transformAsync: async (id, code) => {
|
|
261
|
+
return mergedTransformFunctions.transformAsync.reduce(async (acc, transform) => {
|
|
262
|
+
return await transform?.(id, await acc) ?? acc;
|
|
263
|
+
}, Promise.resolve(code));
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/commands/DevCommand/DevCommand.ts
|
|
269
|
+
var debug = (0, import_debug.default)("cli");
|
|
270
|
+
var DEFAULT_HOST = "0.0.0.0";
|
|
271
|
+
var DEFAULT_LOCALHOST_PORT = 8081;
|
|
272
|
+
var DevCommand = class extends import_clipanion3.Command {
|
|
273
|
+
static paths = [[`dev`]];
|
|
274
|
+
static usage = import_clipanion3.Command.Usage({
|
|
275
|
+
category: "Development",
|
|
276
|
+
description: "Granite \uAC1C\uBC1C \uC11C\uBC84\uB97C \uC2E4\uD589\uD569\uB2C8\uB2E4",
|
|
277
|
+
examples: [["\uAC1C\uBC1C \uC11C\uBC84 \uC2E4\uD589\uD558\uAE30", "granite dev"]]
|
|
278
|
+
});
|
|
279
|
+
host = import_clipanion3.Option.String("--host");
|
|
280
|
+
port = import_clipanion3.Option.String("--port");
|
|
281
|
+
disableEmbeddedReactDevTools = import_clipanion3.Option.Boolean("--disable-embedded-react-devtools", false);
|
|
282
|
+
// mpack dev-server
|
|
283
|
+
experimentalMode = import_clipanion3.Option.Boolean("--experimental-mode");
|
|
284
|
+
preloadBundle = import_clipanion3.Option.String("--preload-bundle", {
|
|
285
|
+
description: "Preload \uD560 \uBC88\uB4E4 \uD30C\uC77C\uC758 \uACBD\uB85C"
|
|
286
|
+
});
|
|
287
|
+
async execute() {
|
|
288
|
+
try {
|
|
289
|
+
const config = await loadConfig();
|
|
290
|
+
const serverOptions = {
|
|
291
|
+
host: this.host,
|
|
292
|
+
port: this.port ? parseInt(this.port, 10) : void 0
|
|
293
|
+
};
|
|
294
|
+
(0, import_assert2.default)(config?.appName, "\uC571 \uC774\uB984\uC744 \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4");
|
|
295
|
+
debug("StartCommand", {
|
|
296
|
+
...serverOptions,
|
|
297
|
+
disableEmbeddedReactDevTools: this.disableEmbeddedReactDevTools,
|
|
298
|
+
experimentalMode: this.experimentalMode,
|
|
299
|
+
preloadBundle: this.preloadBundle
|
|
300
|
+
});
|
|
301
|
+
if (this.experimentalMode) {
|
|
302
|
+
const mpackConfig = config.mpack.devServer.config;
|
|
303
|
+
const mpackDevServerConfig = mpackConfig?.devServer;
|
|
304
|
+
(0, import_assert2.default)(mpackDevServerConfig, "mpack dev server \uC124\uC815\uC744 \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4");
|
|
305
|
+
await mpack.experimental_runServer({
|
|
306
|
+
appName: config.appName,
|
|
307
|
+
scheme: config.scheme,
|
|
308
|
+
devServerConfig: mpackDevServerConfig,
|
|
309
|
+
host: serverOptions.host,
|
|
310
|
+
port: serverOptions.port,
|
|
311
|
+
preloadBundle: this.preloadBundle
|
|
312
|
+
});
|
|
313
|
+
} else {
|
|
314
|
+
const resolvedPlugins = await (0, import_plugin_core2.resolvePlugins)(config.plugins);
|
|
315
|
+
const mergedTransform = await mergeTransformFromPlugins(resolvedPlugins.plugins);
|
|
316
|
+
const additionalMetroConfig = {
|
|
317
|
+
...config.metro,
|
|
318
|
+
transformSync: mergedTransform?.transformSync
|
|
319
|
+
};
|
|
320
|
+
for (const preHandler of resolvedPlugins.devServer.preHandlers) {
|
|
321
|
+
debug("preHandler", preHandler);
|
|
322
|
+
await preHandler?.({
|
|
323
|
+
host: serverOptions.host || DEFAULT_HOST,
|
|
324
|
+
port: serverOptions.port || DEFAULT_LOCALHOST_PORT,
|
|
325
|
+
appName: config.appName,
|
|
326
|
+
outdir: config.outdir,
|
|
327
|
+
cwd: config.cwd,
|
|
328
|
+
entryFile: config.entryFile
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
await mpack.runServer({
|
|
332
|
+
cwd: config.cwd,
|
|
333
|
+
host: serverOptions.host,
|
|
334
|
+
port: serverOptions.port,
|
|
335
|
+
middlewares: config.mpack.devServer.middlewares,
|
|
336
|
+
config: config.mpack.devServer.config,
|
|
337
|
+
onServerReady: async () => {
|
|
338
|
+
for (const postHandler of resolvedPlugins.devServer.postHandlers) {
|
|
339
|
+
debug("postHandler", postHandler);
|
|
340
|
+
await postHandler?.({
|
|
341
|
+
host: serverOptions.host || DEFAULT_HOST,
|
|
342
|
+
port: serverOptions.port || DEFAULT_LOCALHOST_PORT,
|
|
343
|
+
appName: config.appName,
|
|
344
|
+
outdir: config.outdir,
|
|
345
|
+
cwd: config.cwd,
|
|
346
|
+
entryFile: config.entryFile
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
enableEmbeddedReactDevTools: !this.disableEmbeddedReactDevTools,
|
|
351
|
+
additionalConfig: additionalMetroConfig
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
console.log(`ERROR OCCURRED`);
|
|
356
|
+
console.log(error);
|
|
357
|
+
console.log(error.message);
|
|
358
|
+
console.error(error.stack);
|
|
359
|
+
process.exit(1);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
// src/config/defineConfig.ts
|
|
365
|
+
var import_path4 = __toESM(require("path"), 1);
|
|
366
|
+
var import_mpack2 = require("@granite-js/mpack");
|
|
367
|
+
var import_plugin_core4 = require("@granite-js/plugin-core");
|
|
368
|
+
var import_utils2 = require("@granite-js/utils");
|
|
369
|
+
var import_es_toolkit4 = require("es-toolkit");
|
|
370
|
+
var import_zod = require("zod");
|
|
371
|
+
|
|
372
|
+
// src/config/mergeConfigFromPlugins.ts
|
|
373
|
+
var import_plugin_core3 = require("@granite-js/plugin-core");
|
|
374
|
+
var import_es_toolkit2 = require("es-toolkit");
|
|
375
|
+
function concatArray(objValue, srcValue) {
|
|
376
|
+
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
|
|
377
|
+
return objValue.concat(srcValue);
|
|
378
|
+
}
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
function mergeMetroConfig(objValue, srcValue, key) {
|
|
382
|
+
if (key === "getPolyfills" && typeof objValue === "function" && typeof srcValue === "function") {
|
|
383
|
+
return () => [...objValue(), ...srcValue()];
|
|
384
|
+
}
|
|
385
|
+
return void 0;
|
|
386
|
+
}
|
|
387
|
+
function combineComparers(...fns) {
|
|
388
|
+
return (targetValue, sourceValue, key, target, source) => {
|
|
389
|
+
for (const fn of fns) {
|
|
390
|
+
const result = fn(targetValue, sourceValue, key, target, source);
|
|
391
|
+
if (result !== void 0) {
|
|
392
|
+
return result;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return void 0;
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
async function mergeConfigFromPlugins(plugins) {
|
|
399
|
+
const pluginsResolved = await (0, import_plugin_core3.flattenPlugins)(plugins);
|
|
400
|
+
return pluginsResolved.reduce(
|
|
401
|
+
(acc, plugin) => (0, import_es_toolkit2.mergeWith)(acc, plugin.config ?? {}, combineComparers(concatArray, mergeMetroConfig)),
|
|
402
|
+
{}
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// src/presets/service.ts
|
|
407
|
+
var import_es_toolkit3 = require("es-toolkit");
|
|
408
|
+
|
|
409
|
+
// src/presets/common/graniteRequireProtocol.ts
|
|
410
|
+
var graniteRequireProtocol = {
|
|
411
|
+
load: (args) => {
|
|
412
|
+
return { loader: "js", contents: `module.exports = __granite_require__('${args.path}');` };
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
// src/presets/common/optimizations.ts
|
|
417
|
+
var resolveTslibEsm = () => ({
|
|
418
|
+
from: "tslib",
|
|
419
|
+
to: async ({ resolve, args }) => {
|
|
420
|
+
const result = await resolve(args.path, {
|
|
421
|
+
kind: args.kind,
|
|
422
|
+
resolveDir: args.resolveDir
|
|
423
|
+
});
|
|
424
|
+
if (result.errors.length) {
|
|
425
|
+
throw new Error(`resolveTslibEsm: ${args.path}`);
|
|
426
|
+
}
|
|
427
|
+
return result.path.replace(/(tslib\.js|modules\/index\.js)$/, "tslib.es6.js");
|
|
428
|
+
},
|
|
429
|
+
exact: true
|
|
430
|
+
});
|
|
431
|
+
var optimizations = { resolveTslibEsm };
|
|
432
|
+
|
|
433
|
+
// src/presets/common/preludeScripts.ts
|
|
434
|
+
var import_path3 = __toESM(require("path"), 1);
|
|
435
|
+
function getReactNativeSetupScripts(rootDir) {
|
|
436
|
+
const reactNativePath = import_path3.default.dirname(
|
|
437
|
+
require.resolve("react-native/package.json", {
|
|
438
|
+
paths: [rootDir]
|
|
439
|
+
})
|
|
440
|
+
);
|
|
441
|
+
return [
|
|
442
|
+
...require(import_path3.default.join(reactNativePath, "rn-get-polyfills"))(),
|
|
443
|
+
import_path3.default.join(reactNativePath, "Libraries/Core/InitializeCore.js")
|
|
444
|
+
];
|
|
445
|
+
}
|
|
446
|
+
function getGlobalVariables({ dev = true }) {
|
|
447
|
+
return {
|
|
448
|
+
global: "window",
|
|
449
|
+
__DEV__: JSON.stringify(dev),
|
|
450
|
+
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production")
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
var preludePresets = {
|
|
454
|
+
globalVariables: ({ dev }) => {
|
|
455
|
+
return [
|
|
456
|
+
"var __BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now();",
|
|
457
|
+
`var __DEV__=${JSON.stringify(dev)};`,
|
|
458
|
+
`var window=typeof globalThis!=='undefined'?globalThis:typeof global!=='undefined'?global:typeof window!=='undefined'?window:this;`
|
|
459
|
+
].join("\n");
|
|
460
|
+
},
|
|
461
|
+
graniteSharedEnvironment: ({ buildNumber }) => {
|
|
462
|
+
return [
|
|
463
|
+
"window.__granite = window.__granite || {};",
|
|
464
|
+
`window.__granite.shared = { buildNumber: ${JSON.stringify(buildNumber)} };`
|
|
465
|
+
].join("\n");
|
|
466
|
+
},
|
|
467
|
+
graniteAppEnvironment: ({
|
|
468
|
+
appName,
|
|
469
|
+
scheme,
|
|
470
|
+
buildNumber
|
|
471
|
+
}) => {
|
|
472
|
+
return [
|
|
473
|
+
"window.__granite = window.__granite || {};",
|
|
474
|
+
`window.__granite.app = { name: ${JSON.stringify(appName)}, scheme: ${JSON.stringify(scheme)}, buildNumber: ${JSON.stringify(buildNumber)} };`
|
|
475
|
+
].join("\n");
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// src/presets/common/babelConditions.ts
|
|
480
|
+
var babelConditions = [
|
|
481
|
+
/**
|
|
482
|
+
* @TODO
|
|
483
|
+
* zod 에서 hermes 가 지원하지 않는 RegExp 를 사용 중이며,
|
|
484
|
+
* 대응 가능한 swc 구성/플러그인이 존재하지 않기에 babel 로 트랜스파일하도록 합니다
|
|
485
|
+
*
|
|
486
|
+
* @see zod {@link https://github.com/colinhacks/zod/issues/2302}
|
|
487
|
+
*/
|
|
488
|
+
(_code, path5) => path5.includes("node_modules/zod")
|
|
489
|
+
];
|
|
490
|
+
|
|
491
|
+
// src/presets/utils/getBuildNumber.ts
|
|
492
|
+
function getBuildNumber() {
|
|
493
|
+
const date = /* @__PURE__ */ new Date();
|
|
494
|
+
const year = date.getFullYear();
|
|
495
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
496
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
497
|
+
const hours = String(date.getHours()).padStart(2, "0");
|
|
498
|
+
const minutes = String(date.getMinutes()).padStart(2, "0");
|
|
499
|
+
const seconds = String(date.getSeconds()).padStart(2, "0");
|
|
500
|
+
return `${year}${month}${day}${hours}${minutes}${seconds}`;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// src/presets/service.ts
|
|
504
|
+
var DEFAULT_OPTIMIZATION_OPTIONS = {
|
|
505
|
+
"tslib-esm": false
|
|
506
|
+
};
|
|
507
|
+
function commonResolver({ optimization = DEFAULT_OPTIMIZATION_OPTIONS }) {
|
|
508
|
+
return {
|
|
509
|
+
alias: [
|
|
510
|
+
...[
|
|
511
|
+
"@react-navigation/native-stack",
|
|
512
|
+
"@react-navigation/native",
|
|
513
|
+
"react-native-safe-area-context",
|
|
514
|
+
"react-native-screens",
|
|
515
|
+
"react-native-svg",
|
|
516
|
+
"react-native-gesture-handler",
|
|
517
|
+
"react-native",
|
|
518
|
+
"react"
|
|
519
|
+
].map((module2) => ({ from: module2, to: `granite-require:${module2}`, exact: true })),
|
|
520
|
+
optimization["tslib-esm"] ? optimizations.resolveTslibEsm() : null
|
|
521
|
+
].filter(import_es_toolkit3.isNotNil),
|
|
522
|
+
protocols: {
|
|
523
|
+
"granite-require": graniteRequireProtocol
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function getCommonServicePreset(context, { optimization = DEFAULT_OPTIMIZATION_OPTIONS }) {
|
|
528
|
+
const { dev } = context;
|
|
529
|
+
const buildNumber = getBuildNumber();
|
|
530
|
+
return {
|
|
531
|
+
resolver: commonResolver({ optimization }),
|
|
532
|
+
esbuild: {
|
|
533
|
+
/**
|
|
534
|
+
* React Native 및 Granite 앱에서 사용되는 전역변수
|
|
535
|
+
*/
|
|
536
|
+
define: getGlobalVariables({ dev }),
|
|
537
|
+
banner: {
|
|
538
|
+
js: [
|
|
539
|
+
preludePresets.graniteAppEnvironment({
|
|
540
|
+
appName: context.appName,
|
|
541
|
+
scheme: context.scheme,
|
|
542
|
+
buildNumber
|
|
543
|
+
}),
|
|
544
|
+
// symbol-asynciterator polyfill (ES5)
|
|
545
|
+
`(function(){if(typeof Symbol!=="undefined"&&!Symbol.asyncIterator){Symbol.asyncIterator=Symbol.for("@@asyncIterator")}})();`
|
|
546
|
+
].join("\n")
|
|
547
|
+
}
|
|
548
|
+
},
|
|
549
|
+
babel: {
|
|
550
|
+
conditions: babelConditions
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
var service = (options = {}) => (context) => {
|
|
555
|
+
return getCommonServicePreset(context, options);
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
// src/presets/shared.ts
|
|
559
|
+
function getCommonSharedPreset(context) {
|
|
560
|
+
const { rootDir, dev } = context;
|
|
561
|
+
const buildNumber = getBuildNumber();
|
|
562
|
+
return {
|
|
563
|
+
esbuild: {
|
|
564
|
+
define: getGlobalVariables({ dev }),
|
|
565
|
+
banner: {
|
|
566
|
+
js: [preludePresets.globalVariables({ dev }), preludePresets.graniteSharedEnvironment({ buildNumber })].join(
|
|
567
|
+
"\n"
|
|
568
|
+
)
|
|
569
|
+
},
|
|
570
|
+
prelude: [...getReactNativeSetupScripts(rootDir)]
|
|
571
|
+
},
|
|
572
|
+
babel: {
|
|
573
|
+
conditions: babelConditions
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
var shared = () => (context) => {
|
|
578
|
+
return getCommonSharedPreset(context);
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
// src/config/defineConfig.ts
|
|
582
|
+
var graniteConfigSchema = import_zod.z.object({
|
|
583
|
+
appName: import_zod.z.string(),
|
|
584
|
+
scheme: import_zod.z.string(),
|
|
585
|
+
plugins: import_zod.z.custom(),
|
|
586
|
+
outdir: import_zod.z.string().default("dist"),
|
|
587
|
+
entryFile: import_zod.z.string().default("./src/_app.tsx"),
|
|
588
|
+
cwd: import_zod.z.string().default(process.cwd()),
|
|
589
|
+
mpack: import_plugin_core4.mpackConfigScheme.optional(),
|
|
590
|
+
babel: import_mpack2.babelConfigSchema.optional(),
|
|
591
|
+
esbuild: import_mpack2.esbuildConfigSchema.optional(),
|
|
592
|
+
metro: import_zod.z.custom().optional(),
|
|
593
|
+
INTERNAL__useSharedPreset: import_zod.z.boolean().optional()
|
|
594
|
+
});
|
|
595
|
+
var defineConfig = async (config) => {
|
|
596
|
+
const parsedConfig = graniteConfigSchema.parse(config);
|
|
597
|
+
const appName = parsedConfig.appName;
|
|
598
|
+
const scheme = parsedConfig.scheme;
|
|
599
|
+
const outdir = import_path4.default.join((0, import_utils2.getPackageRoot)(), parsedConfig.outdir);
|
|
600
|
+
const entryFile = parsedConfig.entryFile;
|
|
601
|
+
const plugins = await (0, import_plugin_core4.flattenPlugins)(parsedConfig.plugins);
|
|
602
|
+
const mergedConfig = await mergeConfigFromPlugins(plugins);
|
|
603
|
+
const mergedTransform = await mergeTransformFromPlugins(plugins);
|
|
604
|
+
const esbuild = mergedConfig?.esbuild ? (0, import_es_toolkit4.merge)(mergedConfig.esbuild, parsedConfig?.esbuild ?? {}) : void 0;
|
|
605
|
+
const metro = mergedConfig?.metro ? (0, import_es_toolkit4.merge)(mergedConfig.metro, parsedConfig?.metro ?? {}) : void 0;
|
|
606
|
+
const babel = mergedConfig?.babel ? (0, import_es_toolkit4.merge)(mergedConfig.babel, parsedConfig?.babel ?? {}) : void 0;
|
|
607
|
+
const mpackDevServer = mergedConfig?.mpack?.devServer ? (0, import_es_toolkit4.merge)(mergedConfig?.mpack?.devServer ?? {}, parsedConfig?.mpack?.devServer ?? {}) : void 0;
|
|
608
|
+
const buildPreset = config.INTERNAL__useSharedPreset ? shared : service;
|
|
609
|
+
const createTask = (platform) => ({
|
|
610
|
+
tag: `${appName}-${platform}`,
|
|
611
|
+
presets: [buildPreset()],
|
|
612
|
+
build: {
|
|
613
|
+
esbuild,
|
|
614
|
+
babel,
|
|
615
|
+
platform,
|
|
616
|
+
entry: entryFile,
|
|
617
|
+
outfile: import_path4.default.join(outdir, `bundle.${platform}.js`),
|
|
618
|
+
transformSync: mergedTransform?.transformSync,
|
|
619
|
+
transformAsync: mergedTransform?.transformAsync
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
return {
|
|
623
|
+
...parsedConfig,
|
|
624
|
+
outdir,
|
|
625
|
+
mpack: {
|
|
626
|
+
devServer: {
|
|
627
|
+
middlewares: mpackDevServer?.middlewares ?? [],
|
|
628
|
+
config: {
|
|
629
|
+
appName,
|
|
630
|
+
scheme,
|
|
631
|
+
services: {
|
|
632
|
+
/* TODO: Plugin 구조로 변경 필요 */
|
|
633
|
+
sentry: {
|
|
634
|
+
enabled: false
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
devServer: {
|
|
638
|
+
presets: [buildPreset()],
|
|
639
|
+
build: {
|
|
640
|
+
entry: entryFile
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
tasks: []
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
build: {
|
|
647
|
+
config: {
|
|
648
|
+
appName,
|
|
649
|
+
scheme,
|
|
650
|
+
services: {
|
|
651
|
+
/* TODO: Plugin 구조로 변경 필요 */
|
|
652
|
+
sentry: {
|
|
653
|
+
enabled: false
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
concurrency: 2,
|
|
657
|
+
tasks: [createTask("ios"), createTask("android")]
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
metro: {
|
|
662
|
+
...metro,
|
|
663
|
+
babelConfig: babel,
|
|
664
|
+
transformSync: mergedTransform?.transformSync
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
// src/index.ts
|
|
670
|
+
var cli = new import_clipanion4.Cli({
|
|
671
|
+
binaryLabel: "granite",
|
|
672
|
+
binaryName: "granite",
|
|
673
|
+
enableCapture: true
|
|
674
|
+
});
|
|
675
|
+
async function initialize() {
|
|
676
|
+
const explorer = await (0, import_cosmiconfig2.cosmiconfig)("mpack");
|
|
677
|
+
const result = await explorer.search(process.cwd());
|
|
678
|
+
cli.register(BuildCommand);
|
|
679
|
+
cli.register(HermesCommand);
|
|
680
|
+
cli.register(DevCommand);
|
|
681
|
+
cli.register(import_clipanion4.Builtins.HelpCommand);
|
|
682
|
+
if (Array.isArray(result?.config?.commands)) {
|
|
683
|
+
(result?.config?.commands).forEach((command) => cli.register(command));
|
|
684
|
+
}
|
|
685
|
+
cli.runExit(process.argv.slice(2));
|
|
686
|
+
}
|
|
687
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
688
|
+
0 && (module.exports = {
|
|
689
|
+
commonResolver,
|
|
690
|
+
defineConfig,
|
|
691
|
+
getGlobalVariables,
|
|
692
|
+
initialize,
|
|
693
|
+
loadConfig,
|
|
694
|
+
service
|
|
695
|
+
});
|