@yumerijs/loader 2.1.2 → 2.2.1
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.d.ts +0 -5
- package/dist/index.js +26 -87
- package/dist/runtime/vueLoader.js +11 -17
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -40,14 +40,9 @@ export declare class PluginLoader {
|
|
|
40
40
|
private _loadPendingPlugins;
|
|
41
41
|
unloadPlugin(pluginNameToUnload: string, ispending?: boolean): Promise<void>;
|
|
42
42
|
private _unloadSinglePlugin;
|
|
43
|
-
/**
|
|
44
|
-
* Reloads a single plugin's code by clearing the require cache and then reloading it.
|
|
45
|
-
* @param pluginName The name of the plugin to reload.
|
|
46
|
-
*/
|
|
47
43
|
reloadPlugin(pluginName: string): Promise<void>;
|
|
48
44
|
private watchPlugin;
|
|
49
45
|
loadModule(pluginName: string): Promise<Plugin>;
|
|
50
|
-
private clearRequireCache;
|
|
51
46
|
checkPluginDependencies(pluginPath: string): Promise<boolean>;
|
|
52
47
|
installPluginDependencies(pluginName: string): Promise<void>;
|
|
53
48
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,53 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.PluginLoader = void 0;
|
|
37
|
-
const path = __importStar(require("path"));
|
|
38
|
-
const core_1 = require("@yumerijs/core");
|
|
39
|
-
const fs = __importStar(require("fs"));
|
|
40
|
-
const util_1 = require("util");
|
|
41
|
-
const child_process_1 = require("child_process");
|
|
42
|
-
const yaml = __importStar(require("js-yaml"));
|
|
43
|
-
const chokidar = __importStar(require("chokidar"));
|
|
44
|
-
const vueLoader_1 = require("./runtime/vueLoader");
|
|
45
|
-
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
46
|
-
class PluginLoader {
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { Core, Logger, Context, fallback, I18n } from '@yumerijs/core';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import { promisify } from 'util';
|
|
5
|
+
import { exec } from 'child_process';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
import * as yaml from 'js-yaml';
|
|
8
|
+
import * as chokidar from 'chokidar';
|
|
9
|
+
const execAsync = promisify(exec);
|
|
10
|
+
export class PluginLoader {
|
|
47
11
|
pluginsDir;
|
|
48
12
|
core;
|
|
49
13
|
config = null;
|
|
50
|
-
logger = new
|
|
14
|
+
logger = new Logger('loader');
|
|
51
15
|
plugins = {};
|
|
52
16
|
pluginStatus = {};
|
|
53
17
|
pluginWatchers = {};
|
|
@@ -57,10 +21,9 @@ class PluginLoader {
|
|
|
57
21
|
isDev = false;
|
|
58
22
|
constructor(core, pluginsDir = 'plugins') {
|
|
59
23
|
this.pluginsDir = pluginsDir;
|
|
60
|
-
this.core = core || new
|
|
24
|
+
this.core = core || new Core(this, undefined, false);
|
|
61
25
|
this.isDev = process.env.NODE_ENV === 'development';
|
|
62
|
-
|
|
63
|
-
(0, vueLoader_1.registerVueRuntimeLoader)();
|
|
26
|
+
Logger.setCore(this.core);
|
|
64
27
|
}
|
|
65
28
|
/**
|
|
66
29
|
* Reloads the config file from disk into memory and emits a 'config-reloaded' event.
|
|
@@ -84,7 +47,7 @@ class PluginLoader {
|
|
|
84
47
|
}
|
|
85
48
|
getContext(pluginName, injections = {}) {
|
|
86
49
|
if (!this.pluginContexts[pluginName]) {
|
|
87
|
-
this.pluginContexts[pluginName] = new
|
|
50
|
+
this.pluginContexts[pluginName] = new Context(this.core, pluginName, null, injections);
|
|
88
51
|
}
|
|
89
52
|
return this.pluginContexts[pluginName];
|
|
90
53
|
}
|
|
@@ -109,7 +72,7 @@ class PluginLoader {
|
|
|
109
72
|
this.config = doc;
|
|
110
73
|
this.logger.info('Config loaded.');
|
|
111
74
|
this.core.coreConfig = this.config.core || {};
|
|
112
|
-
this.core.i18n = new
|
|
75
|
+
this.core.i18n = new I18n(this.core.coreConfig.lang || ['zh', 'en']);
|
|
113
76
|
}
|
|
114
77
|
catch (e) {
|
|
115
78
|
this.logger.error('Failed to load config:', e);
|
|
@@ -184,11 +147,12 @@ class PluginLoader {
|
|
|
184
147
|
try {
|
|
185
148
|
const rendererPackageMap = {
|
|
186
149
|
'vue': '@yumerijs/vue-renderer',
|
|
187
|
-
'react': '@yumerijs/react-renderer'
|
|
150
|
+
'react': '@yumerijs/react-renderer',
|
|
151
|
+
'ejs': '@yumerijs/ejs-renderer'
|
|
188
152
|
};
|
|
189
153
|
const rendererPackageName = rendererPackageMap[rendererName] || rendererName;
|
|
190
154
|
this.logger.info(`Loading renderer package: "${rendererPackageName}"...`);
|
|
191
|
-
const RendererClass =
|
|
155
|
+
const RendererClass = await import(rendererPackageName);
|
|
192
156
|
// Handle both ES modules (default export) and CommonJS modules
|
|
193
157
|
const ActualRendererClass = RendererClass.default || RendererClass;
|
|
194
158
|
const rendererInstance = new ActualRendererClass();
|
|
@@ -215,11 +179,12 @@ class PluginLoader {
|
|
|
215
179
|
// ### NEW CONFIG LOGIC ###
|
|
216
180
|
const rawConfig = (this.config.plugins && this.config.plugins[pluginName]) || {};
|
|
217
181
|
const schema = pluginInstance.config; // The schema is exported as 'config'
|
|
218
|
-
const finalConfig =
|
|
182
|
+
const finalConfig = fallback(schema, rawConfig);
|
|
219
183
|
// Update the in-memory config with the fully resolved one
|
|
220
184
|
this.config.plugins[pluginName] = finalConfig;
|
|
221
185
|
// ### END NEW CONFIG LOGIC ###
|
|
222
186
|
const context = this.getContext(pluginName, injections);
|
|
187
|
+
context.renderer = this.core.renderers.get(pluginName);
|
|
223
188
|
await this.core.plugin(pluginInstance, context, finalConfig);
|
|
224
189
|
this.pluginStatus[pluginName] = "enabled" /* PluginStatus.ENABLED */;
|
|
225
190
|
if (triggerPendingCheck) {
|
|
@@ -228,7 +193,8 @@ class PluginLoader {
|
|
|
228
193
|
if (this.isDev) {
|
|
229
194
|
let pluginPathToWatch = null;
|
|
230
195
|
try {
|
|
231
|
-
const
|
|
196
|
+
const packageJsonUrl = import.meta.resolve(`${pluginName}/package.json`);
|
|
197
|
+
const pkgJsonPath = fileURLToPath(packageJsonUrl);
|
|
232
198
|
pluginPathToWatch = path.dirname(pkgJsonPath);
|
|
233
199
|
}
|
|
234
200
|
catch (e) {
|
|
@@ -311,22 +277,11 @@ class PluginLoader {
|
|
|
311
277
|
this.logger.error(`Failed to unload plugin "${pluginName}":`, error);
|
|
312
278
|
}
|
|
313
279
|
}
|
|
314
|
-
/**
|
|
315
|
-
* Reloads a single plugin's code by clearing the require cache and then reloading it.
|
|
316
|
-
* @param pluginName The name of the plugin to reload.
|
|
317
|
-
*/
|
|
318
280
|
async reloadPlugin(pluginName) {
|
|
319
281
|
this.logger.info(`Reloading plugin: "${pluginName}"...`);
|
|
320
|
-
try {
|
|
321
|
-
const resolvedPath = require.resolve(pluginName);
|
|
322
|
-
this.clearRequireCache(resolvedPath, new Set());
|
|
323
|
-
}
|
|
324
|
-
catch (e) {
|
|
325
|
-
this.logger.error(`Could not resolve path for plugin ${pluginName} to clear cache.`, e);
|
|
326
|
-
}
|
|
327
282
|
await this.reloadConfigFile();
|
|
328
283
|
await this.unloadPlugin(pluginName, true);
|
|
329
|
-
const success = await this.loadSinglePlugin(pluginName);
|
|
284
|
+
const success = await this.loadSinglePlugin(pluginName, true, false);
|
|
330
285
|
if (success) {
|
|
331
286
|
this.logger.info(`Plugin "${pluginName}" reloaded successfully.`);
|
|
332
287
|
this.core.emit('plugin-reloaded', pluginName);
|
|
@@ -339,7 +294,7 @@ class PluginLoader {
|
|
|
339
294
|
if (this.pluginWatchers[pluginName]) {
|
|
340
295
|
return;
|
|
341
296
|
}
|
|
342
|
-
const logger = new
|
|
297
|
+
const logger = new Logger('hmr');
|
|
343
298
|
const watcher = chokidar.watch(pluginPath, {
|
|
344
299
|
ignored: /(^|[\/])\../,
|
|
345
300
|
persistent: true,
|
|
@@ -364,24 +319,9 @@ class PluginLoader {
|
|
|
364
319
|
this.pluginWatchers[pluginName] = watcher;
|
|
365
320
|
}
|
|
366
321
|
async loadModule(pluginName) {
|
|
367
|
-
const plugin = await
|
|
322
|
+
const plugin = await import(pluginName);
|
|
368
323
|
return plugin.default || plugin;
|
|
369
324
|
}
|
|
370
|
-
clearRequireCache(moduleId, visited) {
|
|
371
|
-
if (visited.has(moduleId)) {
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
visited.add(moduleId);
|
|
375
|
-
const module = require.cache[moduleId];
|
|
376
|
-
if (!module)
|
|
377
|
-
return;
|
|
378
|
-
if (module.children) {
|
|
379
|
-
for (const child of module.children) {
|
|
380
|
-
this.clearRequireCache(child.id, visited);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
delete require.cache[moduleId];
|
|
384
|
-
}
|
|
385
325
|
async checkPluginDependencies(pluginPath) {
|
|
386
326
|
return true;
|
|
387
327
|
}
|
|
@@ -401,5 +341,4 @@ class PluginLoader {
|
|
|
401
341
|
}
|
|
402
342
|
}
|
|
403
343
|
}
|
|
404
|
-
|
|
405
|
-
exports.default = PluginLoader;
|
|
344
|
+
export default PluginLoader;
|
|
@@ -1,24 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.registerVueRuntimeLoader = registerVueRuntimeLoader;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
-
const compiler_sfc_1 = require("@vue/compiler-sfc");
|
|
10
|
-
const esbuild_1 = require("esbuild");
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { parse, compileScript, compileTemplate } from '@vue/compiler-sfc';
|
|
4
|
+
import { transformSync } from 'esbuild';
|
|
11
5
|
let vueLoaderRegistered = false;
|
|
12
6
|
const SUPPORTED_ESBUILD_LOADERS = new Set(['js', 'ts', 'tsx', 'jsx']);
|
|
13
7
|
function getScopeId(filename) {
|
|
14
|
-
return
|
|
8
|
+
return crypto.createHash('md5').update(filename).digest('hex').slice(0, 8);
|
|
15
9
|
}
|
|
16
10
|
function inferLoader(lang) {
|
|
17
11
|
if (!lang)
|
|
18
12
|
return 'js';
|
|
19
13
|
return SUPPORTED_ESBUILD_LOADERS.has(lang) ? lang : 'js';
|
|
20
14
|
}
|
|
21
|
-
function registerVueRuntimeLoader() {
|
|
15
|
+
export function registerVueRuntimeLoader() {
|
|
22
16
|
if (vueLoaderRegistered) {
|
|
23
17
|
return;
|
|
24
18
|
}
|
|
@@ -26,8 +20,8 @@ function registerVueRuntimeLoader() {
|
|
|
26
20
|
require.extensions['.vue'] = function registerVueSFC(module, filename) {
|
|
27
21
|
const nodeModule = module;
|
|
28
22
|
try {
|
|
29
|
-
const source =
|
|
30
|
-
const { descriptor } =
|
|
23
|
+
const source = fs.readFileSync(filename, 'utf8');
|
|
24
|
+
const { descriptor } = parse(source, { filename });
|
|
31
25
|
if (!descriptor.script && !descriptor.scriptSetup && !descriptor.template) {
|
|
32
26
|
nodeModule._compile('module.exports = {};\n', filename);
|
|
33
27
|
return;
|
|
@@ -36,7 +30,7 @@ function registerVueRuntimeLoader() {
|
|
|
36
30
|
let code = '';
|
|
37
31
|
const lang = descriptor.scriptSetup?.lang || descriptor.script?.lang;
|
|
38
32
|
if (!descriptor.script && !descriptor.scriptSetup && descriptor.template) {
|
|
39
|
-
const templateResult =
|
|
33
|
+
const templateResult = compileTemplate({
|
|
40
34
|
id,
|
|
41
35
|
filename,
|
|
42
36
|
source: descriptor.template.content,
|
|
@@ -53,7 +47,7 @@ export default __component__;
|
|
|
53
47
|
`;
|
|
54
48
|
}
|
|
55
49
|
else {
|
|
56
|
-
const compiled =
|
|
50
|
+
const compiled = compileScript(descriptor, {
|
|
57
51
|
id,
|
|
58
52
|
inlineTemplate: Boolean(descriptor.template),
|
|
59
53
|
templateOptions: {
|
|
@@ -62,7 +56,7 @@ export default __component__;
|
|
|
62
56
|
});
|
|
63
57
|
code = compiled.content;
|
|
64
58
|
}
|
|
65
|
-
const transformed =
|
|
59
|
+
const transformed = transformSync(code, {
|
|
66
60
|
loader: inferLoader(lang),
|
|
67
61
|
format: 'cjs',
|
|
68
62
|
target: 'node18',
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yumerijs/loader",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "Module loader for yumeri",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"files": [
|
|
8
9
|
"dist"
|
|
9
10
|
],
|
|
@@ -40,6 +41,6 @@
|
|
|
40
41
|
"js-yaml": "^4.1.0"
|
|
41
42
|
},
|
|
42
43
|
"peerDependencies": {
|
|
43
|
-
"@yumerijs/core": "^2.
|
|
44
|
+
"@yumerijs/core": "^2.2.1"
|
|
44
45
|
}
|
|
45
46
|
}
|