bigint-crypto-utils/build/rollup.config.js

178 lines
4.5 KiB
JavaScript
Raw Normal View History

'use strict'
import { nodeResolve as resolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import { terser } from 'rollup-plugin-terser'
import typescriptPlugin from '@rollup/plugin-typescript'
import commonjs from '@rollup/plugin-commonjs'
2022-08-01 02:19:48 +00:00
import json from '@rollup/plugin-json'
2022-08-01 02:19:48 +00:00
import { join } from 'path'
import { existsSync } from 'fs-extra'
import { directories, name as _name, exports } from '../package.json'
import { compile } from './rollup-plugin-dts.js'
const rootDir = join(__dirname, '..')
const dstDir = join(rootDir, directories.dist)
const srcDir = join(rootDir, 'src', 'ts')
function camelise (str) {
return str.replace(/-([a-z])/g,
function (m, w) {
return w.toUpperCase()
})
}
2021-03-25 12:40:04 +00:00
const regex = /^(?:(?<scope>@.*?)\/)?(?<name>.*)/ // We are going to take only the package name part if there is a scope, e.g. @my-org/package-name
const { name } = _name.match(regex).groups
2021-03-25 12:40:04 +00:00
const pkgCamelisedName = camelise(name)
const input = join(srcDir, 'index.ts')
if (existsSync(input) !== true) throw new Error('The entry point should be index.ts')
2021-03-25 12:40:04 +00:00
const tsBundleOptions = {
tsconfig: join(rootDir, 'tsconfig.json'),
2021-08-06 08:10:32 +00:00
outDir: undefined, // ignore outDir in tsconfig.json
2022-08-01 02:19:48 +00:00
include: ['src/ts/**/*', 'build/typings/is-browser.d.ts'],
exclude: ['src/**/*.spec.ts']
2021-03-25 12:40:04 +00:00
}
const sourcemapOutputOptions = {
sourcemap: 'inline',
sourcemapExcludeSources: true
}
2022-08-01 02:19:48 +00:00
function compileDts () {
return {
2022-08-01 02:19:48 +00:00
name: 'compile-dts',
2021-08-06 15:29:23 +00:00
closeBundle () {
2022-08-01 02:19:48 +00:00
compile()
}
}
}
export default [
2022-08-01 02:19:48 +00:00
{ // ESM for browsers and declarations
input: input,
2021-08-06 08:10:32 +00:00
output: [
{
file: join(rootDir, exports['.'].default),
2021-08-06 08:10:32 +00:00
...sourcemapOutputOptions,
format: 'es'
2022-08-01 02:19:48 +00:00
},
2022-08-01 02:40:02 +00:00
{
file: join(dstDir, 'bundles/esm.js'),
...sourcemapOutputOptions,
format: 'es'
},
2022-08-01 02:19:48 +00:00
{
file: join(dstDir, 'bundles/esm.min.js'),
format: 'es',
plugins: [terser()]
2021-08-06 08:10:32 +00:00
}
],
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({
browser: true,
2022-08-01 02:19:48 +00:00
exportConditions: ['browser', 'default']
}),
compileDts(),
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json()
]
},
2022-08-01 02:19:48 +00:00
{ // Browser bundles
input: input,
output: [
2020-04-07 17:29:23 +00:00
{
file: join(dstDir, 'bundles/iife.js'),
2020-04-07 22:21:02 +00:00
format: 'iife',
2022-08-01 02:19:48 +00:00
name: pkgCamelisedName,
plugins: [terser()]
2021-03-25 12:40:04 +00:00
},
{
file: join(dstDir, 'bundles/umd.js'),
2021-03-25 12:40:04 +00:00
format: 'umd',
2022-08-01 02:19:48 +00:00
name: pkgCamelisedName,
plugins: [terser()]
}
],
plugins: [
2022-08-01 02:19:48 +00:00
replace({
'await import(': 'require(',
delimiters: ['', ''],
preventAssignment: true
}),
replace({
2021-03-25 12:40:04 +00:00
IS_BROWSER: true,
preventAssignment: true
}),
2021-03-25 12:40:04 +00:00
typescriptPlugin(tsBundleOptions),
resolve({
2021-03-25 12:40:04 +00:00
browser: true,
2022-08-01 02:19:48 +00:00
exportConditions: ['browser', 'default'],
mainFields: ['browser', 'module', 'main']
2020-04-07 22:21:02 +00:00
}),
2022-08-01 02:19:48 +00:00
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json()
]
},
2022-08-01 02:19:48 +00:00
{ // Node CJS
input: input,
2022-08-01 02:19:48 +00:00
output: [
{
file: join(rootDir, exports['.'].node.require),
...sourcemapOutputOptions,
format: 'cjs',
exports: 'auto'
}
],
2020-04-07 22:21:02 +00:00
plugins: [
replace({
2022-08-01 02:19:48 +00:00
'await import(': 'require(',
delimiters: ['', ''],
2021-03-25 12:40:04 +00:00
preventAssignment: true
}),
2022-08-01 02:19:48 +00:00
replace({
IS_BROWSER: false,
preventAssignment: true
2021-03-25 12:40:04 +00:00
}),
2022-08-01 02:19:48 +00:00
typescriptPlugin(tsBundleOptions),
resolve({
browser: false,
2022-08-01 02:19:48 +00:00
exportConditions: ['node', 'module', 'import']
}),
2022-08-01 02:19:48 +00:00
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json()
]
2021-03-25 12:40:04 +00:00
},
2022-08-01 02:19:48 +00:00
{ // Node ESM
2021-03-25 12:40:04 +00:00
input: input,
2021-08-04 10:50:36 +00:00
output: [
{
2022-08-01 02:19:48 +00:00
file: join(rootDir, exports['.'].node.import),
2021-08-04 10:50:36 +00:00
...sourcemapOutputOptions,
2022-08-01 02:19:48 +00:00
format: 'es'
2021-08-04 10:50:36 +00:00
}
],
2021-03-25 12:40:04 +00:00
plugins: [
replace({
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({
browser: false,
2022-08-01 02:19:48 +00:00
exportConditions: ['node', 'module', 'import']
}),
2022-08-01 02:19:48 +00:00
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json()
2021-08-06 08:10:32 +00:00
]
}
]