From 3ccc7f45e1af3bc45c7551c995bdf90e82d24c71 Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Sat, 23 Jan 2021 10:20:32 -0800 Subject: [PATCH] enable babel runtime for node --- packages/xarc-app-dev/package.json | 1 + .../xarc-app-dev/src/config/babel/babelrc.ts | 2 +- packages/xarc-app-dev/src/lib/dev-tasks.ts | 4 + .../src/lib/tasks/package-json.ts | 79 +++++++++++++++++++ packages/xarc-app-dev/tsconfig.json | 3 +- packages/xarc-app/package.json | 5 +- packages/xarc-app/tsconfig.json | 15 ++-- samples/react-jest-app/package.json | 5 +- samples/stylus-sample/package.json | 3 +- samples/subapp2-todo-app/package.json | 3 +- samples/universal-react-node/package.json | 11 +-- 11 files changed, 108 insertions(+), 23 deletions(-) create mode 100644 packages/xarc-app-dev/src/lib/tasks/package-json.ts diff --git a/packages/xarc-app-dev/package.json b/packages/xarc-app-dev/package.json index cffc44f18..9927b31cc 100644 --- a/packages/xarc-app-dev/package.json +++ b/packages/xarc-app-dev/package.json @@ -90,6 +90,7 @@ "serve-index-fs": "^1.10.1", "subapp-util": "^1.1.2", "sudo-prompt": "^8.2.5", + "tslib": "^2.1.0", "visual-logger": "^1.1.3", "webpack-dev-middleware": "^3.7.2", "webpack-hot-middleware": "^2.25.0", diff --git a/packages/xarc-app-dev/src/config/babel/babelrc.ts b/packages/xarc-app-dev/src/config/babel/babelrc.ts index 5d0b20b4c..6ca6d03e3 100644 --- a/packages/xarc-app-dev/src/config/babel/babelrc.ts +++ b/packages/xarc-app-dev/src/config/babel/babelrc.ts @@ -134,7 +134,7 @@ const basePlugins = [ // ], !isNodeTarget && "transform-node-env-inline", !isNodeTarget && "babel-plugin-lodash", - !isNodeTarget && "@babel/plugin-transform-runtime", + "@babel/plugin-transform-runtime", addFlowPlugin && [ "@babel/plugin-transform-flow-strip-types", { requireDirective: flowRequireDirective, ...enableFlow } diff --git a/packages/xarc-app-dev/src/lib/dev-tasks.ts b/packages/xarc-app-dev/src/lib/dev-tasks.ts index 2205efe8c..b763616b0 100644 --- a/packages/xarc-app-dev/src/lib/dev-tasks.ts +++ b/packages/xarc-app-dev/src/lib/dev-tasks.ts @@ -20,12 +20,14 @@ const { getWebpackStartConfig, setWebpackProfile } = require("@xarc/webpack/lib/ const chokidar = require("chokidar"); const { spawn } = require("child_process"); const scanDir = require("filter-scan-dir"); +import * as _ from "lodash"; const xsh = require("xsh"); const logger = require("./logger"); import { createGitIgnoreDir } from "./utils"; import { jestTestDirectories } from "./tasks/constants"; import { eslintTasks } from "./tasks/eslint"; +import { updateAppDep } from "./tasks/package-json"; export { xclap }; @@ -298,6 +300,8 @@ ie >= 11 logger.info(`Generating ${configRcFile} for you - please commit it.`); } + updateAppDep(xarcCwd); + /* * * For information on how to specify a task, see: diff --git a/packages/xarc-app-dev/src/lib/tasks/package-json.ts b/packages/xarc-app-dev/src/lib/tasks/package-json.ts new file mode 100644 index 000000000..33ba361ab --- /dev/null +++ b/packages/xarc-app-dev/src/lib/tasks/package-json.ts @@ -0,0 +1,79 @@ +import * as Fs from "fs"; +import * as Path from "path"; +import * as _ from "lodash"; + +/** + * Add a list of packages to a package.json's depedencies + * @param pkgJson - package.json data + * @param packages - list of packages to add + * @param dep - which dependencies section to add + * + * @returns packages actually added + */ +export const addDepToPkgJson = (pkgJson: any, packages: Record, dep: string) => { + const section = (pkgJson[dep] = pkgJson[dep] || {}); + const added = {}; + Object.keys(packages).forEach(name => { + if (!section.hasOwnProperty(name)) { + section[name] = packages[name]; + added[name] = packages[name]; + } + }); + + if (!_.isEmpty(added)) { + pkgJson[dep] = Object.keys(section) + .sort() + .reduce((sorted, key) => { + sorted[key] = section[key]; + return sorted; + }, {}); + } + + return added; +}; + +/** + * Load a package.json from dir + * @param dir - directory + * @returns package.json object + */ +export const loadPkgJson = (dir: string) => { + return JSON.parse(Fs.readFileSync(Path.resolve(dir, "package.json"), "utf-8")); +}; + +/** + * Save a package.json to dir + * @param dir - directory + * @params pkgJson - package.json object + * @returns none + */ +export const savePkgJson = (dir: string, pkgJson: any) => { + return Fs.writeFileSync(Path.resolve(dir, "package.json"), JSON.stringify(pkgJson, null, 2)); +}; + +/* eslint-disable @typescript-eslint/no-var-requires */ +const xarcAppPkgJson = require("@xarc/app/package.json"); +const logger = require("../logger"); + +/** + * Update app's dependencies + * + * @param xarcCwd - CWD for app + * @returns nothing + */ +export const updateAppDep = (xarcCwd: string) => { + const appPkg = loadPkgJson(xarcCwd); + const added = addDepToPkgJson( + appPkg, + _.pick(xarcAppPkgJson.dependencies, ["@babel/runtime"]), + "dependencies" + ); + if (!_.isEmpty(added)) { + savePkgJson(xarcCwd, appPkg); + logger.info( + `*** + Added these packages to your dependencies, please run install again: ${Object.keys(added)} +***` + ); + } +}; diff --git a/packages/xarc-app-dev/tsconfig.json b/packages/xarc-app-dev/tsconfig.json index fe1aef8e2..27b2c37d7 100644 --- a/packages/xarc-app-dev/tsconfig.json +++ b/packages/xarc-app-dev/tsconfig.json @@ -13,7 +13,8 @@ "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, "alwaysStrict": true, - "strictFunctionTypes": true + "strictFunctionTypes": true, + "importHelpers": true }, "include": ["src"], "exclude": ["node_modules", "lib", "config", "dist", "src/**/*.js"] diff --git a/packages/xarc-app/package.json b/packages/xarc-app/package.json index 11f8555a7..049f34291 100644 --- a/packages/xarc-app/package.json +++ b/packages/xarc-app/package.json @@ -36,13 +36,14 @@ "Joel Chen " ], "dependencies": { - "@babel/runtime": "^7.8.3", + "@babel/runtime": "^7.12.5", "babel-plugin-react-css-modules": "^5.2.6", "css-modules-require-hook": "^4.0.2", "ignore-styles": "^5.0.1", "isomorphic-loader": "^4.3.0", "optional-require": "^1.0.0", - "subapp-util": "^1.1.2" + "subapp-util": "^1.1.2", + "tslib": "^2.1.0" }, "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.1", diff --git a/packages/xarc-app/tsconfig.json b/packages/xarc-app/tsconfig.json index 042c6d8fb..2f42f85d3 100644 --- a/packages/xarc-app/tsconfig.json +++ b/packages/xarc-app/tsconfig.json @@ -1,24 +1,19 @@ { "compilerOptions": { "outDir": "dist", - "lib": [ - "es2018" - ], + "lib": ["es2018"], "module": "CommonJS", "esModuleInterop": false, "target": "ES2018", "preserveConstEnums": true, "sourceMap": true, "declaration": true, - "types": [ - "node" - ], + "types": ["node"], "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, "alwaysStrict": true, - "strictFunctionTypes": true + "strictFunctionTypes": true, + "importHelpers": true }, - "include": [ - "src" - ] + "include": ["src"] } diff --git a/samples/react-jest-app/package.json b/samples/react-jest-app/package.json index f97d71b00..89d816400 100644 --- a/samples/react-jest-app/package.json +++ b/samples/react-jest-app/package.json @@ -41,6 +41,7 @@ "prod": "echo 'Starting standalone server in PROD mode'; NODE_ENV=production node ./lib/server/" }, "dependencies": { + "@babel/runtime": "^7.12.5", "@xarc/app": "^5.3.4", "bluebird": "^3.4.6", "electrode-archetype-opt-react": "^2.0.4", @@ -50,8 +51,8 @@ "electrode-redux-router-engine": "^2.0.0", "electrode-server": "^1.0.0", "electrode-static-paths": "^1.0.0", - "good-console": "^7.1.0", "good": "^7.3.0", + "good-console": "^7.1.0", "lodash": "^4.17.10", "milligram": "^1.3.0", "react-notify-toast": "^0.4.1", @@ -89,4 +90,4 @@ } }, "optionalDependencies": {} -} +} \ No newline at end of file diff --git a/samples/stylus-sample/package.json b/samples/stylus-sample/package.json index e181164f0..4b8e3bc66 100644 --- a/samples/stylus-sample/package.json +++ b/samples/stylus-sample/package.json @@ -34,6 +34,7 @@ "prod": "echo 'Starting standalone server in PROD mode'; NODE_ENV=production node ./lib/server/" }, "dependencies": { + "@babel/runtime": "^7.12.5", "@xarc/app": "../../packages/xarc-app", "bluebird": "^3.4.6", "electrode-archetype-opt-react": "^2.0.4", @@ -67,4 +68,4 @@ "@xarc/opt-stylus": "../../packages/xarc-opt-stylus" } } -} +} \ No newline at end of file diff --git a/samples/subapp2-todo-app/package.json b/samples/subapp2-todo-app/package.json index f18f2f2da..71df537b3 100644 --- a/samples/subapp2-todo-app/package.json +++ b/samples/subapp2-todo-app/package.json @@ -30,6 +30,7 @@ "npm": ">= 6" }, "dependencies": { + "@babel/runtime": "^7.12.5", "@xarc/app": "^8.2.0", "@xarc/fastify-server": "^2.0.0", "@xarc/react": "^0.1.0", @@ -52,4 +53,4 @@ "@xarc/app-dev": "../../packages/xarc-app-dev" } } -} +} \ No newline at end of file diff --git a/samples/universal-react-node/package.json b/samples/universal-react-node/package.json index 160efb60a..bbee45709 100644 --- a/samples/universal-react-node/package.json +++ b/samples/universal-react-node/package.json @@ -32,9 +32,12 @@ "flow-typed-install": "clap flow-typed-install" }, "dependencies": { + "@babel/runtime": "^7.12.5", + "@xarc/app": "^7.0.5", "above-the-fold-only-server-render": "^1.1.0", "bluebird": "^3.4.6", - "@xarc/app": "^7.0.5", + "electrode-archetype-opt-react": "^2.0.4", + "electrode-archetype-opt-react-intl": "^1.0.0", "electrode-csrf-jwt": "^1.0.0", "electrode-react-webapp": "^3.2.0", "electrode-redux-router-engine": "^2.1.8", @@ -50,9 +53,7 @@ "react-router-config": "^5.1.1", "react-router-dom": "^5.1.2", "react-vendor-dll": "../react-vendor-dll", - "uuid": "^3.0.1", - "electrode-archetype-opt-react": "^2.0.4", - "electrode-archetype-opt-react-intl": "^1.0.0" + "uuid": "^3.0.1" }, "devDependencies": { "@xarc/app-dev": "^7.0.5", @@ -85,4 +86,4 @@ } }, "optionalDependencies": {} -} +} \ No newline at end of file