@bouygues-telecom/staticjs 1.0.1 → 1.0.3

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.
@@ -84,7 +84,8 @@ program
84
84
  try {
85
85
  console.log('🚀 Starting development server...');
86
86
  const serverEntrypoint = path.join(libDir, 'server', 'index.js');
87
- const devCommand = `NODE_ENV=development tsx ${serverEntrypoint}`;
87
+ // Use tsx to enable loading user's TypeScript/TSX files at runtime
88
+ const devCommand = `NODE_ENV=development npx tsx ${serverEntrypoint}`;
88
89
  execSync(devCommand, {
89
90
  stdio: 'inherit',
90
91
  cwd: projectRoot
@@ -77,13 +77,27 @@ const validateUserConfig = (rawConfig) => {
77
77
  return validatedConfig;
78
78
  };
79
79
  /**
80
- * Load user configuration from static.config.ts in the project root
80
+ * Load user configuration from static.config.js or static.config.ts in the project root
81
81
  * Validates all loaded values against a strict schema
82
82
  */
83
83
  const loadUserConfig = async () => {
84
84
  const projectRoot = path.resolve(process.cwd());
85
- const configPath = path.join(projectRoot, 'static.config.ts');
86
- if (!fs.existsSync(configPath)) {
85
+ // Check for config files in order of preference (JS first, then TS)
86
+ const configFiles = [
87
+ { path: path.join(projectRoot, 'static.config.js'), name: 'static.config.js' },
88
+ { path: path.join(projectRoot, 'static.config.mjs'), name: 'static.config.mjs' },
89
+ { path: path.join(projectRoot, 'static.config.ts'), name: 'static.config.ts' },
90
+ ];
91
+ let configPath = null;
92
+ let configName = '';
93
+ for (const config of configFiles) {
94
+ if (fs.existsSync(config.path)) {
95
+ configPath = config.path;
96
+ configName = config.name;
97
+ break;
98
+ }
99
+ }
100
+ if (!configPath) {
87
101
  return {};
88
102
  }
89
103
  try {
@@ -92,12 +106,20 @@ const loadUserConfig = async () => {
92
106
  // Validate and sanitize the loaded configuration
93
107
  const validatedConfig = validateUserConfig(rawConfig);
94
108
  if (Object.keys(validatedConfig).length > 0) {
95
- console.log('[Config] Loaded user configuration from static.config.ts');
109
+ console.log(`[Config] Loaded user configuration from ${configName}`);
96
110
  }
97
111
  return validatedConfig;
98
112
  }
99
113
  catch (error) {
100
- console.warn(`[Config] Failed to load static.config.ts: ${error.message}`);
114
+ const errorMessage = error.message;
115
+ // Provide helpful message for TypeScript loading errors
116
+ if (configName.endsWith('.ts') && errorMessage.includes('Unknown file extension')) {
117
+ console.warn(`[Config] Cannot load ${configName} - TypeScript files require tsx runtime.`);
118
+ console.warn(`[Config] Either rename to static.config.js, or ensure tsx is installed.`);
119
+ }
120
+ else {
121
+ console.warn(`[Config] Failed to load ${configName}: ${errorMessage}`);
122
+ }
101
123
  return {};
102
124
  }
103
125
  };
@@ -1,8 +1,8 @@
1
1
  import path from "path";
2
2
  import { defineConfig } from "vite";
3
- import { addHydrationCodePlugin } from "./vite.plugin";
3
+ import { addHydrationCodePlugin } from "./vite.plugin.js";
4
4
  import { loadCacheEntries } from "../../helpers/cachePages.js";
5
- import { CONFIG } from "./index";
5
+ import { CONFIG } from "./index.js";
6
6
  // Load cache entries using the refactored helper function
7
7
  const entries = loadCacheEntries(CONFIG.PROJECT_ROOT);
8
8
  // Sanitize entry keys for Rollup: strip dynamic segments [param] and use parent folder name
@@ -113,16 +113,13 @@ export const startStaticJSServer = async () => {
113
113
  export { CONFIG, DEFAULT_CONFIG, isDevelopment } from "./config/index.js";
114
114
  export { initializeViteServer } from "./utils/vite.js";
115
115
  export { setupProcessHandlers, startServer } from "./utils/startup.js";
116
- // Only start the server when this module is run directly (not when imported)
116
+ // Start server when this module is run directly
117
117
  let app;
118
- // Check if this module is being run directly
119
- if (import.meta.url === `file://${process.argv[1]}`) {
120
- console.log('[Server] Module executed directly, starting server...');
118
+ // Check if running as main entry point (works with node, tsx, and other runtimes)
119
+ const isMainModule = process.argv[1]?.includes('server/index');
120
+ if (isMainModule) {
121
121
  app = await startStaticJSServer();
122
122
  }
123
- else {
124
- console.log('[Server] Module imported, not starting server automatically');
125
- }
126
123
  // Default export is the app creation function
127
124
  export default app;
128
125
  // Named export for the main function (for backwards compatibility)
@@ -3,10 +3,11 @@
3
3
  * Handles Vite server initialization and JavaScript serving for development mode
4
4
  */
5
5
  import { createServer as createViteServer } from "vite";
6
- import { isDevelopment } from "../config/index.js";
6
+ import { isDevelopment, CONFIG } from "../config/index.js";
7
7
  import { registerJavaScriptMiddleware, registerCSSMiddleware } from "../middleware/runtime.js";
8
+ import { loadCacheEntries } from "../../helpers/cachePages.js";
9
+ import { addHydrationCodePlugin } from "../config/vite.plugin.js";
8
10
  import path from "path";
9
- import { fileURLToPath } from "url";
10
11
  let viteServer = null;
11
12
  /**
12
13
  * Initialize Vite server for development mode
@@ -19,12 +20,15 @@ export const initializeViteServer = async (app) => {
19
20
  return viteServer;
20
21
  }
21
22
  try {
22
- // Initializing Vite server
23
- // Get the absolute path to the vite config file within the package
24
- const __filename = fileURLToPath(import.meta.url);
25
- const __dirname = path.dirname(__filename);
26
- const configPath = path.resolve(__dirname, '../config/vite.config.js');
23
+ // Load cache entries for build input
24
+ const entries = loadCacheEntries(CONFIG.PROJECT_ROOT);
25
+ // Sanitize entry keys for Rollup: strip dynamic segments [param]
26
+ const sanitizedEntries = Object.fromEntries(Object.entries(entries).map(([key, value]) => [
27
+ key.replace(/\/\[[^\]]+\]$/, ''),
28
+ value
29
+ ]));
27
30
  // Create Vite server for development mode JS compilation
31
+ // Using inline config to avoid path resolution issues when installed as a dependency
28
32
  viteServer = await createViteServer({
29
33
  server: {
30
34
  middlewareMode: true,
@@ -33,7 +37,33 @@ export const initializeViteServer = async (app) => {
33
37
  }
34
38
  },
35
39
  appType: 'custom',
36
- configFile: configPath
40
+ configFile: false, // Don't load external config file
41
+ resolve: {
42
+ alias: {
43
+ "@": path.resolve(CONFIG.PROJECT_ROOT, "src")
44
+ },
45
+ },
46
+ css: {
47
+ devSourcemap: true,
48
+ preprocessorOptions: {
49
+ scss: {
50
+ api: "modern-compiler",
51
+ loadPaths: [path.resolve(CONFIG.PROJECT_ROOT, "src")],
52
+ },
53
+ },
54
+ },
55
+ build: {
56
+ outDir: path.resolve(CONFIG.PROJECT_ROOT, CONFIG.BUILD_DIR),
57
+ emptyOutDir: false,
58
+ rollupOptions: {
59
+ input: sanitizedEntries,
60
+ output: {
61
+ entryFileNames: "[name].js",
62
+ chunkFileNames: "assets/vendor-[hash].js",
63
+ },
64
+ },
65
+ },
66
+ plugins: [addHydrationCodePlugin(entries)],
37
67
  });
38
68
  // Vite server initialized
39
69
  // Add Vite's middleware to handle dependency requests
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bouygues-telecom/staticjs",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "main": "./_build/server/index.js",
6
6
  "exports": {
@@ -38,11 +38,12 @@
38
38
  "path": "^0.12.7",
39
39
  "readline": "^1.3.0",
40
40
  "rimraf": "^6.0.1",
41
- "sass": "^1.77.0",
42
41
  "ws": "^8.18.0"
43
42
  },
44
43
  "peerDependencies": {
45
- "vite": "^5.0.0 || ^6.0.0"
44
+ "vite": "^6.0.0 || ^7.0.0",
45
+ "sass": "^1.77.0",
46
+ "tsx": "^4.0.0"
46
47
  },
47
48
  "engines": {
48
49
  "node": ">=18.0.0"