@shellui/cli 0.0.8 → 0.0.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shellui/cli",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "ShellUI CLI - Command-line tool for ShellUI",
5
5
  "main": "src/cli.js",
6
6
  "type": "module",
@@ -30,7 +30,7 @@
30
30
  "tsx": "^4.21.0",
31
31
  "vite": "7.3.1",
32
32
  "workbox-build": "^7.1.0",
33
- "@shellui/core": "0.0.8"
33
+ "@shellui/core": "0.0.10"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"
@@ -11,6 +11,7 @@ import {
11
11
  createPostCSSConfig,
12
12
  createViteDefine,
13
13
  resolvePackagePath,
14
+ inlineLazyImportsPlugin,
14
15
  } from '../utils/index.js';
15
16
 
16
17
  /**
@@ -83,7 +84,7 @@ export async function buildCommand(root = '.') {
83
84
  // Build main app
84
85
  await build({
85
86
  root: coreSrcPath,
86
- plugins: [react()],
87
+ plugins: [react(), inlineLazyImportsPlugin()],
87
88
  define: createViteDefine(config),
88
89
  resolve: {
89
90
  alias: resolveAlias,
@@ -10,6 +10,7 @@ import {
10
10
  createPostCSSConfig,
11
11
  createViteDefine,
12
12
  resolvePackagePath,
13
+ inlineLazyImportsPlugin,
13
14
  } from '../utils/index.js';
14
15
  import { serviceWorkerDevPlugin } from '../utils/service-worker-plugin.js';
15
16
 
@@ -39,7 +40,7 @@ async function startServer(root, cwd, shouldOpen = false) {
39
40
 
40
41
  const server = await createServer({
41
42
  root: coreSrcPath,
42
- plugins: [react(), serviceWorkerDevPlugin(corePackagePath, coreSrcPath)],
43
+ plugins: [react(), inlineLazyImportsPlugin(), serviceWorkerDevPlugin(corePackagePath, coreSrcPath)],
43
44
  define: createViteDefine(config),
44
45
  resolve: {
45
46
  alias: createResolveAlias(),
@@ -53,6 +54,12 @@ async function startServer(root, cwd, shouldOpen = false) {
53
54
  esbuild: {
54
55
  sourcemap: false,
55
56
  },
57
+ // Prevent Vite from pre-bundling @shellui/core when it's in node_modules
58
+ // We want our transform plugin to handle lazy imports, not esbuild's pre-bundler
59
+ // This ensures consistent behavior between workspace and npm-installed packages
60
+ optimizeDeps: {
61
+ exclude: ['@shellui/core'],
62
+ },
56
63
  server: {
57
64
  port: config.port || 3000,
58
65
  open: shouldOpen,
@@ -13,3 +13,4 @@ export {
13
13
  createPostCSSConfig,
14
14
  createViteDefine,
15
15
  } from './vite.js';
16
+ export { inlineLazyImportsPlugin } from './inline-lazy-imports-plugin.js';
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Plugin that converts lazy(() => import(...)) to direct imports.
3
+ * This prevents Rollup/Vite from creating code-split chunks, ensuring all rendering
4
+ * goes through App.tsx where ConfigProvider wraps everything.
5
+ *
6
+ * This is critical: when the library is consumed via npm, Vite processes the source files.
7
+ * If there are dynamic imports, it creates separate chunks that load separately, causing
8
+ * duplicate module instances and breaking React context (ConfigContext, SettingsContext, etc.).
9
+ *
10
+ * @returns {import('vite').Plugin} Vite plugin
11
+ */
12
+ export function inlineLazyImportsPlugin() {
13
+ return {
14
+ name: 'inline-lazy-imports',
15
+ // Apply to both dev and build modes
16
+ transform(code, id) {
17
+ // Only transform source files, not node_modules (except our own src)
18
+ // Match paths that contain /src/ but exclude node_modules that aren't @shellui/core/src
19
+ if (!id.includes('/src/') || (id.includes('node_modules') && !id.includes('@shellui/core/src'))) {
20
+ return null;
21
+ }
22
+
23
+ // Check if code contains lazy() calls - if not, skip
24
+ if (!code.includes('lazy(') || !code.includes('import(')) return null;
25
+
26
+ const imports = [];
27
+ let transformed = code;
28
+ let hasChanges = false;
29
+
30
+ // Match the exact pattern: const Name = lazy(() => import('path').then((m) => ({ default: m.Name })),);
31
+ // Handles both single-line and multiline patterns
32
+ // The regex captures: const declaration, lazy call, import path, component name, and trailing punctuation
33
+ const lazyPattern = /const\s+(\w+)\s*=\s*lazy\s*\(\s*\(\)\s*=>\s*[\s\n]*import\s*\((['"])([^'"]+)\2\)\s*\.then\s*\([\s\S]*?\(?\s*m\s*\)?\s*=>\s*\([\s\S]*?\{\s*default:\s*m\.(\w+)\s*\}[\s\S]*?\)[\s\S]*?\)[\s\n]*\)[\s\n]*,?[\s\n]*;?/g;
34
+
35
+ transformed = transformed.replace(lazyPattern, (fullMatch, varName, quote, importPath, componentName) => {
36
+ hasChanges = true;
37
+ // Import with the same name as the variable (they match in the source code)
38
+ const importStatement = `import { ${componentName} as ${varName} } from ${quote}${importPath}${quote};`;
39
+ if (!imports.some(imp => imp.includes(`${varName}`))) {
40
+ imports.push(importStatement);
41
+ }
42
+ // Remove the entire const declaration - the import statement replaces it
43
+ return '';
44
+ });
45
+
46
+ if (!hasChanges || imports.length === 0) return null;
47
+
48
+ // Prepend all imports at the top of the file (after any existing imports)
49
+ // Find the last import statement
50
+ const lastImportMatch = transformed.match(/^import\s+.*$/gm);
51
+ if (lastImportMatch) {
52
+ const lastImportIndex = transformed.lastIndexOf(lastImportMatch[lastImportMatch.length - 1]);
53
+ const insertIndex = transformed.indexOf('\n', lastImportIndex) + 1;
54
+ transformed = transformed.slice(0, insertIndex) + imports.join('\n') + '\n' + transformed.slice(insertIndex);
55
+ } else {
56
+ // No imports found, add at the top
57
+ transformed = imports.join('\n') + '\n' + transformed;
58
+ }
59
+
60
+ return {
61
+ code: transformed,
62
+ map: null,
63
+ };
64
+ },
65
+ };
66
+ }