From e4d98eba6baa129b7414c50956e911c11dff9231 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 19 Jul 2019 13:14:32 +0100 Subject: [PATCH] use std/esm to make chef compatible with cjs projects. Remove webpack work for node --- .travis.yml | 3 +- Gruntfile.js | 45 +--------------------------- package-lock.json | 5 ++++ package.json | 7 ++--- src/node/cjs.js | 13 ++++++++ src/node/{repl-index.mjs => repl.js} | 8 ++--- 6 files changed, 27 insertions(+), 54 deletions(-) create mode 100644 src/node/cjs.js rename src/node/{repl-index.mjs => repl.js} (86%) diff --git a/.travis.yml b/.travis.yml index 058ba004..c1929a9d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,6 @@ script: - grunt lint - grunt test - grunt docs - - npm run node-prod - grunt prod --msg="$COMPILE_MSG" - xvfb-run --server-args="-screen 0 1200x800x24" grunt testui before_deploy: @@ -34,7 +33,7 @@ deploy: file_glob: true file: - build/prod/*.zip - - build/node/CyberChef.js + - src/node/cjs.js on: repo: gchq/CyberChef tags: true diff --git a/Gruntfile.js b/Gruntfile.js index ab986085..df68bee4 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -3,7 +3,6 @@ const webpack = require("webpack"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; -const NodeExternals = require("webpack-node-externals"); const glob = require("glob"); const path = require("path"); @@ -15,7 +14,6 @@ const path = require("path"); * @license Apache-2.0 */ -const NODE_PROD = process.env.NODE_ENV === "production"; module.exports = function (grunt) { grunt.file.defaultEncoding = "utf8"; @@ -36,8 +34,7 @@ module.exports = function (grunt) { grunt.registerTask("node", "Compiles CyberChef into a single NodeJS module.", [ - "clean:node", "clean:config", "clean:nodeConfig", "exec:generateConfig", - "exec:generateNodeIndex", "webpack:node", "webpack:nodeRepl", "chmod:build" + "clean:node", "clean:config", "clean:nodeConfig", "exec:generateConfig", "exec:generateNodeIndex" ]); grunt.registerTask("test", @@ -201,46 +198,6 @@ module.exports = function (grunt) { ] }; }, - node: { - mode: NODE_PROD ? "production" : "development", - target: "node", - entry: "./src/node/index.mjs", - externals: [NodeExternals({ - whitelist: ["crypto-api/src/crypto-api"] - })], - output: { - filename: "CyberChef.js", - path: __dirname + "/build/node", - library: "CyberChef", - libraryTarget: "commonjs2" - }, - plugins: [ - new webpack.DefinePlugin(BUILD_CONSTANTS), - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: 1 - }) - ], - }, - nodeRepl: { - mode: NODE_PROD ? "production" : "development", - target: "node", - entry: "./src/node/repl-index.mjs", - externals: [NodeExternals({ - whitelist: ["crypto-api/src/crypto-api"] - })], - output: { - filename: "CyberChef-repl.js", - path: __dirname + "/build/node", - library: "CyberChef", - libraryTarget: "commonjs2" - }, - plugins: [ - new webpack.DefinePlugin(BUILD_CONSTANTS), - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: 1 - }) - ], - } }, "webpack-dev-server": { options: { diff --git a/package-lock.json b/package-lock.json index dfc402b9..ca59a9cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5097,6 +5097,11 @@ "integrity": "sha512-qzm/XxIbxm/FHyH341ZrbnMUpe+5Bocte9xkmFMzPMjRaZMcXww+MpBptFvtU+79L362nqiLhekCxCxDPaUMBQ==", "dev": true }, + "esm": { + "version": "3.2.25", + "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", + "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==" + }, "esmangle": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/esmangle/-/esmangle-1.0.1.tgz", diff --git a/package.json b/package.json index 5dea14a9..bb5d5833 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "type": "git", "url": "https://github.com/gchq/CyberChef/" }, - "main": "build/node/CyberChef.js", + "main": "src/node/cjs.js", "module": "src/node/index.mjs", "bugs": "https://github.com/gchq/CyberChef/issues", "browserslist": [ @@ -108,6 +108,7 @@ "diff": "^4.0.1", "es6-promisify": "^6.0.1", "escodegen": "^1.11.1", + "esm": "^3.2.25", "esmangle": "^1.0.1", "esprima": "^4.0.1", "exif-parser": "^0.1.12", @@ -156,9 +157,7 @@ "scripts": { "start": "grunt dev", "build": "grunt prod", - "node": "NODE_ENV=development grunt node", - "node-prod": "NODE_ENV=production grunt node", - "repl": "grunt node && node build/node/CyberChef-repl.js", + "repl": "node src/node/repl.js", "test": "grunt test", "test-node": "grunt test-node", "testui": "grunt testui", diff --git a/src/node/cjs.js b/src/node/cjs.js new file mode 100644 index 00000000..a17bce53 --- /dev/null +++ b/src/node/cjs.js @@ -0,0 +1,13 @@ +/** + * Export the main ESM module as CommonJS + * + * + * @author d98762656 [d98762625@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +/*eslint no-global-assign: ["off"] */ +require = require("esm")(module); +module.exports = require("./index.mjs"); +module.exports.File = require("./File.mjs"); diff --git a/src/node/repl-index.mjs b/src/node/repl.js similarity index 86% rename from src/node/repl-index.mjs rename to src/node/repl.js index a974e364..c9ed76f1 100644 --- a/src/node/repl-index.mjs +++ b/src/node/repl.js @@ -7,9 +7,9 @@ * @license Apache-2.0 */ -import chef from "./index.mjs"; -import repl from "repl"; -import File from "./File.mjs"; +const chef = require("./cjs.js"); +const repl = require("repl"); + /*eslint no-console: ["off"] */ @@ -26,7 +26,7 @@ const replServer = repl.start({ prompt: "chef > ", }); -global.File = File; +global.File = chef.File; Object.keys(chef).forEach((key) => { if (key !== "operations") {