Skip to content

Commit

Permalink
Use Rollup and Babel to create builds (#93)
Browse files Browse the repository at this point in the history
* feat: install and configure rollup

* chore: remove some circular dependencies

* feat: minify iife.min build

* feat: replace some env variables

* feat: generate package.json to set type

* feat: polyfill assert from nested dependencies

* feat: bundle more dependencies in iife and dedupe more

* feat: add polyfills to iife

* chore: remove redundant assert polyfill

* chore: update scripts and test build

* chore: temporary remove circular dependency warnings

* fix: export accountProviders so they don't get removed

* chore: add browser builds to package.json

* chore: remove some circular dependencies

* chore: remove more circular dependencies

* fix: remove instanceof usage

* chore: remove proposal plugins

* chore: remove unused dependencies
  • Loading branch information
lorisleiva authored May 9, 2022
1 parent 940a5d9 commit 388bfc0
Show file tree
Hide file tree
Showing 31 changed files with 1,983 additions and 107 deletions.
22 changes: 22 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"presets": [
[
"@babel/preset-env",
{
"bugfixes": true
}
],
["@babel/preset-typescript"]
],
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"@": "./src"
}
}
]
]
}
34 changes: 28 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
"types": "dist/types/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
"browser": {
"import": "./dist/esm-browser/index.js",
"require": "./dist/cjs-browser/index.js"
},
"node": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
}
},
"files": [
Expand Down Expand Up @@ -39,9 +45,7 @@
"preversion": "yarn check:publish-ready",
"postversion": "git push --follow-tags",
"clean": "rimraf dist",
"build": "yarn clean && yarn build:esm && yarn build:cjs",
"build:esm": "ttsc -p tsconfig.json && echo '{\"type\":\"module\"}' > dist/esm/package.json",
"build:cjs": "ttsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
"build": "yarn clean && ttsc && rollup -c",
"build:test": "ttsc -p test/tsconfig.json",
"amman:start": "DEBUG='amman:(info|error|debug)' amman start",
"amman:stop": "amman stop",
Expand Down Expand Up @@ -72,16 +76,29 @@
"tweetnacl": "^1.0.3"
},
"devDependencies": {
"@babel/core": "^7.17.10",
"@babel/preset-env": "^7.17.10",
"@babel/preset-typescript": "^7.16.7",
"@metaplex-foundation/amman": "^0.6.1",
"@ovos-media/ts-transform-paths": "^1.7.18-1",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-replace": "^4.0.0",
"@types/bn.js": "^5.1.0",
"@types/debug": "^4.1.7",
"@types/lodash.clonedeep": "^4.5.6",
"@types/mime": "^2.0.3",
"@types/sinon": "^10.0.11",
"@types/tape": "^4.13.2",
"babel-plugin-module-resolver": "^4.1.0",
"prettier": "^2.6.0",
"rimraf": "^3.0.2",
"rollup": "^2.72.0",
"rollup-plugin-generate-package-json": "^3.2.0",
"rollup-plugin-polyfill-node": "^0.9.0",
"rollup-plugin-terser": "^7.0.2",
"sinon": "^13.0.1",
"spok": "^1.4.3",
"tape": "^5.5.2",
Expand All @@ -90,5 +107,10 @@
},
"engines": {
"node": "^14.0 || >=16.0"
}
},
"browserslist": [
"defaults",
"not IE 11",
"maintained node versions"
]
}
154 changes: 154 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import nodeResolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';
import generatePackageJson from 'rollup-plugin-generate-package-json';
import pkg from './package.json';

const builds = [
{
dir: 'dist/esm',
format: 'es',
},
{
dir: 'dist/cjs',
format: 'cjs',
},
{
dir: 'dist/esm-browser',
format: 'es',
browser: true,
},
{
dir: 'dist/cjs-browser',
format: 'cjs',
browser: true,
},
{
file: 'dist/iife/index.js',
format: 'iife',
name: 'MetaplexSDK',
browser: true,
bundle: true,
},
{
file: 'dist/iife/index.min.js',
format: 'iife',
name: 'MetaplexSDK',
browser: true,
bundle: true,
minified: true,
},
];

const extensions = ['.js', '.ts'];
const allDependencies = Object.keys(pkg.dependencies);
const dependenciesToExcludeInBundle = ['@aws-sdk/client-s3'];
const dependenciesToDedupes = [
'@solana/spl-token',
'@solana/wallet-adapter-base',
'@solana/web3.js',
'abort-controller',
'bignumber.js',
'bn.js',
'bs58',
'buffer',
'cross-fetch',
'debug',
'eventemitter3',
'mime',
'tweetnacl',
];

const globals = {
'@aws-sdk/client-s3': 'AwsS3Client',
'@bundlr-network/client': 'BundlrNetworkClient',
'@metaplex-foundation/beet': 'MetaplexBeet',
'@metaplex-foundation/beet-solana': 'MetaplexBeetSolana',
'@metaplex-foundation/mpl-candy-machine': 'MetaplexMplCandyMachine',
'@metaplex-foundation/mpl-token-metadata': 'MetaplexMplTokenMetadata',
'@solana/spl-token': 'SolanaSplToken',
'@solana/wallet-adapter-base': 'SolanaWalletAdapterBase',
'@solana/web3.js': 'SolanaWeb3',
'abort-controller': 'AbortController',
'bignumber.js': 'BigNumber',
'bn.js': 'BN',
bs58: 'Bs58',
buffer: 'Buffer',
'cross-fetch': 'CrossFetch',
debug: 'Debug',
eventemitter3: 'EventEmitter3',
'lodash.clonedeep': 'LodashClonedeep',
mime: 'Mime',
tweetnacl: 'Tweetnacl',
};

const createConfig = (build) => {
const { file, dir, format, name, browser = false, bundle = false, minified = false } = build;

const external = allDependencies.filter((dependency) => {
return !bundle || dependenciesToExcludeInBundle.includes(dependency);
});

return {
input: ['src/index.ts'],
output: {
dir,
file,
format,
name,
exports: 'named',
preserveModules: !bundle,
sourcemap: true,
globals,
},
external,
treeshake: {
moduleSideEffects: false,
},
plugins: [
commonjs(),
nodeResolve({
browser,
dedupe: dependenciesToDedupes,
extensions,
preferBuiltins: !browser,
}),
babel({
exclude: '**/node_modules/**',
extensions,
babelHelpers: 'bundled',
}),
replace({
preventAssignment: true,
values: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.BROWSER': JSON.stringify(browser),
},
}),
...(bundle ? [json(), nodePolyfills()] : []),
...(minified ? [terser()] : []),
...(!bundle && dir
? [
generatePackageJson({
baseContents: {
type: format === 'cjs' ? 'commonjs' : 'module',
},
}),
]
: []),
],
onwarn: function (warning, rollupWarn) {
rollupWarn(warning);
if (!bundle && warning.code === 'CIRCULAR_DEPENDENCY') {
const msg = 'Please eliminate the circular dependencies listed above and retry the build';
throw new Error(msg);
}
},
};
};

export default builds.map((build) => createConfig(build));
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export * from './programs';
export * from './types';
export * from './utils';
export * from './Metaplex';

// Need to be referenced here otherwise rollup will not include the file.
export * from './accountProviders';
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Metaplex } from '@/Metaplex';
import { MetaplexFile, OperationHandler } from '@/types';
import { Plan, DisposableScope } from '@/utils';
import { BundlrStorageDriver } from './BundlrStorageDriver';
import { UploadMetadataOutput } from '../nftModule/uploadMetadata';
import {
getAssetsFromJsonMetadata,
PlanUploadMetadataOperation,
planUploadMetadataOperationHandler,
replaceAssetsWithUris,
UploadMetadataOutput,
} from '../nftModule';
import { MetaplexFile, OperationHandler } from '@/types';
import { Plan, DisposableScope } from '@/utils';
import { BundlrStorageDriver } from './BundlrStorageDriver';
} from '../nftModule/planUploadMetadata';

export const planUploadMetadataUsingBundlrOperationHandler: OperationHandler<PlanUploadMetadataOperation> =
{
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/bundlrStorage/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Metaplex } from '@/Metaplex';
import { MetaplexPlugin } from '@/types';
import { planUploadMetadataOperation } from '../nftModule';
import { BundlrOptions, BundlrStorageDriver } from './BundlrStorageDriver';
import { planUploadMetadataOperation } from '../nftModule/planUploadMetadata';
import { planUploadMetadataUsingBundlrOperationHandler } from './planUploadMetadataUsingBundlrOperationHandler';

export const bundlrStorage = (options: BundlrOptions = {}): MetaplexPlugin => ({
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/candyMachineModule/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Metaplex } from '@/Metaplex';
import type { Metaplex } from '@/Metaplex';
import { MetaplexPlugin } from '@/types';
import { CandyMachineClient } from './CandyMachineClient';
import {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/corePlugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Metaplex } from '../../Metaplex';
import { corePrograms } from '../corePrograms';
import { nftModule } from '../nftModule';
import { candyMachineModule } from '../candyMachineModule';
import type { Metaplex } from '../../Metaplex';
import { corePrograms } from '../corePrograms/plugin';
import { nftModule } from '../nftModule/plugin';
import { candyMachineModule } from '../candyMachineModule/plugin';

export const corePlugins = () => ({
install(metaplex: Metaplex) {
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/corePrograms/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID,
} from '@metaplex-foundation/mpl-token-metadata';
import { Metaplex } from '@/Metaplex';
import { TokenMetadataGpaBuilder, TokenProgramGpaBuilder } from '@/programs';
import { TokenMetadataGpaBuilder } from '@/programs';
import { ErrorWithLogs } from '@/types';

export const corePrograms = () => ({
Expand All @@ -20,7 +20,6 @@ export const corePrograms = () => ({
metaplex.programs().register({
name: 'TokenProgram',
address: TOKEN_PROGRAM_ID,
gpaResolver: (metaplex: Metaplex) => new TokenProgramGpaBuilder(metaplex, TOKEN_PROGRAM_ID),
});

// Token Metadata Program.
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/nftModule/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Metaplex } from '@/Metaplex';
import type { Metaplex } from '@/Metaplex';
import { MetaplexPlugin } from '@/types';
import { NftClient } from './NftClient';
import { createNftOperation, createNftOperationHandler } from './createNft';
Expand Down
10 changes: 3 additions & 7 deletions src/programs/token/TokenProgram.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { TokenProgramGpaBuilder } from './gpaBuilders';
import { MintGpaBuilder, TokenGpaBuilder } from './gpaBuilders';
import { Metaplex } from '@/Metaplex';

export const TokenProgram = {
publicKey: TOKEN_PROGRAM_ID,

accounts(metaplex: Metaplex) {
return new TokenProgramGpaBuilder(metaplex, this.publicKey);
},

mintAccounts(metaplex: Metaplex) {
return this.accounts(metaplex).mintAccounts();
return new MintGpaBuilder(metaplex, this.publicKey);
},

tokenAccounts(metaplex: Metaplex) {
return this.accounts(metaplex).tokenAccounts();
return new TokenGpaBuilder(metaplex, this.publicKey);
},
};
11 changes: 9 additions & 2 deletions src/programs/token/gpaBuilders/MintGpaBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { PublicKey } from '@solana/web3.js';
import { MINT_SIZE, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import BN from 'bn.js';
import { TokenProgramGpaBuilder } from './TokenProgramGpaBuilder';
import { Metaplex } from '@/Metaplex';
import { GpaBuilder } from '@/utils';

export class MintGpaBuilder extends GpaBuilder {
constructor(metaplex: Metaplex, programId?: PublicKey) {
super(metaplex, programId ?? TOKEN_PROGRAM_ID);
this.whereSize(MINT_SIZE);
}

export class MintGpaBuilder extends TokenProgramGpaBuilder {
whereDoesntHaveMintAuthority() {
return this.where(0, 0);
}
Expand Down
11 changes: 9 additions & 2 deletions src/programs/token/gpaBuilders/TokenGpaBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { PublicKey } from '@solana/web3.js';
import { ACCOUNT_SIZE, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import BN from 'bn.js';
import { TokenProgramGpaBuilder } from './TokenProgramGpaBuilder';
import { Metaplex } from '@/Metaplex';
import { GpaBuilder } from '@/utils';

export class TokenGpaBuilder extends GpaBuilder {
constructor(metaplex: Metaplex, programId?: PublicKey) {
super(metaplex, programId ?? TOKEN_PROGRAM_ID);
this.whereSize(ACCOUNT_SIZE);
}

export class TokenGpaBuilder extends TokenProgramGpaBuilder {
selectMint() {
return this.slice(0, 32);
}
Expand Down
13 changes: 0 additions & 13 deletions src/programs/token/gpaBuilders/TokenProgramGpaBuilder.ts

This file was deleted.

Loading

0 comments on commit 388bfc0

Please sign in to comment.