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