bigint-mod-arith/build/rollup.config.js

152 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-04-06 22:49:13 +00:00
'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'
2020-04-06 22:49:13 +00:00
import { dirname, join } from 'path'
import { existsSync, moveSync, removeSync } from 'fs-extra'
import { directories, name as _name, dependencies, peerDependencies, exports, types } from '../package.json'
2020-04-06 22:49:13 +00:00
const rootDir = join(__dirname, '..')
const dstDir = join(rootDir, directories.dist)
const srcDir = join(rootDir, 'src', 'ts')
2020-04-06 22:49:13 +00:00
function camelise (str) {
return str.replace(/-([a-z])/g,
function (m, w) {
return w.toUpperCase()
})
}
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
const pkgCamelisedName = camelise(name)
2020-04-06 22:49:13 +00:00
const input = join(srcDir, 'index.ts')
if (existsSync(input) !== true) throw new Error('The entry point should be index.ts')
const tsBundleOptions = {
tsconfig: join(rootDir, 'tsconfig.json'),
outDir: undefined, // ignore outDir in tsconfig.json
exclude: ['test/**/*', 'src/**/*.spec.ts', './build/typings/global-this-pkg.d.ts']
}
const external = [...Object.keys(dependencies || {}), ...Object.keys(peerDependencies || {})]
const sourcemapOutputOptions = {
sourcemap: 'inline',
sourcemapExcludeSources: true
}
2020-04-06 22:49:13 +00:00
function moveDirPlugin (srcDir, dstDir) {
return {
name: 'move-dir',
closeBundle () {
removeSync(dstDir)
moveSync(srcDir, dstDir, { overwrite: true })
}
}
}
export default [
{ // ESM for browsers
2020-04-06 22:49:13 +00:00
input: input,
output: [
{
file: join(rootDir, exports['.'].default),
...sourcemapOutputOptions,
2020-04-08 09:53:15 +00:00
format: 'es'
2020-04-06 22:49:13 +00:00
}
],
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions)
],
external
2020-04-06 22:49:13 +00:00
},
{ // Browser bundles
input: input,
output: [
{
file: join(dstDir, 'bundles/iife.js'),
2020-04-06 22:49:13 +00:00
format: 'iife',
name: pkgCamelisedName
},
{
file: join(dstDir, 'bundles/esm.js'),
2020-04-06 22:49:13 +00:00
format: 'es'
},
{
file: join(dstDir, 'bundles/umd.js'),
format: 'umd',
name: pkgCamelisedName
2020-04-06 22:49:13 +00:00
}
],
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
2020-04-06 22:49:13 +00:00
}),
typescriptPlugin(tsBundleOptions),
2020-04-06 22:49:13 +00:00
resolve({
browser: true,
exportConditions: ['browser', 'module', 'import', 'default']
2020-04-06 22:49:13 +00:00
}),
2020-04-08 09:53:15 +00:00
terser()
2020-04-06 22:49:13 +00:00
]
},
{ // Node ESM with declaration files
2020-04-06 22:49:13 +00:00
input: input,
2020-04-08 09:53:15 +00:00
output: {
file: join(rootDir, exports['.'].node.import),
...sourcemapOutputOptions,
format: 'es'
},
plugins: [
replace({
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin({
...tsBundleOptions,
// outDir: path.join(rootDir, path.dirname(pkgJson.exports['.'].node.import)),
declaration: true,
declarationDir: 'types',
declarationMap: true
}),
commonjs({ extensions: ['.js', '.cjs', '.ts'] }), // the ".ts" extension is required
moveDirPlugin(join(rootDir, dirname(exports['.'].node.import), 'types'), join(rootDir, dirname(types)))
],
external
},
{ // Node CJS
input: input,
output: [
{
file: join(rootDir, exports['.'].node.require),
...sourcemapOutputOptions,
format: 'cjs'
},
{
file: join(rootDir, exports['.'].node.require).slice(0, -4) + '.js', // .js extension instead of .cjs for Node 10 support
...sourcemapOutputOptions,
format: 'cjs'
}
],
2020-04-06 22:49:13 +00:00
plugins: [
replace({
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
commonjs({ extensions: ['.js', '.cjs', '.ts'] }) // the ".ts" extension is required
2020-04-08 09:53:15 +00:00
]
2020-04-06 22:49:13 +00:00
}
]