@motiadev/workbench 0.8.2-beta.140-111855 → 0.8.2-beta.140-559269

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.
@@ -1,5 +1,7 @@
1
+ import type { Printer } from '@motiadev/core';
1
2
  import type { HmrContext, ModuleNode } from 'vite';
2
3
  import type { WorkbenchPlugin } from './types';
4
+ export declare function isConfigFile(file: string): boolean;
3
5
  /**
4
6
  * Checks if a file change should trigger HMR for plugins.
5
7
  *
@@ -14,6 +16,7 @@ export declare function shouldInvalidatePlugins(file: string, plugins: Workbench
14
16
  *
15
17
  * @param ctx - Vite's HMR context
16
18
  * @param plugins - Current plugin configurations
19
+ * @param printer - Printer instance for logging
17
20
  * @returns Array of modules to update, or undefined to continue with default behavior
18
21
  */
19
- export declare function handlePluginHotUpdate(ctx: HmrContext, plugins: WorkbenchPlugin[]): ModuleNode[] | void;
22
+ export declare function handlePluginHotUpdate(ctx: HmrContext, plugins: WorkbenchPlugin[], printer: Printer): ModuleNode[] | undefined;
@@ -1,9 +1,20 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isConfigFile = isConfigFile;
3
7
  exports.shouldInvalidatePlugins = shouldInvalidatePlugins;
4
8
  exports.handlePluginHotUpdate = handlePluginHotUpdate;
9
+ const path_1 = __importDefault(require("path"));
10
+ const resolver_1 = require("./resolver");
5
11
  const types_1 = require("./types");
6
12
  const utils_1 = require("./utils");
13
+ const WATCHED_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.css', '.scss', '.less'];
14
+ function isConfigFile(file) {
15
+ const normalizedFile = (0, utils_1.normalizePath)(file);
16
+ return normalizedFile.endsWith('motia.config.ts') || normalizedFile.endsWith('motia.config.js');
17
+ }
7
18
  /**
8
19
  * Checks if a file change should trigger HMR for plugins.
9
20
  *
@@ -13,19 +24,28 @@ const utils_1 = require("./utils");
13
24
  */
14
25
  function shouldInvalidatePlugins(file, plugins) {
15
26
  const normalizedFile = (0, utils_1.normalizePath)(file);
16
- // Check if the changed file is a local plugin
27
+ const absoluteFile = path_1.default.isAbsolute(normalizedFile) ? normalizedFile : path_1.default.resolve(process.cwd(), normalizedFile);
28
+ if (isConfigFile(file)) {
29
+ return true;
30
+ }
31
+ const hasWatchedExtension = WATCHED_EXTENSIONS.some((ext) => absoluteFile.endsWith(ext));
32
+ if (!hasWatchedExtension) {
33
+ return false;
34
+ }
17
35
  for (const plugin of plugins) {
18
- if (plugin.packageName.startsWith('~/')) {
19
- const pluginPath = plugin.packageName.replace('~/', '');
20
- if (normalizedFile.includes(pluginPath)) {
36
+ if ((0, utils_1.isLocalPlugin)(plugin.packageName)) {
37
+ const resolved = (0, resolver_1.resolvePluginPackage)(plugin);
38
+ const pluginAbsolutePath = path_1.default.isAbsolute(resolved.resolvedPath)
39
+ ? resolved.resolvedPath
40
+ : path_1.default.resolve(process.cwd(), resolved.resolvedPath);
41
+ const normalizedPluginPath = pluginAbsolutePath.endsWith(path_1.default.sep)
42
+ ? pluginAbsolutePath
43
+ : `${pluginAbsolutePath}${path_1.default.sep}`;
44
+ if (absoluteFile.startsWith(normalizedPluginPath) || absoluteFile === pluginAbsolutePath) {
21
45
  return true;
22
46
  }
23
47
  }
24
48
  }
25
- // Check if it's a plugin configuration file
26
- if (normalizedFile.endsWith('motia.config.ts') || normalizedFile.endsWith('motia.config.js')) {
27
- return true;
28
- }
29
49
  return false;
30
50
  }
31
51
  /**
@@ -34,33 +54,63 @@ function shouldInvalidatePlugins(file, plugins) {
34
54
  *
35
55
  * @param ctx - Vite's HMR context
36
56
  * @param plugins - Current plugin configurations
57
+ * @param printer - Printer instance for logging
37
58
  * @returns Array of modules to update, or undefined to continue with default behavior
38
59
  */
39
- function handlePluginHotUpdate(ctx, plugins) {
40
- const { file, server } = ctx;
41
- console.log('[motia-plugins] HMR: File changed:', file);
60
+ function handlePluginHotUpdate(ctx, plugins, printer) {
61
+ const { file, server, timestamp } = ctx;
62
+ printer.printPluginLog(`HMR: File changed: ${(0, utils_1.normalizePath)(file)}`);
42
63
  // Check if this change affects plugins
43
64
  if (!shouldInvalidatePlugins(file, plugins)) {
44
65
  return; // Let Vite handle it normally
45
66
  }
46
- console.log('[motia-plugins] HMR: Plugin configuration or local plugin changed, invalidating virtual module');
67
+ if (isConfigFile(file)) {
68
+ printer.printPluginLog('HMR: Config file changed, triggering full page reload');
69
+ printer.printPluginWarn('Configuration changes require a server restart for full effect. Please restart the dev server to apply all changes.');
70
+ server.ws.send({
71
+ type: 'full-reload',
72
+ path: '*',
73
+ });
74
+ return;
75
+ }
76
+ printer.printPluginLog('HMR: Plugin change detected, invalidating virtual module');
47
77
  // Find the virtual module
48
78
  const virtualModule = server.moduleGraph.getModuleById(types_1.CONSTANTS.RESOLVED_VIRTUAL_MODULE_ID);
49
- if (virtualModule) {
50
- // Invalidate the virtual module to force regeneration
51
- server.moduleGraph.invalidateModule(virtualModule);
52
- console.log('[motia-plugins] HMR: Virtual module invalidated');
53
- }
54
- // Return modules to update (includes the virtual module and any dependent modules)
55
- const modulesToUpdate = [];
56
- if (virtualModule) {
57
- modulesToUpdate.push(virtualModule);
58
- }
59
- // Add any modules that import the virtual module
60
- const importers = virtualModule?.importers || new Set();
61
- for (const importer of importers) {
62
- modulesToUpdate.push(importer);
63
- console.log('[motia-plugins] HMR: Invalidating importer:', importer.id);
79
+ if (!virtualModule) {
80
+ printer.printPluginWarn('HMR: Virtual module not found, triggering full reload');
81
+ server.ws.send({
82
+ type: 'full-reload',
83
+ path: '*',
84
+ });
85
+ return;
64
86
  }
87
+ server.moduleGraph.invalidateModule(virtualModule, new Set(), timestamp);
88
+ printer.printPluginLog('HMR: Virtual module invalidated');
89
+ const modulesToUpdate = [virtualModule];
90
+ const processedModules = new Set([virtualModule]);
91
+ // Recursively add all importers
92
+ const addImporters = (module) => {
93
+ for (const importer of module.importers) {
94
+ if (!processedModules.has(importer)) {
95
+ processedModules.add(importer);
96
+ modulesToUpdate.push(importer);
97
+ server.moduleGraph.invalidateModule(importer, new Set(), timestamp);
98
+ addImporters(importer);
99
+ }
100
+ }
101
+ };
102
+ addImporters(virtualModule);
103
+ server.ws.send({
104
+ type: 'update',
105
+ updates: modulesToUpdate
106
+ .filter((m) => m.url)
107
+ .map((m) => ({
108
+ type: m.type === 'css' ? 'css-update' : 'js-update',
109
+ path: m.url,
110
+ acceptedPath: m.url,
111
+ timestamp,
112
+ })),
113
+ });
114
+ printer.printPluginLog(`HMR: Updated ${modulesToUpdate.length} module(s)`);
65
115
  return modulesToUpdate;
66
116
  }
@@ -1,27 +1,3 @@
1
1
  import type { Plugin } from 'vite';
2
2
  import type { WorkbenchPlugin } from './types';
3
- /**
4
- * Vite plugin for loading and managing Motia workbench plugins.
5
- *
6
- * Features:
7
- * - Hot Module Replacement (HMR) support
8
- * - Runtime validation with detailed error messages
9
- * - Verbose logging for debugging
10
- * - CSS injection for plugin styles
11
- *
12
- * @param plugins - Array of plugin configurations
13
- * @param options - Optional loader configuration
14
- * @returns Vite plugin instance
15
- *
16
- * @example
17
- * ```ts
18
- * export default defineConfig({
19
- * plugins: [
20
- * motiaPluginsPlugin([
21
- * { packageName: '@my-org/plugin', label: 'My Plugin' }
22
- * ])
23
- * ]
24
- * })
25
- * ```
26
- */
27
3
  export default function motiaPluginsPlugin(plugins: WorkbenchPlugin[]): Plugin;
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.default = motiaPluginsPlugin;
7
+ const core_1 = require("@motiadev/core");
8
+ const path_1 = __importDefault(require("path"));
4
9
  const generator_1 = require("./generator");
5
10
  const hmr_1 = require("./hmr");
6
11
  const resolver_1 = require("./resolver");
@@ -31,6 +36,7 @@ const validator_1 = require("./validator");
31
36
  * })
32
37
  * ```
33
38
  */
39
+ const printer = new core_1.Printer(process.cwd());
34
40
  function motiaPluginsPlugin(plugins) {
35
41
  let devServer = null;
36
42
  try {
@@ -38,25 +44,29 @@ function motiaPluginsPlugin(plugins) {
38
44
  failFast: false,
39
45
  });
40
46
  if (!validationResult.valid) {
41
- console.error('[motia-plugins] Plugin configuration validation failed:');
42
- validationResult.errors.forEach((err) => console.error(`[motia-plugins] ${err}`));
47
+ printer.printPluginError('Plugin configuration validation failed:');
48
+ for (const err of validationResult.errors) {
49
+ printer.printPluginError(` ${err}`);
50
+ }
43
51
  throw new Error('Invalid plugin configuration. See errors above.');
44
52
  }
45
53
  if (validationResult.warnings.length > 0) {
46
- validationResult.warnings.forEach((warning) => console.warn('[motia-plugins]', warning));
54
+ for (const warning of validationResult.warnings) {
55
+ printer.printPluginWarn(warning);
56
+ }
47
57
  }
48
58
  }
49
59
  catch (error) {
50
- console.error('[motia-plugins] Failed to validate plugins:', error);
60
+ printer.printPluginError(`Failed to validate plugins: ${error}`);
51
61
  throw error;
52
62
  }
53
63
  const alias = (0, resolver_1.createAliasConfig)(plugins);
54
- console.log(`[motia-plugins] Initialized with ${plugins.length} plugin(s)`);
64
+ printer.printPluginLog(`Initialized with ${plugins.length} plugin(s)`);
55
65
  return {
56
66
  name: 'vite-plugin-motia-plugins',
57
67
  enforce: 'pre',
58
68
  buildStart() {
59
- console.log('[motia-plugins] Build started');
69
+ printer.printPluginLog('Build started');
60
70
  },
61
71
  config: () => ({
62
72
  resolve: {
@@ -65,7 +75,30 @@ function motiaPluginsPlugin(plugins) {
65
75
  }),
66
76
  configureServer(server) {
67
77
  devServer = server;
68
- console.log('[motia-plugins] Dev server configured, HMR enabled');
78
+ printer.printPluginLog('Dev server configured, HMR enabled');
79
+ const configPaths = [path_1.default.join(process.cwd(), 'motia.config.ts'), path_1.default.join(process.cwd(), 'motia.config.js')];
80
+ for (const configPath of configPaths) {
81
+ server.watcher.add(configPath);
82
+ }
83
+ printer.printPluginLog('Watching for config file changes');
84
+ const localPlugins = plugins.filter((p) => (0, utils_1.isLocalPlugin)(p.packageName));
85
+ if (localPlugins.length > 0) {
86
+ printer.printPluginLog(`Watching ${localPlugins.length} local plugin(s)`);
87
+ for (const plugin of localPlugins) {
88
+ const resolved = (0, resolver_1.resolvePluginPackage)(plugin);
89
+ const watchPath = resolved.resolvedPath;
90
+ server.watcher.add(watchPath);
91
+ printer.printPluginLog(`Watching: ${watchPath}`);
92
+ }
93
+ server.watcher.on('change', (file) => {
94
+ const normalizedFile = (0, utils_1.normalizePath)(file);
95
+ printer.printPluginLog(`File watcher detected change: ${normalizedFile}`);
96
+ });
97
+ server.watcher.on('add', (file) => {
98
+ const normalizedFile = (0, utils_1.normalizePath)(file);
99
+ printer.printPluginLog(`File watcher detected new file: ${normalizedFile}`);
100
+ });
101
+ }
69
102
  },
70
103
  resolveId(id) {
71
104
  if (id === types_1.CONSTANTS.VIRTUAL_MODULE_ID) {
@@ -76,22 +109,22 @@ function motiaPluginsPlugin(plugins) {
76
109
  if (id !== types_1.CONSTANTS.RESOLVED_VIRTUAL_MODULE_ID) {
77
110
  return null;
78
111
  }
79
- console.log('[motia-plugins] Loading plugins virtual module');
80
- console.log('[motia-plugins] Generating plugin code...');
112
+ printer.printPluginLog('Loading plugins virtual module');
113
+ printer.printPluginLog('Generating plugin code...');
81
114
  const code = (0, generator_1.generatePluginCode)(plugins);
82
115
  if (!(0, generator_1.isValidCode)(code)) {
83
- console.error('[motia-plugins] Generated code is invalid or empty');
116
+ printer.printPluginError('Generated code is invalid or empty');
84
117
  return 'export const plugins = []';
85
118
  }
86
- console.log('[motia-plugins] Plugin code generated successfully');
119
+ printer.printPluginLog('Plugin code generated successfully');
87
120
  return code;
88
121
  },
89
122
  async transform(code, id) {
90
123
  const normalizedId = (0, utils_1.normalizePath)(id);
91
- if (!normalizedId.endsWith('/workbench/src/index.css')) {
124
+ if (!normalizedId.endsWith('src/index.css')) {
92
125
  return null;
93
126
  }
94
- console.log('[motia-plugins] Injecting plugin CSS imports');
127
+ printer.printPluginLog('Injecting plugin CSS imports');
95
128
  const cssImports = (0, generator_1.generateCssImports)(plugins);
96
129
  if (!cssImports) {
97
130
  return null;
@@ -103,16 +136,17 @@ function motiaPluginsPlugin(plugins) {
103
136
  },
104
137
  handleHotUpdate(ctx) {
105
138
  if (!devServer) {
139
+ printer.printPluginWarn('HMR: Dev server not available');
106
140
  return;
107
141
  }
108
- const modulesToUpdate = (0, hmr_1.handlePluginHotUpdate)(ctx, plugins);
109
- if (modulesToUpdate) {
110
- console.log('[motia-plugins] Hot reloaded plugins');
142
+ const modulesToUpdate = (0, hmr_1.handlePluginHotUpdate)(ctx, plugins, printer);
143
+ if (modulesToUpdate && modulesToUpdate.length > 0) {
144
+ printer.printPluginLog(`HMR: Successfully updated ${modulesToUpdate.length} module(s)`);
111
145
  return modulesToUpdate;
112
146
  }
113
147
  },
114
148
  buildEnd() {
115
- console.log('[motia-plugins] Build ended');
149
+ printer.printPluginLog('Build ended');
116
150
  },
117
151
  };
118
152
  }