2020-04-06 22:49:13 +00:00
|
|
|
'use strict'
|
|
|
|
|
2023-04-13 16:30:48 +00:00
|
|
|
import commonjs from '@rollup/plugin-commonjs'
|
2023-04-11 08:43:47 +00:00
|
|
|
import inject from '@rollup/plugin-inject'
|
2023-04-13 16:30:48 +00:00
|
|
|
import json from '@rollup/plugin-json'
|
2021-08-06 22:14:30 +00:00
|
|
|
import { nodeResolve as resolve } from '@rollup/plugin-node-resolve'
|
|
|
|
import replace from '@rollup/plugin-replace'
|
2023-04-11 08:43:47 +00:00
|
|
|
import terser from '@rollup/plugin-terser'
|
2023-04-13 16:30:48 +00:00
|
|
|
import rollupPluginTs from '@rollup/plugin-typescript'
|
2023-04-11 08:43:47 +00:00
|
|
|
import { existsSync, readFileSync } from 'fs'
|
2023-06-28 14:42:09 +00:00
|
|
|
import { builtinModules } from 'module'
|
|
|
|
import { join } from 'path'
|
2023-04-13 16:30:48 +00:00
|
|
|
import dts from 'rollup-plugin-dts'
|
2022-01-17 10:04:55 +00:00
|
|
|
import { compile } from './rollup-plugin-dts.js'
|
2020-04-06 22:49:13 +00:00
|
|
|
|
2023-04-11 08:43:47 +00:00
|
|
|
import * as url from 'url'
|
|
|
|
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
const rootDir = join(__dirname, '..')
|
2023-04-11 08:43:47 +00:00
|
|
|
const pkgJson = JSON.parse(readFileSync(join(rootDir, 'package.json')))
|
2023-04-13 16:30:48 +00:00
|
|
|
const pkgJsonLock = JSON.parse(readFileSync(join(rootDir, 'package-lock.json')))
|
2021-08-06 22:14:30 +00:00
|
|
|
const srcDir = join(rootDir, 'src', 'ts')
|
2020-04-06 22:49:13 +00:00
|
|
|
|
2023-04-13 16:30:48 +00:00
|
|
|
const tsConfigPath = join(rootDir, 'tsconfig.json')
|
|
|
|
|
2020-04-06 22:49:13 +00:00
|
|
|
function camelise (str) {
|
|
|
|
return str.replace(/-([a-z])/g,
|
|
|
|
function (m, w) {
|
|
|
|
return w.toUpperCase()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-13 16:30:48 +00:00
|
|
|
function isDevDependency (moduleName) {
|
|
|
|
const packageEntry = pkgJsonLock.packages['node_modules/' + moduleName]
|
|
|
|
return (packageEntry ?? {}).dev === true
|
|
|
|
}
|
|
|
|
|
2021-03-24 13:04:30 +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
|
2023-04-11 08:43:47 +00:00
|
|
|
const { name } = pkgJson.name.match(regex).groups
|
2021-03-24 13:04:30 +00:00
|
|
|
const pkgCamelisedName = camelise(name)
|
2020-04-06 22:49:13 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
const input = join(srcDir, 'index.ts')
|
|
|
|
if (existsSync(input) !== true) throw new Error('The entry point should be index.ts')
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2023-04-13 16:30:48 +00:00
|
|
|
const tsPluginOptions = {
|
|
|
|
tsconfig: tsConfigPath,
|
|
|
|
outDir: undefined,
|
2023-06-28 14:42:09 +00:00
|
|
|
include: ['src/ts/**/*', 'build/typings/**/*.d.ts'],
|
2022-01-17 10:04:55 +00:00
|
|
|
exclude: ['src/**/*.spec.ts']
|
2021-03-24 13:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sourcemapOutputOptions = {
|
|
|
|
sourcemap: 'inline',
|
|
|
|
sourcemapExcludeSources: true
|
|
|
|
}
|
2020-04-06 22:49:13 +00:00
|
|
|
|
2023-04-13 16:30:48 +00:00
|
|
|
function compileDts (outDir) {
|
2021-08-06 22:14:30 +00:00
|
|
|
return {
|
2022-01-17 10:04:55 +00:00
|
|
|
name: 'compile-dts',
|
2021-08-06 22:14:30 +00:00
|
|
|
closeBundle () {
|
2023-04-13 16:30:48 +00:00
|
|
|
compile(outDir)
|
2021-08-06 22:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 16:30:48 +00:00
|
|
|
function resolveOnly (module) { // if a dev dependency is imported we will resolve it so that the dist modules always work
|
|
|
|
const moduleNameMatch = module.match(/^(?:@[a-z0-9_-]+\/)?(?:node:)?[a-z0-9_-]+/)
|
|
|
|
if (moduleNameMatch === null || moduleNameMatch.length !== 1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const moduleName = moduleNameMatch[0].replace(/^node:/, '')
|
|
|
|
// don't resolve if it is a native module
|
|
|
|
if (builtinModules.includes(moduleName)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDevDependency(moduleName)) {
|
|
|
|
console.warn(`\x1b[33m⚠ WARM: dev dependency \x1b[0m${module}\x1b[33m being bundled. Should it be a dependency instead?\x1b[0m`)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const tmpDeclarationsDir = join(rootDir, '.types')
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
export default [
|
2023-04-13 16:30:48 +00:00
|
|
|
{ // Browser ESM
|
2023-04-11 08:43:47 +00:00
|
|
|
input,
|
2020-04-06 22:49:13 +00:00
|
|
|
output: [
|
|
|
|
{
|
2023-04-13 16:30:48 +00:00
|
|
|
file: join(rootDir, pkgJson.exports['.'].default.default),
|
2021-03-24 13:04:30 +00:00
|
|
|
...sourcemapOutputOptions,
|
2023-04-13 16:30:48 +00:00
|
|
|
format: 'es',
|
|
|
|
plugins: [
|
|
|
|
terser()
|
|
|
|
]
|
2020-04-06 22:49:13 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
replace({
|
2023-04-11 08:43:47 +00:00
|
|
|
IS_BROWSER: true,
|
2023-06-28 14:42:09 +00:00
|
|
|
environment: 'browser',
|
2023-04-11 08:43:47 +00:00
|
|
|
_MODULE_TYPE: "'ESM'",
|
2023-06-28 14:42:09 +00:00
|
|
|
_NPM_PKG_VERSION: `'${process.env.npm_package_version}'` ?? "'0.0.1'",
|
2021-03-24 13:04:30 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2023-04-13 16:30:48 +00:00
|
|
|
rollupPluginTs(tsPluginOptions),
|
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.jsx', '.cjsx'] }),
|
|
|
|
json(),
|
|
|
|
resolve({
|
|
|
|
browser: true,
|
|
|
|
exportConditions: ['browser', 'default'],
|
|
|
|
mainFields: ['browser', 'module', 'main'],
|
|
|
|
resolveOnly
|
|
|
|
})
|
2022-10-03 15:35:35 +00:00
|
|
|
]
|
|
|
|
},
|
2023-04-11 08:43:47 +00:00
|
|
|
{ // Browser bundles
|
|
|
|
input,
|
2022-10-03 15:35:35 +00:00
|
|
|
output: [
|
|
|
|
{
|
2023-04-11 08:43:47 +00:00
|
|
|
file: join(rootDir, pkgJson.exports['./esm-browser-bundle-nomin']),
|
|
|
|
format: 'es'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
file: join(rootDir, pkgJson.exports['./esm-browser-bundle']),
|
|
|
|
format: 'es',
|
|
|
|
plugins: [terser()]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
file: join(rootDir, pkgJson.exports['./iife-browser-bundle']),
|
|
|
|
format: 'iife',
|
|
|
|
name: pkgCamelisedName,
|
|
|
|
plugins: [terser()]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
file: join(rootDir, pkgJson.exports['./umd-browser-bundle']),
|
|
|
|
format: 'umd',
|
|
|
|
name: pkgCamelisedName,
|
|
|
|
plugins: [terser()]
|
2022-10-03 15:35:35 +00:00
|
|
|
}
|
2021-03-24 13:04:30 +00:00
|
|
|
],
|
2022-10-03 15:35:35 +00:00
|
|
|
plugins: [
|
|
|
|
replace({
|
2023-04-11 08:43:47 +00:00
|
|
|
IS_BROWSER: true,
|
2023-06-28 14:42:09 +00:00
|
|
|
environment: 'browser',
|
2023-04-11 08:43:47 +00:00
|
|
|
_MODULE_TYPE: "'BUNDLE'",
|
2023-06-28 14:42:09 +00:00
|
|
|
_NPM_PKG_VERSION: `'${process.env.npm_package_version}'` ?? "'0.0.1'",
|
2022-10-03 15:35:35 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2023-04-13 16:30:48 +00:00
|
|
|
rollupPluginTs({
|
|
|
|
...tsPluginOptions,
|
2023-04-11 08:43:47 +00:00
|
|
|
sourceMap: false
|
|
|
|
}),
|
2023-04-13 16:30:48 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.jsx', '.cjsx'] }),
|
|
|
|
json(),
|
|
|
|
resolve({ browser: true })
|
2022-10-03 15:35:35 +00:00
|
|
|
]
|
2020-04-06 22:49:13 +00:00
|
|
|
},
|
2023-04-11 08:43:47 +00:00
|
|
|
{ // Node CJS
|
|
|
|
input,
|
2020-04-06 22:49:13 +00:00
|
|
|
output: [
|
|
|
|
{
|
2023-04-11 08:43:47 +00:00
|
|
|
file: join(rootDir, pkgJson.exports['.'].node.require.default),
|
2022-10-03 15:35:35 +00:00
|
|
|
...sourcemapOutputOptions,
|
2023-04-11 08:43:47 +00:00
|
|
|
format: 'cjs',
|
|
|
|
exports: 'auto',
|
2023-06-28 14:42:09 +00:00
|
|
|
interop: 'auto',
|
|
|
|
dynamicImportInCjs: false,
|
|
|
|
plugins: [terser()]
|
2020-04-06 22:49:13 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
replace({
|
2023-04-11 08:43:47 +00:00
|
|
|
IS_BROWSER: false,
|
2023-06-28 14:42:09 +00:00
|
|
|
environment: 'nodejs',
|
2023-04-11 08:43:47 +00:00
|
|
|
_MODULE_TYPE: "'CJS'",
|
2023-06-28 14:42:09 +00:00
|
|
|
_NPM_PKG_VERSION: `'${process.env.npm_package_version}'` ?? "'0.0.1'",
|
2021-03-24 13:04:30 +00:00
|
|
|
preventAssignment: true
|
2020-04-06 22:49:13 +00:00
|
|
|
}),
|
2023-04-13 16:30:48 +00:00
|
|
|
rollupPluginTs(tsPluginOptions),
|
2023-04-11 08:43:47 +00:00
|
|
|
inject({
|
|
|
|
crypto: ['crypto', 'webcrypto']
|
2021-03-24 13:04:30 +00:00
|
|
|
}),
|
2023-04-13 16:30:48 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.jsx', '.cjsx'] }),
|
|
|
|
json(),
|
|
|
|
resolve({
|
|
|
|
exportConditions: ['node'],
|
|
|
|
resolveOnly
|
|
|
|
})
|
2022-01-17 10:04:55 +00:00
|
|
|
]
|
2021-03-24 13:04:30 +00:00
|
|
|
},
|
2023-04-11 08:43:47 +00:00
|
|
|
{ // Node ESM and type declarations
|
|
|
|
input,
|
2021-08-06 22:14:30 +00:00
|
|
|
output: [
|
|
|
|
{
|
2023-04-11 08:43:47 +00:00
|
|
|
file: join(rootDir, pkgJson.exports['.'].node.import.default),
|
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'es',
|
2023-06-28 14:42:09 +00:00
|
|
|
plugins: [terser()]
|
2021-08-06 22:14:30 +00:00
|
|
|
}
|
|
|
|
],
|
2020-04-06 22:49:13 +00:00
|
|
|
plugins: [
|
|
|
|
replace({
|
2023-04-11 08:43:47 +00:00
|
|
|
IS_BROWSER: false,
|
2023-06-28 14:42:09 +00:00
|
|
|
environment: 'nodejs',
|
2023-04-11 08:43:47 +00:00
|
|
|
_MODULE_TYPE: "'ESM'",
|
2023-06-28 14:42:09 +00:00
|
|
|
_NPM_PKG_VERSION: `'${process.env.npm_package_version}'` ?? "'0.0.1'",
|
|
|
|
__filename: 'fileURLToPath(import.meta.url)',
|
|
|
|
__dirname: 'fileURLToPath(new URL(\'.\', import.meta.url))',
|
2021-03-24 13:04:30 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2023-04-13 16:30:48 +00:00
|
|
|
rollupPluginTs(tsPluginOptions),
|
|
|
|
compileDts(tmpDeclarationsDir),
|
2023-04-11 08:43:47 +00:00
|
|
|
inject({
|
2023-06-28 14:42:09 +00:00
|
|
|
crypto: ['crypto', 'webcrypto'],
|
|
|
|
fileURLToPath: ['url', 'fileURLToPath']
|
2022-10-03 15:35:35 +00:00
|
|
|
}),
|
2023-04-13 16:30:48 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.jsx', '.cjsx'] }),
|
|
|
|
json(),
|
|
|
|
resolve({
|
|
|
|
exportConditions: ['node'],
|
|
|
|
resolveOnly
|
|
|
|
})
|
2022-10-03 15:35:35 +00:00
|
|
|
]
|
2023-04-13 16:30:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: join(tmpDeclarationsDir, 'index.d.ts'),
|
|
|
|
output: [{ file: 'dist/index.d.ts', format: 'es' }],
|
|
|
|
plugins: [
|
|
|
|
dts({
|
|
|
|
respectExternal: true
|
|
|
|
})
|
|
|
|
],
|
|
|
|
external: (module) => {
|
|
|
|
if (/^[./]/.test(module)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !resolveOnly(module)
|
|
|
|
}
|
2020-04-06 22:49:13 +00:00
|
|
|
}
|
|
|
|
]
|