Skip to content

Instantly share code, notes, and snippets.

@myobie
Created January 4, 2021 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/a5b064a1a5f759466a2dd9ea80ee04b6 to your computer and use it in GitHub Desktop.
Save myobie/a5b064a1a5f759466a2dd9ea80ee04b6 to your computer and use it in GitHub Desktop.
Build a project with estrella by convention using the settings in package.json
/* eslint-disable @typescript-eslint/no-var-requires */
const { join } = require('path')
const { build, file, cliopts } = require('estrella')
module.exports = {
buildPackage,
buildBrowserAndNodeModule,
buildBrowserModule,
buildNodeModule
}
async function buildPackage (cwd) {
const pkg = JSON.parse(await file.read(join(cwd, 'package.json'), { encoding: 'utf8' }))
if (pkg.exports) {
if (pkg.exports.module && pkg.exports.require) {
buildBrowserAndNodeModule(cwd)
} else if (pkg.exports.module) {
buildBrowserModule(cwd)
} else if (pkg.exports.require) {
buildNodeModule(cwd)
}
}
}
async function buildBrowserAndNodeModule (cwd) {
const pkg = JSON.parse(await file.read(join(cwd, 'package.json'), { encoding: 'utf8' }))
const common = {
entry: join(cwd, pkg.source),
sourcemap: true,
target: ['esnext'],
tslint: false,
incremental: !!cliopts.watch
}
build({
...common,
outfile: join(cwd, pkg.exports.module),
format: 'esm',
platform: 'browser'
})
build({
...common,
outfile: join(cwd, pkg.exports.require),
format: 'cjs',
platform: 'node',
tslint: false
})
}
async function buildBrowserModule (cwd) {
const pkg = JSON.parse(await file.read(join(cwd, 'package.json'), { encoding: 'utf8' }))
build({
entry: join(cwd, pkg.source),
sourcemap: true,
target: ['esnext'],
outfile: join(cwd, pkg.exports.module),
format: 'esm',
platform: 'browser',
incremental: !!cliopts.watch
})
}
async function buildNodeModule (cwd) {
const pkg = JSON.parse(await file.read(join(cwd, 'package.json'), { encoding: 'utf8' }))
build({
entry: join(cwd, pkg.source),
sourcemap: true,
target: ['esnext'],
tslint: { format: 'short' },
outfile: join(cwd, pkg.exports.require),
format: 'cjs',
platform: 'node',
incremental: !!cliopts.watch
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment