chore: replace microbundle with rollup (#3115)

* refactor: mv gas estimate chains out of component

* chore: replace microbundle with rollup

* chore: use ts rollup config

* chore: rename lib to widgets

* chore: add rollup doc comment

* feat: rollup typings

* fix: retain tsconfig decl dir
This commit is contained in:
Zach Pomerantz 2022-01-13 17:11:27 -08:00 committed by GitHub
parent e68e1afd9d
commit e5a1cb4276
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 1148 additions and 1445 deletions

@ -1,4 +1,4 @@
name: Bundle Dependency Check name: Widgets
on: on:
push: push:
branches: branches:
@ -8,8 +8,8 @@ on:
- main - main
jobs: jobs:
depcheck: build:
name: Bundle depcheck name: Build
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
@ -36,8 +36,5 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: yarn install --frozen-lockfile run: yarn install --frozen-lockfile
- name: Bundle - name: Build
run: yarn bundle run: yarn widgets:build
- name: Depcheck
run: yarn bundle:depcheck

6
.gitignore vendored

@ -11,10 +11,6 @@
/src/locales/**/pseudo.po /src/locales/**/pseudo.po
/src/state/data/generated.ts /src/state/data/generated.ts
# generated assets
/src/lib/assets/svg/*.tsx
/src/lib/assets/fonts/*.css
# dependencies # dependencies
/node_modules /node_modules
@ -24,7 +20,7 @@
# production # production
/build /build
# bundle # widgets
/dist /dist
# misc # misc

@ -1,28 +0,0 @@
#!/bin/node
/**
* Checks if any dependencies have been bundled with the interface library.
* Exits with non-zero status if dependencies are included in the bundle.
*/
/* eslint-disable */
const { readFile } = require('fs')
function checkDeps(err, sourcemap) {
if (err) {
console.error(err)
process.exit(1)
}
const includesDeps = sourcemap.includes('node_modules')
if (includesDeps) {
const deps = [...sourcemap.toString().matchAll(/node_modules[\\\/]([^\\\/]*)/g)].map(([, match]) => match)
console.error(`
Sourcemap includes node_modules folder(s). External deps must be bundled under "dependencies".
To fix, run: \`yarn add ${deps.join(' ')}\`
`)
process.exit(1)
}
}
readFile('dist/interface.esm.js.map', checkDeps)

@ -23,7 +23,11 @@
"@reach/portal": "^0.10.3", "@reach/portal": "^0.10.3",
"@react-hook/window-scroll": "^1.3.0", "@react-hook/window-scroll": "^1.3.0",
"@reduxjs/toolkit": "^1.6.1", "@reduxjs/toolkit": "^1.6.1",
"@svgr/cli": "^5.5.0", "@rollup/plugin-eslint": "^8.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-replace": "^3.0.1",
"@rollup/plugin-url": "^6.1.0",
"@svgr/rollup": "^6.2.0",
"@testing-library/jest-dom": "^5.14.1", "@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0", "@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.2", "@testing-library/react-hooks": "^7.0.2",
@ -85,7 +89,6 @@
"graphql-request": "^3.4.0", "graphql-request": "^3.4.0",
"inter-ui": "^3.13.1", "inter-ui": "^3.13.1",
"jest-styled-components": "^7.0.5", "jest-styled-components": "^7.0.5",
"microbundle": "^0.13.3",
"ms.macro": "^2.0.0", "ms.macro": "^2.0.0",
"polyfill-object.fromentries": "^1.0.1", "polyfill-object.fromentries": "^1.0.1",
"prettier": "^2.2.1", "prettier": "^2.2.1",
@ -100,6 +103,10 @@
"react-spring": "^8.0.27", "react-spring": "^8.0.27",
"react-use-gesture": "^6.0.14", "react-use-gesture": "^6.0.14",
"redux-localstorage-simple": "^2.3.1", "redux-localstorage-simple": "^2.3.1",
"rollup": "^2.63.0",
"rollup-plugin-dts": "^4.1.0",
"rollup-plugin-scss": "^3.0.0",
"rollup-plugin-typescript2": "^0.31.1",
"sass": "^1.45.1", "sass": "^1.45.1",
"serve": "^11.3.2", "serve": "^11.3.2",
"start-server-and-test": "^1.11.0", "start-server-and-test": "^1.11.0",
@ -126,17 +133,13 @@
"i18n:extract": "lingui extract --locale en-US", "i18n:extract": "lingui extract --locale en-US",
"i18n:compile": "yarn i18n:extract && lingui compile", "i18n:compile": "yarn i18n:extract && lingui compile",
"i18n:pseudo": "lingui extract --locale pseudo && lingui compile", "i18n:pseudo": "lingui extract --locale pseudo && lingui compile",
"postinstall": "yarn contracts:compile && yarn graphql:generate && yarn i18n:compile && yarn assets:generate", "postinstall": "yarn contracts:compile && yarn graphql:generate && yarn i18n:compile",
"start": "react-scripts start", "start": "react-scripts start",
"build": "react-scripts build", "build": "react-scripts build",
"test": "react-scripts test --env=./custom-test-env.js", "test": "react-scripts test --env=./custom-test-env.js",
"test:e2e": "start-server-and-test 'serve build -l 3000' http://localhost:3000 'cypress run --record'", "test:e2e": "start-server-and-test 'serve build -l 3000' http://localhost:3000 'cypress run --record'",
"assets:generate": "yarn assets:svg:generate && yarn assets:font:generate", "widgets:start": "cross-env FAST_REFRESH=false REACT_APP_IS_WIDGET=true cosmos",
"assets:svg:generate": "svgr -d src/lib/assets/svg --ext tsx --typescript src/lib/assets/svg && rm src/lib/assets/svg/index.tsx", "widgets:build": "rollup --config --failAfterWarnings --configPlugin typescript2"
"assets:font:generate": "sass src/lib/assets/fonts/index.scss src/lib/assets/fonts/index.css --no-source-map -I node_modules",
"bundle": "microbundle --define process.env.REACT_APP_IS_WIDGET=true --tsconfig tsconfig.lib.json src/lib/index.tsx --format esm,cjs",
"bundle:depcheck": "node depcheck.js",
"cosmos": "cross-env FAST_REFRESH=false REACT_APP_IS_WIDGET=true cosmos"
}, },
"browserslist": { "browserslist": {
"production": [ "production": [

63
rollup.config.ts Normal file

@ -0,0 +1,63 @@
/**
* Bundles the widgets library, which is released independently of the interface application.
* This library lives in src/lib, but shares code with the interface application.
*/
import eslint from '@rollup/plugin-eslint'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import url from '@rollup/plugin-url'
import svgr from '@svgr/rollup'
import dts from 'rollup-plugin-dts'
import sass from 'rollup-plugin-scss'
import typescript from 'rollup-plugin-typescript2'
import { dependencies } from './package.json'
const deps = Object.keys(dependencies)
const replacements = {
'process.env.REACT_APP_IS_WIDGET': true,
}
const library = {
input: 'src/lib/index.tsx',
output: [
{
file: 'dist/widgets.js',
format: 'cjs',
inlineDynamicImports: true,
sourcemap: true,
},
{
file: 'dist/widgets.esm.js',
format: 'esm',
inlineDynamicImports: true,
sourcemap: true,
},
],
// necessary because some nested imports (eg jotai/*) would otherwise not resolve.
external: (source: string) => Boolean(deps.find((dep) => source === dep || source.startsWith(dep + '/'))),
plugins: [
eslint({ include: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'] }),
json(), // imports json
replace({ ...replacements, preventAssignment: true }),
url(), // imports files (including svgs) as data URIs
svgr({ exportType: 'named', svgo: false }), // imports svgs as React components
sass(), // imports sass styles
typescript({ tsconfig: './tsconfig.lib.json', useTsconfigDeclarationDir: true }),
],
}
const typings = {
input: 'dist/dts/lib/index.d.ts',
output: {
file: 'dist/widgets.d.ts',
format: 'es',
},
external: (source: string) => source.endsWith('.scss'),
plugins: [dts({ compilerOptions: { baseUrl: 'dist/dts' } })],
}
const config = [library, typings]
export default config

@ -2,6 +2,7 @@ import { Trans } from '@lingui/macro'
import { Currency, Percent, TradeType } from '@uniswap/sdk-core' import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
import Card from 'components/Card' import Card from 'components/Card'
import { LoadingRows } from 'components/Loader/styled' import { LoadingRows } from 'components/Loader/styled'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'constants/chains'
import useActiveWeb3React from 'hooks/useActiveWeb3React' import useActiveWeb3React from 'hooks/useActiveWeb3React'
import { useContext, useMemo } from 'react' import { useContext, useMemo } from 'react'
import { InterfaceTrade } from 'state/routing/types' import { InterfaceTrade } from 'state/routing/types'
@ -12,7 +13,6 @@ import { computeRealizedLPFeePercent } from '../../utils/prices'
import { AutoColumn } from '../Column' import { AutoColumn } from '../Column'
import { RowBetween, RowFixed } from '../Row' import { RowBetween, RowFixed } from '../Row'
import FormattedPriceImpact from './FormattedPriceImpact' import FormattedPriceImpact from './FormattedPriceImpact'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from './GasEstimateBadge'
const StyledCard = styled(Card)` const StyledCard = styled(Card)`
padding: 0; padding: 0;

@ -10,7 +10,6 @@ import styled from 'styled-components/macro'
import { ThemedText } from 'theme' import { ThemedText } from 'theme'
import { ReactComponent as GasIcon } from '../../assets/images/gas-icon.svg' import { ReactComponent as GasIcon } from '../../assets/images/gas-icon.svg'
import { SupportedChainId } from '../../constants/chains'
import { ResponsiveTooltipContainer } from './styleds' import { ResponsiveTooltipContainer } from './styleds'
import SwapRoute from './SwapRoute' import SwapRoute from './SwapRoute'
@ -32,8 +31,6 @@ const StyledGasIcon = styled(GasIcon)`
} }
` `
export const SUPPORTED_GAS_ESTIMATE_CHAIN_IDS = [SupportedChainId.MAINNET, SupportedChainId.POLYGON]
export default function GasEstimateBadge({ export default function GasEstimateBadge({
trade, trade,
loading, loading,

@ -6,6 +6,7 @@ import { AutoColumn } from 'components/Column'
import { LoadingOpacityContainer } from 'components/Loader/styled' import { LoadingOpacityContainer } from 'components/Loader/styled'
import Row, { RowBetween, RowFixed } from 'components/Row' import Row, { RowBetween, RowFixed } from 'components/Row'
import { MouseoverTooltipContent } from 'components/Tooltip' import { MouseoverTooltipContent } from 'components/Tooltip'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'constants/chains'
import useActiveWeb3React from 'hooks/useActiveWeb3React' import useActiveWeb3React from 'hooks/useActiveWeb3React'
import { darken } from 'polished' import { darken } from 'polished'
import { useState } from 'react' import { useState } from 'react'
@ -15,7 +16,7 @@ import styled, { keyframes, useTheme } from 'styled-components/macro'
import { HideSmall, ThemedText } from 'theme' import { HideSmall, ThemedText } from 'theme'
import { AdvancedSwapDetails } from './AdvancedSwapDetails' import { AdvancedSwapDetails } from './AdvancedSwapDetails'
import GasEstimateBadge, { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from './GasEstimateBadge' import GasEstimateBadge from './GasEstimateBadge'
import { ResponsiveTooltipContainer } from './styleds' import { ResponsiveTooltipContainer } from './styleds'
import SwapRoute from './SwapRoute' import SwapRoute from './SwapRoute'
import TradePrice from './TradePrice' import TradePrice from './TradePrice'

@ -7,6 +7,7 @@ import { AutoColumn } from 'components/Column'
import { LoadingRows } from 'components/Loader/styled' import { LoadingRows } from 'components/Loader/styled'
import RoutingDiagram, { RoutingDiagramEntry } from 'components/RoutingDiagram/RoutingDiagram' import RoutingDiagram, { RoutingDiagramEntry } from 'components/RoutingDiagram/RoutingDiagram'
import { AutoRow, RowBetween } from 'components/Row' import { AutoRow, RowBetween } from 'components/Row'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'constants/chains'
import useActiveWeb3React from 'hooks/useActiveWeb3React' import useActiveWeb3React from 'hooks/useActiveWeb3React'
import useAutoRouterSupported from 'hooks/useAutoRouterSupported' import useAutoRouterSupported from 'hooks/useAutoRouterSupported'
import { memo, useState } from 'react' import { memo, useState } from 'react'
@ -16,7 +17,6 @@ import { useDarkModeManager } from 'state/user/hooks'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
import { Separator, ThemedText } from 'theme' import { Separator, ThemedText } from 'theme'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from './GasEstimateBadge'
import { AutoRouterLabel, AutoRouterLogo } from './RouterLabel' import { AutoRouterLabel, AutoRouterLogo } from './RouterLabel'
const Wrapper = styled(AutoColumn)<{ darkMode?: boolean; fixedOpen?: boolean }>` const Wrapper = styled(AutoColumn)<{ darkMode?: boolean; fixedOpen?: boolean }>`

@ -25,6 +25,8 @@ export const ALL_SUPPORTED_CHAIN_IDS: SupportedChainId[] = Object.values(Support
(id) => typeof id === 'number' (id) => typeof id === 'number'
) as SupportedChainId[] ) as SupportedChainId[]
export const SUPPORTED_GAS_ESTIMATE_CHAIN_IDS = [SupportedChainId.MAINNET, SupportedChainId.POLYGON]
/** /**
* All the chain IDs that are running the Ethereum protocol. * All the chain IDs that are running the Ethereum protocol.
*/ */

@ -1,6 +1,6 @@
import { Trade } from '@uniswap/router-sdk' import { Trade } from '@uniswap/router-sdk'
import { Currency, CurrencyAmount, Percent, TradeType } from '@uniswap/sdk-core' import { Currency, CurrencyAmount, Percent, TradeType } from '@uniswap/sdk-core'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'components/swap/GasEstimateBadge' import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'constants/chains'
import { L2_CHAIN_IDS } from 'constants/chains' import { L2_CHAIN_IDS } from 'constants/chains'
import useActiveWeb3React from 'hooks/useActiveWeb3React' import useActiveWeb3React from 'hooks/useActiveWeb3React'
import JSBI from 'jsbi' import JSBI from 'jsbi'

@ -1,8 +1,4 @@
/* eslint-disable no-restricted-imports */ /* eslint-disable no-restricted-imports */
import CheckIcon from 'lib/assets/svg/Check'
import ExpandoIcon from 'lib/assets/svg/Expando'
import LogoIcon from 'lib/assets/svg/Logo'
import SpinnerIcon from 'lib/assets/svg/Spinner'
import styled, { Color, css, keyframes } from 'lib/theme' import styled, { Color, css, keyframes } from 'lib/theme'
import { FunctionComponent, SVGProps } from 'react' import { FunctionComponent, SVGProps } from 'react'
import { Icon as FeatherIcon } from 'react-feather' import { Icon as FeatherIcon } from 'react-feather'
@ -22,6 +18,11 @@ import {
X as XIcon, X as XIcon,
} from 'react-feather' } from 'react-feather'
import { ReactComponent as CheckIcon } from '../assets/svg/check.svg'
import { ReactComponent as ExpandoIcon } from '../assets/svg/expando.svg'
import { ReactComponent as LogoIcon } from '../assets/svg/logo.svg'
import { ReactComponent as SpinnerIcon } from '../assets/svg/spinner.svg'
type SVGIcon = FunctionComponent<SVGProps<SVGSVGElement>> type SVGIcon = FunctionComponent<SVGProps<SVGSVGElement>>
function icon(Icon: FeatherIcon | SVGIcon) { function icon(Icon: FeatherIcon | SVGIcon) {

43
src/lib/lib.d.ts vendored Normal file

@ -0,0 +1,43 @@
declare module '*.avif' {
const src: string
export default src
}
declare module '*.bmp' {
const src: string
export default src
}
declare module '*.gif' {
const src: string
export default src
}
declare module '*.jpg' {
const src: string
export default src
}
declare module '*.jpeg' {
const src: string
export default src
}
declare module '*.png' {
const src: string
export default src
}
declare module '*.webp' {
const src: string
export default src
}
declare module '*.svg' {
import * as React from 'react'
export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement> & { title?: string }>
const src: string
export default src
}

@ -1,4 +1,4 @@
import '../assets/fonts/index.css' // microbundle requires relative css paths import '../assets/fonts.scss'
import { mix, transparentize } from 'polished' import { mix, transparentize } from 'polished'
import { createContext, ReactNode, useContext, useMemo, useState } from 'react' import { createContext, ReactNode, useContext, useMemo, useState } from 'react'

@ -1,15 +1,18 @@
{ {
"extends": "./tsconfig.base.json", "extends": "./tsconfig.base.json",
"compilerOptions": { "compilerOptions": {
"jsx": "preserve", "jsx": "react-jsx",
"declaration": true,
"declarationDir": "dist/dts",
"baseUrl": "src/lib", "baseUrl": "src/lib",
"paths": { "paths": {
"lib/*": ["./*"], "lib/*": ["./*"],
"abis/*": ["../abis/*"], "abis/*": ["../abis/*"],
"assets/*": ["../assets/*"],
"constants/*": ["../constants/*"], "constants/*": ["../constants/*"],
"hooks/*": ["../hooks/*"], "hooks/*": ["../hooks/*"],
"state/*": ["../state/*"], "state/*": ["../state/*"],
"types/*": ["../types/*"] "types/*": ["../types/*"],
}, },
}, },
"exclude": ["node_modules", "src/lib/**/*.test.*"], "exclude": ["node_modules", "src/lib/**/*.test.*"],

2387
yarn.lock

File diff suppressed because it is too large Load Diff