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

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-04-06 22:49:13 +00:00
'use strict'
const resolve = require('@rollup/plugin-node-resolve').nodeResolve
2020-04-06 22:49:13 +00:00
const replace = require('@rollup/plugin-replace')
const { terser } = require('rollup-plugin-terser')
const typescriptPlugin = require('@rollup/plugin-typescript')
const commonjs = require('@rollup/plugin-commonjs')
2020-04-06 22:49:13 +00:00
const path = require('path')
const fs = require('fs')
2020-04-06 22:49:13 +00:00
const pkgJson = require('../package.json')
const rootDir = path.join(__dirname, '..')
const dstDir = path.join(rootDir, pkgJson.directories.dist)
const srcDir = path.join(rootDir, 'src')
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 } = pkgJson.name.match(regex).groups
const pkgCamelisedName = camelise(name)
2020-04-06 22:49:13 +00:00
const input = path.join(srcDir, 'index.ts')
if (fs.existsSync(input) !== true) throw new Error('The entry point should be index.ts')
const tsBundleOptions = {
exclude: ['test/**/*', 'src/**/*.spec.ts', './build/typings/global-this-pkg.d.ts']
}
const external = [...Object.keys(pkgJson.dependencies || {}), ...Object.keys(pkgJson.peerDependencies || {})]
const sourcemapOutputOptions = {
sourcemap: 'inline',
sourcemapExcludeSources: true
}
2020-04-06 22:49:13 +00:00
module.exports = [
{ // ESM for browsers
2020-04-06 22:49:13 +00:00
input: input,
output: [
{
file: path.join(rootDir, pkgJson.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: path.join(dstDir, `bundles/${name}.iife.js`),
2020-04-06 22:49:13 +00:00
format: 'iife',
name: pkgCamelisedName
},
{
file: path.join(dstDir, `bundles/${name}.esm.js`),
2020-04-06 22:49:13 +00:00
format: 'es'
},
{
file: path.join(dstDir, `bundles/${name}.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
2020-04-06 22:49:13 +00:00
input: input,
2020-04-08 09:53:15 +00:00
output: {
dir: path.join(rootDir, path.dirname(pkgJson.exports['.'].node.import)),
entryFileNames: path.basename(pkgJson.exports['.'].node.import),
...sourcemapOutputOptions,
format: 'es'
},
plugins: [
replace({
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin({
...tsBundleOptions,
declaration: true,
outDir: path.join(rootDir, path.dirname(pkgJson.exports['.'].node.import)),
declarationDir: path.join(rootDir, path.dirname(pkgJson.exports['.'].node.import), 'types'),
declarationMap: true
}),
commonjs({ extensions: ['.js', '.ts'] }) // the ".ts" extension is required
],
external
},
{ // Node CJS with declaration files
input: input,
output: {
dir: path.join(rootDir, path.dirname(pkgJson.exports['.'].node.require)),
entryFileNames: path.basename(pkgJson.exports['.'].node.require),
...sourcemapOutputOptions,
2020-04-08 09:53:15 +00:00
format: 'cjs'
},
2020-04-06 22:49:13 +00:00
plugins: [
replace({
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
commonjs({ extensions: ['.js', '.ts'] }) // the ".ts" extension is required
2020-04-08 09:53:15 +00:00
]
2020-04-06 22:49:13 +00:00
}
]