@teambit/aspect-loader 1.0.108 → 1.0.109
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/artifacts/__bit_junit.xml +6 -0
- package/artifacts/preview/teambit_harmony_aspect_loader-preview.js +1 -0
- package/dist/{preview-1703647408454.js → preview-1703698405864.js} +2 -2
- package/package.json +9 -9
- package/aspect-definition.ts +0 -51
- package/aspect-loader.aspect.ts +0 -8
- package/aspect-loader.main.runtime.ts +0 -860
- package/constants.ts +0 -16
- package/core-aspects.spec.ts +0 -10
- package/core-aspects.ts +0 -153
- package/index.ts +0 -23
- package/is-esm-module.ts +0 -14
- package/plugin-definition.ts +0 -24
- package/plugin.ts +0 -27
- package/plugins.ts +0 -150
package/constants.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
|
|
3
|
-
export const UNABLE_TO_LOAD_EXTENSION = (id: string, errMsg?: string) =>
|
|
4
|
-
`error: Bit received an error loading "${id}", due to the error "${
|
|
5
|
-
errMsg || '<unknown-error>'
|
|
6
|
-
}", please use the '--log=error' flag for the full error.`;
|
|
7
|
-
export const UNABLE_TO_LOAD_EXTENSION_FROM_LIST = (ids: string[], errMsg?: string, neededFor?: string) => {
|
|
8
|
-
// const installOutput = err?.code === 'MODULE_NOT_FOUND' ? `try running "bit install" to install the missing dependencies` : '';
|
|
9
|
-
const installOutput = `try running ${chalk.cyan('"bit install"')} to fix this issue`;
|
|
10
|
-
return `Bit received an error loading ${chalk.cyan(ids.join(', '))}, due to the error:
|
|
11
|
-
"${errMsg || '<unknown-error>'}".
|
|
12
|
-
This is required for the component: ${chalk.cyan(neededFor || 'unknown')}
|
|
13
|
-
Please use the ${chalk.cyan("'--log=error'")} flag for the full error.
|
|
14
|
-
${installOutput}
|
|
15
|
-
`;
|
|
16
|
-
};
|
package/core-aspects.spec.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { getCoreAspectName } from './core-aspects';
|
|
2
|
-
|
|
3
|
-
describe('core-aspects', () => {
|
|
4
|
-
describe('getCoreAspectName', () => {
|
|
5
|
-
it('should include the namespace in the aspect name', () => {
|
|
6
|
-
const aspectName = getCoreAspectName('teambit.workspace/e2e/workspace');
|
|
7
|
-
expect(aspectName).toEqual('e2e.workspace');
|
|
8
|
-
});
|
|
9
|
-
});
|
|
10
|
-
});
|
package/core-aspects.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { BitError } from '@teambit/bit-error';
|
|
2
|
-
import { existsSync, readdir } from 'fs-extra';
|
|
3
|
-
import { join, resolve } from 'path';
|
|
4
|
-
import { Config } from '@teambit/bvm.config';
|
|
5
|
-
import { findCurrentBvmDir } from '@teambit/bvm.path';
|
|
6
|
-
|
|
7
|
-
let _bvmConfig;
|
|
8
|
-
|
|
9
|
-
export function getBvmDir(): string {
|
|
10
|
-
if (!_bvmConfig) {
|
|
11
|
-
_bvmConfig = Config.load(false, ['env', 'file']);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const bvmConfig = _bvmConfig;
|
|
15
|
-
return bvmConfig.getBvmDirectory();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function isRunFromBvm() {
|
|
19
|
-
return __dirname.includes('.bvm') || __dirname.includes(getBvmDir());
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function resolveFromPaths(packageName: string, aspectName: string, pathsToResolveAspects: string[]): string {
|
|
23
|
-
const moduleDirectory = require.resolve(packageName, { paths: pathsToResolveAspects });
|
|
24
|
-
const dirPath = join(moduleDirectory, '../..'); // to remove the "index.js" at the end
|
|
25
|
-
if (!existsSync(dirPath)) {
|
|
26
|
-
throw new Error(`unable to find ${aspectName} in ${dirPath}`);
|
|
27
|
-
}
|
|
28
|
-
return dirPath;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function resolveFromCurrDir(packageName: string, aspectName: string): string | undefined {
|
|
32
|
-
try {
|
|
33
|
-
const moduleDirectory = require.resolve(packageName);
|
|
34
|
-
const dirPath = join(moduleDirectory, '../..'); // to remove the "index.js" at the end
|
|
35
|
-
if (!existsSync(dirPath)) {
|
|
36
|
-
throw new Error(`unable to find ${aspectName} in ${dirPath}`);
|
|
37
|
-
}
|
|
38
|
-
return dirPath;
|
|
39
|
-
} catch (err) {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
function resolveFromBvmDir(packageName: string): string | undefined {
|
|
44
|
-
const currentBitDir = findCurrentBvmDir();
|
|
45
|
-
if (currentBitDir) {
|
|
46
|
-
return resolve(currentBitDir, 'node_modules', packageName);
|
|
47
|
-
}
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getAspectDirFromPath(id: string, pathsToResolveAspects?: string[]): string {
|
|
52
|
-
const aspectName = getCoreAspectName(id);
|
|
53
|
-
const packageName = getCoreAspectPackageName(id);
|
|
54
|
-
|
|
55
|
-
if (pathsToResolveAspects && pathsToResolveAspects.length) {
|
|
56
|
-
const fromPaths = resolveFromPaths(packageName, aspectName, pathsToResolveAspects);
|
|
57
|
-
return fromPaths;
|
|
58
|
-
}
|
|
59
|
-
const isRunFromBvmDir = isRunFromBvm();
|
|
60
|
-
const resolvers = isRunFromBvmDir ? [resolveFromBvmDir, resolveFromCurrDir] : [resolveFromCurrDir, resolveFromBvmDir];
|
|
61
|
-
for (const resolver of resolvers) {
|
|
62
|
-
const currResolved = resolver(packageName, aspectName);
|
|
63
|
-
if (currResolved) return currResolved;
|
|
64
|
-
}
|
|
65
|
-
throw new Error(`unable to find ${aspectName}`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function getAspectDir(id: string): string {
|
|
69
|
-
const aspectName = getCoreAspectName(id);
|
|
70
|
-
let dirPath;
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
dirPath = getAspectDirFromPath(id);
|
|
74
|
-
} catch (err: any) {
|
|
75
|
-
dirPath = resolve(__dirname, '../..', aspectName, 'dist');
|
|
76
|
-
}
|
|
77
|
-
if (!existsSync(dirPath)) {
|
|
78
|
-
throw new Error(`unable to find ${aspectName} in ${dirPath}`);
|
|
79
|
-
}
|
|
80
|
-
return dirPath;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
type BvmDirOptions = {
|
|
84
|
-
version?: string;
|
|
85
|
-
linkName?: string;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export function getAspectDirFromBvm(id: string, bvmDirOptions?: BvmDirOptions): string {
|
|
89
|
-
// Resolve from default link
|
|
90
|
-
if (!bvmDirOptions) {
|
|
91
|
-
const packageName = getCoreAspectPackageName(id);
|
|
92
|
-
const resolved = resolveFromBvmDir(packageName);
|
|
93
|
-
if (resolved) return resolved;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!_bvmConfig) {
|
|
97
|
-
_bvmConfig = Config.load(false, ['env', 'file']);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const bvmConfig = _bvmConfig;
|
|
101
|
-
let version;
|
|
102
|
-
if (bvmDirOptions?.version) {
|
|
103
|
-
version = bvmDirOptions?.version;
|
|
104
|
-
} else {
|
|
105
|
-
const link = bvmDirOptions?.linkName || bvmConfig.getDefaultLinkName();
|
|
106
|
-
if (!link) {
|
|
107
|
-
throw new BitError(`can't find link in bvm config. most likely bvm is not installed`);
|
|
108
|
-
}
|
|
109
|
-
const links = bvmConfig.getLinks();
|
|
110
|
-
version = links[link];
|
|
111
|
-
if (!version) {
|
|
112
|
-
throw new BitError(`can't find link named ${bvmDirOptions?.linkName} in bvm config`);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
const { versionDir, exists } = bvmConfig.getSpecificVersionDir(version, true);
|
|
116
|
-
if (!exists) {
|
|
117
|
-
throw new BitError(`can't find version ${version} in bvm folder`);
|
|
118
|
-
}
|
|
119
|
-
return getAspectDirFromPath(id, [versionDir]);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function getAspectDistDir(id: string) {
|
|
123
|
-
return resolve(`${getAspectDir(id)}/dist`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export function getCoreAspectName(id: string): string {
|
|
127
|
-
const [, ...name] = id.split('/');
|
|
128
|
-
const aspectName = name.join('.');
|
|
129
|
-
return aspectName;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function getCoreAspectPackageName(id: string): string {
|
|
133
|
-
const aspectName = getCoreAspectName(id);
|
|
134
|
-
return `@teambit/${aspectName}`;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export async function getAspectDef(aspectName: string, runtime?: string) {
|
|
138
|
-
const dirPath = getAspectDistDir(aspectName);
|
|
139
|
-
|
|
140
|
-
const files = await readdir(dirPath);
|
|
141
|
-
let runtimeFile;
|
|
142
|
-
if (runtime) {
|
|
143
|
-
runtimeFile = files.find((file) => file.includes(`.${runtime}.runtime.js`)) || null;
|
|
144
|
-
}
|
|
145
|
-
const aspectFile = files.find((file) => file.includes(`.aspect.js`)) || null;
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
id: aspectName,
|
|
149
|
-
aspectPath: join(dirPath, '..'),
|
|
150
|
-
aspectFilePath: aspectFile ? resolve(`${dirPath}/${aspectFile}`) : null,
|
|
151
|
-
runtimePath: runtimeFile ? resolve(`${dirPath}/${runtimeFile}`) : null,
|
|
152
|
-
};
|
|
153
|
-
}
|
package/index.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { AspectLoaderAspect } from './aspect-loader.aspect';
|
|
2
|
-
|
|
3
|
-
export type {
|
|
4
|
-
AspectLoaderMain,
|
|
5
|
-
AspectDescriptor,
|
|
6
|
-
MainAspect,
|
|
7
|
-
OnLoadRequireableExtension,
|
|
8
|
-
AspectResolver,
|
|
9
|
-
ResolvedAspect,
|
|
10
|
-
} from './aspect-loader.main.runtime';
|
|
11
|
-
export {
|
|
12
|
-
getAspectDef,
|
|
13
|
-
getAspectDir,
|
|
14
|
-
getAspectDistDir,
|
|
15
|
-
getCoreAspectPackageName,
|
|
16
|
-
getCoreAspectName,
|
|
17
|
-
getAspectDirFromBvm,
|
|
18
|
-
} from './core-aspects';
|
|
19
|
-
export type { PluginDefinition } from './plugin-definition';
|
|
20
|
-
export { AspectDefinition } from './aspect-definition';
|
|
21
|
-
export { UNABLE_TO_LOAD_EXTENSION } from './constants';
|
|
22
|
-
export { AspectLoaderAspect };
|
|
23
|
-
export default AspectLoaderAspect;
|
package/is-esm-module.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import findRoot from 'find-root';
|
|
2
|
-
import { join } from 'path';
|
|
3
|
-
import { readFileSync } from 'fs';
|
|
4
|
-
|
|
5
|
-
export function isEsmModule(path: string) {
|
|
6
|
-
try {
|
|
7
|
-
const root = findRoot(path);
|
|
8
|
-
const packageJsonString = readFileSync(join(root, 'package.json')).toString('utf-8');
|
|
9
|
-
const packageJson = JSON.parse(packageJsonString);
|
|
10
|
-
return packageJson.type === 'module';
|
|
11
|
-
} catch (err) {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
}
|
package/plugin-definition.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Aspect } from '@teambit/harmony';
|
|
2
|
-
|
|
3
|
-
export interface PluginDefinition {
|
|
4
|
-
/**
|
|
5
|
-
* regex pattern for detecting the definition file within a component.
|
|
6
|
-
*/
|
|
7
|
-
pattern: string | RegExp;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* runtimes for the plugin to apply.
|
|
11
|
-
*/
|
|
12
|
-
runtimes: string[];
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* register the plugin to its slot registry.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Plugin implementation.
|
|
20
|
-
* @param object The object that was exported as default by the *.plugin-pattern file.
|
|
21
|
-
* @param sourceAspect Pointer to the aspect that is using the plugin (the aspect that contain the *.plugin-pattern file).
|
|
22
|
-
*/
|
|
23
|
-
register<T>(object: T, sourceAspect: Aspect): void;
|
|
24
|
-
}
|
package/plugin.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
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
|
-
}
|