2020-04-06 11:17:22 +00:00
|
|
|
'use strict'
|
|
|
|
|
2023-04-11 09:01:32 +00:00
|
|
|
import inject from '@rollup/plugin-inject'
|
2021-08-06 15:13:51 +00:00
|
|
|
import { nodeResolve as resolve } from '@rollup/plugin-node-resolve'
|
|
|
|
import replace from '@rollup/plugin-replace'
|
2023-04-11 09:01:32 +00:00
|
|
|
import terser from '@rollup/plugin-terser'
|
2021-08-06 15:13:51 +00:00
|
|
|
import typescriptPlugin from '@rollup/plugin-typescript'
|
|
|
|
import commonjs from '@rollup/plugin-commonjs'
|
2022-08-01 02:19:48 +00:00
|
|
|
import json from '@rollup/plugin-json'
|
2020-04-06 11:17:22 +00:00
|
|
|
|
2022-08-01 17:10:00 +00:00
|
|
|
import { dirname, join } from 'path'
|
2023-04-11 09:01:32 +00:00
|
|
|
import { existsSync, readFileSync } from 'fs'
|
|
|
|
|
|
|
|
// import { browser, name as _name, exports } from '../package.json' assert { type: 'json' }
|
2022-08-01 02:19:48 +00:00
|
|
|
import { compile } from './rollup-plugin-dts.js'
|
2020-04-06 11:17:22 +00:00
|
|
|
|
2023-04-11 09:01:32 +00:00
|
|
|
import * as url from 'url'
|
|
|
|
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
|
|
|
|
|
2021-08-06 15:13:51 +00:00
|
|
|
const rootDir = join(__dirname, '..')
|
2023-04-11 09:01:32 +00:00
|
|
|
const pkgJson = JSON.parse(readFileSync(join(rootDir, 'package.json')))
|
|
|
|
// const dstDir = join(rootDir, directories.dist)
|
2021-08-06 15:13:51 +00:00
|
|
|
const srcDir = join(rootDir, 'src', 'ts')
|
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
|
2023-04-11 09:01:32 +00:00
|
|
|
const { name } = pkgJson.name.match(regex).groups
|
2021-03-25 12:40:04 +00:00
|
|
|
const pkgCamelisedName = camelise(name)
|
2020-04-06 11:17:22 +00:00
|
|
|
|
2021-08-06 15:13:51 +00:00
|
|
|
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 = {
|
2021-08-06 15:13:51 +00:00
|
|
|
tsconfig: join(rootDir, 'tsconfig.json'),
|
2021-08-06 08:10:32 +00:00
|
|
|
outDir: undefined, // ignore outDir in tsconfig.json
|
2022-08-01 02:19:48 +00:00
|
|
|
include: ['src/ts/**/*', 'build/typings/is-browser.d.ts'],
|
|
|
|
exclude: ['src/**/*.spec.ts']
|
2021-03-25 12:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sourcemapOutputOptions = {
|
|
|
|
sourcemap: 'inline',
|
|
|
|
sourcemapExcludeSources: true
|
|
|
|
}
|
2020-04-06 11:17:22 +00:00
|
|
|
|
2022-08-01 02:19:48 +00:00
|
|
|
function compileDts () {
|
2021-08-06 15:13:51 +00:00
|
|
|
return {
|
2022-08-01 02:19:48 +00:00
|
|
|
name: 'compile-dts',
|
2021-08-06 15:29:23 +00:00
|
|
|
closeBundle () {
|
2022-08-01 02:19:48 +00:00
|
|
|
compile()
|
2021-08-06 15:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default [
|
2022-10-03 17:57:08 +00:00
|
|
|
{ // Browser ESM bundle
|
2023-04-11 09:01:32 +00:00
|
|
|
input,
|
2021-08-06 08:10:32 +00:00
|
|
|
output: [
|
|
|
|
{
|
2023-04-11 09:01:32 +00:00
|
|
|
file: join(rootDir, pkgJson.browser),
|
2021-08-06 08:10:32 +00:00
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'es'
|
|
|
|
}
|
|
|
|
],
|
2021-04-21 09:14:04 +00:00
|
|
|
plugins: [
|
|
|
|
replace({
|
|
|
|
IS_BROWSER: true,
|
2023-04-11 09:01:32 +00:00
|
|
|
_MODULE_TYPE: "'ESM'",
|
2021-04-21 09:14:04 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2021-11-15 11:00:37 +00:00
|
|
|
typescriptPlugin(tsBundleOptions),
|
2021-10-06 16:49:42 +00:00
|
|
|
resolve({
|
|
|
|
browser: true,
|
2023-04-11 09:01:32 +00:00
|
|
|
exportConditions: ['browser', 'default'],
|
|
|
|
mainFields: ['browser', 'module', 'main']
|
2022-08-01 02:19:48 +00:00
|
|
|
}),
|
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
|
|
|
json()
|
2021-11-15 11:00:37 +00:00
|
|
|
]
|
2021-04-21 09:14:04 +00:00
|
|
|
},
|
2022-08-01 02:19:48 +00:00
|
|
|
{ // Browser bundles
|
2023-04-11 09:01:32 +00:00
|
|
|
input,
|
2020-04-20 22:50:53 +00:00
|
|
|
output: [
|
2020-04-07 17:29:23 +00:00
|
|
|
{
|
2023-04-11 09:01:32 +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']),
|
2020-04-07 22:21:02 +00:00
|
|
|
format: 'iife',
|
2022-08-01 02:19:48 +00:00
|
|
|
name: pkgCamelisedName,
|
|
|
|
plugins: [terser()]
|
2021-03-25 12:40:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-11 09:01:32 +00:00
|
|
|
file: join(rootDir, pkgJson.exports['./umd-browser-bundle']),
|
2021-03-25 12:40:04 +00:00
|
|
|
format: 'umd',
|
2022-08-01 02:19:48 +00:00
|
|
|
name: pkgCamelisedName,
|
|
|
|
plugins: [terser()]
|
2020-04-06 11:17:22 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
replace({
|
2021-03-25 12:40:04 +00:00
|
|
|
IS_BROWSER: true,
|
2023-04-11 09:01:32 +00:00
|
|
|
_MODULE_TYPE: "'BUNDLE'",
|
2021-03-25 12:40:04 +00:00
|
|
|
preventAssignment: true
|
2020-04-06 11:17:22 +00:00
|
|
|
}),
|
2023-04-11 09:01:32 +00:00
|
|
|
typescriptPlugin({
|
|
|
|
...tsBundleOptions,
|
|
|
|
sourceMap: false
|
|
|
|
}),
|
2020-04-06 11:17:22 +00:00
|
|
|
resolve({
|
2021-03-25 12:40:04 +00:00
|
|
|
browser: true,
|
2022-08-01 02:19:48 +00:00
|
|
|
exportConditions: ['browser', 'default'],
|
|
|
|
mainFields: ['browser', 'module', 'main']
|
2020-04-07 22:21:02 +00:00
|
|
|
}),
|
2022-08-01 02:19:48 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
|
|
|
json()
|
2020-04-06 11:17:22 +00:00
|
|
|
]
|
|
|
|
},
|
2022-08-01 02:19:48 +00:00
|
|
|
{ // Node CJS
|
2023-04-11 09:01:32 +00:00
|
|
|
input,
|
2022-08-01 02:19:48 +00:00
|
|
|
output: [
|
|
|
|
{
|
2023-04-11 09:01:32 +00:00
|
|
|
file: join(rootDir, pkgJson.exports['.'].node.require.default),
|
2022-08-01 02:19:48 +00:00
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'cjs',
|
2023-04-11 09:01:32 +00:00
|
|
|
exports: 'auto',
|
|
|
|
plugins: [
|
|
|
|
terser()
|
|
|
|
]
|
2022-08-01 02:19:48 +00:00
|
|
|
}
|
|
|
|
],
|
2020-04-07 22:21:02 +00:00
|
|
|
plugins: [
|
|
|
|
replace({
|
2022-08-01 02:19:48 +00:00
|
|
|
'await import(': 'require(',
|
|
|
|
delimiters: ['', ''],
|
2021-03-25 12:40:04 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2022-08-01 02:19:48 +00:00
|
|
|
replace({
|
|
|
|
IS_BROWSER: false,
|
2023-04-11 09:01:32 +00:00
|
|
|
_MODULE_TYPE: "'CJS'",
|
2022-08-01 02:19:48 +00:00
|
|
|
preventAssignment: true
|
2021-03-25 12:40:04 +00:00
|
|
|
}),
|
2023-04-11 09:01:32 +00:00
|
|
|
inject({
|
|
|
|
crypto: ['crypto', 'webcrypto']
|
|
|
|
}),
|
2022-08-01 02:19:48 +00:00
|
|
|
typescriptPlugin(tsBundleOptions),
|
2021-10-06 16:49:42 +00:00
|
|
|
resolve({
|
2021-11-15 11:00:37 +00:00
|
|
|
browser: false,
|
2023-04-11 09:01:32 +00:00
|
|
|
exportConditions: ['require', 'node', 'module']
|
2021-10-06 16:49:42 +00:00
|
|
|
}),
|
2022-08-01 02:19:48 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
|
|
|
json()
|
2021-11-15 11:00:37 +00:00
|
|
|
]
|
2021-03-25 12:40:04 +00:00
|
|
|
},
|
2022-10-03 17:57:08 +00:00
|
|
|
{ // Node ESM and type declarations
|
2023-04-11 09:01:32 +00:00
|
|
|
input,
|
2021-08-04 10:50:36 +00:00
|
|
|
output: [
|
|
|
|
{
|
2023-04-11 09:01:32 +00:00
|
|
|
file: join(rootDir, pkgJson.exports['.'].node.import.default),
|
2021-08-04 10:50:36 +00:00
|
|
|
...sourcemapOutputOptions,
|
2023-04-11 09:01:32 +00:00
|
|
|
format: 'es',
|
|
|
|
plugins: [
|
|
|
|
terser()
|
|
|
|
]
|
2021-08-04 10:50:36 +00:00
|
|
|
}
|
|
|
|
],
|
2021-03-25 12:40:04 +00:00
|
|
|
plugins: [
|
|
|
|
replace({
|
|
|
|
IS_BROWSER: false,
|
2023-04-11 09:01:32 +00:00
|
|
|
_MODULE_TYPE: "'ESM'",
|
|
|
|
__filename: `'${pkgJson.exports['.'].node.import.default}'`,
|
|
|
|
__dirname: `'${dirname(pkgJson.exports['.'].node.import.default)}'`,
|
2021-03-25 12:40:04 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2023-04-11 09:01:32 +00:00
|
|
|
inject({
|
|
|
|
crypto: ['crypto', 'webcrypto']
|
|
|
|
}),
|
2021-03-25 12:40:04 +00:00
|
|
|
typescriptPlugin(tsBundleOptions),
|
2021-10-06 16:49:42 +00:00
|
|
|
resolve({
|
2021-11-15 11:00:37 +00:00
|
|
|
browser: false,
|
2023-04-11 09:01:32 +00:00
|
|
|
exportConditions: ['node', 'import']
|
2021-10-06 16:49:42 +00:00
|
|
|
}),
|
2022-08-01 19:53:46 +00:00
|
|
|
compileDts(),
|
2022-08-01 02:19:48 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
|
|
|
json()
|
2021-08-06 08:10:32 +00:00
|
|
|
]
|
2020-04-06 11:17:22 +00:00
|
|
|
}
|
|
|
|
]
|