lbundle
Version:
Small zero-configuration bundler build on top of Rollup.js and SWC for NPM libraries
64 lines (61 loc) • 2.17 kB
JavaScript
import path from 'path';
import fs from 'fs';
import merge from 'lodash.merge';
import { getDefaultOptions } from './get-default-options.mjs';
import { getLibOutputs } from './get-lib-outputs.mjs';
import { getBinOutput } from './get-bin-output.mjs';
import { isNil } from '../utils/checks.mjs';
import { getCSSFilename } from './get-css-filename.mjs';
import { pascalCase } from 'change-case';
import { isTs } from '../utils/is-ts.mjs';
import { isJsx } from '../utils/is-jsx.mjs';
const getCtx = async (baseOptions)=>{
const options = merge(getDefaultOptions(), baseOptions);
const pkgPath = path.resolve(options.cwd, 'package.json');
const pkg = JSON.parse(await fs.promises.readFile(pkgPath, 'utf-8'));
const tsconfigPath = path.resolve(options.cwd, 'tsconfig.json');
const swcPath = path.resolve(options.cwd, '.swc');
if (isNil(pkg.source) && isNil(pkg['bin:source'])) {
throw new Error('[bundle] provide source entry for you library, set `package.json` `source` or `bin:source` field');
}
const isModule = pkg.type === 'module';
const globalName = pascalCase(pkg.name);
const cssFilename = getCSSFilename({
pkg
}) ?? 'index.css';
const libOutputs = getLibOutputs({
pkg,
globalName,
options,
isModule,
cssFilename
});
const binOutput = getBinOutput({
pkg,
options,
isModule
});
const resolvedSource = pkg.source ? path.resolve(options.cwd, pkg.source) : undefined;
const resolvedBinSource = pkg['bin:source'] ? path.resolve(options.cwd, pkg['bin:source']) : undefined;
const ts = isTs(pkg);
const jsx = isJsx(pkg);
return {
baseOptions: baseOptions,
options: options,
globalName,
pkgPath,
pkg,
swcPath: fs.existsSync(swcPath) ? swcPath : undefined,
tsconfigPath: fs.existsSync(tsconfigPath) ? tsconfigPath : undefined,
isModule,
resolvedSource,
resolvedBinSource,
isTs: ts,
isJsx: jsx,
libOutputs,
binOutput,
cssFilename
};
};
export { getCtx };
//# sourceMappingURL=get-ctx.mjs.map