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

162 lines
4.3 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'
import { dirname, join } from 'path'
2021-08-06 15:29:23 +00:00
import { existsSync, moveSync, removeSync } from 'fs-extra'
import { directories, name as _name, /* dependencies, peerDependencies, */ exports, types } from '../package.json'
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
2021-03-25 12:40:04 +00:00
exclude: ['test/**/*', 'src/**/*.spec.ts', './build/typings/global-this-pkg.d.ts']
}
// const external = [...Object.keys(dependencies || {}), ...Object.keys(peerDependencies || {})]
2021-03-25 12:40:04 +00:00
const sourcemapOutputOptions = {
sourcemap: 'inline',
sourcemapExcludeSources: true
}
function moveDirPlugin (srcDir, dstDir) {
return {
name: 'move-dir',
2021-08-06 15:29:23 +00:00
closeBundle () {
removeSync(dstDir)
moveSync(srcDir, dstDir, { overwrite: true })
}
}
}
export default [
2021-08-06 08:10:32 +00:00
{ // ESM for browsers
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'
}
],
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({
browser: true,
exportConditions: ['browser', 'module', 'import', 'default']
})
]
},
{ // Other 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',
name: pkgCamelisedName
},
{
file: join(dstDir, 'bundles/esm.js'),
format: 'es'
2021-03-25 12:40:04 +00:00
},
{
file: join(dstDir, 'bundles/umd.js'),
2021-03-25 12:40:04 +00:00
format: 'umd',
name: pkgCamelisedName
}
],
plugins: [
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,
exportConditions: ['browser', 'module', 'import', 'default']
2020-04-07 22:21:02 +00:00
}),
terser()
]
},
2021-03-25 12:40:04 +00:00
{ // Node ESM with declaration files
input: input,
output: {
file: join(rootDir, exports['.'].node.import),
2021-03-25 12:40:04 +00:00
...sourcemapOutputOptions,
format: 'es'
},
2020-04-07 22:21:02 +00:00
plugins: [
replace({
2021-03-25 12:40:04 +00:00
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin({
...tsBundleOptions,
// outDir: path.join(rootDir, path.dirname(pkgJson.exports['.'].node.import)),
2021-03-25 12:40:04 +00:00
declaration: true,
declarationDir: 'types',
2021-03-25 12:40:04 +00:00
declarationMap: true
}),
resolve({
browser: false,
exportConditions: ['node', 'module', 'require']
}),
commonjs({ extensions: ['.js', '.cjs', '.ts'] }), // the ".ts" extension is required
moveDirPlugin(join(rootDir, dirname(exports['.'].node.import), 'types'), join(rootDir, dirname(types)))
]
2021-03-25 12:40:04 +00:00
},
{ // Node CJS
input: input,
2021-08-04 10:50:36 +00:00
output: [
{
file: join(rootDir, exports['.'].node.require),
2021-08-04 10:50:36 +00:00
...sourcemapOutputOptions,
format: 'cjs'
},
{
file: join(rootDir, exports['.'].node.require).slice(0, -4) + '.js', // .js extension instead of .cjs for Node 10 support
2021-08-04 10:50:36 +00:00
...sourcemapOutputOptions,
format: 'cjs'
}
],
2021-03-25 12:40:04 +00:00
plugins: [
replace({
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({
browser: false,
exportConditions: ['node', 'module', 'require']
}),
commonjs({ extensions: ['.js', '.cjs', '.ts'] }) // the ".ts" extension is required
2021-08-06 08:10:32 +00:00
]
}
]