import typescript from '@rollup/plugin-typescript'; import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; const target = process.env.TARGET || 'node'; const config = { input: target === 'browser' ? 'src/browser.ts' : 'src/node.ts', plugins: [ resolve(), commonjs(), typescript({ tsconfig: './tsconfig.json', declaration: false, declarationDir: undefined }) ] }; // Extensions are explicit rather than bare `.js`: the package is not // "type": "module", so Node treats a .js file as CommonJS. An ES-format bundle // named .js therefore fails to import on Node < 22, and only works above that // because Node re-parses it after guessing — at a cost on every import. if (target === 'node') { config.output = [ { file: 'dist/node/index.js', format: 'cjs', exports: 'named' }, { file: 'dist/esm/index.mjs', format: 'es', // The source loads several modules lazily via require(): the crypto // fallback, fs/path for key persistence, the optional native addon, // and jose. `require` does not exist in ES module scope, so without // this shim an ESM consumer crashes with "require is not defined" // the moment one of those paths runs. Node-only output, so // node:module is safe here. banner: "import { createRequire as __nomyoCreateRequire } from 'node:module';\n" + 'const require = __nomyoCreateRequire(import.meta.url);' } ]; config.external = ['crypto', 'https', 'fs', 'path']; } else if (target === 'browser') { config.output = [ { file: 'dist/browser/index.mjs', format: 'es', name: 'Nomyo' }, { file: 'dist/browser/index.cjs', format: 'cjs', exports: 'named', name: 'Nomyo' } ]; } export default config;