@moostjs/vite 0.5.19 → 0.5.21

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/index.cjs CHANGED
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ var vite = require('vite');
3
4
  var moost = require('moost');
5
+ var node_fs = require('node:fs');
6
+ var node_module = require('node:module');
4
7
 
5
8
  function createAdapterDetector(adapter, onInit) {
6
9
  return {
@@ -80,7 +83,7 @@ function patchMoostHandlerLogging() {
80
83
  };
81
84
  }
82
85
 
83
- const PLUGIN_NAME = 'moost-vite-dev';
86
+ const PLUGIN_NAME = 'moost-vite';
84
87
  function gatherAllImporters(moduleNode, visited = new Set()) {
85
88
  if (!moduleNode) {
86
89
  return visited;
@@ -100,6 +103,18 @@ const logger = new moost.EventLogger('', { level: 99 }).createTopic('' + '[
100
103
  function getLogger() {
101
104
  return logger;
102
105
  }
106
+ function getExternals({ node, workspace }) {
107
+ const pkg = JSON.parse(node_fs.readFileSync('package.json', 'utf8').toString());
108
+ const externals = workspace
109
+ ? Object.keys(pkg.dependencies || {})
110
+ : Object.entries(pkg.dependencies || {})
111
+ .filter(([key, ver]) => !ver.startsWith('workspace:'))
112
+ .map(i => i[0]);
113
+ if (node) {
114
+ externals.push(...node_module.builtinModules, ...node_module.builtinModules.map(m => `node:${m}`));
115
+ }
116
+ return externals;
117
+ }
103
118
 
104
119
  function moostRestartCleanup(adapters, onEject, cleanupInstances) {
105
120
  const logger = getLogger();
@@ -189,8 +204,12 @@ function constructorName(i) {
189
204
  const REG_HAS_EXPORT_CLASS = /(^\s*@(Injectable|Controller)\()/m;
190
205
  const REG_REPLACE_EXPORT_CLASS = /(^\s*@(Injectable|Controller)\()/gm;
191
206
  function moostVite(options) {
207
+ const isTest = process.env.NODE_ENV === 'test';
208
+ const isProd = process.env.NODE_ENV === 'production';
192
209
  let moostMiddleware = null;
193
- const adapters = [
210
+ const adapters = isTest
211
+ ? []
212
+ : [
194
213
  createAdapterDetector('http', MoostHttp => {
195
214
  MoostHttp.prototype.listen = function (...args) {
196
215
  moostMiddleware = this.getServerCb();
@@ -203,17 +222,16 @@ function moostVite(options) {
203
222
  createAdapterDetector('cli'),
204
223
  createAdapterDetector('wf'),
205
224
  ];
206
- const logger = getLogger();
225
+ const logger = isTest ? console : getLogger();
207
226
  let reloadRequired = false;
208
227
  patchMoostHandlerLogging();
209
- return {
228
+ const pluginConfig = {
210
229
  name: PLUGIN_NAME,
211
- apply: 'serve',
212
230
  enforce: 'pre',
213
231
  config(cfg) {
214
232
  const entry = cfg.build?.rollupOptions?.input || options.entry;
215
233
  const outfile = typeof entry === 'string' ? entry.split('/').pop().replace(/\.ts$/, '.js') : undefined;
216
- return {
234
+ const pluginConfig = {
217
235
  server: {
218
236
  port: cfg.server?.port || options.port || 3000,
219
237
  host: cfg.server?.host || options.host,
@@ -228,6 +246,15 @@ function moostVite(options) {
228
246
  ssr: cfg.build?.ssr ?? true,
229
247
  minify: cfg.build?.minify || false,
230
248
  rollupOptions: {
249
+ external: isTest
250
+ ? cfg.build?.rollupOptions?.external
251
+ : cfg.build?.rollupOptions?.external ||
252
+ (options.externals === false
253
+ ? []
254
+ : getExternals({
255
+ node: Boolean(options.externals === true || options.externals?.node),
256
+ workspace: Boolean(options.externals === true || options.externals?.workspace),
257
+ })),
231
258
  input: entry,
232
259
  output: {
233
260
  format: options.format,
@@ -238,6 +265,7 @@ function moostVite(options) {
238
265
  },
239
266
  },
240
267
  };
268
+ return vite.mergeConfig(cfg, pluginConfig);
241
269
  },
242
270
  async transform(code, id) {
243
271
  if (!id.endsWith('.ts')) {
@@ -311,6 +339,14 @@ function moostVite(options) {
311
339
  }
312
340
  },
313
341
  };
342
+ if (isProd || isTest) {
343
+ delete pluginConfig.configureServer;
344
+ delete pluginConfig.resolveId;
345
+ delete pluginConfig.load;
346
+ delete pluginConfig.transform;
347
+ delete pluginConfig.hotUpdate;
348
+ }
349
+ return pluginConfig;
314
350
  }
315
351
 
316
352
  exports.moostVite = moostVite;
package/dist/index.d.ts CHANGED
@@ -1,20 +1,89 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
3
  interface TMoostViteDevOptions {
4
+ /**
5
+ * The entry file for the application.
6
+ * This file serves as the main entry point for the build process and SSR server.
7
+ *
8
+ * Example: './src/main.ts'
9
+ */
4
10
  entry: string;
11
+ /**
12
+ * The port number for the Vite dev server.
13
+ *
14
+ * Default: `3000`.
15
+ */
5
16
  port?: number;
17
+ /**
18
+ * The hostname or IP address for the Vite dev server.
19
+ * If not specified, defaults to `'localhost'`.
20
+ *
21
+ * Example: '0.0.0.0'
22
+ */
6
23
  host?: string;
24
+ /**
25
+ * The output directory for the build artifacts.
26
+ *
27
+ * Default to: `'dist'`.
28
+ */
7
29
  outDir?: string;
30
+ /**
31
+ * The output format for the build artifacts.
32
+ * - `'cjs'`: CommonJS, suitable for Node.js environments.
33
+ * - `'esm'`: ES Module, suitable for modern environments.
34
+ *
35
+ * Default: `'esm'`.
36
+ */
8
37
  format?: 'cjs' | 'esm';
38
+ /**
39
+ * Whether to generate source maps for the build output.
40
+ * Source maps are useful for debugging purposes by mapping minified code back to the original source.
41
+ *
42
+ * Default: `true`.
43
+ */
9
44
  sourcemap?: boolean;
45
+ /**
46
+ * Configuration for defining external dependencies during the build process.
47
+ *
48
+ * This helps in excluding certain modules from the bundled output, reducing the bundle size and
49
+ * allowing them to be resolved at runtime instead.
50
+ *
51
+ * Default: `true`.
52
+ */
53
+ externals?: {
54
+ /**
55
+ * Whether to exclude Node.js built-in modules (e.g., `fs`, `path`, `node:fs`) from the build output.
56
+ * If `true`, all Node.js built-ins will be marked as external.
57
+ * Default: `false`.
58
+ */
59
+ node?: boolean;
60
+ /**
61
+ * Whether to exclude workspace dependencies (e.g., packages marked with `workspace:*` in `package.json`) from the build output.
62
+ * If `true`, all workspace dependencies will be marked as external.
63
+ * Default: `false`.
64
+ */
65
+ workspace?: boolean;
66
+ } | boolean;
10
67
  onEject?: (instance: object, dependency: Function) => boolean;
11
68
  }
12
69
  /**
13
- * The main Vite plugin that:
14
- * - Detects when moost adapter usage (http, cli, wf).
15
- * - Patches `MoostHttp.prototype.listen` in dev mode to register a custom middleware instead of listening on a port.
16
- * - Injects a `__VITE_ID()` decorator into exported classes to track them for hot reload cleanup.
17
- * - Cleans up internal Moost state upon hot updates, then reloads the SSR entry.
70
+ * The main Vite plugin for integrating Moost applications.
71
+ *
72
+ * Features:
73
+ * - **Adapter Detection**: Detects Moost adapter usage (`http`, `cli`, `wf`) and applies relevant configurations.
74
+ * - **Dev Mode Middleware**:
75
+ * - Patches `MoostHttp.prototype.listen` to register a custom middleware for serving the app via Vite's dev server instead of binding to a port.
76
+ * - Handles Moost state cleanup and hot module replacement (HMR) during development.
77
+ * - **Class Tracking**: Injects a `__VITE_ID()` decorator into exported classes to enable tracking and cleanup during hot reloads.
78
+ * - **Externals Support**:
79
+ * - Allows marking Node.js built-in modules and dependencies from `package.json` (optionally excluding workspace dependencies) as external during builds.
80
+ * - Configured via the `externals` option, which helps reduce bundle size and ensures compatibility with runtime environments.
81
+ * - **Build and Test Friendly**:
82
+ * - Avoids interfering with Vitest runs by skipping dev-specific behaviors during tests.
83
+ * - Ensures proper SSR entry setup and Rollup configurations for production builds.
84
+ *
85
+ * @param {TMoostViteDevOptions} options - Configuration options for the Moost Vite plugin.
86
+ * @returns {Plugin} The configured Vite plugin.
18
87
  */
19
88
  declare function moostVite(options: TMoostViteDevOptions): Plugin;
20
89
 
package/dist/index.mjs CHANGED
@@ -1,4 +1,7 @@
1
+ import { mergeConfig } from 'vite';
1
2
  import { Moost, EventLogger, getMoostInfact, getMoostMate, clearGlobalWooks } from 'moost';
3
+ import { readFileSync } from 'node:fs';
4
+ import { builtinModules } from 'node:module';
2
5
 
3
6
  function createAdapterDetector(adapter, onInit) {
4
7
  return {
@@ -78,7 +81,7 @@ function patchMoostHandlerLogging() {
78
81
  };
79
82
  }
80
83
 
81
- const PLUGIN_NAME = 'moost-vite-dev';
84
+ const PLUGIN_NAME = 'moost-vite';
82
85
  function gatherAllImporters(moduleNode, visited = new Set()) {
83
86
  if (!moduleNode) {
84
87
  return visited;
@@ -98,6 +101,18 @@ const logger = new EventLogger('', { level: 99 }).createTopic('' + '' +
98
101
  function getLogger() {
99
102
  return logger;
100
103
  }
104
+ function getExternals({ node, workspace }) {
105
+ const pkg = JSON.parse(readFileSync('package.json', 'utf8').toString());
106
+ const externals = workspace
107
+ ? Object.keys(pkg.dependencies || {})
108
+ : Object.entries(pkg.dependencies || {})
109
+ .filter(([key, ver]) => !ver.startsWith('workspace:'))
110
+ .map(i => i[0]);
111
+ if (node) {
112
+ externals.push(...builtinModules, ...builtinModules.map(m => `node:${m}`));
113
+ }
114
+ return externals;
115
+ }
101
116
 
102
117
  function moostRestartCleanup(adapters, onEject, cleanupInstances) {
103
118
  const logger = getLogger();
@@ -187,8 +202,12 @@ function constructorName(i) {
187
202
  const REG_HAS_EXPORT_CLASS = /(^\s*@(Injectable|Controller)\()/m;
188
203
  const REG_REPLACE_EXPORT_CLASS = /(^\s*@(Injectable|Controller)\()/gm;
189
204
  function moostVite(options) {
205
+ const isTest = process.env.NODE_ENV === 'test';
206
+ const isProd = process.env.NODE_ENV === 'production';
190
207
  let moostMiddleware = null;
191
- const adapters = [
208
+ const adapters = isTest
209
+ ? []
210
+ : [
192
211
  createAdapterDetector('http', MoostHttp => {
193
212
  MoostHttp.prototype.listen = function (...args) {
194
213
  moostMiddleware = this.getServerCb();
@@ -201,17 +220,16 @@ function moostVite(options) {
201
220
  createAdapterDetector('cli'),
202
221
  createAdapterDetector('wf'),
203
222
  ];
204
- const logger = getLogger();
223
+ const logger = isTest ? console : getLogger();
205
224
  let reloadRequired = false;
206
225
  patchMoostHandlerLogging();
207
- return {
226
+ const pluginConfig = {
208
227
  name: PLUGIN_NAME,
209
- apply: 'serve',
210
228
  enforce: 'pre',
211
229
  config(cfg) {
212
230
  const entry = cfg.build?.rollupOptions?.input || options.entry;
213
231
  const outfile = typeof entry === 'string' ? entry.split('/').pop().replace(/\.ts$/, '.js') : undefined;
214
- return {
232
+ const pluginConfig = {
215
233
  server: {
216
234
  port: cfg.server?.port || options.port || 3000,
217
235
  host: cfg.server?.host || options.host,
@@ -226,6 +244,15 @@ function moostVite(options) {
226
244
  ssr: cfg.build?.ssr ?? true,
227
245
  minify: cfg.build?.minify || false,
228
246
  rollupOptions: {
247
+ external: isTest
248
+ ? cfg.build?.rollupOptions?.external
249
+ : cfg.build?.rollupOptions?.external ||
250
+ (options.externals === false
251
+ ? []
252
+ : getExternals({
253
+ node: Boolean(options.externals === true || options.externals?.node),
254
+ workspace: Boolean(options.externals === true || options.externals?.workspace),
255
+ })),
229
256
  input: entry,
230
257
  output: {
231
258
  format: options.format,
@@ -236,6 +263,7 @@ function moostVite(options) {
236
263
  },
237
264
  },
238
265
  };
266
+ return mergeConfig(cfg, pluginConfig);
239
267
  },
240
268
  async transform(code, id) {
241
269
  if (!id.endsWith('.ts')) {
@@ -309,6 +337,14 @@ function moostVite(options) {
309
337
  }
310
338
  },
311
339
  };
340
+ if (isProd || isTest) {
341
+ delete pluginConfig.configureServer;
342
+ delete pluginConfig.resolveId;
343
+ delete pluginConfig.load;
344
+ delete pluginConfig.transform;
345
+ delete pluginConfig.hotUpdate;
346
+ }
347
+ return pluginConfig;
312
348
  }
313
349
 
314
350
  export { moostVite };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/vite",
3
- "version": "0.5.19",
3
+ "version": "0.5.21",
4
4
  "description": "Vite Dev plugin for moostjs",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -38,8 +38,8 @@
38
38
  },
39
39
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/vite#readme",
40
40
  "peerDependencies": {
41
- "moost": "0.5.19",
42
- "@moostjs/event-http": "0.5.19",
41
+ "moost": "0.5.21",
42
+ "@moostjs/event-http": "0.5.21",
43
43
  "vite": "^6.0.5"
44
44
  }
45
45
  }