From dfc5eec43c481b1eeaa0ad069aeba8b7559d4440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 5 Sep 2024 08:59:12 +0100 Subject: feat: Allow importing .cjs files (#25426) This commit adds support for executing top-level `.cjs` files, as well as import `.cjs` files from within npm packages. This works only for `.cjs` files, the contents of sibling `package.json` are not consulted for the `"type"` field. Closes https://github.com/denoland/deno/issues/25384 --------- Signed-off-by: David Sherret Co-authored-by: Luca Casonato Co-authored-by: David Sherret --- .../specs/run/import_common_js/node_modules/foo/index.mjs | 14 ++++++++++++++ .../run/import_common_js/node_modules/foo/package.json | 3 +++ 2 files changed, 17 insertions(+) create mode 100644 tests/specs/run/import_common_js/node_modules/foo/index.mjs create mode 100644 tests/specs/run/import_common_js/node_modules/foo/package.json (limited to 'tests/specs/run/import_common_js/node_modules') diff --git a/tests/specs/run/import_common_js/node_modules/foo/index.mjs b/tests/specs/run/import_common_js/node_modules/foo/index.mjs new file mode 100644 index 000000000..cc93554c7 --- /dev/null +++ b/tests/specs/run/import_common_js/node_modules/foo/index.mjs @@ -0,0 +1,14 @@ +import process from "node:process"; +import path from "node:path"; +import url from "node:url"; + +export default async function () { + console.log("hello from foo node module"); + + const cjsFileToImport = path.join(process.cwd(), "index.cjs"); + + const cjsModule = await import(url.pathToFileURL(cjsFileToImport)); + + console.log("cjsModule.cwd()", cjsModule.cwd()); + console.log("cjsModule.foobar()", cjsModule.foobar()); +} diff --git a/tests/specs/run/import_common_js/node_modules/foo/package.json b/tests/specs/run/import_common_js/node_modules/foo/package.json new file mode 100644 index 000000000..ac525b7b8 --- /dev/null +++ b/tests/specs/run/import_common_js/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "./index.mjs" +} \ No newline at end of file -- cgit v1.2.3