Skip to content

Commit

Permalink
feat(sdk): make library tree shakeable (#331)
Browse files Browse the repository at this point in the history
* chore(sdk): move tests in dedicated folder

* chore(sdk): flatten module structure

* chore(sdk): fix broken import paths

* chore(sdk): make the package tree shakeable

* chore(frontend): use esbuild instead of terser to minify build

* chore(sdk): fix liting issues

* chore(frontend): minimize css

* chore(frontend): remove console log

---------

Co-authored-by: luzzifoss <[email protected]>
  • Loading branch information
luzzif and luzzifoss authored Aug 9, 2023
1 parent 1f78d72 commit 338ada3
Show file tree
Hide file tree
Showing 29 changed files with 681 additions and 91 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-kings-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/sdk": minor
---

Make the SDK package tree shakeable
5 changes: 5 additions & 0 deletions .changeset/giant-cups-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/host-frontend": minor
---

Use ESBuild instead of Terser to minify build
5 changes: 5 additions & 0 deletions .changeset/grumpy-frogs-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/sdk": patch
---

Move tests in dedicated tests folder
5 changes: 5 additions & 0 deletions .changeset/proud-lizards-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/host-frontend": minor
---

Minify CSS
5 changes: 5 additions & 0 deletions .changeset/sharp-falcons-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/sdk": patch
---

Flattened module structure
14 changes: 14 additions & 0 deletions packages/frontend/craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require("dotenv").config();

const WorkboxWebpackPlugin = require("workbox-webpack-plugin");
const webpack = require("webpack");
const { EsbuildPlugin } = require("esbuild-loader");
const { join } = require("path");
const shared = require("./shared-dependencies.json");
const { getEnv } = require("./utils/env");
Expand All @@ -10,6 +11,19 @@ module.exports = {
webpack: {
configure: (config, { env }) => {
const production = env === "production";

for (const rule of config.module.rules) {
for (oneOf of rule.oneOf) {
if (!oneOf.use) continue;
for (const use of oneOf.use) {
if (use.loader.includes("postcss-loader")) {
use.options.postcssOptions.plugins.push("cssnano");
}
}
}
}

config.optimization.minimizer = [new EsbuildPlugin({})];
config.plugins.push(
new webpack.container.ModuleFederationPlugin({
name: "host",
Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
"autoprefixer": "^10.4.14",
"buffer": "^6.0.3",
"cross-env": "^7.0.3",
"cssnano": "^6.0.1",
"dotenv": "^16.3.1",
"esbuild-loader": "^4.0.0",
"eslint-config-custom": "*",
"eslint-plugin-prettier": "^5.0.0",
"i18next": "^23.2.3",
Expand Down
77 changes: 77 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,86 @@
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"typings": "./dist/index.d.ts",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./commons": {
"types": "./dist/commons.d.ts",
"import": "./dist/commons.js"
},
"./cacher": {
"types": "./dist/cacher.d.ts",
"import": "./dist/cacher.js"
},
"./fetcher": {
"types": "./dist/fetcher/index.d.ts",
"import": "./dist/fetcher/index.js"
},
"./utils": {
"types": "./dist/utils/index.d.ts",
"import": "./dist/utils/index.js"
},
"./entities/amount": {
"types": "./dist/entities/amount.d.ts",
"import": "./dist/entities/amount.js"
},
"./entities/currency": {
"types": "./dist/entities/currency.d.ts",
"import": "./dist/entities/currency.js"
},
"./entities/kpi-token": {
"types": "./dist/entities/kpi-token.d.ts",
"import": "./dist/entities/kpi-token.js"
},
"./entities/oracle": {
"types": "./dist/entities/oracle.d.ts",
"import": "./dist/entities/oracle.js"
},
"./entities/template": {
"types": "./dist/entities/template.d.ts",
"import": "./dist/entities/template.js"
},
"./entities/token": {
"types": "./dist/entities/token.d.ts",
"import": "./dist/entities/token.js"
}
},
"typesVersions": {
"*": {
"commons": [
"./dist/commons.d.ts"
],
"cacher": [
"./dist/cacher.d.ts"
],
"fetcher": [
"./dist/fetcher/index.d.ts"
],
"utils": [
"./dist/utils/index.d.ts"
],
"entities/amount": [
"./dist/entities/amount.d.ts"
],
"entities/currency": [
"./dist/entities/currency.d.ts"
],
"entities/kpi-token": [
"./dist/entities/kpi-token.d.ts"
],
"entities/oracle": [
"./dist/entities/oracle.d.ts"
],
"entities/template": [
"./dist/entities/template.d.ts"
],
"entities/token": [
"./dist/entities/token.d.ts"
]
}
},
"publishConfig": {
Expand Down
20 changes: 16 additions & 4 deletions packages/sdk/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ import typescript from "rollup-plugin-typescript2";

export default [
{
input: resolve("./src/index.ts"),
preserveSymlinks: true,
input: [
"src/index.ts",
"src/commons.ts",
"src/cacher.ts",
"src/fetcher/index.ts",
"src/utils/index.ts",
"src/entities/amount.ts",
"src/entities/currency.ts",
"src/entities/kpi-token.ts",
"src/entities/oracle.ts",
"src/entities/template.ts",
"src/entities/token.ts",
],
plugins: [
peerDepsExternal(),
nodeResolve({ preferBuiltins: true }),
Expand All @@ -16,10 +27,11 @@ export default [
],
output: [
{
file: resolve("./dist/index.js"),
dir: resolve("./dist"),
preserveModules: true,
preserveModulesRoot: "src",
format: "es",
sourcemap: true,
inlineDynamicImports: true,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import FACTORY_ABI from "../abis/factory";
import KPI_TOKEN_ABI from "../abis/kpi-token";
import ORACLE_ABI from "../abis/oracle";
import KPI_TOKENS_MANAGER_ABI from "../abis/kpi-tokens-manager";
import ORACLES_MANAGER_ABI from "../abis/oracles-manager";
import ERC20_ABI from "../abis/erc20";
import { Cacher } from "../cacher";
import FACTORY_ABI from "./abis/factory";
import KPI_TOKEN_ABI from "./abis/kpi-token";
import ORACLE_ABI from "./abis/oracle";
import KPI_TOKENS_MANAGER_ABI from "./abis/kpi-tokens-manager";
import ORACLES_MANAGER_ABI from "./abis/oracles-manager";
import ERC20_ABI from "./abis/erc20";
import { Cacher } from "./cacher";
import { type Address } from "viem";

export enum ChainId {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Token, currencyEquals } from "../token";
import { Currency } from "../currency";
import { Token, currencyEquals } from "./token";
import { Currency } from "./currency";
import { formatUnits } from "viem";
import Decimal from "decimal.js-light";
import { enforce } from "../../utils";
import { enforce } from "../utils";

export type TokenOrCurrency = Token | Currency;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Template, ResolvedTemplate } from "../template";
import { Oracle, ResolvedOracle, ResolvedOracleWithData } from "../oracle";
import { ChainId } from "../../commons";
import { Template, ResolvedTemplate } from "./template";
import { Oracle, ResolvedOracle, ResolvedOracleWithData } from "./oracle";
import { ChainId } from "../commons";
import { type Address, type Hex } from "viem";

export interface KPITokenSpecification {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Address, type Hex } from "viem";
import { ChainId } from "../../commons";
import { Template, ResolvedTemplate } from "../template";
import { ChainId } from "../commons";
import { Template, ResolvedTemplate } from "./template";

export class BaseOracle {
constructor(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isAddress, getAddress, type Address } from "viem";
import { Currency } from "../currency";
import { enforce } from "../../utils/invariant";
import { Currency } from "./currency";
import { enforce } from "../utils/invariant";

export class Token extends Currency {
public readonly address: Address;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Address, PublicClient } from "viem";
import { ChainId } from "../../commons";
import { KPIToken } from "../../entities/kpi-token";
import { Oracle } from "../../entities/oracle";
import { ResolvedTemplate, Template } from "../../entities/template";
import { Token } from "../../entities/token";
import type { ResolvedKPITokensMap, ResolvedOraclesMap } from "../types";
import { ChainId } from "../commons";
import { KPIToken } from "../entities/kpi-token";
import { Oracle } from "../entities/oracle";
import { ResolvedTemplate, Template } from "../entities/template";
import { Token } from "../entities/token";
import type { ResolvedKPITokensMap, ResolvedOraclesMap } from "./types";

export interface FetchERC20TokensParams {
publicClient: PublicClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import { CHAIN_ADDRESSES, ChainId, ERC20_ABI } from "../../commons";
import {
cacheERC20Token,
enforce,
getCachedERC20Token,
warn,
} from "../../utils";
import { CHAIN_ADDRESSES, ChainId, ERC20_ABI } from "../commons";
import { cacheERC20Token, enforce, getCachedERC20Token, warn } from "../utils";
import { type Address } from "viem";
import { Token } from "../../entities/token";
import BYTES_NAME_ERC20_ABI from "../../abis/erc20-name-bytes";
import BYTES_SYMBOL_ERC20_ABI from "../../abis/erc20-symbol-bytes";
import { Token } from "../entities/token";
import BYTES_NAME_ERC20_ABI from "../abis/erc20-name-bytes";
import BYTES_SYMBOL_ERC20_ABI from "../abis/erc20-symbol-bytes";
import type {
FetchContentFromIPFSParams,
FetchERC20TokensParams,
ICoreFetcher,
ResolveKPITokensParams,
ResolveOraclesParams,
ResolveTemplatesParams,
} from "../abstraction";
import { KPIToken, ResolvedKPIToken } from "../../entities/kpi-token";
import { Oracle, ResolvedOracle } from "../../entities/oracle";
import {
ResolvedTemplate,
TemplateSpecification,
} from "../../entities/template";
import type { ResolvedKPITokensMap, ResolvedOraclesMap } from "../types";
} from "./abstraction";
import { KPIToken, ResolvedKPIToken } from "../entities/kpi-token";
import { Oracle, ResolvedOracle } from "../entities/oracle";
import { ResolvedTemplate, TemplateSpecification } from "../entities/template";
import type { ResolvedKPITokensMap, ResolvedOraclesMap } from "./types";

// TODO: check if validation can be extracted in its own function
export class CoreFetcher implements ICoreFetcher {
Expand Down
Loading

0 comments on commit 338ada3

Please sign in to comment.