@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.
@@ -30,83 +30,99 @@ class ForgeCommand {
30
30
  }
31
31
  async bundle(options) {
32
32
  const { path, upload } = options;
33
- const module = await import(`${path}/ptkl.config.js`);
34
- const { views, name, version, distPath, icon, type, label, permissions, } = module.default ?? {};
35
- // build manifest file
36
- const manifest = {
37
- name,
38
- version,
39
- views: {},
40
- label,
41
- icon,
42
- permissions,
43
- type: type || 'platform', // default to 'platform' if not specified
44
- };
45
- const client = Util.getClientForProfile();
46
- // get base url of the platform client
47
- const baseUrl = client.getPlatformBaseURL();
48
- const base = `${baseUrl}/luma/appservice/v1/forge/static/bundle/${name}/${version}/`;
49
- manifest.icon = `${base}/${icon}`;
50
- const buildViews = Object.keys(views).map((view) => {
51
- manifest.views[view] = `${view}.bundle.js`;
52
- return build({
53
- root: path,
54
- base,
55
- build: {
56
- rollupOptions: {
57
- input: views[view],
58
- output: {
59
- format: 'esm',
60
- entryFileNames: `[name].bundle.js`,
61
- assetFileNames: (assetInfo) => {
62
- return '[name].[ext]'; // Example: Customize the output file name format
63
- },
64
- }
65
- },
66
- },
67
- plugins: [
68
- {
69
- name: 'transform-dynamic-imports',
70
- transform(code, id) {
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
- return code;
77
- }
72
+ },
78
73
  },
79
- ]
80
- });
81
- });
82
- await Promise.allSettled(buildViews);
83
- console.log("Packaging app...");
84
- // // write manifest file
85
- const manifestPath = `${distPath}/manifest.json`;
86
- writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
87
- if (upload) {
88
- let buffer = Buffer.alloc(0);
89
- const bufferStream = new Writable({
90
- write(chunk, encoding, callback) {
91
- buffer = Buffer.concat([buffer, chunk]);
92
- callback();
93
- },
94
- });
95
- c({ gzip: true, cwd: distPath }, ['.']).pipe(bufferStream).on('finish', () => {
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
- else {
102
- try {
103
- await c({ gzip: true, cwd: distPath, file: join(distPath, `/../bundle-${manifest.version}.tar.gz`) }, ['.']);
104
- console.log('Bundle created successfully');
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
- catch (error) {
107
- throw error;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptkl/toolkit",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "scripts": {
5
5
  "test": "echo \"Error: no test specified\" && exit 1",
6
6
  "build": "npx tsc",