Skip to content

Commit

Permalink
add base support for exporting objects and classes in node_loader
Browse files Browse the repository at this point in the history
  • Loading branch information
rxbryan committed Dec 6, 2022
1 parent f0515af commit f817d9c
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 157 deletions.
42 changes: 39 additions & 3 deletions source/loaders/node_loader/bootstrap/lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function node_loader_trampoline_discover_function(func) {
if (node_loader_trampoline_is_valid_symbol(node)) {
const args = node_loader_trampoline_discover_arguments(node);
const discover = {
ptr: func,
func,
signature: args,
async: node.async,
};
Expand All @@ -293,6 +293,41 @@ function node_loader_trampoline_discover_function(func) {
}
}

function node_loader_trampoline_discover_klass(klass) {
try {
if (node_loader_trampoline_is_callable(klass)) {
const str = klass.toString();
const ast = espree.parse(`(${str})`, {
ecmaVersion: 14
});

const node = (ast.body[0].type === 'ExpressionStatement') && ast.body[0].expression;

if (node.type === 'ClassExpression') {
const discover = {
klass
};

if (node.id && node.id.name) {
discover['name'] = node.id.name;
}

return discover;
}
}
} catch (ex) {
console.log(`Exception while parsing '${klass}' in node_loader_trampoline_discover_klass`, ex);
}
}

function node_loader_trampoline_discover_object(obj) {
if (typeof obj === 'object') {
return {
obj
};
}
}

function node_loader_trampoline_discover(handle) {
const discover = {};

Expand All @@ -305,8 +340,9 @@ function node_loader_trampoline_discover(handle) {

for (let j = 0; j < keys.length; ++j) {
const key = keys[j];
const func = exports[key];
const descriptor = node_loader_trampoline_discover_function(func);
const value = exports[key];
const descriptor = node_loader_trampoline_discover_function(value)
|| node_loader_trampoline_discover_klass(value) || node_loader_trampoline_discover_object(value);

if (descriptor !== undefined) {
discover[key] = descriptor;
Expand Down
Loading

0 comments on commit f817d9c

Please sign in to comment.