Skip to content

Commit

Permalink
feat: ast mono repo poc (#15610)
Browse files Browse the repository at this point in the history
* POC for Shared AST Logic using Yarn Symlinks

* fix: preinstall script for bundling shared packages

* Merge commit

* fix: updated the script to link, unlink the package as shared dep

* fix: updated dependencies

* Add a post-install script and fix yarn.lock file

* Remove commented code

* fix: added verification script, readme, moved scripts to shared

Co-authored-by: Ayangade Adeoluwa <[email protected]>
  • Loading branch information
AmanAgarwal041 and Irongade authored Aug 11, 2022
1 parent ee32ffb commit 3b00508
Show file tree
Hide file tree
Showing 15 changed files with 1,632 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@
"test:unit": "$(npm bin)/jest -b --colors --no-cache --silent --coverage --collectCoverage=true --coverageDirectory='../../' --coverageReporters='json-summary'",
"test:jest": "$(npm bin)/jest --watch",
"generate:widget": "plop --plopfile generators/index.js",
"postinstall": "patch-package"
"postinstall": "patch-package && CURRENT_SCOPE=client node ../shared/install-dependencies.js",
"preinstall": "CURRENT_SCOPE=client node ../shared/build-shared-dep.js"
},
"browserslist": [
">0.2%",
Expand Down
4 changes: 4 additions & 0 deletions app/rts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
"socket.io-adapter": "^2.3.2",
"source-map-support": "^0.5.19",
"typescript": "^4.2.3"
},
"scripts": {
"preinstall": "CURRENT_SCOPE=rts node ../shared/build-shared-dep.js",
"postinstall": "CURRENT_SCOPE=rts node ../shared/install-dependencies.js"
}
}
4 changes: 4 additions & 0 deletions app/shared/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**build
**node_modules
yarn-error.log
yarn.lock
19 changes: 19 additions & 0 deletions app/shared/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Shared Dependencies
We wanted to share common logic with different applications within our repo, so we picked [yarn symlinks](https://classic.yarnpkg.com/en/docs/cli/link) as our approach to tackle this problem. Following are the way in which you can take advantage of the module sharing architecture.

## Creation of a Shared Module
- Create a directory inside `shared` directory with name eg. `abc`
- Inside `package.json` of module, keep the name like `@shared/abc`
- Add a rollup config to generate `package.json` after the module is build

## Installation of Shared Modules
- Add an entry for an application inside `shared-dependencies.json` eg. for `client` there should be an entry `"client": []`
- Add the name of the shared module in the entry of the application in the above file eg. `"client": ["@shared/abc"]`
- If the application does not have any postinstall or preinstall scripts for shared modules then add the two commands described below in the application's (eg. `client`) `package.json` :
`"postinstall": "CURRENT_SCOPE=client node ../shared/install-dependencies.js"`
`"preinstall": "CURRENT_SCOPE=client node ../shared/build-shared-dep.js"`
CURRENT_SCOPE is the environment variable that's being used in the scripts

## Verifying the Installed Shared Modules
- Run `yarn run verify` inside `shared` directory to verify shared dependencies for an application.

1 change: 1 addition & 0 deletions app/shared/ast/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "@shared/ast";
37 changes: 37 additions & 0 deletions app/shared/ast/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@shared/ast",
"version": "1.0.0",
"description": "",
"main": "build/index.js",
"module": "build/index.es.js",
"types": "build/index.d.ts",
"files": [
"build"
],
"publishConfig": {
"directory": "build"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c",
"start": "rollup -c",
"link-package": "yarn install && rollup -c && cd build && yarn link"
},
"dependencies": {
"acorn-walk": "^8.2.0",
"rollup": "^2.77.0",
"typescript": "4.5.5"
},
"devDependencies": {
"@babel/preset-typescript": "^7.17.12",
"@rollup/plugin-commonjs": "^22.0.0",
"@typescript-eslint/eslint-plugin": "^5.25.0",
"@typescript-eslint/parser": "^5.25.0",
"rollup-plugin-generate-package-json": "^3.2.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-typescript2": "^0.32.0",
"ts-jest": "27.0.0"
},
"author": "",
"license": "ISC"
}
31 changes: 31 additions & 0 deletions app/shared/ast/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import peerDepsExternal from "rollup-plugin-peer-deps-external";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "rollup-plugin-typescript2";
import generatePackageJson from "rollup-plugin-generate-package-json";

const packageJson = require("./package.json");

export default {
// TODO: Figure out regex where each directory can be a separate module without having to manually add them
input: ["src/index.ts"],
output: [
{
file: packageJson.module,
format: "esm",
sourcemap: true,
},
{
file: packageJson.main,
format: "cjs",
sourcemap: true,
},
],
plugins: [
peerDepsExternal(),
commonjs(),
typescript({ useTsconfigDeclarationDir: true }),
generatePackageJson({
baseContents: (pkg) => pkg,
}),
],
};
9 changes: 9 additions & 0 deletions app/shared/ast/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const isNullOrWhitespace = (value: string) =>
value === undefined || value === null || !value.trim();
const isNullX = (value: string) => value === undefined || value === null;

export type Animal = {
name: string;
age: number;
};
export { isNullOrWhitespace, isNullX };
37 changes: 37 additions & 0 deletions app/shared/ast/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"compilerOptions": {
"target": "ES6",
"lib": [
"DOM",
"ES6",
"DOM.Iterable",
"ScriptHost",
"ES2016.Array.Include",
"es2020.string",
"esnext"
],
"strict": true,
"declaration": true,
"declarationDir": "build",
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"downlevelIteration": true,
"experimentalDecorators": true,
"importHelpers": true,
"typeRoots": ["./typings", "./node_modules/@types"],
"sourceMap": true,
"baseUrl": "./src",
"noFallthroughCasesInSwitch": true
},
"include": ["./src/**/*", "index.d.ts"],
"exclude": ["node_modules", "build"]
}
Loading

0 comments on commit 3b00508

Please sign in to comment.