zoho-sites-dev-cli 0.0.0-stage → 1.0.0
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/README.md +401 -2
- package/bin/zoho-sites-dev-cli.js +107 -0
- package/config/default.json +18 -0
- package/index.js +6 -0
- package/package.json +55 -4
- package/src/commands/auth.js +136 -0
- package/src/commands/create.js +175 -0
- package/src/commands/deploy.js +380 -0
- package/src/commands/generate-zip.js +49 -0
- package/src/commands/index.js +23 -0
- package/src/commands/init-tailwind.js +57 -0
- package/src/commands/init.js +448 -0
- package/src/commands/install.js +89 -0
- package/src/commands/login.js +248 -0
- package/src/commands/pull.js +41 -0
- package/src/commands/serve.js +162 -0
- package/src/compilers/css-compiler.js +230 -0
- package/src/compilers/css-ignore-loader.js +33 -0
- package/src/compilers/plugins/index.js +12 -0
- package/src/compilers/plugins/root-selector-transform-plugin.js +79 -0
- package/src/compilers/tailwind-compiler.js +102 -0
- package/src/compilers/webpack-compiler.js +512 -0
- package/src/compilers/webpack-config.js +103 -0
- package/src/config/constants.js +125 -0
- package/src/config/environments/audiences.js +19 -0
- package/src/config/environments/index.js +78 -0
- package/src/config/environments/internal.js +72 -0
- package/src/config/environments/production.js +39 -0
- package/src/locales/en.json +216 -0
- package/src/services/devServer.js +489 -0
- package/src/services/oauthService.js +672 -0
- package/src/templates/component/fields.json +6 -0
- package/src/templates/component/index.js +11 -0
- package/src/templates/component/llm.md +9 -0
- package/src/templates/component/meta.json +9 -0
- package/src/templates/component/styles.css +3 -0
- package/src/templates/oauth/complete.html +41 -0
- package/src/templates/oauth/error.html +69 -0
- package/src/templates/oauth/invalid-request.html +58 -0
- package/src/templates/oauth/loading.html +80 -0
- package/src/templates/oauth/not-found.html +33 -0
- package/src/templates/oauth/success.html +70 -0
- package/src/templates/oauth/timeout.html +56 -0
- package/src/templates/oauth/token-error.html +57 -0
- package/src/utils/configUtil.js +66 -0
- package/src/utils/httpUtil.js +351 -0
- package/src/utils/i18n.js +205 -0
- package/src/utils/loadingUtil.js +190 -0
- package/src/utils/secureConfig.js +75 -0
- package/src/utils/secureStorage.js +230 -0
- package/src/utils/templateLoader.js +89 -0
- package/src/utils/urlUtil.js +371 -0
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
const webpack = require("webpack");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const os = require("os");
|
|
5
|
+
const webpackConfig = require("./webpack-config");
|
|
6
|
+
const compileCSS = require("./css-compiler.js");
|
|
7
|
+
const {
|
|
8
|
+
compileTailwindToScopedCSS,
|
|
9
|
+
resolveTailwindConfigPath
|
|
10
|
+
} = require("./tailwind-compiler.js");
|
|
11
|
+
const { WEBPACK_OVERRIDES_FILE, HTTP_PROTOCOL } = require("../config/constants.js");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compiles widget components directly from a real project directory on disk
|
|
15
|
+
* (no in-memory filesystem) — this SDK only ever compiles for local preview
|
|
16
|
+
* (`zs serve`), where component source files already exist on the user's disk.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} rootDir
|
|
19
|
+
* webpack.overrides.js
|
|
20
|
+
* package.json
|
|
21
|
+
* ComponentA/
|
|
22
|
+
* fields.json
|
|
23
|
+
* styles.css
|
|
24
|
+
* index.js
|
|
25
|
+
* meta.json
|
|
26
|
+
* ComponentB/
|
|
27
|
+
* folder/ (optional folder can be in different names too and in multiple levels too
|
|
28
|
+
* layoutA.js
|
|
29
|
+
* layoutB.js
|
|
30
|
+
* ...
|
|
31
|
+
* fields.json
|
|
32
|
+
* styles.css
|
|
33
|
+
* index.js
|
|
34
|
+
* meta.json
|
|
35
|
+
* ...
|
|
36
|
+
* ...
|
|
37
|
+
*
|
|
38
|
+
* @returns {Promise<string>} outDir — a real directory containing:
|
|
39
|
+
* index.js
|
|
40
|
+
* meta.json
|
|
41
|
+
* styles.css
|
|
42
|
+
* ComponentA/
|
|
43
|
+
* fields.json
|
|
44
|
+
* ComponentB/
|
|
45
|
+
* fields.json
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
class WebpackCompiler {
|
|
49
|
+
rootDir = null;
|
|
50
|
+
namespace = "";
|
|
51
|
+
node_modules = null;
|
|
52
|
+
aliasMap = {};
|
|
53
|
+
mode = "development";
|
|
54
|
+
/** @type {string|null} Optional path to project root for Tailwind config and content scanning */
|
|
55
|
+
contentRoot = null;
|
|
56
|
+
/** @type {string|null} Real directory holding the most recent successful build's output */
|
|
57
|
+
outDir = null;
|
|
58
|
+
|
|
59
|
+
constructor(rootDir, namespace, node_modules = null, mode = "development", aliasMap = {}, contentRoot = null) {
|
|
60
|
+
this.rootDir = rootDir ? path.resolve(rootDir) : process.cwd();
|
|
61
|
+
this.namespace = namespace;
|
|
62
|
+
this.node_modules = node_modules;
|
|
63
|
+
this.aliasMap = aliasMap;
|
|
64
|
+
this.mode = mode;
|
|
65
|
+
this.contentRoot = contentRoot ? path.resolve(contentRoot) : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Load webpack.overrides.js from the real project directory, if present
|
|
70
|
+
* @returns {Object} User webpack configuration or empty object if not found
|
|
71
|
+
*/
|
|
72
|
+
loadWebpackConfigOverrides() {
|
|
73
|
+
try {
|
|
74
|
+
const overridesPath = path.join(this.rootDir, WEBPACK_OVERRIDES_FILE);
|
|
75
|
+
if (fs.existsSync(overridesPath)) {
|
|
76
|
+
const webpackConfigContent = fs.readFileSync(overridesPath, "utf8");
|
|
77
|
+
return this.evaluateWebpackConfig(webpackConfigContent);
|
|
78
|
+
}
|
|
79
|
+
return {};
|
|
80
|
+
} catch (error) {
|
|
81
|
+
return {};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Evaluate webpack configuration content
|
|
87
|
+
* @param {string} configContent - The webpack config file content
|
|
88
|
+
* @returns {Object} Evaluated webpack configuration
|
|
89
|
+
*/
|
|
90
|
+
evaluateWebpackConfig(configContent) {
|
|
91
|
+
// Get the project directory path
|
|
92
|
+
const projectDir = process.cwd();
|
|
93
|
+
|
|
94
|
+
const configFunction = new Function(
|
|
95
|
+
"module",
|
|
96
|
+
"exports",
|
|
97
|
+
"require",
|
|
98
|
+
"__dirname",
|
|
99
|
+
"process",
|
|
100
|
+
configContent
|
|
101
|
+
);
|
|
102
|
+
const mockModule = { exports: {} };
|
|
103
|
+
configFunction(mockModule, mockModule.exports, require, projectDir, process);
|
|
104
|
+
let userConfig = mockModule.exports;
|
|
105
|
+
|
|
106
|
+
// If it's a function, call it with mode and context
|
|
107
|
+
if (typeof userConfig === "function") {
|
|
108
|
+
userConfig = userConfig(this.mode, { namespace: this.namespace });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return userConfig;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Merge webpack configurations
|
|
116
|
+
* @param {Object} buildInConfig - Built-in webpack configuration
|
|
117
|
+
* @param {Object} userConfig - User webpack configuration
|
|
118
|
+
* @returns {Object} Merged webpack configuration
|
|
119
|
+
*/
|
|
120
|
+
mergeWebpackConfigs(buildInConfig, userConfig) {
|
|
121
|
+
// Always preserve built-in entry and output, but use constructor mode
|
|
122
|
+
const mergedConfig = {
|
|
123
|
+
...buildInConfig,
|
|
124
|
+
...userConfig,
|
|
125
|
+
mode: this.mode,
|
|
126
|
+
entry: buildInConfig.entry,
|
|
127
|
+
output: buildInConfig.output,
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// Merge module rules
|
|
131
|
+
mergedConfig.module = {
|
|
132
|
+
...buildInConfig.module,
|
|
133
|
+
...(userConfig.module || {}),
|
|
134
|
+
rules: [
|
|
135
|
+
...(buildInConfig.module?.rules || []),
|
|
136
|
+
...(userConfig.module?.rules || []),
|
|
137
|
+
],
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// Merge and validate plugins
|
|
141
|
+
const validUserPlugins = (userConfig.plugins || [])
|
|
142
|
+
.filter((plugin) => plugin && typeof plugin === "object")
|
|
143
|
+
.map((plugin) =>
|
|
144
|
+
!plugin.apply ? { ...plugin, apply: () => {} } : plugin
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
mergedConfig.plugins = [
|
|
148
|
+
...(buildInConfig.plugins || []),
|
|
149
|
+
...validUserPlugins,
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
// Merge resolve with aliasMap and node_modules path
|
|
153
|
+
const baseResolve = {
|
|
154
|
+
...buildInConfig.resolve,
|
|
155
|
+
...(userConfig.resolve || {}),
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Properly merge aliases from all sources
|
|
159
|
+
const mergedAliases = {
|
|
160
|
+
...(buildInConfig.resolve?.alias || {}),
|
|
161
|
+
...(userConfig.resolve?.alias || {}),
|
|
162
|
+
...(this.aliasMap || {}),
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
if (Object.keys(mergedAliases).length > 0) {
|
|
166
|
+
baseResolve.alias = mergedAliases;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Merge modules from all sources
|
|
170
|
+
const mergedModules = [
|
|
171
|
+
...(buildInConfig.resolve?.modules || []),
|
|
172
|
+
...(userConfig.resolve?.modules || []),
|
|
173
|
+
];
|
|
174
|
+
|
|
175
|
+
// Add constructor node_modules path if present
|
|
176
|
+
if (this.node_modules && !mergedModules.includes(this.node_modules)) {
|
|
177
|
+
mergedModules.push(this.node_modules);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (mergedModules.length > 0) {
|
|
181
|
+
baseResolve.modules = mergedModules;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
mergedConfig.resolve = baseResolve;
|
|
185
|
+
|
|
186
|
+
mergedConfig.externals = {
|
|
187
|
+
...buildInConfig.externals,
|
|
188
|
+
...(userConfig.externals || {}),
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// Merge optimization settings
|
|
192
|
+
mergedConfig.optimization = {
|
|
193
|
+
...buildInConfig.optimization,
|
|
194
|
+
...userConfig.optimization,
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
return mergedConfig;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Run webpack compilation
|
|
202
|
+
* @param {Object} config - Webpack configuration
|
|
203
|
+
* @returns {Promise<Object>} Webpack compilation stats
|
|
204
|
+
*/
|
|
205
|
+
runWebpackCompilation(config) {
|
|
206
|
+
return new Promise((resolve, reject) => {
|
|
207
|
+
webpack(config, (err, stats) => {
|
|
208
|
+
if (err) {
|
|
209
|
+
reject(err);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (stats.hasErrors()) {
|
|
214
|
+
const info = stats.toJson();
|
|
215
|
+
const errorMessages = info.errors?.map(e => e.message || e).join('\n') || 'Unknown error';
|
|
216
|
+
reject(new Error(`Webpack compilation failed: ${errorMessages}`));
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
resolve(stats);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Compile components straight from disk using webpack and process additional assets.
|
|
227
|
+
* Each call produces a fresh real output directory; the previous one (if any) is
|
|
228
|
+
* removed once the new build succeeds.
|
|
229
|
+
* @param {string} namespace - Component namespace
|
|
230
|
+
* @returns {Promise<Object>} Compilation result — { outputDir, stats }
|
|
231
|
+
*/
|
|
232
|
+
async compile(namespace) {
|
|
233
|
+
if (namespace && !this.namespace) {
|
|
234
|
+
this.namespace = namespace;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const previousOutDir = this.outDir;
|
|
238
|
+
const newOutDir = fs.mkdtempSync(path.join(os.tmpdir(), "zs-build-"));
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
const entryContent = this.generateEntryContent();
|
|
242
|
+
|
|
243
|
+
const buildInConfig = webpackConfig(
|
|
244
|
+
this.namespace,
|
|
245
|
+
entryContent,
|
|
246
|
+
newOutDir,
|
|
247
|
+
this.rootDir
|
|
248
|
+
);
|
|
249
|
+
const userConfig = this.loadWebpackConfigOverrides();
|
|
250
|
+
const finalConfig = this.mergeWebpackConfigs(buildInConfig, userConfig);
|
|
251
|
+
|
|
252
|
+
const stats = await this.runWebpackCompilation(finalConfig);
|
|
253
|
+
|
|
254
|
+
await Promise.all([
|
|
255
|
+
this.compileComponentStylesToDist(newOutDir),
|
|
256
|
+
this.copyFieldsJsonToDist(newOutDir),
|
|
257
|
+
this.aggregateMetaJsonToDist(newOutDir),
|
|
258
|
+
]);
|
|
259
|
+
|
|
260
|
+
this.outDir = newOutDir;
|
|
261
|
+
|
|
262
|
+
// Clean up the previous build now that the new one is ready
|
|
263
|
+
if (previousOutDir && previousOutDir !== this.outDir) {
|
|
264
|
+
fs.rm(previousOutDir, { recursive: true, force: true }, () => {});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
outputDir: this.outDir,
|
|
269
|
+
stats,
|
|
270
|
+
};
|
|
271
|
+
} catch (error) {
|
|
272
|
+
// Discard the failed build's temp directory; keep serving the previous one
|
|
273
|
+
fs.rm(newOutDir, { recursive: true, force: true }, () => {});
|
|
274
|
+
throw error;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Get all directory names under rootDir that contain a meta.json file
|
|
280
|
+
* @returns {string[]} Array of component directory names
|
|
281
|
+
*/
|
|
282
|
+
getComponentDirectories() {
|
|
283
|
+
try {
|
|
284
|
+
const entries = fs.readdirSync(this.rootDir, { withFileTypes: true });
|
|
285
|
+
return entries
|
|
286
|
+
.filter((entry) => entry.isDirectory())
|
|
287
|
+
.filter((entry) => fs.existsSync(path.join(this.rootDir, entry.name, "meta.json")))
|
|
288
|
+
.map((entry) => entry.name);
|
|
289
|
+
} catch (error) {
|
|
290
|
+
return [];
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Read meta.json content for a component
|
|
296
|
+
* @param {string} dirName - Component directory name
|
|
297
|
+
* @returns {Object} Parsed meta.json content or empty object
|
|
298
|
+
*/
|
|
299
|
+
readComponentMeta(dirName) {
|
|
300
|
+
try {
|
|
301
|
+
const metaPath = path.join(this.rootDir, dirName, "meta.json");
|
|
302
|
+
if (fs.existsSync(metaPath)) {
|
|
303
|
+
const content = fs.readFileSync(metaPath, "utf8");
|
|
304
|
+
return JSON.parse(content);
|
|
305
|
+
}
|
|
306
|
+
} catch (err) {
|
|
307
|
+
// Return empty object on error
|
|
308
|
+
}
|
|
309
|
+
return {};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Generate export object for all components. Components are required by their
|
|
314
|
+
* absolute path on disk, so the generated entry file can live anywhere (a temp
|
|
315
|
+
* build directory) without needing component source files copied alongside it.
|
|
316
|
+
* @param {string[]} componentDirs - Array of component directory names
|
|
317
|
+
* @returns {string} Generated export object
|
|
318
|
+
*/
|
|
319
|
+
generateExportObject(componentDirs) {
|
|
320
|
+
if (componentDirs.length === 0) {
|
|
321
|
+
return "module.exports = {}";
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const namespace = this.namespace;
|
|
325
|
+
const isDevelopment = this.mode === "development";
|
|
326
|
+
|
|
327
|
+
// Generate registration calls for each component with inlined meta.json content
|
|
328
|
+
const registrationCalls = componentDirs
|
|
329
|
+
.map((dirName) => {
|
|
330
|
+
const componentKey = `${namespace}_${dirName}`;
|
|
331
|
+
const meta = this.readComponentMeta(dirName);
|
|
332
|
+
const metaJson = JSON.stringify(meta, null, 2).replace(/\n/g, "\n ");
|
|
333
|
+
const widgetType = meta.type || dirName;
|
|
334
|
+
const componentAbsPath = JSON.stringify(path.join(this.rootDir, dirName));
|
|
335
|
+
const devOnlyProps = isDevelopment
|
|
336
|
+
? `
|
|
337
|
+
fieldsUrl: "${HTTP_PROTOCOL}localhost:8083/${namespace}/__widgets/dist/${widgetType}/fields.json",
|
|
338
|
+
stylesheets: ["${HTTP_PROTOCOL}localhost:8083/${namespace}/__widgets/dist/styles.css"],`
|
|
339
|
+
: "";
|
|
340
|
+
|
|
341
|
+
return `
|
|
342
|
+
(function() {
|
|
343
|
+
var meta = ${metaJson};
|
|
344
|
+
var component = require(${componentAbsPath}).default;
|
|
345
|
+
window.ComponentRegistry.register({
|
|
346
|
+
...meta,
|
|
347
|
+
namespace: "${namespace}",
|
|
348
|
+
component: component,
|
|
349
|
+
componentKey: "${componentKey}",${devOnlyProps}
|
|
350
|
+
});
|
|
351
|
+
})();`;
|
|
352
|
+
})
|
|
353
|
+
.join("\n");
|
|
354
|
+
|
|
355
|
+
return `
|
|
356
|
+
if (typeof window !== 'undefined') {
|
|
357
|
+
${registrationCalls}
|
|
358
|
+
}`;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Generate the entry file content that registers every discovered component
|
|
363
|
+
* @returns {string} Generated entry content
|
|
364
|
+
*/
|
|
365
|
+
generateEntryContent() {
|
|
366
|
+
const componentDirs = this.getComponentDirectories();
|
|
367
|
+
return this.generateExportObject(componentDirs);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Compile all component CSS files and combine into a single styles.css in outDir.
|
|
372
|
+
* If contentRoot is set and tailwind.config.js exists, builds scoped Tailwind CSS and prepends it.
|
|
373
|
+
* @param {string} outDir - Real directory to write styles.css into
|
|
374
|
+
*/
|
|
375
|
+
async compileComponentStylesToDist(outDir) {
|
|
376
|
+
// Set up atob shim if needed
|
|
377
|
+
if (typeof global.atob !== "function") {
|
|
378
|
+
global.atob = (b64) => Buffer.from(b64, "base64").toString("utf8");
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const componentDirs = this.getComponentDirectories();
|
|
382
|
+
const combinedCssChunks = [];
|
|
383
|
+
|
|
384
|
+
// Build scoped Tailwind CSS when enabled (tailwind.config.js in contentRoot)
|
|
385
|
+
if (this.contentRoot && resolveTailwindConfigPath(this.contentRoot)) {
|
|
386
|
+
try {
|
|
387
|
+
const tailwindCss = await compileTailwindToScopedCSS({
|
|
388
|
+
namespace: this.namespace,
|
|
389
|
+
contentRoot: this.contentRoot
|
|
390
|
+
});
|
|
391
|
+
if (tailwindCss) {
|
|
392
|
+
combinedCssChunks.push(tailwindCss);
|
|
393
|
+
}
|
|
394
|
+
} catch (err) {
|
|
395
|
+
// Log but do not fail the build; component CSS still compiles
|
|
396
|
+
console.warn("[WebpackCompiler] Tailwind build failed:", err.message);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Process each component's CSS and collect compiled output
|
|
401
|
+
for (const dirName of componentDirs) {
|
|
402
|
+
const stylesPath = path.join(this.rootDir, dirName, "styles.css");
|
|
403
|
+
if (!fs.existsSync(stylesPath)) continue;
|
|
404
|
+
|
|
405
|
+
try {
|
|
406
|
+
const cssContent = fs.readFileSync(stylesPath, "utf8");
|
|
407
|
+
const base64Css = Buffer.from(cssContent, "utf8").toString("base64");
|
|
408
|
+
const processedCss = await compileCSS(
|
|
409
|
+
base64Css,
|
|
410
|
+
dirName,
|
|
411
|
+
this.namespace
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
if (processedCss && processedCss.trim()) {
|
|
415
|
+
combinedCssChunks.push(processedCss);
|
|
416
|
+
}
|
|
417
|
+
} catch (err) {
|
|
418
|
+
// Skip components with CSS compilation errors
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Write combined CSS to single styles.css file (always create, even if empty)
|
|
423
|
+
const combinedCss = combinedCssChunks.join("\n");
|
|
424
|
+
fs.writeFileSync(path.join(outDir, "styles.css"), combinedCss, "utf8");
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Helper method to process components with their output directories
|
|
429
|
+
* @param {string} outDir - Real base output directory
|
|
430
|
+
* @param {function(string, string): Promise<void>} processor - Function to process each component
|
|
431
|
+
*/
|
|
432
|
+
async processComponentsWithOutputDirs(outDir, processor) {
|
|
433
|
+
const componentDirs = this.getComponentDirectories();
|
|
434
|
+
const namespace = this.namespace;
|
|
435
|
+
|
|
436
|
+
for (const dirName of componentDirs) {
|
|
437
|
+
// Use namespace_componentName format for directory structure
|
|
438
|
+
// This matches the URL pattern: /:namespace/__widgets/dist/:namespace_componentName/fields.json
|
|
439
|
+
const componentOutDir = path.join(outDir, `${namespace}_${dirName}`);
|
|
440
|
+
fs.mkdirSync(componentOutDir, { recursive: true });
|
|
441
|
+
await processor(dirName, componentOutDir);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Copy fields.json from component directories to outDir
|
|
447
|
+
* @param {string} outDir - Real base output directory
|
|
448
|
+
*/
|
|
449
|
+
async copyFieldsJsonToDist(outDir) {
|
|
450
|
+
await this.processComponentsWithOutputDirs(
|
|
451
|
+
outDir,
|
|
452
|
+
async (dirName, componentOutDir) => {
|
|
453
|
+
const fieldsSrc = path.join(this.rootDir, dirName, "fields.json");
|
|
454
|
+
if (!fs.existsSync(fieldsSrc)) {
|
|
455
|
+
console.warn(`[WebpackCompiler] fields.json not found for component: ${dirName} at ${fieldsSrc}`);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
try {
|
|
460
|
+
const fieldsContent = fs.readFileSync(fieldsSrc, "utf8");
|
|
461
|
+
fs.writeFileSync(
|
|
462
|
+
path.join(componentOutDir, "fields.json"),
|
|
463
|
+
fieldsContent,
|
|
464
|
+
"utf8"
|
|
465
|
+
);
|
|
466
|
+
} catch (err) {
|
|
467
|
+
console.error(`[WebpackCompiler] Error copying fields.json for ${dirName}:`, err.message);
|
|
468
|
+
throw err;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Combine all component meta.json files into a single meta.json in outDir
|
|
476
|
+
* @param {string} outDir - Real base output directory
|
|
477
|
+
*/
|
|
478
|
+
async aggregateMetaJsonToDist(outDir) {
|
|
479
|
+
const componentDirs = this.getComponentDirectories();
|
|
480
|
+
const namespace = this.namespace;
|
|
481
|
+
|
|
482
|
+
const metaMap = {};
|
|
483
|
+
|
|
484
|
+
// Collect metadata from each component
|
|
485
|
+
for (const dirName of componentDirs) {
|
|
486
|
+
const metaSrc = path.join(this.rootDir, dirName, "meta.json");
|
|
487
|
+
if (!fs.existsSync(metaSrc)) {
|
|
488
|
+
console.warn(`[WebpackCompiler] meta.json not found for component: ${dirName} at ${metaSrc}`);
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
try {
|
|
492
|
+
const raw = fs.readFileSync(metaSrc, "utf8");
|
|
493
|
+
const parsed = JSON.parse(raw || "{}");
|
|
494
|
+
metaMap[`${namespace}_${dirName}`] = parsed;
|
|
495
|
+
} catch (err) {
|
|
496
|
+
console.error(`[WebpackCompiler] Error reading meta.json for ${dirName}:`, err.message);
|
|
497
|
+
throw err;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Write aggregated metadata
|
|
502
|
+
try {
|
|
503
|
+
const metaJsonContent = JSON.stringify(metaMap, null, 2);
|
|
504
|
+
fs.writeFileSync(path.join(outDir, "meta.json"), metaJsonContent, "utf8");
|
|
505
|
+
} catch (err) {
|
|
506
|
+
console.error(`[WebpackCompiler] Error writing aggregated meta.json:`, err.message);
|
|
507
|
+
throw err;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
module.exports = WebpackCompiler;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build the webpack configuration used to compile widget components directly
|
|
6
|
+
* from real disk (no in-memory filesystem involved).
|
|
7
|
+
*
|
|
8
|
+
* @param {string} namespace - Component namespace
|
|
9
|
+
* @param {string} entryContent - Generated entry file content (registers each component)
|
|
10
|
+
* @param {string} outDir - Real directory to write the generated entry file + webpack bundle into
|
|
11
|
+
* @param {string} rootDir - Real project directory containing component source folders
|
|
12
|
+
* @returns {Object} webpack configuration
|
|
13
|
+
*/
|
|
14
|
+
module.exports = (namespace, entryContent, outDir, rootDir) => {
|
|
15
|
+
const entryPath = path.join(outDir, "_zs_entry.js");
|
|
16
|
+
fs.writeFileSync(entryPath, entryContent, "utf8");
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
entry: { [namespace]: [entryPath] },
|
|
20
|
+
mode: "development",
|
|
21
|
+
output: {
|
|
22
|
+
path: outDir,
|
|
23
|
+
filename: "index.js",
|
|
24
|
+
library: {
|
|
25
|
+
name: `${namespace}Widgets`,
|
|
26
|
+
type: "umd"
|
|
27
|
+
},
|
|
28
|
+
chunkFilename: "[name].umd.js",
|
|
29
|
+
umdNamedDefine: true,
|
|
30
|
+
globalObject: "typeof self !== 'undefined' ? self : this",
|
|
31
|
+
// outDir is a fresh temp directory for every build, and other build
|
|
32
|
+
// artifacts (styles.css, fields.json, meta.json) are written into it
|
|
33
|
+
// after webpack runs — don't let webpack clean those out.
|
|
34
|
+
clean: false
|
|
35
|
+
},
|
|
36
|
+
target: ['web', 'es5'],
|
|
37
|
+
module: {
|
|
38
|
+
rules: [
|
|
39
|
+
{
|
|
40
|
+
test: /\.(js|jsx)$/,
|
|
41
|
+
exclude: /node_modules/,
|
|
42
|
+
use: {
|
|
43
|
+
loader: path.resolve(__dirname, '../../node_modules/babel-loader'),
|
|
44
|
+
options: {
|
|
45
|
+
sourceType: 'module',
|
|
46
|
+
presets: [
|
|
47
|
+
[path.resolve(__dirname, '../../node_modules/@babel/preset-env'), {
|
|
48
|
+
targets: { browsers: ["defaults"] },
|
|
49
|
+
// Convert ESM import/export to CommonJS so Babel helpers
|
|
50
|
+
// (e.g. _extends for JSX spreads) are not injected before
|
|
51
|
+
// import statements — that pattern makes webpack's parser
|
|
52
|
+
// treat the file as a script and fail with
|
|
53
|
+
// "import/export may appear only with sourceType: module".
|
|
54
|
+
modules: "commonjs"
|
|
55
|
+
}],
|
|
56
|
+
[path.resolve(__dirname, '../../node_modules/@babel/preset-react'), { runtime: "classic" }]
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
// Handle CSS imports - CSS is processed separately by PostCSS
|
|
63
|
+
// and combined into dist/styles.css, so webpack should ignore CSS imports
|
|
64
|
+
test: /\.css$/,
|
|
65
|
+
use: [
|
|
66
|
+
{
|
|
67
|
+
loader: path.resolve(__dirname, 'css-ignore-loader.js')
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
resolve: {
|
|
74
|
+
extensions: [".js", ".jsx"],
|
|
75
|
+
modules: [
|
|
76
|
+
path.join(rootDir, 'node_modules'), // Project's node_modules
|
|
77
|
+
path.resolve(__dirname, '../node_modules') // SDK's node_modules
|
|
78
|
+
]
|
|
79
|
+
},
|
|
80
|
+
plugins: [
|
|
81
|
+
],
|
|
82
|
+
externals: {
|
|
83
|
+
react: {
|
|
84
|
+
commonjs: "react",
|
|
85
|
+
commonjs2: "react",
|
|
86
|
+
amd: "react",
|
|
87
|
+
root: "React"
|
|
88
|
+
},
|
|
89
|
+
"react-dom": {
|
|
90
|
+
commonjs: "react-dom",
|
|
91
|
+
commonjs2: "react-dom",
|
|
92
|
+
amd: "react-dom",
|
|
93
|
+
root: "ReactDOM"
|
|
94
|
+
},
|
|
95
|
+
"react-dom/client": {
|
|
96
|
+
commonjs: "react-dom/client",
|
|
97
|
+
commonjs2: "react-dom/client",
|
|
98
|
+
amd: "react-dom/client",
|
|
99
|
+
root: "ReactDOM"
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
};
|