Skip to content

Commit

Permalink
enable babel runtime for node
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jan 23, 2021
1 parent dc3d39a commit 3ccc7f4
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/xarc-app-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/src/config/babel/babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 4 additions & 0 deletions packages/xarc-app-dev/src/lib/dev-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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:
Expand Down
79 changes: 79 additions & 0 deletions packages/xarc-app-dev/src/lib/tasks/package-json.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>, 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)}
***`
);
}
};
3 changes: 2 additions & 1 deletion packages/xarc-app-dev/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 3 additions & 2 deletions packages/xarc-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
"Joel Chen <[email protected]>"
],
"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",
Expand Down
15 changes: 5 additions & 10 deletions packages/xarc-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
5 changes: 3 additions & 2 deletions samples/react-jest-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -89,4 +90,4 @@
}
},
"optionalDependencies": {}
}
}
3 changes: 2 additions & 1 deletion samples/stylus-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -67,4 +68,4 @@
"@xarc/opt-stylus": "../../packages/xarc-opt-stylus"
}
}
}
}
3 changes: 2 additions & 1 deletion samples/subapp2-todo-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -52,4 +53,4 @@
"@xarc/app-dev": "../../packages/xarc-app-dev"
}
}
}
}
11 changes: 6 additions & 5 deletions samples/universal-react-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -85,4 +86,4 @@
}
},
"optionalDependencies": {}
}
}

0 comments on commit 3ccc7f4

Please sign in to comment.