@teambit/aspect-loader 1.0.106 → 1.0.108
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/aspect-definition.ts +51 -0
- package/aspect-loader.aspect.ts +8 -0
- package/aspect-loader.main.runtime.ts +860 -0
- package/constants.ts +16 -0
- package/core-aspects.spec.ts +10 -0
- package/core-aspects.ts +153 -0
- package/dist/aspect-definition.d.ts +8 -8
- package/dist/aspect-loader.composition.d.ts +2 -2
- package/dist/aspect-loader.main.runtime.d.ts +12 -12
- package/dist/aspect-loader.main.runtime.js +7 -12
- package/dist/aspect-loader.main.runtime.js.map +1 -1
- package/dist/core-aspects.d.ts +3 -3
- package/dist/core-aspects.js +4 -4
- package/dist/core-aspects.js.map +1 -1
- package/dist/plugins.d.ts +1 -1
- package/dist/{preview-1703505948637.js → preview-1703647408454.js} +2 -2
- package/index.ts +23 -0
- package/is-esm-module.ts +14 -0
- package/package.json +17 -24
- package/plugin-definition.ts +24 -0
- package/plugin.ts +27 -0
- package/plugins.ts +150 -0
- package/tsconfig.json +16 -21
- package/types/asset.d.ts +15 -3
package/plugin.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Aspect } from '@teambit/harmony';
|
|
2
|
+
import { PluginDefinition } from './plugin-definition';
|
|
3
|
+
|
|
4
|
+
export class Plugin {
|
|
5
|
+
constructor(readonly def: PluginDefinition, readonly path: string) {}
|
|
6
|
+
|
|
7
|
+
// consider adding a more abstract type here to allow users to ask for dependencies.
|
|
8
|
+
private _instance: undefined | unknown;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* determines whether the plugin supports a certain runtime.
|
|
12
|
+
*/
|
|
13
|
+
supportsRuntime(runtime: string) {
|
|
14
|
+
return this.def.runtimes.includes(runtime);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
register(sourceAspect: Aspect, module?: unknown) {
|
|
18
|
+
const object = module || this.require();
|
|
19
|
+
this.def.register<unknown>(object, sourceAspect);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
require() {
|
|
23
|
+
// eslint-disable-next-line global-require, import/no-dynamic-require
|
|
24
|
+
this._instance = require(this.path).default;
|
|
25
|
+
return this._instance;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/plugins.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { Component } from '@teambit/component';
|
|
2
|
+
import esmLoader from '@teambit/node.utils.esm-loader';
|
|
3
|
+
import { NativeCompileCache } from '@teambit/toolbox.performance.v8-cache';
|
|
4
|
+
import { Logger } from '@teambit/logger';
|
|
5
|
+
import { Aspect } from '@teambit/harmony';
|
|
6
|
+
import { PluginDefinition } from './plugin-definition';
|
|
7
|
+
import { isEsmModule } from './is-esm-module';
|
|
8
|
+
import { Plugin } from './plugin';
|
|
9
|
+
import { OnAspectLoadErrorHandler } from './aspect-loader.main.runtime';
|
|
10
|
+
|
|
11
|
+
export type PluginMap = { [filePath: string]: PluginDefinition };
|
|
12
|
+
|
|
13
|
+
export class Plugins {
|
|
14
|
+
constructor(
|
|
15
|
+
readonly component: Component,
|
|
16
|
+
readonly plugins: Plugin[],
|
|
17
|
+
private triggerOnAspectLoadError: OnAspectLoadErrorHandler,
|
|
18
|
+
private logger: Logger
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
// computeDependencies(runtime: string): Aspect[] {
|
|
22
|
+
// const inRuntime = this.getByRuntime(runtime);
|
|
23
|
+
// return inRuntime.flatMap((plugin) => {
|
|
24
|
+
// return plugin.def.dependencies;
|
|
25
|
+
// });
|
|
26
|
+
// }
|
|
27
|
+
|
|
28
|
+
getByRuntime(runtime: string) {
|
|
29
|
+
return this.plugins.filter((plugin) => {
|
|
30
|
+
return plugin.supportsRuntime(runtime);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async load(runtime: string) {
|
|
35
|
+
const plugins = this.getByRuntime(runtime);
|
|
36
|
+
const aspect = Aspect.create({
|
|
37
|
+
id: this.component.id.toString(),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
aspect.addRuntime({
|
|
41
|
+
provider: async () => {
|
|
42
|
+
await Promise.all(
|
|
43
|
+
plugins.map(async (plugin) => {
|
|
44
|
+
return this.registerPluginWithTryCatch(plugin, aspect);
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
// Return an empty object so haromny will have something in the extension instance
|
|
48
|
+
// otherwise it will throw an error when trying to access the extension instance (harmony.get)
|
|
49
|
+
return {};
|
|
50
|
+
},
|
|
51
|
+
runtime,
|
|
52
|
+
// dependencies: this.computeDependencies(runtime)
|
|
53
|
+
dependencies: [],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return aspect;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async loadModule(path: string) {
|
|
60
|
+
NativeCompileCache.uninstall();
|
|
61
|
+
const module = await esmLoader(path);
|
|
62
|
+
return module.default;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async registerPluginWithTryCatch(plugin: Plugin, aspect: Aspect) {
|
|
66
|
+
const isModule = isEsmModule(plugin.path);
|
|
67
|
+
const module = isModule ? await this.loadModule(plugin.path) : undefined;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
if (isModule && !module) {
|
|
71
|
+
this.logger.consoleFailure(
|
|
72
|
+
`failed to load plugin ${plugin.path}, make sure to use 'export default' to expose your plugin`
|
|
73
|
+
);
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
return plugin.register(aspect, module);
|
|
77
|
+
} catch (firstErr: any) {
|
|
78
|
+
this.logger.warn(
|
|
79
|
+
`failed loading plugin with pattern "${
|
|
80
|
+
plugin.def.pattern
|
|
81
|
+
}", in component ${this.component.id.toString()}, will try to fix and reload`,
|
|
82
|
+
firstErr
|
|
83
|
+
);
|
|
84
|
+
const isFixed = await this.triggerOnAspectLoadError(firstErr, this.component);
|
|
85
|
+
let errAfterReLoad;
|
|
86
|
+
if (isFixed) {
|
|
87
|
+
this.logger.info(
|
|
88
|
+
`the loading issue might be fixed now, re-loading plugin with pattern "${
|
|
89
|
+
plugin.def.pattern
|
|
90
|
+
}", in component ${this.component.id.toString()}`
|
|
91
|
+
);
|
|
92
|
+
try {
|
|
93
|
+
return plugin.register(aspect, module);
|
|
94
|
+
} catch (err: any) {
|
|
95
|
+
this.logger.warn(
|
|
96
|
+
`re-load of the plugin with pattern "${
|
|
97
|
+
plugin.def.pattern
|
|
98
|
+
}", in component ${this.component.id.toString()} failed as well`,
|
|
99
|
+
err
|
|
100
|
+
);
|
|
101
|
+
errAfterReLoad = err;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const error = errAfterReLoad || firstErr;
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
has() {
|
|
110
|
+
return Boolean(this.plugins.length);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static from(
|
|
114
|
+
component: Component,
|
|
115
|
+
defs: PluginDefinition[],
|
|
116
|
+
triggerOnAspectLoadError: OnAspectLoadErrorHandler,
|
|
117
|
+
logger: Logger,
|
|
118
|
+
resolvePath?: (path: string) => string
|
|
119
|
+
) {
|
|
120
|
+
const plugins = defs.flatMap((pluginDef) => {
|
|
121
|
+
const files =
|
|
122
|
+
typeof pluginDef.pattern === 'string'
|
|
123
|
+
? component.filesystem.byGlob([pluginDef.pattern])
|
|
124
|
+
: component.filesystem.byRegex(pluginDef.pattern);
|
|
125
|
+
|
|
126
|
+
return files.map((file) => {
|
|
127
|
+
return new Plugin(pluginDef, resolvePath ? resolvePath(file.relative) : file.path);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return new Plugins(component, plugins, triggerOnAspectLoadError, logger);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get the plugin files from the component.
|
|
136
|
+
*/
|
|
137
|
+
static files(component: Component, defs: PluginDefinition[], resolvePath?: (path: string) => string): string[] {
|
|
138
|
+
const files = defs.flatMap((pluginDef) => {
|
|
139
|
+
const matches =
|
|
140
|
+
typeof pluginDef.pattern === 'string'
|
|
141
|
+
? component.filesystem.byGlob([pluginDef.pattern])
|
|
142
|
+
: component.filesystem.byRegex(pluginDef.pattern);
|
|
143
|
+
|
|
144
|
+
return matches.map((file) => {
|
|
145
|
+
return resolvePath ? resolvePath(file.relative) : file.path;
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
return files;
|
|
149
|
+
}
|
|
150
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,38 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"lib": [
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"DOM.Iterable",
|
|
8
|
-
"ScriptHost"
|
|
4
|
+
"esnext",
|
|
5
|
+
"dom",
|
|
6
|
+
"dom.Iterable"
|
|
9
7
|
],
|
|
10
|
-
"target": "
|
|
11
|
-
"module": "
|
|
12
|
-
"jsx": "react",
|
|
13
|
-
"allowJs": true,
|
|
14
|
-
"composite": true,
|
|
8
|
+
"target": "es2020",
|
|
9
|
+
"module": "es2020",
|
|
10
|
+
"jsx": "react-jsx",
|
|
15
11
|
"declaration": true,
|
|
16
12
|
"sourceMap": true,
|
|
17
|
-
"skipLibCheck": true,
|
|
18
13
|
"experimentalDecorators": true,
|
|
19
|
-
"
|
|
14
|
+
"skipLibCheck": true,
|
|
20
15
|
"moduleResolution": "node",
|
|
21
16
|
"esModuleInterop": true,
|
|
22
|
-
"rootDir": ".",
|
|
23
17
|
"resolveJsonModule": true,
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"strictPropertyInitialization": false,
|
|
28
|
-
"strict": true,
|
|
29
|
-
"noImplicitAny": false,
|
|
30
|
-
"preserveConstEnums": true
|
|
18
|
+
"allowJs": true,
|
|
19
|
+
"outDir": "dist",
|
|
20
|
+
"emitDeclarationOnly": true
|
|
31
21
|
},
|
|
32
22
|
"exclude": [
|
|
23
|
+
"artifacts",
|
|
24
|
+
"public",
|
|
33
25
|
"dist",
|
|
26
|
+
"node_modules",
|
|
27
|
+
"package.json",
|
|
34
28
|
"esm.mjs",
|
|
35
|
-
"
|
|
29
|
+
"**/*.cjs",
|
|
30
|
+
"./dist"
|
|
36
31
|
],
|
|
37
32
|
"include": [
|
|
38
33
|
"**/*",
|
package/types/asset.d.ts
CHANGED
|
@@ -5,12 +5,12 @@ declare module '*.png' {
|
|
|
5
5
|
declare module '*.svg' {
|
|
6
6
|
import type { FunctionComponent, SVGProps } from 'react';
|
|
7
7
|
|
|
8
|
-
export const ReactComponent: FunctionComponent<
|
|
8
|
+
export const ReactComponent: FunctionComponent<
|
|
9
|
+
SVGProps<SVGSVGElement> & { title?: string }
|
|
10
|
+
>;
|
|
9
11
|
const src: string;
|
|
10
12
|
export default src;
|
|
11
13
|
}
|
|
12
|
-
|
|
13
|
-
// @TODO Gilad
|
|
14
14
|
declare module '*.jpg' {
|
|
15
15
|
const value: any;
|
|
16
16
|
export = value;
|
|
@@ -27,3 +27,15 @@ declare module '*.bmp' {
|
|
|
27
27
|
const value: any;
|
|
28
28
|
export = value;
|
|
29
29
|
}
|
|
30
|
+
declare module '*.otf' {
|
|
31
|
+
const value: any;
|
|
32
|
+
export = value;
|
|
33
|
+
}
|
|
34
|
+
declare module '*.woff' {
|
|
35
|
+
const value: any;
|
|
36
|
+
export = value;
|
|
37
|
+
}
|
|
38
|
+
declare module '*.woff2' {
|
|
39
|
+
const value: any;
|
|
40
|
+
export = value;
|
|
41
|
+
}
|