Created
January 4, 2021 09:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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