Skip to content

Commit

Permalink
Restore rollup.config.ts (#762)
Browse files Browse the repository at this point in the history
* restore rollup.config.ts

* separate config for walletlib
  • Loading branch information
Funkatronics authored Mar 25, 2024
1 parent 97eb85a commit be1ddb3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
4 changes: 2 additions & 2 deletions js/packages/mobile-wallet-adapter-walletlib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"scripts": {
"clean": "shx rm -rf lib/*",
"build": "yarn clean && rollup --config ../../rollup.config.ts --configPlugin rollup-plugin-ts",
"build:watch": "yarn clean && rollup --config ../../rollup.config.ts --configPlugin rollup-plugin-ts --watch",
"build": "yarn clean && rollup --config rollup.config.ts --configPlugin rollup-plugin-ts",
"build:watch": "yarn clean && rollup --config rollup.config.ts --configPlugin rollup-plugin-ts --watch",
"postbuild": "cross-env echo {\\\"type\\\":\\\"module\\\"} | npx json > lib/esm/package.json",
"example": "yarn --cwd example",
"bootstrap": "yarn example && yarn install && yarn example pods"
Expand Down
87 changes: 87 additions & 0 deletions js/packages/mobile-wallet-adapter-walletlib/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import alias from '@rollup/plugin-alias';
import commonJsPlugin from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import fs from 'fs';
import * as path from 'path';
import type { RollupOptions } from 'rollup';
import externals from 'rollup-plugin-node-externals';
import ts from 'rollup-plugin-ts';

function createConfig({
bundleName,
format,
runtime,
}: {
bundleName: string;
format: 'cjs' | 'esm';
runtime: 'node' | 'react-native';
}): RollupOptions {
return {
input: 'src/index.ts',
output: {
file: 'lib/' + format + '/' + bundleName,
format,
},
plugins: [
alias({
entries: [
{
find: /^\./, // Relative paths.
replacement: '.',
async customResolver(source, importer, options) {
const resolved = await this.resolve(source, importer, {
skipSelf: true,
...options,
});
if (resolved == null) {
return;
}
const { id: resolvedId } = resolved;
const directory = path.dirname(resolvedId);
const moduleFilename = path.basename(resolvedId);
const forkPath = path.join(directory, '__forks__', runtime, moduleFilename);
const hasForkCacheKey = `has_fork:${forkPath}`;
let hasFork = this.cache.get(hasForkCacheKey);
if (hasFork === undefined) {
hasFork = fs.existsSync(forkPath);
this.cache.set(hasForkCacheKey, hasFork);
}
if (hasFork) {
return forkPath;
}
},
},
],
}),
externals(),
nodeResolve({
browser: false,
extensions: ['.ts'],
preferBuiltins: runtime === 'node',
}),
replace({
preventAssignment: true,
values: {
'process.env.BROWSER': JSON.stringify(false),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
},
}),
ts({
tsconfig: format === 'cjs' ? 'tsconfig.cjs.json' : 'tsconfig.json',
}),
commonJsPlugin({ exclude: 'node_modules', extensions: ['.js', '.ts'] }),
],
};
}

const config: RollupOptions[] = [
createConfig({ bundleName: 'index.js', format: 'esm', runtime: 'node' }),
createConfig({
bundleName: 'index.native.js',
format: 'esm',
runtime: 'react-native',
}),
];

export default config;
19 changes: 15 additions & 4 deletions js/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function createConfig({
}: {
bundleName: string;
format: 'cjs' | 'esm';
runtime: 'node' | 'react-native';
runtime: 'browser' | 'node' | 'react-native';
}): RollupOptions {
return {
input: 'src/index.ts',
Expand Down Expand Up @@ -56,14 +56,14 @@ function createConfig({
}),
externals(),
nodeResolve({
browser: false,
browser: runtime === 'browser',
extensions: ['.ts'],
preferBuiltins: runtime === 'node',
}),
replace({
preventAssignment: true,
values: {
'process.env.BROWSER': JSON.stringify(false),
'process.env.BROWSER': JSON.stringify(runtime === 'browser'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
},
}),
Expand All @@ -76,10 +76,21 @@ function createConfig({
}

const config: RollupOptions[] = [
createConfig({ bundleName: 'index.js', format: 'cjs', runtime: 'node' }),
createConfig({
bundleName: 'index.browser.js',
format: 'cjs',
runtime: 'browser',
}),
createConfig({ bundleName: 'index.js', format: 'esm', runtime: 'node' }),
createConfig({
bundleName: 'index.native.js',
bundleName: 'index.browser.js',
format: 'esm',
runtime: 'browser',
}),
createConfig({
bundleName: 'index.native.js',
format: 'cjs',
runtime: 'react-native',
}),
];
Expand Down

0 comments on commit be1ddb3

Please sign in to comment.