@moostjs/vite 0.5.0 → 0.5.2

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
@@ -26,6 +26,60 @@ function createAdapterDetector(adapter, onInit) {
26
26
  };
27
27
  }
28
28
 
29
+ function flattenItem(item) {
30
+ return `${item.eventName}||${item.classConstructor.name}||${item.method}`;
31
+ }
32
+ class LogsStorage {
33
+ constructor() {
34
+ this.oldMap = new Map();
35
+ this.newItems = [];
36
+ this.isFirstRun = true;
37
+ }
38
+ startRecording() {
39
+ this.newItems = [];
40
+ }
41
+ record(item) {
42
+ const flat = flattenItem(item);
43
+ this.newItems.push(item);
44
+ return !this.oldMap.has(flat);
45
+ }
46
+ endRecording(logRemovedItem) {
47
+ const newMap = new Map();
48
+ for (const item of this.newItems) {
49
+ const flat = flattenItem(item);
50
+ newMap.set(flat, item);
51
+ }
52
+ for (const [flat, item] of this.oldMap.entries()) {
53
+ if (!newMap.has(flat)) {
54
+ logRemovedItem(item);
55
+ }
56
+ }
57
+ this.oldMap = newMap;
58
+ this.newItems = [];
59
+ this.isFirstRun = false;
60
+ }
61
+ }
62
+ let logsStorage;
63
+ function patchMoostHandlerLogging() {
64
+ const origInit = moost.Moost.prototype.init;
65
+ moost.Moost.prototype.init = async function init() {
66
+ if (!logsStorage) {
67
+ logsStorage = new LogsStorage();
68
+ }
69
+ logsStorage.startRecording();
70
+ await origInit.call(this);
71
+ logsStorage.endRecording((item) => {
72
+ origLogMappedHandler.call(this, item.eventName, item.classConstructor, item.method, true, '✖️ ');
73
+ });
74
+ };
75
+ const origLogMappedHandler = moost.Moost.prototype.logMappedHandler;
76
+ moost.Moost.prototype.logMappedHandler = function logMappedHandler(eventName, classConstructor, method) {
77
+ if (logsStorage.record({ eventName, classConstructor, method })) {
78
+ origLogMappedHandler.call(this, eventName, classConstructor, method, false, logsStorage.isFirstRun ? '' : '➕ ');
79
+ }
80
+ };
81
+ }
82
+
29
83
  const PLUGIN_NAME = 'moost-vite-dev';
30
84
  function gatherAllImporters(moduleNode, visited = new Set()) {
31
85
  if (!moduleNode) {
@@ -42,12 +96,12 @@ function gatherAllImporters(moduleNode, visited = new Set()) {
42
96
  }
43
97
  return visited;
44
98
  }
45
- const logger = new moost.EventLogger('', { level: 99 }).createTopic(PLUGIN_NAME);
99
+ const logger = new moost.EventLogger('', { level: 99 }).createTopic('' + '' + PLUGIN_NAME);
46
100
  function getLogger() {
47
101
  return logger;
48
102
  }
49
103
 
50
- function moostRestartCleanup(adapters, cleanupInstances) {
104
+ function moostRestartCleanup(adapters, onEject, cleanupInstances) {
51
105
  const logger = getLogger();
52
106
  const infact = moost.getMoostInfact();
53
107
  const { registry } = infact;
@@ -65,13 +119,14 @@ function moostRestartCleanup(adapters, cleanupInstances) {
65
119
  for (const key of Object.getOwnPropertySymbols(registry)) {
66
120
  const instance = registry[key];
67
121
  scanParams(instance, (type) => {
68
- if (type === moost.Moost || type instanceof moost.Moost || type.prototype instanceof moost.Moost) {
122
+ if ((type === moost.Moost || type instanceof moost.Moost || type.prototype instanceof moost.Moost) &&
123
+ (!onEject || onEject(instance, type))) {
69
124
  delete registry[key];
70
125
  logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "Moost")`);
71
126
  return true;
72
127
  }
73
128
  for (const adapter of adapters) {
74
- if (adapter.compare(type)) {
129
+ if (adapter.compare(type) && (!onEject || onEject(instance, type))) {
75
130
  delete registry[key];
76
131
  logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "${adapter.constructor.name}")`);
77
132
  return true;
@@ -79,13 +134,13 @@ function moostRestartCleanup(adapters, cleanupInstances) {
79
134
  }
80
135
  });
81
136
  }
82
- clearDependantRegistry(registry);
137
+ clearDependantRegistry(registry, onEject);
83
138
  infact.registry = registry;
84
139
  }
85
140
  moost.getMoostMate()._cleanup();
86
- moost.getGlobalWooks(undefined, undefined, 'cleanup');
141
+ moost.clearGlobalWooks();
87
142
  }
88
- function clearDependantRegistry(registry) {
143
+ function clearDependantRegistry(registry, onEject) {
89
144
  const logger = getLogger();
90
145
  const objSet = new Set();
91
146
  let somethingIsDeleted = true;
@@ -98,7 +153,7 @@ function clearDependantRegistry(registry) {
98
153
  for (const key of Object.getOwnPropertySymbols(registry)) {
99
154
  const instance = registry[key];
100
155
  scanParams(instance, (type) => {
101
- if (!objSet.has(type)) {
156
+ if (!objSet.has(type) && (!onEject || onEject(instance, type))) {
102
157
  delete registry[key];
103
158
  logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on "${type.name}" which is not in registry)`);
104
159
  somethingIsDeleted = true;
@@ -147,6 +202,7 @@ function moostViteDev(options) {
147
202
  ];
148
203
  const logger = getLogger();
149
204
  let reloadRequired = false;
205
+ patchMoostHandlerLogging();
150
206
  return {
151
207
  name: PLUGIN_NAME,
152
208
  apply: 'serve',
@@ -208,7 +264,7 @@ function moostViteDev(options) {
208
264
  }
209
265
  },
210
266
  async configureServer(server) {
211
- moostRestartCleanup(adapters);
267
+ moostRestartCleanup(adapters, options.onEject);
212
268
  await server.ssrLoadModule(entry);
213
269
  server.middlewares.use(async (req, res, next) => {
214
270
  if (reloadRequired) {
@@ -240,7 +296,7 @@ function moostViteDev(options) {
240
296
  this.environment.moduleGraph.invalidateModule(mod);
241
297
  }
242
298
  moostMiddleware = null;
243
- moostRestartCleanup(adapters, cleanupInstances);
299
+ moostRestartCleanup(adapters, options.onEject, cleanupInstances);
244
300
  reloadRequired = true;
245
301
  }
246
302
  return [];
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ interface TMoostViteDevOptions {
7
7
  outDir?: string;
8
8
  format?: 'cjs' | 'esm';
9
9
  sourcemap?: boolean;
10
+ onEject?: (instance: object, dependency: Function) => boolean;
10
11
  }
11
12
  /**
12
13
  * The main Vite plugin that:
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { EventLogger, getMoostInfact, getMoostMate, getGlobalWooks, Moost } from 'moost';
1
+ import { Moost, EventLogger, getMoostInfact, getMoostMate, clearGlobalWooks } from 'moost';
2
2
 
3
3
  function createAdapterDetector(adapter, onInit) {
4
4
  return {
@@ -24,6 +24,60 @@ function createAdapterDetector(adapter, onInit) {
24
24
  };
25
25
  }
26
26
 
27
+ function flattenItem(item) {
28
+ return `${item.eventName}||${item.classConstructor.name}||${item.method}`;
29
+ }
30
+ class LogsStorage {
31
+ constructor() {
32
+ this.oldMap = new Map();
33
+ this.newItems = [];
34
+ this.isFirstRun = true;
35
+ }
36
+ startRecording() {
37
+ this.newItems = [];
38
+ }
39
+ record(item) {
40
+ const flat = flattenItem(item);
41
+ this.newItems.push(item);
42
+ return !this.oldMap.has(flat);
43
+ }
44
+ endRecording(logRemovedItem) {
45
+ const newMap = new Map();
46
+ for (const item of this.newItems) {
47
+ const flat = flattenItem(item);
48
+ newMap.set(flat, item);
49
+ }
50
+ for (const [flat, item] of this.oldMap.entries()) {
51
+ if (!newMap.has(flat)) {
52
+ logRemovedItem(item);
53
+ }
54
+ }
55
+ this.oldMap = newMap;
56
+ this.newItems = [];
57
+ this.isFirstRun = false;
58
+ }
59
+ }
60
+ let logsStorage;
61
+ function patchMoostHandlerLogging() {
62
+ const origInit = Moost.prototype.init;
63
+ Moost.prototype.init = async function init() {
64
+ if (!logsStorage) {
65
+ logsStorage = new LogsStorage();
66
+ }
67
+ logsStorage.startRecording();
68
+ await origInit.call(this);
69
+ logsStorage.endRecording((item) => {
70
+ origLogMappedHandler.call(this, item.eventName, item.classConstructor, item.method, true, '✖️ ');
71
+ });
72
+ };
73
+ const origLogMappedHandler = Moost.prototype.logMappedHandler;
74
+ Moost.prototype.logMappedHandler = function logMappedHandler(eventName, classConstructor, method) {
75
+ if (logsStorage.record({ eventName, classConstructor, method })) {
76
+ origLogMappedHandler.call(this, eventName, classConstructor, method, false, logsStorage.isFirstRun ? '' : '➕ ');
77
+ }
78
+ };
79
+ }
80
+
27
81
  const PLUGIN_NAME = 'moost-vite-dev';
28
82
  function gatherAllImporters(moduleNode, visited = new Set()) {
29
83
  if (!moduleNode) {
@@ -40,12 +94,12 @@ function gatherAllImporters(moduleNode, visited = new Set()) {
40
94
  }
41
95
  return visited;
42
96
  }
43
- const logger = new EventLogger('', { level: 99 }).createTopic(PLUGIN_NAME);
97
+ const logger = new EventLogger('', { level: 99 }).createTopic('' + '' + PLUGIN_NAME);
44
98
  function getLogger() {
45
99
  return logger;
46
100
  }
47
101
 
48
- function moostRestartCleanup(adapters, cleanupInstances) {
102
+ function moostRestartCleanup(adapters, onEject, cleanupInstances) {
49
103
  const logger = getLogger();
50
104
  const infact = getMoostInfact();
51
105
  const { registry } = infact;
@@ -63,13 +117,14 @@ function moostRestartCleanup(adapters, cleanupInstances) {
63
117
  for (const key of Object.getOwnPropertySymbols(registry)) {
64
118
  const instance = registry[key];
65
119
  scanParams(instance, (type) => {
66
- if (type === Moost || type instanceof Moost || type.prototype instanceof Moost) {
120
+ if ((type === Moost || type instanceof Moost || type.prototype instanceof Moost) &&
121
+ (!onEject || onEject(instance, type))) {
67
122
  delete registry[key];
68
123
  logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "Moost")`);
69
124
  return true;
70
125
  }
71
126
  for (const adapter of adapters) {
72
- if (adapter.compare(type)) {
127
+ if (adapter.compare(type) && (!onEject || onEject(instance, type))) {
73
128
  delete registry[key];
74
129
  logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "${adapter.constructor.name}")`);
75
130
  return true;
@@ -77,13 +132,13 @@ function moostRestartCleanup(adapters, cleanupInstances) {
77
132
  }
78
133
  });
79
134
  }
80
- clearDependantRegistry(registry);
135
+ clearDependantRegistry(registry, onEject);
81
136
  infact.registry = registry;
82
137
  }
83
138
  getMoostMate()._cleanup();
84
- getGlobalWooks(undefined, undefined, 'cleanup');
139
+ clearGlobalWooks();
85
140
  }
86
- function clearDependantRegistry(registry) {
141
+ function clearDependantRegistry(registry, onEject) {
87
142
  const logger = getLogger();
88
143
  const objSet = new Set();
89
144
  let somethingIsDeleted = true;
@@ -96,7 +151,7 @@ function clearDependantRegistry(registry) {
96
151
  for (const key of Object.getOwnPropertySymbols(registry)) {
97
152
  const instance = registry[key];
98
153
  scanParams(instance, (type) => {
99
- if (!objSet.has(type)) {
154
+ if (!objSet.has(type) && (!onEject || onEject(instance, type))) {
100
155
  delete registry[key];
101
156
  logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on "${type.name}" which is not in registry)`);
102
157
  somethingIsDeleted = true;
@@ -145,6 +200,7 @@ function moostViteDev(options) {
145
200
  ];
146
201
  const logger = getLogger();
147
202
  let reloadRequired = false;
203
+ patchMoostHandlerLogging();
148
204
  return {
149
205
  name: PLUGIN_NAME,
150
206
  apply: 'serve',
@@ -206,7 +262,7 @@ function moostViteDev(options) {
206
262
  }
207
263
  },
208
264
  async configureServer(server) {
209
- moostRestartCleanup(adapters);
265
+ moostRestartCleanup(adapters, options.onEject);
210
266
  await server.ssrLoadModule(entry);
211
267
  server.middlewares.use(async (req, res, next) => {
212
268
  if (reloadRequired) {
@@ -238,7 +294,7 @@ function moostViteDev(options) {
238
294
  this.environment.moduleGraph.invalidateModule(mod);
239
295
  }
240
296
  moostMiddleware = null;
241
- moostRestartCleanup(adapters, cleanupInstances);
297
+ moostRestartCleanup(adapters, options.onEject, cleanupInstances);
242
298
  reloadRequired = true;
243
299
  }
244
300
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/vite",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
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.0",
42
- "@moostjs/event-http": "0.5.0",
41
+ "moost": "0.5.2",
42
+ "@moostjs/event-http": "0.5.2",
43
43
  "vite": "^6.0.5"
44
44
  }
45
45
  }