@ptkl/toolkit 0.5.0 → 0.6.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/dist/commands/forge.js +86 -70
- package/package.json +1 -1
package/dist/commands/forge.js
CHANGED
|
@@ -30,83 +30,99 @@ class ForgeCommand {
|
|
|
30
30
|
}
|
|
31
31
|
async bundle(options) {
|
|
32
32
|
const { path, upload } = options;
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
views
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (code.includes('import(')) {
|
|
72
|
-
// Transform relative dynamic imports to use full base URL
|
|
73
|
-
const transformedCode = code.replace(/import\(['"`]\.\/locales\/([^'"`]+)['"`]\)/g, `import('https://lemon.stage.protokol.io/luma/appservice/v1/forge/static/bundle/operator/0.3.0/$1')`);
|
|
74
|
-
return transformedCode;
|
|
33
|
+
// Store original working directory
|
|
34
|
+
const originalCwd = process.cwd();
|
|
35
|
+
try {
|
|
36
|
+
// Change to the app directory
|
|
37
|
+
process.chdir(path);
|
|
38
|
+
const module = await import(`${path}/ptkl.config.js`);
|
|
39
|
+
const { views, name, version, distPath, icon, type, label, permissions, } = module.default ?? {};
|
|
40
|
+
// build manifest file
|
|
41
|
+
const manifest = {
|
|
42
|
+
name,
|
|
43
|
+
version,
|
|
44
|
+
views: {},
|
|
45
|
+
label,
|
|
46
|
+
icon,
|
|
47
|
+
permissions,
|
|
48
|
+
type: type || 'platform', // default to 'platform' if not specified
|
|
49
|
+
};
|
|
50
|
+
const client = Util.getClientForProfile();
|
|
51
|
+
// get base url of the platform client
|
|
52
|
+
const baseUrl = client.getPlatformBaseURL();
|
|
53
|
+
const base = `${baseUrl}/luma/appservice/v1/forge/static/bundle/${name}/${version}/`;
|
|
54
|
+
manifest.icon = `${base}/${icon}`;
|
|
55
|
+
const buildViews = Object.keys(views).map((view) => {
|
|
56
|
+
manifest.views[view] = `${view}.bundle.js`;
|
|
57
|
+
return build({
|
|
58
|
+
root: path,
|
|
59
|
+
base,
|
|
60
|
+
build: {
|
|
61
|
+
rollupOptions: {
|
|
62
|
+
input: views[view],
|
|
63
|
+
output: {
|
|
64
|
+
format: 'esm',
|
|
65
|
+
entryFileNames: `[name].bundle.js`,
|
|
66
|
+
assetFileNames: (assetInfo) => {
|
|
67
|
+
return '[name].[ext]'; // Example: Customize the output file name format
|
|
68
|
+
},
|
|
69
|
+
manualChunks: undefined,
|
|
70
|
+
inlineDynamicImports: true,
|
|
75
71
|
}
|
|
76
|
-
|
|
77
|
-
}
|
|
72
|
+
},
|
|
78
73
|
},
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
client.forge().bundleUpload(buffer).then(() => {
|
|
97
|
-
console.log('Bundle uploaded successfully');
|
|
74
|
+
plugins: [
|
|
75
|
+
{
|
|
76
|
+
name: 'transform-dynamic-imports',
|
|
77
|
+
generateBundle(options, bundle) {
|
|
78
|
+
// Transform after bundling is complete
|
|
79
|
+
for (const fileName in bundle) {
|
|
80
|
+
const chunk = bundle[fileName];
|
|
81
|
+
if (chunk.type === 'chunk' && chunk.code) {
|
|
82
|
+
// Transform dynamic imports in the final bundled code
|
|
83
|
+
chunk.code = chunk.code.replace(/import\(['"`]\.\/([^'"`]+)['"`]\)/g, `dynamicImport('${base}$1')`);
|
|
84
|
+
// Also handle relative paths without ./
|
|
85
|
+
chunk.code = chunk.code.replace(/import\(['"`]([^'"`\/]+\.js)['"`]\)/g, `dynamicImport('${base}$1')`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
]
|
|
98
91
|
});
|
|
99
92
|
});
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
93
|
+
await Promise.allSettled(buildViews);
|
|
94
|
+
console.log("Packaging app...");
|
|
95
|
+
// // write manifest file
|
|
96
|
+
const manifestPath = `${distPath}/manifest.json`;
|
|
97
|
+
writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
|
|
98
|
+
if (upload) {
|
|
99
|
+
let buffer = Buffer.alloc(0);
|
|
100
|
+
const bufferStream = new Writable({
|
|
101
|
+
write(chunk, encoding, callback) {
|
|
102
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
103
|
+
callback();
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
c({ gzip: true, cwd: distPath }, ['.']).pipe(bufferStream).on('finish', () => {
|
|
107
|
+
client.forge().bundleUpload(buffer).then(() => {
|
|
108
|
+
console.log('Bundle uploaded successfully');
|
|
109
|
+
});
|
|
110
|
+
});
|
|
105
111
|
}
|
|
106
|
-
|
|
107
|
-
|
|
112
|
+
else {
|
|
113
|
+
try {
|
|
114
|
+
await c({ gzip: true, cwd: distPath, file: join(distPath, `/../bundle-${manifest.version}.tar.gz`) }, ['.']);
|
|
115
|
+
console.log('Bundle created successfully');
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
108
120
|
}
|
|
109
121
|
}
|
|
122
|
+
finally {
|
|
123
|
+
// Always restore the original working directory
|
|
124
|
+
process.chdir(originalCwd);
|
|
125
|
+
}
|
|
110
126
|
}
|
|
111
127
|
async runDev(options) {
|
|
112
128
|
const { path } = options;
|