Bläddra i källkod

Improve CJS and ESM module support #1037

n1474335 3 år sedan
förälder
incheckning
75dba51f56

+ 1 - 8
Gruntfile.js

@@ -50,7 +50,7 @@ module.exports = function (grunt) {
 
     grunt.registerTask("testnodeconsumer",
         "A task which checks whether consuming CJS and ESM apps work with the CyberChef build",
-        ["exec:setupNodeConsumers", "exec:testCJSNodeConsumer", "exec:testESMNodeConsumer", "exec:testESMDeepImportNodeConsumer", "exec:teardownNodeConsumers"]);
+        ["exec:setupNodeConsumers", "exec:testCJSNodeConsumer", "exec:testESMNodeConsumer", "exec:teardownNodeConsumers"]);
 
     grunt.registerTask("default",
         "Lints the code base",
@@ -403,13 +403,6 @@ module.exports = function (grunt) {
                 ]),
                 stdout: false,
             },
-            testESMDeepImportNodeConsumer: {
-                command: chainCommands([
-                    `cd ${nodeConsumerTestPath}`,
-                    `node ${nodeFlags} esm-deep-import-consumer.mjs`,
-                ]),
-                stdout: false,
-            },
             fixCryptoApiImports: {
                 command: [
                     `[[ "$OSTYPE" == "darwin"* ]]`,

+ 4 - 7
package.json

@@ -27,13 +27,10 @@
     "type": "git",
     "url": "https://github.com/gchq/CyberChef/"
   },
-  "main": "src/node/cjs.js",
-  "module": "src/node/index.mjs",
+  "main": "src/node/wrapper.js",
   "exports": {
-    ".": {
-      "require": "./src/node/cjs.js",
-      "import": "./src/node/index.mjs"
-    }
+    "import": "./src/node/index.mjs",
+    "require": "./src/node/wrapper.js"
   },
   "bugs": "https://github.com/gchq/CyberChef/issues",
   "browserslist": [
@@ -174,7 +171,7 @@
   "scripts": {
     "start": "npx grunt dev",
     "build": "npx grunt prod",
-    "repl": "node src/node/repl.js",
+    "repl": "node --experimental-modules --experimental-json-modules --experimental-specifier-resolution=node --no-warnings src/node/repl.mjs",
     "test": "npx grunt configTests && node --experimental-modules --experimental-json-modules --no-warnings --no-deprecation tests/node/index.mjs && node --experimental-modules --experimental-json-modules --no-warnings --no-deprecation tests/operations/index.mjs",
     "test-node-consumer": "npx grunt testnodeconsumer",
     "testui": "npx grunt testui",

+ 2 - 2
src/node/repl.js → src/node/repl.mjs

@@ -7,8 +7,8 @@
  * @license Apache-2.0
  */
 
-const chef = require("./cjs.js");
-const repl = require("repl");
+import chef from "./index.mjs";
+import repl from "repl";
 
 
 /* eslint no-console: ["off"] */

+ 0 - 0
src/node/cjs.js → src/node/wrapper.js


+ 22 - 2
tests/node/consumers/esm-consumer.mjs

@@ -7,8 +7,28 @@
  */
 import assert from "assert";
 import chef from "cyberchef";
+import { bake, toHex, reverse, unique, multiply } from "cyberchef";
 
-const d = chef.bake("Testing, 1 2 3", [
+const a = bake("Testing, 1 2 3", [
+    toHex,
+    reverse,
+    {
+        op: unique,
+        args: {
+            delimiter: "Space",
+        }
+    },
+    {
+        op: multiply,
+        args: {
+            delimiter: "Space",
+        }
+    }
+]);
+
+assert.equal(a.value, "630957449041920");
+
+const b = chef.bake("Testing, 1 2 3", [
     chef.toHex,
     chef.reverse,
     {
@@ -25,4 +45,4 @@ const d = chef.bake("Testing, 1 2 3", [
     }
 ]);
 
-assert.equal(d.value, "630957449041920");
+assert.equal(b.value, "630957449041920");

+ 0 - 28
tests/node/consumers/esm-deep-import-consumer.mjs

@@ -1,28 +0,0 @@
-/**
- * Tests to ensure that a consuming app can use named imports from deep import patch
- *
- * @author d98762625 [d98762625@gmail.com]
- * @copyright Crown Copyright 2019
- * @license Apache-2.0
- */
-import assert from "assert";
-import { bake, toHex, reverse, unique, multiply } from "cyberchef";
-
-const d = bake("Testing, 1 2 3", [
-    toHex,
-    reverse,
-    {
-        op: unique,
-        args: {
-            delimiter: "Space",
-        }
-    },
-    {
-        op: multiply,
-        args: {
-            delimiter: "Space",
-        }
-    }
-]);
-
-assert.equal(d.value, "630957449041920");