2021-08-07 23:24:37 +02:00
|
|
|
import * as path from 'path'
|
2021-08-07 21:41:07 +02:00
|
|
|
import {build} from 'esbuild'
|
|
|
|
import type {Plugin} from 'esbuild'
|
2021-08-07 23:30:07 +02:00
|
|
|
import {existsSync, mkdirSync, writeFileSync} from 'fs'
|
2021-08-07 21:41:07 +02:00
|
|
|
|
|
|
|
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',
|
|
|
|
}
|
|
|
|
|
|
|
|
function createBundles(watch: boolean) {
|
|
|
|
const pathToPackage = path.join(__dirname, '../')
|
|
|
|
const esbuildConfig: Parameters<typeof build>[0] = {
|
|
|
|
entryPoints: [path.join(pathToPackage, 'src/index.tsx')],
|
|
|
|
bundle: true,
|
|
|
|
sourcemap: true,
|
|
|
|
define: definedGlobals,
|
|
|
|
watch,
|
|
|
|
platform: 'neutral',
|
|
|
|
mainFields: ['browser', 'module', 'main'],
|
|
|
|
target: ['firefox57', 'chrome58'],
|
|
|
|
conditions: ['browser', 'node'],
|
|
|
|
// every dependency is considered external
|
|
|
|
plugins: [externalPlugin([/^[\@a-zA-Z]+/])],
|
|
|
|
}
|
|
|
|
|
|
|
|
build({
|
|
|
|
...esbuildConfig,
|
|
|
|
define: {...definedGlobals, 'process.env.NODE_ENV': '"production"'},
|
2021-08-07 22:30:29 +02:00
|
|
|
outfile: path.join(pathToPackage, 'dist/index.production.js'),
|
2021-08-07 21:41:07 +02:00
|
|
|
format: 'cjs',
|
|
|
|
})
|
|
|
|
|
|
|
|
build({
|
|
|
|
...esbuildConfig,
|
|
|
|
define: {...definedGlobals, 'process.env.NODE_ENV': '"development"'},
|
2021-08-07 22:30:29 +02:00
|
|
|
outfile: path.join(pathToPackage, 'dist/index.development.js'),
|
2021-08-07 21:41:07 +02:00
|
|
|
format: 'cjs',
|
|
|
|
})
|
|
|
|
|
2021-08-07 23:30:07 +02:00
|
|
|
if (!existsSync(path.join(pathToPackage, 'dist')))
|
|
|
|
mkdirSync(path.join(pathToPackage, 'dist'))
|
2021-08-07 23:24:37 +02:00
|
|
|
|
2021-08-07 21:41:07 +02:00
|
|
|
writeFileSync(
|
2021-08-07 22:30:29 +02:00
|
|
|
path.join(pathToPackage, 'dist/index.js'),
|
2021-08-07 21:41:07 +02:00
|
|
|
`module.exports =
|
|
|
|
process.env.NODE_ENV === "production"
|
2021-08-07 22:30:29 +02:00
|
|
|
? require("./index.production.js")
|
|
|
|
: require("./index.development.js")`,
|
2021-08-07 21:41:07 +02:00
|
|
|
{encoding: 'utf-8'},
|
|
|
|
)
|
|
|
|
|
2021-08-07 23:24:37 +02:00
|
|
|
// build({
|
|
|
|
// ...esbuildConfig,
|
|
|
|
// outfile: path.join(pathToPackage, 'dist/index.mjs'),
|
|
|
|
// format: 'esm',
|
|
|
|
// })
|
2021-08-07 21:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createBundles(false)
|