@magmonium/cli 0.7.8
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 +392 -0
- package/bin/README.md +244 -0
- package/bin/cli.js +55 -0
- package/bin/commands/assets-wc.js +249 -0
- package/bin/commands/assets.js +943 -0
- package/bin/commands/build.js +242 -0
- package/bin/commands/clean.js +621 -0
- package/bin/commands/compile-i18n.js +91 -0
- package/bin/commands/config-ci.js +266 -0
- package/bin/commands/configure-wc.js +167 -0
- package/bin/commands/connect.js +188 -0
- package/bin/commands/create-app.js +946 -0
- package/bin/commands/deploy.js +100 -0
- package/bin/commands/fix-i18n.js +40 -0
- package/bin/commands/generate.js +765 -0
- package/bin/commands/init/config-files.js +1114 -0
- package/bin/commands/init/mag-assets.js +541 -0
- package/bin/commands/init/mag-cli-config.js +120 -0
- package/bin/commands/init/monorepo-structure.js +735 -0
- package/bin/commands/init/package-json.js +308 -0
- package/bin/commands/init/project-structure.js +189 -0
- package/bin/commands/init/pwa-files.js +204 -0
- package/bin/commands/init/source-files.js +2596 -0
- package/bin/commands/init.js +591 -0
- package/bin/commands/lib.js +508 -0
- package/bin/commands/patch-wc-env.js +136 -0
- package/bin/commands/refractor.js +51 -0
- package/bin/commands/sanitize-i18n.js +41 -0
- package/bin/commands/translate.js +32 -0
- package/bin/commands/zip.js +540 -0
- package/package.json +53 -0
- package/src/compilers/button-group-compiler.d.ts +18 -0
- package/src/compilers/button-group-compiler.d.ts.map +1 -0
- package/src/compilers/button-group-compiler.js +151 -0
- package/src/compilers/button-group-compiler.js.map +1 -0
- package/src/compilers/copy-compiler.d.ts +14 -0
- package/src/compilers/copy-compiler.d.ts.map +1 -0
- package/src/compilers/copy-compiler.js +170 -0
- package/src/compilers/copy-compiler.js.map +1 -0
- package/src/compilers/field-compiler.d.ts +15 -0
- package/src/compilers/field-compiler.d.ts.map +1 -0
- package/src/compilers/field-compiler.js +153 -0
- package/src/compilers/field-compiler.js.map +1 -0
- package/src/compilers/form-compiler.d.ts +15 -0
- package/src/compilers/form-compiler.d.ts.map +1 -0
- package/src/compilers/form-compiler.js +220 -0
- package/src/compilers/form-compiler.js.map +1 -0
- package/src/compilers/logo-compiler.d.ts +14 -0
- package/src/compilers/logo-compiler.d.ts.map +1 -0
- package/src/compilers/logo-compiler.js +114 -0
- package/src/compilers/logo-compiler.js.map +1 -0
- package/src/compilers/options-compiler.d.ts +46 -0
- package/src/compilers/options-compiler.d.ts.map +1 -0
- package/src/compilers/options-compiler.js +329 -0
- package/src/compilers/options-compiler.js.map +1 -0
- package/src/compilers/pwa-build.d.ts +7 -0
- package/src/compilers/pwa-build.d.ts.map +1 -0
- package/src/compilers/pwa-build.js +62 -0
- package/src/compilers/pwa-build.js.map +1 -0
- package/src/compilers/svg-compiler.d.ts +12 -0
- package/src/compilers/svg-compiler.d.ts.map +1 -0
- package/src/compilers/svg-compiler.js +123 -0
- package/src/compilers/svg-compiler.js.map +1 -0
- package/src/compilers/tabs-compiler.d.ts +25 -0
- package/src/compilers/tabs-compiler.d.ts.map +1 -0
- package/src/compilers/tabs-compiler.js +422 -0
- package/src/compilers/tabs-compiler.js.map +1 -0
- package/src/compilers/theme-compiler.d.ts +14 -0
- package/src/compilers/theme-compiler.d.ts.map +1 -0
- package/src/compilers/theme-compiler.js +144 -0
- package/src/compilers/theme-compiler.js.map +1 -0
- package/src/compilers/translation-compiler.d.ts +17 -0
- package/src/compilers/translation-compiler.d.ts.map +1 -0
- package/src/compilers/translation-compiler.js +232 -0
- package/src/compilers/translation-compiler.js.map +1 -0
- package/src/compilers/web-component/index.d.ts +3 -0
- package/src/compilers/web-component/index.d.ts.map +1 -0
- package/src/compilers/web-component/index.js +6 -0
- package/src/compilers/web-component/index.js.map +1 -0
- package/src/compilers/web-component/lib/builder.d.ts +45 -0
- package/src/compilers/web-component/lib/builder.d.ts.map +1 -0
- package/src/compilers/web-component/lib/builder.js +341 -0
- package/src/compilers/web-component/lib/builder.js.map +1 -0
- package/src/compilers/web-component/lib/class-name.d.ts +4 -0
- package/src/compilers/web-component/lib/class-name.d.ts.map +1 -0
- package/src/compilers/web-component/lib/class-name.js +24 -0
- package/src/compilers/web-component/lib/class-name.js.map +1 -0
- package/src/compilers/web-component/lib/config-data.d.ts +9 -0
- package/src/compilers/web-component/lib/config-data.d.ts.map +1 -0
- package/src/compilers/web-component/lib/config-data.js +14 -0
- package/src/compilers/web-component/lib/config-data.js.map +1 -0
- package/src/compilers/web-component/lib/ng-build.d.ts +8 -0
- package/src/compilers/web-component/lib/ng-build.d.ts.map +1 -0
- package/src/compilers/web-component/lib/ng-build.js +685 -0
- package/src/compilers/web-component/lib/ng-build.js.map +1 -0
- package/src/compilers/web-component/lib/resource-inliner.d.ts +9 -0
- package/src/compilers/web-component/lib/resource-inliner.d.ts.map +1 -0
- package/src/compilers/web-component/lib/resource-inliner.js +355 -0
- package/src/compilers/web-component/lib/resource-inliner.js.map +1 -0
- package/src/compilers/web-component/lib/web-component-name.d.ts +3 -0
- package/src/compilers/web-component/lib/web-component-name.d.ts.map +1 -0
- package/src/compilers/web-component/lib/web-component-name.js +47 -0
- package/src/compilers/web-component/lib/web-component-name.js.map +1 -0
- package/src/compilers/web-component/lib/web-component-wrapper.d.ts +3 -0
- package/src/compilers/web-component/lib/web-component-wrapper.d.ts.map +1 -0
- package/src/compilers/web-component/lib/web-component-wrapper.js +335 -0
- package/src/compilers/web-component/lib/web-component-wrapper.js.map +1 -0
- package/src/compilers/web-component/model/type.d.ts +29 -0
- package/src/compilers/web-component/model/type.d.ts.map +1 -0
- package/src/compilers/web-component/model/type.js +3 -0
- package/src/compilers/web-component/model/type.js.map +1 -0
- package/src/compilers/worker-compiler.d.ts +12 -0
- package/src/compilers/worker-compiler.d.ts.map +1 -0
- package/src/compilers/worker-compiler.js +123 -0
- package/src/compilers/worker-compiler.js.map +1 -0
- package/src/compilers/yaml-compiler.d.ts +18 -0
- package/src/compilers/yaml-compiler.d.ts.map +1 -0
- package/src/compilers/yaml-compiler.js +139 -0
- package/src/compilers/yaml-compiler.js.map +1 -0
- package/src/core/base-compiler.d.ts +17 -0
- package/src/core/base-compiler.d.ts.map +1 -0
- package/src/core/base-compiler.js +78 -0
- package/src/core/base-compiler.js.map +1 -0
- package/src/core/config.d.ts +640 -0
- package/src/core/config.d.ts.map +1 -0
- package/src/core/config.js +432 -0
- package/src/core/config.js.map +1 -0
- package/src/core/file-watcher.d.ts +23 -0
- package/src/core/file-watcher.d.ts.map +1 -0
- package/src/core/file-watcher.js +63 -0
- package/src/core/file-watcher.js.map +1 -0
- package/src/index.d.ts +31 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +34 -0
- package/src/index.js.map +1 -0
- package/src/types/asset-configs.d.ts +189 -0
- package/src/types/asset-configs.d.ts.map +1 -0
- package/src/types/asset-configs.js +7 -0
- package/src/types/asset-configs.js.map +1 -0
- package/src/types/theme.d.ts +67 -0
- package/src/types/theme.d.ts.map +1 -0
- package/src/types/theme.js +3 -0
- package/src/types/theme.js.map +1 -0
- package/src/utils/file-utils.d.ts +16 -0
- package/src/utils/file-utils.d.ts.map +1 -0
- package/src/utils/file-utils.js +58 -0
- package/src/utils/file-utils.js.map +1 -0
- package/src/utils/i18n-compile-validator.d.ts +20 -0
- package/src/utils/i18n-compile-validator.d.ts.map +1 -0
- package/src/utils/i18n-compile-validator.js +198 -0
- package/src/utils/i18n-compile-validator.js.map +1 -0
- package/src/utils/i18n-fixer.d.ts +39 -0
- package/src/utils/i18n-fixer.d.ts.map +1 -0
- package/src/utils/i18n-fixer.js +396 -0
- package/src/utils/i18n-fixer.js.map +1 -0
- package/src/utils/i18n-sanitizer.d.ts +17 -0
- package/src/utils/i18n-sanitizer.d.ts.map +1 -0
- package/src/utils/i18n-sanitizer.js +101 -0
- package/src/utils/i18n-sanitizer.js.map +1 -0
- package/src/utils/i18n-utils.d.ts +21 -0
- package/src/utils/i18n-utils.d.ts.map +1 -0
- package/src/utils/i18n-utils.js +89 -0
- package/src/utils/i18n-utils.js.map +1 -0
- package/src/utils/interactive-translator.d.ts +18 -0
- package/src/utils/interactive-translator.d.ts.map +1 -0
- package/src/utils/interactive-translator.js +401 -0
- package/src/utils/interactive-translator.js.map +1 -0
- package/src/utils/llama-cpp-translator.d.ts +33 -0
- package/src/utils/llama-cpp-translator.d.ts.map +1 -0
- package/src/utils/llama-cpp-translator.js +267 -0
- package/src/utils/llama-cpp-translator.js.map +1 -0
- package/src/utils/logger.d.ts +14 -0
- package/src/utils/logger.d.ts.map +1 -0
- package/src/utils/logger.js +42 -0
- package/src/utils/logger.js.map +1 -0
- package/src/utils/ollama-translator.d.ts +38 -0
- package/src/utils/ollama-translator.d.ts.map +1 -0
- package/src/utils/ollama-translator.js +371 -0
- package/src/utils/ollama-translator.js.map +1 -0
- package/src/utils/option-utils.d.ts +58 -0
- package/src/utils/option-utils.d.ts.map +1 -0
- package/src/utils/option-utils.js +123 -0
- package/src/utils/option-utils.js.map +1 -0
- package/src/utils/platform-utils.d.ts +33 -0
- package/src/utils/platform-utils.d.ts.map +1 -0
- package/src/utils/platform-utils.js +114 -0
- package/src/utils/platform-utils.js.map +1 -0
- package/src/utils/pwa-utils.d.ts +28 -0
- package/src/utils/pwa-utils.d.ts.map +1 -0
- package/src/utils/pwa-utils.js +48 -0
- package/src/utils/pwa-utils.js.map +1 -0
- package/src/utils/translation-manager.d.ts +43 -0
- package/src/utils/translation-manager.d.ts.map +1 -0
- package/src/utils/translation-manager.js +302 -0
- package/src/utils/translation-manager.js.map +1 -0
- package/src/utils/web-translation-providers.d.ts +53 -0
- package/src/utils/web-translation-providers.d.ts.map +1 -0
- package/src/utils/web-translation-providers.js +190 -0
- package/src/utils/web-translation-providers.js.map +1 -0
- package/src/utils/yaml-utils.d.ts +9 -0
- package/src/utils/yaml-utils.d.ts.map +1 -0
- package/src/utils/yaml-utils.js +26 -0
- package/src/utils/yaml-utils.js.map +1 -0
- package/templates/mag-compile.config.js +77 -0
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildWebComponent = buildWebComponent;
|
|
4
|
+
exports.createDirectoryIfNotExists = createDirectoryIfNotExists;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const esbuild_1 = require("esbuild");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const fsExtra = tslib_1.__importStar(require("fs-extra"));
|
|
10
|
+
const child_process_1 = require("child_process");
|
|
11
|
+
const resource_inliner_1 = require("./resource-inliner");
|
|
12
|
+
function findWorkspaceRoot(startPath) {
|
|
13
|
+
let current = (0, path_1.resolve)(startPath);
|
|
14
|
+
for (let i = 0; i < 10; i++) {
|
|
15
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(current, 'nx.json')) ||
|
|
16
|
+
(0, fs_1.existsSync)((0, path_1.join)(current, 'tsconfig.base.json'))) {
|
|
17
|
+
return current;
|
|
18
|
+
}
|
|
19
|
+
const parent = (0, path_1.dirname)(current);
|
|
20
|
+
if (parent === current)
|
|
21
|
+
break;
|
|
22
|
+
current = parent;
|
|
23
|
+
}
|
|
24
|
+
return process.cwd();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Whether the root we resolved is an Nx workspace.
|
|
28
|
+
*
|
|
29
|
+
* An app scaffolded by the m-one-ui editor is a standalone Angular CLI
|
|
30
|
+
* workspace: there is no `nx.json`, no `libs/one` to stage from, and no
|
|
31
|
+
* `nx build one` to run. Its `@magmonium/*` dependencies arrive from
|
|
32
|
+
* `node_modules` already compiled, so every step that exists to turn workspace
|
|
33
|
+
* *sources* into something compilable is skipped rather than attempted and
|
|
34
|
+
* caught.
|
|
35
|
+
*/
|
|
36
|
+
function isNxWorkspace(workspaceRoot) {
|
|
37
|
+
return (0, fs_1.existsSync)((0, path_1.join)(workspaceRoot, 'nx.json'));
|
|
38
|
+
}
|
|
39
|
+
function detectPackageManager(workspaceRoot) {
|
|
40
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(workspaceRoot, 'yarn.lock')))
|
|
41
|
+
return 'yarn';
|
|
42
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(workspaceRoot, 'pnpm-lock.yaml')))
|
|
43
|
+
return 'pnpm';
|
|
44
|
+
return 'npm';
|
|
45
|
+
}
|
|
46
|
+
function getExecutor(_pm) {
|
|
47
|
+
return 'npx';
|
|
48
|
+
}
|
|
49
|
+
function createTempTsConfig(stagingProjectSrc, dependencies, outputDir, workspaceRoot, stagedLibs = {}) {
|
|
50
|
+
const tempConfigPath = (0, path_1.resolve)(workspaceRoot, `tsconfig.wc.${Date.now()}.json`);
|
|
51
|
+
const relativeStagingPath = (0, path_1.relative)(workspaceRoot, stagingProjectSrc);
|
|
52
|
+
// Get current project paths from base tsconfig
|
|
53
|
+
const baseTsConfigPath = [
|
|
54
|
+
(0, path_1.join)(workspaceRoot, 'tsconfig.base.json'),
|
|
55
|
+
(0, path_1.join)(workspaceRoot, 'tsconfig.json'),
|
|
56
|
+
].find((p) => (0, fs_1.existsSync)(p));
|
|
57
|
+
const baseTsConfig = baseTsConfigPath
|
|
58
|
+
? JSON.parse((0, fs_1.readFileSync)(baseTsConfigPath, 'utf8'))
|
|
59
|
+
: { compilerOptions: { paths: {} } };
|
|
60
|
+
const basePaths = baseTsConfig.compilerOptions?.paths || {};
|
|
61
|
+
// Determine staging root relative to workspace root
|
|
62
|
+
const stagingRoot = (0, path_1.resolve)(workspaceRoot, '.tmp-staging-wc');
|
|
63
|
+
const relativeStagingRoot = (0, path_1.relative)(workspaceRoot, stagingRoot);
|
|
64
|
+
const tsConfig = {
|
|
65
|
+
// DO NOT EXTEND - we want total control to avoid ngc finding original files
|
|
66
|
+
compilerOptions: {
|
|
67
|
+
outDir: outputDir,
|
|
68
|
+
baseUrl: '.',
|
|
69
|
+
module: 'esnext',
|
|
70
|
+
target: 'es2020',
|
|
71
|
+
lib: ['es2020', 'dom'],
|
|
72
|
+
skipLibCheck: true,
|
|
73
|
+
moduleResolution: 'bundler',
|
|
74
|
+
esModuleInterop: true,
|
|
75
|
+
experimentalDecorators: true,
|
|
76
|
+
emitDecoratorMetadata: true,
|
|
77
|
+
resolveJsonModule: true,
|
|
78
|
+
declaration: false,
|
|
79
|
+
paths: {
|
|
80
|
+
...basePaths,
|
|
81
|
+
...stagedLibs,
|
|
82
|
+
...dependencies.reduce((acc, dep) => {
|
|
83
|
+
// Only use dist/libs if not already in stagedLibs
|
|
84
|
+
if (stagedLibs[dep])
|
|
85
|
+
return acc;
|
|
86
|
+
const distPath = (0, path_1.join)('dist/libs', dep.replace('@magmonium/', ''));
|
|
87
|
+
if ((0, fs_1.existsSync)((0, path_1.resolve)(workspaceRoot, distPath))) {
|
|
88
|
+
acc[dep] = [distPath];
|
|
89
|
+
return acc;
|
|
90
|
+
}
|
|
91
|
+
// Standalone: no workspace source to stage and no dist to point
|
|
92
|
+
// at, so the published package is the only copy there is. Mapped
|
|
93
|
+
// explicitly rather than left to node resolution — `paths` and
|
|
94
|
+
// `moduleResolution: bundler` interact badly enough that an
|
|
95
|
+
// omission here reads as "not found" rather than as "fall through".
|
|
96
|
+
const modulePath = (0, path_1.join)('node_modules', dep);
|
|
97
|
+
if ((0, fs_1.existsSync)((0, path_1.resolve)(workspaceRoot, modulePath))) {
|
|
98
|
+
acc[dep] = [modulePath];
|
|
99
|
+
}
|
|
100
|
+
return acc;
|
|
101
|
+
}, {}),
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
angularCompilerOptions: {
|
|
105
|
+
enableIvy: true,
|
|
106
|
+
compilationMode: 'full',
|
|
107
|
+
enableResourceInlining: false,
|
|
108
|
+
inlineStyleLanguage: 'css',
|
|
109
|
+
},
|
|
110
|
+
include: [
|
|
111
|
+
`${relativeStagingPath}/**/*.ts`,
|
|
112
|
+
`${relativeStagingRoot}/libs/**/*.ts`,
|
|
113
|
+
],
|
|
114
|
+
exclude: ['node_modules', '**/*.spec.ts'],
|
|
115
|
+
};
|
|
116
|
+
(0, fs_1.writeFileSync)(tempConfigPath, JSON.stringify(tsConfig, null, 2));
|
|
117
|
+
console.log(` ✅ Isolated tsconfig created: ${relativeStagingPath}`);
|
|
118
|
+
return tempConfigPath;
|
|
119
|
+
}
|
|
120
|
+
function compileWithNgc(tsConfigPath, workspaceRoot, pm) {
|
|
121
|
+
console.log(` 🔨 Compiling with ngc...`);
|
|
122
|
+
const executor = getExecutor(pm);
|
|
123
|
+
const configName = (0, path_1.basename)(tsConfigPath);
|
|
124
|
+
try {
|
|
125
|
+
(0, child_process_1.execSync)(`${executor} ngc -p "${configName}"`, {
|
|
126
|
+
cwd: workspaceRoot,
|
|
127
|
+
encoding: 'utf8',
|
|
128
|
+
stdio: 'pipe',
|
|
129
|
+
});
|
|
130
|
+
console.log(` ✅ Compilation complete`);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
const stderr = error instanceof Error && 'stderr' in error
|
|
134
|
+
? error.stderr?.toString()
|
|
135
|
+
: String(error);
|
|
136
|
+
throw new Error(`ngc failed: ${stderr}`);
|
|
137
|
+
}
|
|
138
|
+
const config = JSON.parse((0, fs_1.readFileSync)(tsConfigPath, 'utf8'));
|
|
139
|
+
return (0, path_1.resolve)(workspaceRoot, config.compilerOptions.outDir);
|
|
140
|
+
}
|
|
141
|
+
function createCompilerShim(compiledWrapperPath) {
|
|
142
|
+
const shimPath = compiledWrapperPath.replace('.js', '.shim.js');
|
|
143
|
+
const relativeWrapperPath = './' + (0, path_1.basename)(compiledWrapperPath);
|
|
144
|
+
const shimContent = `import '${relativeWrapperPath}';\n`;
|
|
145
|
+
(0, fs_1.writeFileSync)(shimPath, shimContent, 'utf8');
|
|
146
|
+
console.log(` ✅ Compiler shim created`);
|
|
147
|
+
return shimPath;
|
|
148
|
+
}
|
|
149
|
+
function applyEnvironmentOverride(dir, buildConfig) {
|
|
150
|
+
const entries = (0, fs_1.readdirSync)(dir);
|
|
151
|
+
for (const entry of entries) {
|
|
152
|
+
const fullPath = (0, path_1.join)(dir, entry);
|
|
153
|
+
if ((0, fs_1.statSync)(fullPath).isDirectory()) {
|
|
154
|
+
applyEnvironmentOverride(fullPath, buildConfig);
|
|
155
|
+
}
|
|
156
|
+
else if (entry === 'environment.ts') {
|
|
157
|
+
const overridePath = (0, path_1.join)(dir, `environment.${buildConfig}.ts`);
|
|
158
|
+
if ((0, fs_1.existsSync)(overridePath)) {
|
|
159
|
+
(0, fs_1.writeFileSync)(fullPath, (0, fs_1.readFileSync)(overridePath, 'utf8'));
|
|
160
|
+
console.log(` 🌍 environment.ts → environment.${buildConfig}.ts`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function cleanupTempFiles(tsConfigPath, compiledDir, shimPath) {
|
|
166
|
+
try {
|
|
167
|
+
const filesToClean = [
|
|
168
|
+
tsConfigPath,
|
|
169
|
+
tsConfigPath.replace('.json', '.tsbuildinfo'),
|
|
170
|
+
];
|
|
171
|
+
if (shimPath)
|
|
172
|
+
filesToClean.push(shimPath);
|
|
173
|
+
for (const file of filesToClean) {
|
|
174
|
+
if ((0, fs_1.existsSync)(file))
|
|
175
|
+
(0, fs_1.rmSync)(file, { force: true });
|
|
176
|
+
}
|
|
177
|
+
if ((0, fs_1.existsSync)(compiledDir)) {
|
|
178
|
+
(0, fs_1.rmSync)(compiledDir, { recursive: true, force: true });
|
|
179
|
+
}
|
|
180
|
+
console.log(` 🧹 Cleaned up`);
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
console.error(`cleanupTempFiles failed: ${error}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function findCompiledWrapper(compiledOutputDir, relativeWrapper) {
|
|
187
|
+
const compiledWrapperPath = (0, path_1.join)(compiledOutputDir, relativeWrapper);
|
|
188
|
+
if ((0, fs_1.existsSync)(compiledWrapperPath))
|
|
189
|
+
return compiledWrapperPath;
|
|
190
|
+
const findJsFiles = (dir, files = []) => {
|
|
191
|
+
try {
|
|
192
|
+
for (const file of (0, fs_1.readdirSync)(dir)) {
|
|
193
|
+
const fullPath = (0, path_1.join)(dir, file);
|
|
194
|
+
if ((0, fs_1.statSync)(fullPath).isDirectory()) {
|
|
195
|
+
findJsFiles(fullPath, files);
|
|
196
|
+
}
|
|
197
|
+
else if (file.endsWith('.js')) {
|
|
198
|
+
files.push((0, path_1.relative)(compiledOutputDir, fullPath));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
// ignore unreadable directories
|
|
204
|
+
}
|
|
205
|
+
return files;
|
|
206
|
+
};
|
|
207
|
+
const allJsFiles = findJsFiles(compiledOutputDir);
|
|
208
|
+
const wrapperBase = (0, path_1.basename)(relativeWrapper);
|
|
209
|
+
const match = allJsFiles.find((f) => (0, path_1.basename)(f) === wrapperBase);
|
|
210
|
+
if (match)
|
|
211
|
+
return (0, path_1.join)(compiledOutputDir, match);
|
|
212
|
+
throw new Error(`Could not find compiled wrapper: ${relativeWrapper}`);
|
|
213
|
+
}
|
|
214
|
+
async function buildWebComponent(config, wrapperPath, outputFile, options = false) {
|
|
215
|
+
let isDev;
|
|
216
|
+
let aotOptions;
|
|
217
|
+
if (typeof options === 'boolean') {
|
|
218
|
+
isDev = options;
|
|
219
|
+
aotOptions = { enableOptimization: !isDev };
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
isDev = !options.enableOptimization;
|
|
223
|
+
aotOptions = options;
|
|
224
|
+
}
|
|
225
|
+
const componentDir = (0, path_1.dirname)(wrapperPath);
|
|
226
|
+
const workspaceRoot = findWorkspaceRoot(componentDir);
|
|
227
|
+
const pm = detectPackageManager(workspaceRoot);
|
|
228
|
+
const skipCleanup = process.env['SKIP_WC_CLEANUP'] === '1';
|
|
229
|
+
// When debugging cyclic inheritance, force unminified output so minified names
|
|
230
|
+
// like 'im'/'jt' become real class names that can be traced.
|
|
231
|
+
const debugBundle = skipCleanup;
|
|
232
|
+
const stagingRoot = (0, path_1.resolve)(workspaceRoot, '.tmp-staging-wc');
|
|
233
|
+
const compiledDir = (0, path_1.resolve)(workspaceRoot, `.tmp-compiled-wc/${config.componentName}`);
|
|
234
|
+
let tsConfigPath = null;
|
|
235
|
+
let compiledOutputDir = null;
|
|
236
|
+
let shimPath = null;
|
|
237
|
+
const extraIncludePaths = [];
|
|
238
|
+
try {
|
|
239
|
+
console.log(`\n📦 STEP 1: Dependencies`);
|
|
240
|
+
console.log(`${'='.repeat(60)}`);
|
|
241
|
+
const projectSrcDir = componentDir.includes('/src')
|
|
242
|
+
? componentDir.split('/src')[0] + '/src'
|
|
243
|
+
: componentDir;
|
|
244
|
+
const scanDirs = [projectSrcDir];
|
|
245
|
+
if (config.appConfigPath) {
|
|
246
|
+
const configDir = (0, path_1.dirname)((0, path_1.resolve)(workspaceRoot, config.appConfigPath));
|
|
247
|
+
if ((0, fs_1.existsSync)(configDir) && !scanDirs.includes(configDir)) {
|
|
248
|
+
scanDirs.push(configDir);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const dependencies = new Set();
|
|
252
|
+
for (const dir of scanDirs) {
|
|
253
|
+
extractLibraryDependencies(dir, workspaceRoot).forEach((dep) => dependencies.add(dep));
|
|
254
|
+
}
|
|
255
|
+
const depArray = Array.from(dependencies);
|
|
256
|
+
buildLibraryDependencies(depArray, workspaceRoot, pm);
|
|
257
|
+
console.log(`\n📝 STEP 1.5: Staging & Resource Inlining`);
|
|
258
|
+
console.log(`${'='.repeat(60)}`);
|
|
259
|
+
const relativeProjectSrc = (0, path_1.relative)(workspaceRoot, projectSrcDir);
|
|
260
|
+
const stagingProjectSrc = (0, path_1.join)(stagingRoot, relativeProjectSrc);
|
|
261
|
+
if ((0, fs_1.existsSync)(stagingProjectSrc)) {
|
|
262
|
+
(0, fs_1.rmSync)(stagingProjectSrc, { recursive: true, force: true });
|
|
263
|
+
}
|
|
264
|
+
(0, fs_1.mkdirSync)(stagingProjectSrc, { recursive: true });
|
|
265
|
+
console.log(` 📂 Staging project: ${relativeProjectSrc} → .tmp-staging-wc/${relativeProjectSrc}`);
|
|
266
|
+
fsExtra.copySync(projectSrcDir, stagingProjectSrc);
|
|
267
|
+
// Replace environment.ts with env-specific version when BUILD_CONFIG is set
|
|
268
|
+
const buildConfig = process.env['BUILD_CONFIG'];
|
|
269
|
+
if (buildConfig && buildConfig !== 'development') {
|
|
270
|
+
applyEnvironmentOverride(stagingProjectSrc, buildConfig);
|
|
271
|
+
}
|
|
272
|
+
// Stage library sources
|
|
273
|
+
const stagedLibs = {};
|
|
274
|
+
const stagingLibsDir = (0, path_1.join)(stagingRoot, 'libs');
|
|
275
|
+
if (!(0, fs_1.existsSync)(stagingLibsDir))
|
|
276
|
+
(0, fs_1.mkdirSync)(stagingLibsDir, { recursive: true });
|
|
277
|
+
// Always stage libs/one/src/lib/shared/sass if it exists, as it's often a style dependency
|
|
278
|
+
const sassSourceDir = (0, path_1.resolve)(workspaceRoot, 'libs/one/src/lib/shared/sass');
|
|
279
|
+
if ((0, fs_1.existsSync)(sassSourceDir)) {
|
|
280
|
+
const stagingSassPath = (0, path_1.join)(stagingLibsDir, 'one/src/lib/shared/sass');
|
|
281
|
+
if (!(0, fs_1.existsSync)(stagingSassPath)) {
|
|
282
|
+
(0, fs_1.mkdirSync)((0, path_1.dirname)(stagingSassPath), { recursive: true });
|
|
283
|
+
console.log(` 📂 Staging utility library: libs/one/src/lib/shared/sass → .tmp-staging-wc/libs/one/src/lib/shared/sass`);
|
|
284
|
+
fsExtra.copySync(sassSourceDir, stagingSassPath);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
for (const dep of depArray) {
|
|
288
|
+
if (dep.startsWith('@magmonium/')) {
|
|
289
|
+
const libName = dep.replace('@magmonium/', '');
|
|
290
|
+
const libSourceDir = (0, path_1.resolve)(workspaceRoot, 'libs', libName, 'src');
|
|
291
|
+
if ((0, fs_1.existsSync)(libSourceDir)) {
|
|
292
|
+
const stagingLibPath = (0, path_1.join)(stagingLibsDir, libName, 'src');
|
|
293
|
+
if ((0, fs_1.existsSync)(stagingLibPath)) {
|
|
294
|
+
(0, fs_1.rmSync)((0, path_1.dirname)(stagingLibPath), { recursive: true, force: true });
|
|
295
|
+
}
|
|
296
|
+
(0, fs_1.mkdirSync)((0, path_1.dirname)(stagingLibPath), { recursive: true });
|
|
297
|
+
console.log(` 📂 Staging library: libs/${libName}/src → .tmp-staging-wc/libs/${libName}/src`);
|
|
298
|
+
fsExtra.copySync(libSourceDir, stagingLibPath);
|
|
299
|
+
// In the tsconfig paths, we point to the staged index.ts
|
|
300
|
+
stagedLibs[dep] = [
|
|
301
|
+
(0, path_1.relative)(workspaceRoot, (0, path_1.join)(stagingLibPath, 'index.ts')),
|
|
302
|
+
];
|
|
303
|
+
console.log(` 🎨 Inlining library: .tmp-staging-wc/libs/${libName}/src`);
|
|
304
|
+
(0, resource_inliner_1.inlineComponentResources)(stagingLibPath);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const wrapperFileName = (0, path_1.basename)(wrapperPath);
|
|
309
|
+
const relativeWrapperDir = (0, path_1.relative)(projectSrcDir, componentDir);
|
|
310
|
+
const stagingWrapperPath = (0, path_1.join)(stagingProjectSrc, relativeWrapperDir, wrapperFileName);
|
|
311
|
+
if ((0, fs_1.existsSync)(wrapperPath)) {
|
|
312
|
+
fsExtra.copySync(wrapperPath, stagingWrapperPath);
|
|
313
|
+
}
|
|
314
|
+
console.log(` 🎨 Inlining project: .tmp-staging-wc/${relativeProjectSrc}`);
|
|
315
|
+
(0, resource_inliner_1.inlineComponentResources)(stagingProjectSrc, config.componentName);
|
|
316
|
+
console.log(`\n🔨 STEP 2: Compilation`);
|
|
317
|
+
console.log(`${'='.repeat(60)}`);
|
|
318
|
+
tsConfigPath = createTempTsConfig(stagingProjectSrc, depArray, compiledDir, workspaceRoot, stagedLibs);
|
|
319
|
+
compiledOutputDir = compileWithNgc(tsConfigPath, workspaceRoot, pm);
|
|
320
|
+
// POST-COMPILATION FIX: Re-scan compiled JS for any Sass that ngc didn't compile
|
|
321
|
+
// This is necessary because ngc sometimes copies raw metadata properties
|
|
322
|
+
console.log(` 🔧 Post-compilation style check...`);
|
|
323
|
+
if (compiledOutputDir) {
|
|
324
|
+
const outDir = compiledOutputDir;
|
|
325
|
+
const fixCompiledStyles = (dir) => {
|
|
326
|
+
const items = (0, fs_1.readdirSync)(dir);
|
|
327
|
+
for (const item of items) {
|
|
328
|
+
const fullPath = (0, path_1.join)(dir, item);
|
|
329
|
+
if ((0, fs_1.statSync)(fullPath).isDirectory()) {
|
|
330
|
+
fixCompiledStyles(fullPath);
|
|
331
|
+
}
|
|
332
|
+
else if (item.endsWith('.js')) {
|
|
333
|
+
let jsContent = (0, fs_1.readFileSync)(fullPath, 'utf8');
|
|
334
|
+
let jsChanged = false;
|
|
335
|
+
// Match styles: ["..."] in compiled ɵɵdefineComponent
|
|
336
|
+
// Use a more robust pattern that stops only at the true end of the styles array
|
|
337
|
+
const stylesRegex = /styles\s*:\s*\[\s*(['"`])([\s\S]*?)\1\s*\](?=\s*(?:,|\s*\}))/g;
|
|
338
|
+
jsContent = jsContent.replace(stylesRegex, (match, quote, styleContent) => {
|
|
339
|
+
const rawContent = styleContent
|
|
340
|
+
.replace(/\\n/g, '\n')
|
|
341
|
+
.replace(/\\"/g, '"')
|
|
342
|
+
.replace(/\\/g, "'");
|
|
343
|
+
if ((0, resource_inliner_1.isLikelyStyle)(rawContent)) {
|
|
344
|
+
// If it has indentation but no braces, it's Sass
|
|
345
|
+
if (!rawContent.includes('{') && rawContent.includes('\n')) {
|
|
346
|
+
console.log(` 🎨 Compiling missed Sass in ${item} (length: ${rawContent.length})`);
|
|
347
|
+
// Try to find the real source directory
|
|
348
|
+
let sourceDir = (0, path_1.dirname)(fullPath);
|
|
349
|
+
if (fullPath.includes('.tmp-staging-wc')) {
|
|
350
|
+
const relativePath = fullPath
|
|
351
|
+
.split('.tmp-staging-wc')[1]
|
|
352
|
+
.split(/[/\\]/)
|
|
353
|
+
.slice(1)
|
|
354
|
+
.join('/');
|
|
355
|
+
sourceDir = (0, path_1.resolve)(workspaceRoot, (0, path_1.dirname)(relativePath));
|
|
356
|
+
}
|
|
357
|
+
try {
|
|
358
|
+
const css = (0, resource_inliner_1.compileInlinedStyle)(rawContent, sourceDir, 'indented', extraIncludePaths);
|
|
359
|
+
if (css !== rawContent) {
|
|
360
|
+
jsChanged = true;
|
|
361
|
+
// Use JSON.stringify to safely escape the CSS string for JS
|
|
362
|
+
return `styles: [${JSON.stringify(css)}]`;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch (err) {
|
|
366
|
+
console.warn(` ⚠️ Failed to compile Sass in ${item}: ${err}`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return match;
|
|
371
|
+
});
|
|
372
|
+
if (jsChanged) {
|
|
373
|
+
(0, fs_1.writeFileSync)(fullPath, jsContent, 'utf-8');
|
|
374
|
+
console.log(` ✅ Fixed styles in ${(0, path_1.relative)(outDir, fullPath)}`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
fixCompiledStyles(outDir);
|
|
380
|
+
}
|
|
381
|
+
// Account for .tmp-staging-wc prefix in compiled output
|
|
382
|
+
const relativeWrapper = (0, path_1.relative)(workspaceRoot, wrapperPath);
|
|
383
|
+
const compiledWrapperPath = findCompiledWrapper(compiledOutputDir, (0, path_1.join)('.tmp-staging-wc', relativeWrapper.replace('.ts', '.js')));
|
|
384
|
+
console.log(` 🔍 Finding: ${(0, path_1.relative)(compiledOutputDir, compiledWrapperPath)}`);
|
|
385
|
+
console.log(` ✅ Found wrapper`);
|
|
386
|
+
console.log(`\n🔧 STEP 3: Compiler Shim`);
|
|
387
|
+
console.log(`${'='.repeat(60)}`);
|
|
388
|
+
shimPath = createCompilerShim(compiledWrapperPath);
|
|
389
|
+
console.log(`\n📦 STEP 4: Bundling`);
|
|
390
|
+
console.log(`${'='.repeat(60)}`);
|
|
391
|
+
const esbuildAliases = {};
|
|
392
|
+
// Use compiled (and inlined) versions of staged libs
|
|
393
|
+
for (const dep of depArray) {
|
|
394
|
+
if (dep.startsWith('@magmonium/')) {
|
|
395
|
+
const libName = dep.replace('@magmonium/', '');
|
|
396
|
+
// ngc outputs to outDir + .tmp-staging-wc/libs/...
|
|
397
|
+
const compiledLibPath = (0, path_1.join)(compiledOutputDir, '.tmp-staging-wc/libs', libName, 'src');
|
|
398
|
+
if ((0, fs_1.existsSync)(compiledLibPath)) {
|
|
399
|
+
esbuildAliases[dep] = compiledLibPath;
|
|
400
|
+
console.log(` 🔗 Aliased ${dep} → .tmp-compiled-wc/.../libs/${libName}/src`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const distLibsDir = (0, path_1.join)(workspaceRoot, 'dist', 'libs');
|
|
405
|
+
if ((0, fs_1.existsSync)(distLibsDir)) {
|
|
406
|
+
const prebuiltLibs = (0, fs_1.readdirSync)(distLibsDir);
|
|
407
|
+
for (const libName of prebuiltLibs) {
|
|
408
|
+
const distLibPath = (0, path_1.join)(distLibsDir, libName);
|
|
409
|
+
const pkgJsonPath = (0, path_1.join)(distLibPath, 'package.json');
|
|
410
|
+
if ((0, fs_1.existsSync)(pkgJsonPath)) {
|
|
411
|
+
try {
|
|
412
|
+
const pkg = JSON.parse((0, fs_1.readFileSync)(pkgJsonPath, 'utf8'));
|
|
413
|
+
const pkgName = pkg.name;
|
|
414
|
+
if (pkgName && !esbuildAliases[pkgName]) {
|
|
415
|
+
esbuildAliases[pkgName] = distLibPath;
|
|
416
|
+
console.log(` 🔗 Aliased ${pkgName} → dist/libs/${libName}`);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
catch {
|
|
420
|
+
// ignore unreadable package.json
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (!esbuildAliases['@magmonium/one'] &&
|
|
426
|
+
(0, fs_1.existsSync)((0, path_1.join)(distLibsDir, 'one'))) {
|
|
427
|
+
esbuildAliases['@magmonium/one'] = (0, path_1.join)(distLibsDir, 'one');
|
|
428
|
+
}
|
|
429
|
+
if (!esbuildAliases['@magmonium/sass']) {
|
|
430
|
+
const sassDir = (0, path_1.join)(workspaceRoot, 'libs/one/src/lib/shared/sass');
|
|
431
|
+
if ((0, fs_1.existsSync)(sassDir)) {
|
|
432
|
+
esbuildAliases['@magmonium/sass'] = sassDir;
|
|
433
|
+
console.log(` 🔗 Aliased @magmonium/sass → libs/one/src/lib/shared/sass`);
|
|
434
|
+
}
|
|
435
|
+
else if ((0, fs_1.existsSync)((0, path_1.join)(distLibsDir, 'one'))) {
|
|
436
|
+
esbuildAliases['@magmonium/sass'] = (0, path_1.join)(distLibsDir, 'one');
|
|
437
|
+
console.log(` 🔗 Aliased @magmonium/sass → dist/libs/one`);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (!esbuildAliases['@magmonium/cli'] &&
|
|
441
|
+
(0, fs_1.existsSync)((0, path_1.join)(distLibsDir, 'cli'))) {
|
|
442
|
+
esbuildAliases['@magmonium/cli'] = (0, path_1.join)(distLibsDir, 'cli');
|
|
443
|
+
}
|
|
444
|
+
const esbuildResult = await (0, esbuild_1.build)({
|
|
445
|
+
entryPoints: [shimPath],
|
|
446
|
+
bundle: true,
|
|
447
|
+
format: 'esm',
|
|
448
|
+
platform: 'browser',
|
|
449
|
+
target: ['es2020'],
|
|
450
|
+
outfile: outputFile,
|
|
451
|
+
minify: debugBundle ? false : aotOptions.enableOptimization || !isDev,
|
|
452
|
+
sourcemap: isDev || debugBundle,
|
|
453
|
+
metafile: true,
|
|
454
|
+
treeShaking: true,
|
|
455
|
+
resolveExtensions: ['.js', '.mjs'],
|
|
456
|
+
mainFields: ['browser', 'module', 'main'],
|
|
457
|
+
alias: esbuildAliases,
|
|
458
|
+
define: {
|
|
459
|
+
global: 'globalThis',
|
|
460
|
+
ngDevMode: String(isDev),
|
|
461
|
+
ngJitMode: 'false',
|
|
462
|
+
'process.env.NODE_ENV': isDev ? '"development"' : '"production"',
|
|
463
|
+
},
|
|
464
|
+
loader: {
|
|
465
|
+
'.svg': 'text',
|
|
466
|
+
'.png': 'dataurl',
|
|
467
|
+
'.jpg': 'dataurl',
|
|
468
|
+
},
|
|
469
|
+
banner: {
|
|
470
|
+
js: `/* ${config.selector || config.componentName} - ${new Date().toISOString()} */`,
|
|
471
|
+
},
|
|
472
|
+
legalComments: 'none',
|
|
473
|
+
logLevel: 'warning',
|
|
474
|
+
});
|
|
475
|
+
if (esbuildResult.metafile) {
|
|
476
|
+
const metaPath = outputFile.replace(/\.js$/, '.meta.json');
|
|
477
|
+
(0, fs_1.writeFileSync)(metaPath, JSON.stringify(esbuildResult.metafile, null, 2), 'utf8');
|
|
478
|
+
if (skipCleanup)
|
|
479
|
+
console.log(` 📝 Metafile written: ${metaPath}`);
|
|
480
|
+
}
|
|
481
|
+
assertNoCyclicInheritance(outputFile);
|
|
482
|
+
const sizeKB = ((0, fs_1.statSync)(outputFile).size / 1024).toFixed(2);
|
|
483
|
+
console.log(`\n✅ Success!`);
|
|
484
|
+
console.log(`📦 ${outputFile}`);
|
|
485
|
+
console.log(`📊 ${sizeKB}KB`);
|
|
486
|
+
}
|
|
487
|
+
catch (error) {
|
|
488
|
+
console.error(`\n❌ Failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
489
|
+
throw error;
|
|
490
|
+
}
|
|
491
|
+
finally {
|
|
492
|
+
if (tsConfigPath && compiledOutputDir) {
|
|
493
|
+
if (skipCleanup) {
|
|
494
|
+
console.log(`\n🧹 Cleanup skipped (SKIP_WC_CLEANUP=1)`);
|
|
495
|
+
console.log(` Staged sources: ${stagingRoot}`);
|
|
496
|
+
console.log(` Compiled output: ${compiledOutputDir}`);
|
|
497
|
+
console.log(` Bundle: ${outputFile}`);
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
console.log(`\n🧹 Cleanup`);
|
|
501
|
+
cleanupTempFiles(tsConfigPath, compiledOutputDir, shimPath || undefined);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
function extractLibraryDependencies(componentDir, workspaceRoot) {
|
|
507
|
+
const tsConfigPath = [
|
|
508
|
+
(0, path_1.join)(workspaceRoot, 'tsconfig.base.json'),
|
|
509
|
+
(0, path_1.join)(workspaceRoot, 'tsconfig.json'),
|
|
510
|
+
].find((p) => (0, fs_1.existsSync)(p));
|
|
511
|
+
if (!tsConfigPath)
|
|
512
|
+
return [];
|
|
513
|
+
const tsConfig = JSON.parse((0, fs_1.readFileSync)(tsConfigPath, 'utf8'));
|
|
514
|
+
const paths = tsConfig.compilerOptions?.paths || {};
|
|
515
|
+
const libraryAliases = Object.keys(paths).filter((key) => key.startsWith('@'));
|
|
516
|
+
const dependencies = new Set();
|
|
517
|
+
const scanDirectory = (dir) => {
|
|
518
|
+
try {
|
|
519
|
+
for (const file of (0, fs_1.readdirSync)(dir)) {
|
|
520
|
+
const fullPath = (0, path_1.join)(dir, file);
|
|
521
|
+
const stat = (0, fs_1.statSync)(fullPath);
|
|
522
|
+
if (stat.isDirectory() &&
|
|
523
|
+
!file.startsWith('.') &&
|
|
524
|
+
file !== 'node_modules') {
|
|
525
|
+
scanDirectory(fullPath);
|
|
526
|
+
}
|
|
527
|
+
else if (file.endsWith('.ts') && !file.endsWith('.spec.ts')) {
|
|
528
|
+
const content = (0, fs_1.readFileSync)(fullPath, 'utf8');
|
|
529
|
+
// Robust regex for finding @magmonium imports
|
|
530
|
+
const matches = content.matchAll(/from\s+['"](@magmonium\/[^'"]+)['"]/g);
|
|
531
|
+
for (const match of matches) {
|
|
532
|
+
const libName = match[1].split('/').slice(0, 2).join('/');
|
|
533
|
+
if (libraryAliases.some((alias) => alias.startsWith(libName) || libName.startsWith(alias))) {
|
|
534
|
+
dependencies.add(libName);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
catch {
|
|
541
|
+
// ignore unreadable directories
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
scanDirectory(componentDir);
|
|
545
|
+
// Safety: if we found ANY @magmonium dependency, we probably need @magmonium/one
|
|
546
|
+
if (Array.from(dependencies).some((d) => d.startsWith('@magmonium/'))) {
|
|
547
|
+
dependencies.add('@magmonium/one');
|
|
548
|
+
}
|
|
549
|
+
const result = Array.from(dependencies);
|
|
550
|
+
console.log(` 🔍 Identified library dependencies: ${result.join(', ')}`);
|
|
551
|
+
return result;
|
|
552
|
+
}
|
|
553
|
+
function buildLibraryDependencies(dependencies, workspaceRoot, pm) {
|
|
554
|
+
if (dependencies.length === 0)
|
|
555
|
+
return;
|
|
556
|
+
// Outside an Nx workspace there is no project to build — the dependency is a
|
|
557
|
+
// published package, already compiled. Returning here rather than letting the
|
|
558
|
+
// loop's catch swallow the failure keeps a standalone build from printing a
|
|
559
|
+
// row of red "Failed to build dependency" lines for work that was never
|
|
560
|
+
// needed.
|
|
561
|
+
if (!isNxWorkspace(workspaceRoot)) {
|
|
562
|
+
console.log(` 📦 Standalone workspace — resolving ${dependencies.join(', ')} from node_modules`);
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
console.log(` 📦 Building ${dependencies.length} dependencies...`);
|
|
566
|
+
const executor = getExecutor(pm);
|
|
567
|
+
for (const dep of dependencies) {
|
|
568
|
+
const libName = dep.replace('@magmonium/', '');
|
|
569
|
+
try {
|
|
570
|
+
console.log(` 🔨 ${libName}`);
|
|
571
|
+
(0, child_process_1.execSync)(`${executor} nx build ${libName} --configuration=production`, {
|
|
572
|
+
cwd: workspaceRoot,
|
|
573
|
+
stdio: 'inherit',
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
console.error(` ❌ Failed to build dependency ${dep}`);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
function createDirectoryIfNotExists(dirPath) {
|
|
582
|
+
try {
|
|
583
|
+
if (!(0, fs_1.existsSync)(dirPath)) {
|
|
584
|
+
(0, fs_1.mkdirSync)(dirPath, { recursive: true });
|
|
585
|
+
console.log(`📁 Created directory: ${dirPath}`);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
catch (error) {
|
|
589
|
+
console.error(`❌ Failed to create directory ${dirPath}:`, error);
|
|
590
|
+
throw error;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
// Detects same-module TDZ hazards: a class that extends a parent declared
|
|
594
|
+
// later in the SAME __esm wrapper throws "Class extends value undefined" at
|
|
595
|
+
// runtime. We intentionally do NOT flag cross-wrapper references — esbuild
|
|
596
|
+
// guarantees parent modules are initialised (via init_*() calls) before any
|
|
597
|
+
// code in the child wrapper runs, so textual order across wrappers is safe.
|
|
598
|
+
// Likewise, class expressions nested inside function bodies are deferred until
|
|
599
|
+
// the function is called (well after every wrapper has initialised), so those
|
|
600
|
+
// are safe too. Only same-wrapper + parent-declared-later is a real crash.
|
|
601
|
+
//
|
|
602
|
+
// Wrapper-boundary detection must work in BOTH unminified and minified bundles.
|
|
603
|
+
// esbuild emits the wrapper as `__esm({ "file.js"() {...} })` unminified, but
|
|
604
|
+
// under minification it renames the helper (e.g. to `v`) and rewrites the call
|
|
605
|
+
// form to `v(()=>{...})`. Anchoring only on the literal `__esm({` token makes
|
|
606
|
+
// wrapperStart return -1 for every position in a minified bundle, collapsing
|
|
607
|
+
// all wrappers into one phantom wrapper and flagging every forward cross-wrapper
|
|
608
|
+
// class reference as a TDZ violation (false positive). We therefore resolve the
|
|
609
|
+
// helper's actual name from the bundle before scanning.
|
|
610
|
+
function detectEsmHelperName(code) {
|
|
611
|
+
// Unminified bundles keep the canonical `__esm` identifier.
|
|
612
|
+
if (code.includes('__esm('))
|
|
613
|
+
return '__esm';
|
|
614
|
+
// Minified: the __esm helper is the identifier called most often as
|
|
615
|
+
// `NAME(()=>{` — esbuild rewrites the object-form call to an arrow call,
|
|
616
|
+
// and the wrapper is invoked once per bundled module, so it dominates.
|
|
617
|
+
const counts = new Map();
|
|
618
|
+
for (const m of code.matchAll(/(?<![.\w])([A-Za-z_$][\w$]*)\(\(\)=>\{/g)) {
|
|
619
|
+
counts.set(m[1], (counts.get(m[1]) ?? 0) + 1);
|
|
620
|
+
}
|
|
621
|
+
let best = null;
|
|
622
|
+
let bestCount = 1; // require >1 call so a one-off callback can't win
|
|
623
|
+
for (const [name, count] of counts) {
|
|
624
|
+
if (count > bestCount) {
|
|
625
|
+
best = name;
|
|
626
|
+
bestCount = count;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return best;
|
|
630
|
+
}
|
|
631
|
+
function assertNoCyclicInheritance(bundlePath) {
|
|
632
|
+
const code = (0, fs_1.readFileSync)(bundlePath, 'utf8');
|
|
633
|
+
const violations = [];
|
|
634
|
+
const helperName = detectEsmHelperName(code);
|
|
635
|
+
// Minified call form: `v(()=>{` (covers both 1- and 2-arg wrapper variants,
|
|
636
|
+
// since both begin with `NAME(()=>{`). Unminified object form: `__esm({`.
|
|
637
|
+
const callToken = helperName ? `${helperName}(()=>{` : '';
|
|
638
|
+
const objToken = helperName === '__esm' ? '__esm({' : '';
|
|
639
|
+
// Index of the __esm wrapper enclosing `idx`, or -1 if none. Anchored on the
|
|
640
|
+
// resolved helper's call token so child/parent compare on the same basis in
|
|
641
|
+
// both unminified and minified bundles.
|
|
642
|
+
const wrapperStart = (idx) => {
|
|
643
|
+
const before = code.slice(0, idx);
|
|
644
|
+
return Math.max(before.lastIndexOf(callToken), before.lastIndexOf(objToken), before.lastIndexOf('= __esm({'), before.lastIndexOf('__esm({'));
|
|
645
|
+
};
|
|
646
|
+
// First declaration position of a class named `name` (assignment or
|
|
647
|
+
// declaration form), or -1. Minified bundles use `X=class`, unminified use
|
|
648
|
+
// `class X`; we check both so dev and prod bundles are validated equally.
|
|
649
|
+
const declIdx = (name) => {
|
|
650
|
+
let m;
|
|
651
|
+
if ((m = new RegExp(`(?<!\\w)${name}\\s*=\\s*class\\b`).exec(code)))
|
|
652
|
+
return m.index;
|
|
653
|
+
if ((m = new RegExp(`\\bclass\\s+${name}\\b`).exec(code)))
|
|
654
|
+
return m.index;
|
|
655
|
+
return -1;
|
|
656
|
+
};
|
|
657
|
+
// Match `X = class extends Y {` (minified) and `class X extends Y {` (raw).
|
|
658
|
+
const extRe = /(?:(\w+)\s*=\s*class\s+extends\s+(\w+)|\bclass\s+(\w+)\s+extends\s+(\w+))\s*\{/g;
|
|
659
|
+
let m;
|
|
660
|
+
while ((m = extRe.exec(code))) {
|
|
661
|
+
const child = m[1] ?? m[3];
|
|
662
|
+
const parent = m[2] ?? m[4];
|
|
663
|
+
const childIdx = m.index ?? 0;
|
|
664
|
+
const pIdx = declIdx(parent);
|
|
665
|
+
// Parent declared earlier (or not found) → no forward reference → safe.
|
|
666
|
+
if (pIdx < 0 || pIdx <= childIdx)
|
|
667
|
+
continue;
|
|
668
|
+
const childWrap = wrapperStart(childIdx);
|
|
669
|
+
const parentWrap = wrapperStart(pIdx);
|
|
670
|
+
// Different __esm wrappers (or one side unwrapped) → esbuild inserts the
|
|
671
|
+
// parent's init_*() call before the child wrapper runs. Safe; skip.
|
|
672
|
+
if (childWrap !== parentWrap)
|
|
673
|
+
continue;
|
|
674
|
+
violations.push(`'${child}' extends '${parent}' but '${parent}' is declared later in the same __esm wrapper (TDZ at runtime)`);
|
|
675
|
+
}
|
|
676
|
+
if (violations.length > 0) {
|
|
677
|
+
throw new Error(`❌ Cyclic inheritance detected in bundle — runtime crash guaranteed:\n` +
|
|
678
|
+
violations.map((v) => ` • ${v}`).join('\n') +
|
|
679
|
+
'\n' +
|
|
680
|
+
`Fix: break the circular import between these modules (import from the specific module, not a barrel). ` +
|
|
681
|
+
`Run the WC build with SKIP_WC_CLEANUP=1 and inspect the .meta.json file.`);
|
|
682
|
+
}
|
|
683
|
+
console.log(` ✅ No cyclic inheritance detected`);
|
|
684
|
+
}
|
|
685
|
+
//# sourceMappingURL=ng-build.js.map
|