2020-04-06 22:49:13 +00:00
|
|
|
'use strict'
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
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'
|
2022-01-17 10:04:55 +00:00
|
|
|
import json from '@rollup/plugin-json'
|
2020-04-06 22:49:13 +00:00
|
|
|
|
2022-01-17 10:04:55 +00:00
|
|
|
import { join } from 'path'
|
2022-10-03 15:35:35 +00:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
import { directories, name as _name, exports } from '../package.json'
|
2022-01-17 10:04:55 +00:00
|
|
|
import { compile } from './rollup-plugin-dts.js'
|
2020-04-06 22:49:13 +00:00
|
|
|
|
2021-08-06 22:14:30 +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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2021-08-06 22:14:30 +00:00
|
|
|
const { name } = _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
|
|
|
|
|
|
|
const tsBundleOptions = {
|
2021-08-06 22:14:30 +00:00
|
|
|
tsconfig: join(rootDir, 'tsconfig.json'),
|
|
|
|
outDir: undefined, // ignore outDir in tsconfig.json
|
2022-01-17 10:04:55 +00:00
|
|
|
include: ['src/ts/**/*', 'build/typings/is-browser.d.ts'],
|
|
|
|
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
|
|
|
|
2022-01-17 10:04:55 +00:00
|
|
|
function compileDts () {
|
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 () {
|
2022-01-17 10:04:55 +00:00
|
|
|
compile()
|
2021-08-06 22:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default [
|
2022-10-03 15:35:35 +00:00
|
|
|
{ // Node ESM with declarations
|
2020-04-06 22:49:13 +00:00
|
|
|
input: input,
|
|
|
|
output: [
|
|
|
|
{
|
2022-10-03 15:35:35 +00:00
|
|
|
file: join(rootDir, exports['.'].node.import),
|
2021-03-24 13:04:30 +00:00
|
|
|
...sourcemapOutputOptions,
|
2020-04-08 09:53:15 +00:00
|
|
|
format: 'es'
|
2020-04-06 22:49:13 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
replace({
|
2022-10-03 15:35:35 +00:00
|
|
|
IS_BROWSER: false,
|
2021-03-24 13:04:30 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
2022-01-17 10:04:55 +00:00
|
|
|
typescriptPlugin(tsBundleOptions),
|
|
|
|
compileDts(),
|
2022-10-03 15:35:35 +00:00
|
|
|
// resolve({
|
|
|
|
// browser: false,
|
|
|
|
// exportConditions: ['node']
|
|
|
|
// }),
|
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
2022-01-17 10:04:55 +00:00
|
|
|
json()
|
2022-10-03 15:35:35 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{ // Node CJS
|
|
|
|
input: input,
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
file: join(rootDir, exports['.'].node.require),
|
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'cjs',
|
|
|
|
exports: 'auto'
|
|
|
|
}
|
2021-03-24 13:04:30 +00:00
|
|
|
],
|
2022-10-03 15:35:35 +00:00
|
|
|
plugins: [
|
|
|
|
// replace({
|
|
|
|
// 'await import(': 'require(',
|
|
|
|
// delimiters: ['', ''],
|
|
|
|
// preventAssignment: true
|
|
|
|
// }),
|
|
|
|
replace({
|
|
|
|
IS_BROWSER: false,
|
|
|
|
preventAssignment: true
|
|
|
|
}),
|
|
|
|
typescriptPlugin(tsBundleOptions),
|
|
|
|
// resolve({
|
|
|
|
// browser: false,
|
|
|
|
// exportConditions: ['node']
|
|
|
|
// }),
|
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
|
|
|
json()
|
|
|
|
]
|
2020-04-06 22:49:13 +00:00
|
|
|
},
|
2022-10-03 15:35:35 +00:00
|
|
|
{ // ESM for browsers
|
2020-04-06 22:49:13 +00:00
|
|
|
input: input,
|
|
|
|
output: [
|
|
|
|
{
|
2022-10-03 15:35:35 +00:00
|
|
|
file: join(rootDir, exports['.'].default),
|
|
|
|
...sourcemapOutputOptions,
|
|
|
|
format: 'es'
|
2020-04-06 22:49:13 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 22:14:30 +00:00
|
|
|
file: join(dstDir, 'bundles/esm.js'),
|
2022-01-17 10:04:55 +00:00
|
|
|
...sourcemapOutputOptions,
|
2020-04-06 22:49:13 +00:00
|
|
|
format: 'es'
|
2021-03-24 13:04:30 +00:00
|
|
|
},
|
2022-01-17 10:04:55 +00:00
|
|
|
{
|
|
|
|
file: join(dstDir, 'bundles/esm.min.js'),
|
|
|
|
format: 'es',
|
|
|
|
plugins: [terser()]
|
2020-04-06 22:49:13 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
replace({
|
2021-03-24 13:04:30 +00:00
|
|
|
IS_BROWSER: true,
|
|
|
|
preventAssignment: true
|
2020-04-06 22:49:13 +00:00
|
|
|
}),
|
2021-03-24 13:04:30 +00:00
|
|
|
typescriptPlugin(tsBundleOptions),
|
2020-04-06 22:49:13 +00:00
|
|
|
resolve({
|
2021-03-24 13:04:30 +00:00
|
|
|
browser: true,
|
2022-10-03 15:35:35 +00:00
|
|
|
exportConditions: ['browser', 'default']
|
2021-03-24 13:04:30 +00:00
|
|
|
}),
|
2022-10-03 15:35:35 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
2022-01-17 10:04:55 +00:00
|
|
|
json()
|
|
|
|
]
|
2021-03-24 13:04:30 +00:00
|
|
|
},
|
2022-10-03 15:35:35 +00:00
|
|
|
{ // Browser bundles
|
2021-03-24 13:04:30 +00:00
|
|
|
input: input,
|
2021-08-06 22:14:30 +00:00
|
|
|
output: [
|
|
|
|
{
|
2022-10-03 15:35:35 +00:00
|
|
|
file: join(dstDir, 'bundles/iife.js'),
|
|
|
|
format: 'iife',
|
|
|
|
name: pkgCamelisedName,
|
|
|
|
plugins: [terser()]
|
2021-08-06 22:14:30 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-03 15:35:35 +00:00
|
|
|
file: join(dstDir, 'bundles/umd.js'),
|
|
|
|
format: 'umd',
|
|
|
|
name: pkgCamelisedName,
|
|
|
|
plugins: [terser()]
|
2021-08-06 22:14:30 +00:00
|
|
|
}
|
|
|
|
],
|
2020-04-06 22:49:13 +00:00
|
|
|
plugins: [
|
2022-10-03 15:35:35 +00:00
|
|
|
// replace({
|
|
|
|
// 'await import(': 'require(',
|
|
|
|
// delimiters: ['', ''],
|
|
|
|
// preventAssignment: true
|
|
|
|
// }),
|
2020-04-06 22:49:13 +00:00
|
|
|
replace({
|
2022-10-03 15:35:35 +00:00
|
|
|
IS_BROWSER: true,
|
2021-03-24 13:04:30 +00:00
|
|
|
preventAssignment: true
|
|
|
|
}),
|
|
|
|
typescriptPlugin(tsBundleOptions),
|
2022-10-03 15:35:35 +00:00
|
|
|
resolve({
|
|
|
|
browser: true,
|
|
|
|
exportConditions: ['browser', 'default'],
|
|
|
|
mainFields: ['browser', 'module', 'main']
|
|
|
|
}),
|
2022-01-17 10:04:55 +00:00
|
|
|
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
|
|
|
|
json()
|
2022-10-03 15:35:35 +00:00
|
|
|
]
|
2020-04-06 22:49:13 +00:00
|
|
|
}
|
|
|
|
]
|