Skip to content

Commit

Permalink
added support for full esm builds
Browse files Browse the repository at this point in the history
  • Loading branch information
jleeson committed May 28, 2024
1 parent 3091600 commit 0abadd3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/firefly/src/cli/cmds/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export default async function build(args) {
logger.log("building the project...");

/* get the projects package.json and determine language */
const { main, engines, dependencies } = loadPackage();
const { main, type, engines, dependencies } = loadPackage();
const isTypeScript = fs.existsSync(path.join(process.cwd(), "tsconfig.json"));
const isModule = (type != undefined) ? (type == "module") ? true : false : false;
const dist = main.split("/")[0];

/* determine the node version being targeted */
Expand All @@ -47,7 +48,7 @@ export default async function build(args) {

const config = {
input: findInputFiles(),
output: { dir: dist, format: "cjs" },
output: { dir: dist, format: isModule ? "esm" : "cjs" },
external: [
...Object.keys(dependencies).map((dependency) => new RegExp("^" + dependency + "(\\/.+)*$")),
...module.builtinModules
Expand Down
5 changes: 3 additions & 2 deletions packages/firefly/src/modules/core/router.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Module } from "../../utils/files";
import path from "path";
import fs from "fs";

Expand All @@ -24,7 +25,7 @@ export async function loadInjectables(directory, injectables = {}, root = true)
}

else if (stat.isFile() && route.endsWith(".service.js")) {
const exports = await import(path.join(path.relative(__dirname, directory), route));
const exports = await import(path.join(path.relative(Module.__dirname(import.meta.url), directory), route));

for (let name of Object.keys(exports)) {
if (!exports[name]?._injectable) continue;
Expand Down Expand Up @@ -72,7 +73,7 @@ export async function loadControllers(root, directory, injectables, controllers
}

else if (stat.isFile() && route.endsWith(".controller.js")) {
const exports = await import(path.join(path.relative(__dirname, directory), route));
const exports = await import(path.join(path.relative(Module.__dirname(import.meta.url), directory), route));

for (let name of Object.keys(exports)) {
if (!exports[name]?._controller) continue;
Expand Down
16 changes: 14 additions & 2 deletions packages/firefly/src/utils/files.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import path from "path";
import url from "url";
import fs from "fs";

/* module with object scope to mock the __filename and __dirname for esm */
export const Module = {
__filename: (fileUrl) => {
return (import.meta.url) ? url.fileURLToPath(fileUrl) : __filename;
},
__dirname: (fileUrl) => {
return (import.meta.url) ? path.dirname(Module.__filename(fileUrl)) : __dirname;
}
};

/* delete a directory */
export function deleteDirectory(directory, deleteRoot = true) {
if (!fs.existsSync(directory)) return;
Expand All @@ -17,11 +28,12 @@ export function deleteDirectory(directory, deleteRoot = true) {

/* load and validate the package.json */
export function loadPackage() {
const { main, engines, dependencies } = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json")));
const { main, ...pkg } = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json")));

/* ensure the main field exists and is not empty */
if (!main || !main.length) {
throw new Error("the package.json main field is required.");
}

return { main, engines, dependencies };
return { main, ...pkg };
}

0 comments on commit 0abadd3

Please sign in to comment.