2020-04-06 11:17:22 +00:00
|
|
|
'use strict'
|
|
|
|
|
2020-11-12 11:04:13 +00:00
|
|
|
const resolve = require('@rollup/plugin-node-resolve').nodeResolve
|
2020-04-06 11:17:22 +00:00
|
|
|
const replace = require('@rollup/plugin-replace')
|
|
|
|
const { terser } = require('rollup-plugin-terser')
|
2021-03-25 12:40:04 +00:00
|
|
|
const typescriptPlugin = require('@rollup/plugin-typescript')
|
|
|
|
const commonjs = require('@rollup/plugin-commonjs')
|
2020-04-06 11:17:22 +00:00
|
|
|
|
|
|
|
const path = require('path')
|
2021-03-25 12:40:04 +00:00
|
|
|
const fs = require('fs')
|
2020-04-06 11:17:22 +00:00
|
|
|
const pkgJson = require('../package.json')
|
|
|
|
|
|
|
|
const rootDir = path.join(__dirname, '..')
|
2021-03-25 12:40:04 +00:00
|
|
|
const srcDir = path.join(rootDir, 'src')
|
2020-04-06 11:17:22 +00:00
|
|
|
|
|
|
|
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 } = pkgJson.name.match(regex).groups
|
|
|
|
const pkgCamelisedName = camelise(name)
|
2020-04-06 11:17:22 +00:00
|
|
|
|
2021-03-25 12:40:04 +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 11:17:22 +00:00
|
|
|
|
|
|
|
module.exports = [
|
2021-04-21 09:14:04 +00:00
|
|
|
{ // Browser ESM
|
|
|
|
input: input,
|
|
|
|
output: {
|
|
|
|
file: path.join(rootDir, pkgJson.browser),
|
|
|
|
format: 'es',
|
|
|
|
...sourcemapOutputOptions
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
replace({
|
|
|
|
IS_BROWSER: true,
|
|
|
|
preventAssignment: true
|
|
|
|
}),
|
|
|
|
typescriptPlugin(tsBundleOptions),
|
|
|
|
resolve({ // For the workers to properly load all the functions when minified (e.g. by webpack), bigint-mod-arith should be resolved
|
|
|
|
browser: true,
|
|
|
|
exportConditions: ['browser', 'module', 'import', 'default']
|
|
|
|
})
|
|
|
|
]
|
|
|
|
},
|
2020-04-20 22:50:53 +00:00
|
|
|
{ // Browser bundles
|
|
|
|
input: input,
|
|
|
|
output: [
|
2020-04-07 17:29:23 +00:00
|
|
|
{
|
2021-04-21 09:14:04 +00:00
|
|
|
file: path.join(rootDir, pkgJson.exports['./iife-browser-bundle']),
|
2020-04-07 22:21:02 +00:00
|
|
|
format: 'iife',
|
|
|
|
name: pkgCamelisedName
|
2020-04-06 11:17:22 +00:00
|
|
|
},
|
|
|
|
{
|
2021-04-21 09:14:04 +00:00
|
|
|
file: path.join(rootDir, pkgJson.exports['./esm-browser-bundle']),
|
2020-04-06 11:17:22 +00:00
|
|
|
format: 'es'
|
2021-03-25 12:40:04 +00:00
|
|
|
},
|
|
|
|
{
|
2021-04-21 09:14:04 +00:00
|
|
|
file: path.join(rootDir, pkgJson.exports['./umd-browser-bundle']),
|
2021-03-25 12:40:04 +00:00
|
|
|
format: 'umd',
|
|
|
|
name: pkgCamelisedName
|
2020-04-06 11:17:22 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
replace({
|
2021-03-25 12:40:04 +00:00
|
|
|
IS_BROWSER: true,
|
|
|
|
preventAssignment: true
|
2020-04-06 11:17:22 +00:00
|
|
|
}),
|
2021-03-25 12:40:04 +00:00
|
|
|
typescriptPlugin(tsBundleOptions),
|
2020-04-06 11:17:22 +00:00
|
|
|
resolve({
|
2021-03-25 12:40:04 +00:00
|
|
|
browser: true,
|
|
|
|
exportConditions: ['browser', 'module', 'import', 'default']
|
2020-04-07 22:21:02 +00:00
|
|
|
}),
|
2020-04-20 22:50:53 +00:00
|
|
|
terser()
|
2020-04-06 11:17:22 +00:00
|
|
|
]
|
|
|
|
},
|
2021-03-25 12:40:04 +00:00
|
|
|
{ // Node ESM with declaration files
|
2020-04-06 11:17:22 +00:00
|
|
|
input: input,
|
|
|
|
output: {
|
2021-03-25 12:40:04 +00:00
|
|
|
dir: path.join(rootDir, path.dirname(pkgJson.exports['.'].node.import)),
|
|
|
|
entryFileNames: path.basename(pkgJson.exports['.'].node.import),
|
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'es'
|
2020-04-07 15:03:30 +00:00
|
|
|
},
|
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,
|
|
|
|
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
|
|
|
|
input: input,
|
2021-08-04 10:50:36 +00:00
|
|
|
output: [
|
|
|
|
{
|
|
|
|
file: path.join(rootDir, pkgJson.exports['.'].node.require),
|
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'cjs'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
file: path.join(rootDir, pkgJson.exports['./node-js']),
|
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'cjs'
|
|
|
|
}
|
|
|
|
],
|
2021-03-25 12:40:04 +00:00
|
|
|
plugins: [
|
|
|
|
replace({
|
|
|
|
IS_BROWSER: false,
|
|
|
|
preventAssignment: true
|
|
|
|
}),
|
|
|
|
typescriptPlugin(tsBundleOptions),
|
|
|
|
commonjs({ extensions: ['.js', '.ts'] }) // the ".ts" extension is required
|
2021-04-21 09:14:04 +00:00
|
|
|
],
|
|
|
|
external
|
2020-04-06 11:17:22 +00:00
|
|
|
}
|
|
|
|
]
|