Skip to content

Commit 25846b6

Browse files
committed
feat(boot): support loading ESM artifacts
BREAKING CHANGE: `loadClassesFromFiles` now returns a `Promise` see: #10744 Signed-off-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com>
1 parent 43f5d83 commit 25846b6

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class DummyCJSClass {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class DummyESMClass {}

packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,37 @@ describe('booter-utils unit tests', () => {
6666
const files = [resolve(sandbox.path, 'multiple.artifact.js')];
6767
const NUM_CLASSES = 2; // Number of classes in above file
6868

69-
const classes = loadClassesFromFiles(files, sandbox.path);
69+
const classes = await loadClassesFromFiles(files, sandbox.path);
70+
expect(classes).to.have.lengthOf(NUM_CLASSES);
71+
expect(classes[0]).to.be.a.Function();
72+
expect(classes[1]).to.be.a.Function();
73+
});
74+
75+
it('loads classes from CJS files', async () => {
76+
const artifactFilename = 'multiple.artifact.cjs';
77+
await sandbox.copyFile(
78+
resolve(__dirname, '../../fixtures/multiple.artifact.cjs'),
79+
);
80+
const NUM_CLASSES = 2;
81+
const classes = await loadClassesFromFiles(
82+
[resolve(sandbox.path, artifactFilename)],
83+
sandbox.path,
84+
);
85+
expect(classes).to.have.lengthOf(NUM_CLASSES);
86+
expect(classes[0]).to.be.a.Function();
87+
expect(classes[1]).to.be.a.Function();
88+
});
89+
90+
it('loads classes from ESM files', async () => {
91+
const artifactFilename = 'multiple.artifact.mjs';
92+
await sandbox.copyFile(
93+
resolve(__dirname, '../../fixtures/multiple.artifact.mjs'),
94+
);
95+
const NUM_CLASSES = 2;
96+
const classes = await loadClassesFromFiles(
97+
[resolve(sandbox.path, artifactFilename)],
98+
sandbox.path,
99+
);
70100
expect(classes).to.have.lengthOf(NUM_CLASSES);
71101
expect(classes[0]).to.be.a.Function();
72102
expect(classes[1]).to.be.a.Function();
@@ -78,16 +108,16 @@ describe('booter-utils unit tests', () => {
78108
);
79109
const files = [resolve(sandbox.path, 'empty.artifact.js')];
80110

81-
const classes = loadClassesFromFiles(files, sandbox.path);
111+
const classes = await loadClassesFromFiles(files, sandbox.path);
82112
expect(classes).to.be.an.Array();
83113
expect(classes).to.be.empty();
84114
});
85115

86116
it('throws an error given a non-existent file', async () => {
87117
const files = [resolve(sandbox.path, 'fake.artifact.js')];
88-
expect(() => loadClassesFromFiles(files, sandbox.path)).to.throw(
89-
/Cannot find module/,
90-
);
118+
await expect(
119+
loadClassesFromFiles(files, sandbox.path),
120+
).to.be.rejectedWith(/Cannot find module/);
91121
});
92122
});
93123
});

packages/boot/src/booters/base-artifact.booter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ export class BaseArtifactBooter implements Booter {
137137
* and then process the artifact classes as appropriate.
138138
*/
139139
async load() {
140-
this.classes = loadClassesFromFiles(this.discovered, this.projectRoot);
140+
this.classes = await loadClassesFromFiles(
141+
this.discovered,
142+
this.projectRoot,
143+
);
141144
}
142145
}

packages/boot/src/booters/booter-utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ export function isClass(target: any): target is Constructor<any> {
4646
* @param projectRootDir - The project root directory
4747
* @returns An array of Class constructors from a file
4848
*/
49-
export function loadClassesFromFiles(
49+
export async function loadClassesFromFiles(
5050
files: string[],
5151
projectRootDir: string,
52-
): Constructor<{}>[] {
52+
): Promise<Constructor<{}>[]> {
5353
const classes: Constructor<{}>[] = [];
5454
for (const file of files) {
5555
debug('Loading artifact file %j', path.relative(projectRootDir, file));
56-
const moduleObj = require(file);
56+
const moduleObj = await import(file);
5757
for (const k in moduleObj) {
5858
const exported = moduleObj[k];
5959
if (isClass(exported)) {

0 commit comments

Comments
 (0)