2023-01-22 18:01:31 +01:00
|
|
|
import * as path from 'path'
|
|
|
|
import {build} from 'esbuild'
|
|
|
|
import type {Plugin} from 'esbuild'
|
|
|
|
|
|
|
|
const externalPlugin = (patterns: RegExp[]): Plugin => {
|
|
|
|
return {
|
|
|
|
name: `external`,
|
|
|
|
|
|
|
|
setup(build) {
|
|
|
|
build.onResolve({filter: /.*/}, (args) => {
|
|
|
|
const external = patterns.some((p) => {
|
|
|
|
return p.test(args.path)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (external) {
|
|
|
|
return {path: args.path, external}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const definedGlobals = {
|
|
|
|
global: 'window',
|
|
|
|
}
|
|
|
|
|
2023-08-03 10:46:36 +02:00
|
|
|
async function createBundles(watch: boolean) {
|
2023-01-22 18:01:31 +01:00
|
|
|
const pathToPackage = path.join(__dirname, '../')
|
2023-01-22 18:51:55 +01:00
|
|
|
const pkgJson = require(path.join(pathToPackage, 'package.json'))
|
|
|
|
const listOfDependencies = Object.keys(pkgJson.dependencies || {})
|
|
|
|
const listOfPeerDependencies = Object.keys(pkgJson.peerDependencies || {})
|
|
|
|
const listOfAllDependencies = [
|
|
|
|
...listOfDependencies,
|
|
|
|
...listOfPeerDependencies,
|
|
|
|
]
|
|
|
|
|
2023-01-22 18:01:31 +01:00
|
|
|
const esbuildConfig: Parameters<typeof build>[0] = {
|
|
|
|
entryPoints: [path.join(pathToPackage, 'src/index.ts')],
|
|
|
|
bundle: true,
|
|
|
|
sourcemap: true,
|
|
|
|
define: definedGlobals,
|
|
|
|
watch,
|
|
|
|
platform: 'neutral',
|
|
|
|
mainFields: ['browser', 'module', 'main'],
|
|
|
|
target: ['firefox57', 'chrome58'],
|
|
|
|
conditions: ['browser', 'node'],
|
2023-01-22 18:51:55 +01:00
|
|
|
plugins: [
|
|
|
|
externalPlugin([
|
|
|
|
// if a dependency is listed in the package.json, it should be external
|
|
|
|
...listOfAllDependencies.map((d) => new RegExp(`^${d}`)),
|
|
|
|
]),
|
|
|
|
],
|
2023-01-22 18:01:31 +01:00
|
|
|
}
|
|
|
|
|
2023-08-03 10:46:36 +02:00
|
|
|
await build({
|
2023-01-22 18:01:31 +01:00
|
|
|
...esbuildConfig,
|
|
|
|
outfile: path.join(pathToPackage, 'dist/index.js'),
|
|
|
|
format: 'cjs',
|
|
|
|
})
|
|
|
|
|
|
|
|
// build({
|
|
|
|
// ...esbuildConfig,
|
|
|
|
// outfile: path.join(pathToPackage, 'dist/index.mjs'),
|
|
|
|
// format: 'esm',
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
|
2023-08-03 10:46:36 +02:00
|
|
|
void createBundles(false)
|