Comparing version 3.0.1 to 3.1.0
@@ -40,2 +40,11 @@ #!/usr/bin/env node | ||
const formatDuration = (duration)=>duration >= 1000 ? `${duration / 1000}s` : `${duration}ms`; | ||
function hasPackageJson(cwd) { | ||
return _hasPackageJson.apply(this, arguments); | ||
} | ||
function _hasPackageJson() { | ||
_hasPackageJson = _async_to_generator$1(function*(cwd) { | ||
return yield fileExists(path.resolve(cwd, 'package.json')); | ||
}); | ||
return _hasPackageJson.apply(this, arguments); | ||
} | ||
function getPackageMeta(cwd) { | ||
@@ -68,4 +77,21 @@ return _getPackageMeta.apply(this, arguments); | ||
}; | ||
function fileExists(filePath) { | ||
return _fileExists.apply(this, arguments); | ||
} | ||
function _fileExists() { | ||
_fileExists = _async_to_generator$1(function*(filePath) { | ||
try { | ||
yield fs.access(filePath); | ||
return true; | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
}); | ||
return _fileExists.apply(this, arguments); | ||
} | ||
var version = "3.0.1"; | ||
var version = "3.1.0"; | ||
@@ -128,2 +154,6 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
_lintPackage = _async_to_generator(function*(cwd) { | ||
// Not package.json detected, skip package linting | ||
if (!(yield hasPackageJson(cwd))) { | ||
return; | ||
} | ||
const { publint } = yield import('publint'); | ||
@@ -130,0 +160,0 @@ const { printMessage } = yield import('publint/utils'); |
@@ -26,4 +26,4 @@ import { JscTarget } from '@swc/core'; | ||
declare function bundle(entryPath: string, { cwd, ...options }?: BundleConfig): Promise<any>; | ||
declare function bundle(entryPath: string, { cwd: _cwd, ...options }?: BundleConfig): Promise<any>; | ||
export { BundleConfig, bundle }; |
@@ -6,4 +6,2 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
var rollup = require('rollup'); | ||
var fs$1 = require('fs'); | ||
var module$1 = require('module'); | ||
var rollupPluginSwc3 = require('rollup-plugin-swc3'); | ||
@@ -15,7 +13,41 @@ var commonjs = require('@rollup/plugin-commonjs'); | ||
var replace = require('@rollup/plugin-replace'); | ||
var prettyBytes = require('pretty-bytes'); | ||
var module$1 = require('module'); | ||
const rootDir = process.cwd(); | ||
var config = { | ||
rootDir | ||
}; | ||
function chunkSizeCollector() { | ||
const sizes = new Map(); | ||
function addSize(name, size) { | ||
sizes.set(name, size); | ||
} | ||
return { | ||
plugin: (cwd)=>{ | ||
return { | ||
name: 'collect-sizes', | ||
augmentChunkHash () { | ||
// Do nothing, but use the hook to keep the plugin instance alive | ||
}, | ||
renderChunk (code, chunk, options) { | ||
const dir = options.dir || options.file && path.dirname(options.file); | ||
let fileName = chunk.fileName; | ||
if (dir) { | ||
fileName = path.relative(cwd, path.join(dir, fileName)); | ||
} | ||
addSize(fileName, code.length); | ||
return null; | ||
} | ||
}; | ||
}, | ||
getSizeStats () { | ||
const sizeStats = []; | ||
sizes.forEach((size, name)=>{ | ||
sizeStats.push([ | ||
name, | ||
prettyBytes(size), | ||
size | ||
]); | ||
}); | ||
return sizeStats; | ||
} | ||
}; | ||
} | ||
@@ -25,4 +57,4 @@ function getTypings(pkg) { | ||
} | ||
function getDistPath(distPath) { | ||
return path.resolve(config.rootDir, distPath); | ||
function getDistPath(distPath, cwd) { | ||
return path.resolve(cwd, distPath); | ||
} | ||
@@ -74,3 +106,3 @@ function findExport(field) { | ||
} | ||
function getExportDist(pkg) { | ||
function getExportDist(pkg, cwd) { | ||
const paths = getExportPaths(pkg)['.']; | ||
@@ -81,3 +113,3 @@ const dist = []; | ||
format: 'cjs', | ||
file: getDistPath(paths.main) | ||
file: getDistPath(paths.main, cwd) | ||
}); | ||
@@ -88,3 +120,3 @@ } | ||
format: 'esm', | ||
file: getDistPath(paths.module) | ||
file: getDistPath(paths.module, cwd) | ||
}); | ||
@@ -95,3 +127,3 @@ } | ||
format: 'esm', | ||
file: getDistPath(paths.export) | ||
file: getDistPath(paths.export, cwd) | ||
}); | ||
@@ -103,3 +135,3 @@ } | ||
format: 'esm', | ||
file: getDistPath('dist/index.js') | ||
file: getDistPath('dist/index.js', cwd) | ||
}); | ||
@@ -109,4 +141,3 @@ } | ||
} | ||
function getExportConditionDist(pkg, exportCondition) { | ||
// const pkgExports = pkg.exports || {} | ||
function getExportConditionDist(pkg, exportCondition, cwd) { | ||
const dist = []; | ||
@@ -117,14 +148,12 @@ // "exports": "..." | ||
format: pkg.type === 'module' ? 'esm' : 'cjs', | ||
file: getDistPath(exportCondition) | ||
file: getDistPath(exportCondition, cwd) | ||
}); | ||
} else { | ||
// "./<subexport>": { } | ||
const subExports = exportCondition // pkgExports[subExport] | ||
; | ||
const subExports = exportCondition; | ||
// Ignore json exports, like "./package.json" | ||
// if (subExport.endsWith('.json')) return dist | ||
if (typeof subExports === 'string') { | ||
dist.push({ | ||
format: 'esm', | ||
file: getDistPath(subExports) | ||
file: getDistPath(subExports, cwd) | ||
}); | ||
@@ -135,3 +164,3 @@ } else { | ||
format: 'cjs', | ||
file: getDistPath(subExports.require) | ||
file: getDistPath(subExports.require, cwd) | ||
}); | ||
@@ -142,3 +171,3 @@ } | ||
format: 'esm', | ||
file: getDistPath(subExports.import) | ||
file: getDistPath(subExports.import, cwd) | ||
}); | ||
@@ -151,3 +180,3 @@ } | ||
function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) { | ||
function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
@@ -166,3 +195,3 @@ var info = gen[key](arg); | ||
} | ||
function _async_to_generator$1(fn) { | ||
function _async_to_generator$3(fn) { | ||
return function() { | ||
@@ -173,6 +202,6 @@ var self = this, args = arguments; | ||
function _next(value) { | ||
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value); | ||
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err); | ||
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
@@ -187,3 +216,2 @@ _next(undefined); | ||
} | ||
const formatDuration = (duration)=>duration >= 1000 ? `${duration / 1000}s` : `${duration}ms`; | ||
function getPackageMeta(cwd) { | ||
@@ -193,3 +221,3 @@ return _getPackageMeta.apply(this, arguments); | ||
function _getPackageMeta() { | ||
_getPackageMeta = _async_to_generator$1(function*(cwd) { | ||
_getPackageMeta = _async_to_generator$3(function*(cwd) { | ||
const pkgFilePath = path.resolve(cwd, 'package.json'); | ||
@@ -217,6 +245,2 @@ let targetPackageJson = {}; | ||
}; | ||
function isTypescript(filename) { | ||
const ext = path.extname(filename); | ||
return ext === '.ts' || ext === '.tsx'; | ||
} | ||
function fileExists(filePath) { | ||
@@ -226,3 +250,3 @@ return _fileExists.apply(this, arguments); | ||
function _fileExists() { | ||
_fileExists = _async_to_generator$1(function*(filePath) { | ||
_fileExists = _async_to_generator$3(function*(filePath) { | ||
try { | ||
@@ -240,4 +264,74 @@ yield fs.access(filePath); | ||
} | ||
// . -> pkg name | ||
// ./lite -> <pkg name>/lite | ||
function getExportPath(pkg, cwd, exportName) { | ||
const name = pkg.name || path.basename(cwd); | ||
if (exportName === '.' || !exportName) return name; | ||
return path.join(name, exportName); | ||
} | ||
const isNotNull = (n)=>Boolean(n); | ||
const SRC = 'src' // resolve from src/ directory | ||
; | ||
function resolveSourceFile(cwd, filename) { | ||
return path.resolve(cwd, SRC, filename); | ||
} | ||
// Map '.' -> './index.[ext]' | ||
// Map './lite' -> './lite.[ext]' | ||
// Return undefined if no match or if it's package.json exports | ||
function getSourcePathFromExportPath(cwd, exportPath) { | ||
return _getSourcePathFromExportPath.apply(this, arguments); | ||
} | ||
function _getSourcePathFromExportPath() { | ||
_getSourcePathFromExportPath = _async_to_generator$3(function*(cwd, exportPath) { | ||
const exts = [ | ||
'js', | ||
'cjs', | ||
'mjs', | ||
'jsx', | ||
'ts', | ||
'tsx' | ||
]; | ||
for (const ext of exts){ | ||
// ignore package.json | ||
if (exportPath.endsWith('package.json')) return; | ||
if (exportPath === '.') exportPath = './index'; | ||
const filename = resolveSourceFile(cwd, `${exportPath}.${ext}`); | ||
if (yield fileExists(filename)) { | ||
return filename; | ||
} | ||
} | ||
return; | ||
}); | ||
return _getSourcePathFromExportPath.apply(this, arguments); | ||
} | ||
function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _async_to_generator$2(fn) { | ||
return function() { | ||
var self = this, args = arguments; | ||
return new Promise(function(resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
function _extends$1() { | ||
@@ -268,18 +362,4 @@ _extends$1 = Object.assign || function(target) { | ||
}; | ||
let hasLoggedTsWarning = false; | ||
function resolveTypescript(cwd) { | ||
let ts; | ||
const m = new module$1.Module('', undefined); | ||
m.paths = module$1.Module._nodeModulePaths(cwd); | ||
try { | ||
ts = m.require('typescript'); | ||
} catch (_) { | ||
console.error(_); | ||
if (!hasLoggedTsWarning) { | ||
hasLoggedTsWarning = true; | ||
exit('Could not load TypeScript compiler. Try to install `typescript` as dev dependency'); | ||
} | ||
} | ||
return ts; | ||
} | ||
// This can also be passed down as stats from top level | ||
const sizeCollector = chunkSizeCollector(); | ||
function getBuildEnv(envs) { | ||
@@ -298,3 +378,3 @@ if (!envs.includes('NODE_ENV')) { | ||
} | ||
function buildInputConfig(entry, pkg, options, { tsConfigPath , tsCompilerOptions , dtsOnly }) { | ||
function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerOptions }, dtsOnly) { | ||
var _options_external; | ||
@@ -310,4 +390,9 @@ const externals = options.noExternal ? [] : [ | ||
const hasSpecifiedTsTarget = Boolean((tsCompilerOptions == null ? void 0 : tsCompilerOptions.target) && tsConfigPath); | ||
const sizePlugin = sizeCollector.plugin(cwd); | ||
const commonPlugins = [ | ||
shebang(), | ||
sizePlugin | ||
]; | ||
const plugins = (dtsOnly ? [ | ||
shebang(), | ||
...commonPlugins, | ||
useTypescript && require('rollup-plugin-dts').default({ | ||
@@ -330,2 +415,3 @@ compilerOptions: _extends$1({}, tsCompilerOptions, { | ||
] : [ | ||
...commonPlugins, | ||
replace({ | ||
@@ -349,3 +435,2 @@ values: getBuildEnv(options.env || []), | ||
json(), | ||
shebang(), | ||
rollupPluginSwc3.swc({ | ||
@@ -403,3 +488,3 @@ include: /\.(m|c)?[jt]sx?$/, | ||
} | ||
function buildOutputConfigs(options, pkg, { tsCompilerOptions , dtsOnly }) { | ||
function buildOutputConfigs(pkg, options, cwd, { tsCompilerOptions }, dtsOnly) { | ||
const { format , exportCondition } = options; | ||
@@ -409,11 +494,10 @@ const exportPaths = getExportPaths(pkg); | ||
// add ESModule mark if cjs and ESModule are both generated; | ||
// TODO: support `import` in exportCondition | ||
const mainExport = exportPaths['.']; | ||
const useEsModuleMark = Boolean(tsCompilerOptions.esModuleInterop || mainExport.main && mainExport.module); | ||
const typings = getTypings(pkg); | ||
const file = options.file && path.resolve(config.rootDir, options.file); | ||
const dtsDir = typings ? path.dirname(path.resolve(config.rootDir, typings)) : path.resolve(config.rootDir, 'dist'); | ||
const file = options.file && path.resolve(cwd, options.file); | ||
const dtsDir = typings ? path.dirname(path.resolve(cwd, typings)) : path.resolve(cwd, 'dist'); | ||
// file base name without extension | ||
const name = file ? file.replace(new RegExp(`${path.extname(file)}$`), '') : undefined; | ||
const dtsFile = file ? name + '.d.ts' : (exportCondition == null ? void 0 : exportCondition.name) ? path.resolve(dtsDir, (exportCondition.name === '.' ? 'index' : exportCondition.name) + '.d.ts') : typings && path.resolve(config.rootDir, typings); | ||
const dtsFile = file ? name + '.d.ts' : (exportCondition == null ? void 0 : exportCondition.name) ? path.resolve(dtsDir, (exportCondition.name === '.' ? 'index' : exportCondition.name) + '.d.ts') : typings && path.resolve(cwd, typings); | ||
// If there's dts file, use `output.file` | ||
@@ -438,30 +522,35 @@ const dtsPathConfig = dtsFile ? { | ||
} | ||
function buildConfig(entry, pkg, bundleConfig, dtsOnly) { | ||
// build configs for each entry from package exports | ||
function buildEntryConfig(pkg, bundleConfig, cwd, tsOptions, dtsOnly) { | ||
return _buildEntryConfig.apply(this, arguments); | ||
} | ||
function _buildEntryConfig() { | ||
_buildEntryConfig = _async_to_generator$2(function*(pkg, bundleConfig, cwd, tsOptions, dtsOnly) { | ||
const packageExports = pkg.exports || {}; | ||
const configs = Object.keys(packageExports).map(/*#__PURE__*/ _async_to_generator$2(function*(entryExport) { | ||
const source = yield getSourcePathFromExportPath(cwd, entryExport); | ||
if (!source) return undefined; | ||
if (dtsOnly && !(tsOptions == null ? void 0 : tsOptions.tsConfigPath)) return; | ||
bundleConfig.exportCondition = { | ||
source, | ||
name: entryExport, | ||
export: packageExports[entryExport] | ||
}; | ||
const entry = resolveSourceFile(cwd, source); | ||
const rollupConfig = buildConfig(entry, pkg, bundleConfig, cwd, tsOptions, dtsOnly); | ||
return rollupConfig; | ||
})); | ||
return (yield Promise.all(configs)).filter((n)=>Boolean(n)); | ||
}); | ||
return _buildEntryConfig.apply(this, arguments); | ||
} | ||
function buildConfig(entry, pkg, bundleConfig, cwd, tsOptions, dtsOnly) { | ||
var _options_exportCondition; | ||
const { file } = bundleConfig; | ||
const useTypescript = isTypescript(entry); | ||
const useTypescript = Boolean(tsOptions.tsConfigPath); | ||
const options = _extends$1({}, bundleConfig, { | ||
useTypescript | ||
}); | ||
let tsCompilerOptions = {}; | ||
let tsConfigPath; | ||
if (useTypescript) { | ||
const ts = resolveTypescript(config.rootDir); | ||
tsConfigPath = path.resolve(config.rootDir, 'tsconfig.json'); | ||
if (fs$1.existsSync(tsConfigPath)) { | ||
const basePath = tsConfigPath ? path.dirname(tsConfigPath) : config.rootDir; | ||
const tsconfigJSON = ts.readConfigFile(tsConfigPath, ts.sys.readFile).config; | ||
tsCompilerOptions = ts.parseJsonConfigFileContent(tsconfigJSON, ts.sys, basePath).options; | ||
} else { | ||
tsConfigPath = undefined; | ||
exit('tsconfig.json is missing in your project directory'); | ||
} | ||
} | ||
const typescriptOptions = { | ||
dtsOnly, | ||
tsConfigPath, | ||
tsCompilerOptions | ||
}; | ||
const inputOptions = buildInputConfig(entry, pkg, options, typescriptOptions); | ||
const outputExports = options.exportCondition ? getExportConditionDist(pkg, options.exportCondition.export) : getExportDist(pkg); | ||
const inputOptions = buildInputConfig(entry, pkg, options, cwd, tsOptions, dtsOnly); | ||
const outputExports = options.exportCondition ? getExportConditionDist(pkg, options.exportCondition.export, cwd) : getExportDist(pkg, cwd); | ||
let outputConfigs = []; | ||
@@ -471,6 +560,6 @@ // Generate dts job - single config | ||
outputConfigs = [ | ||
buildOutputConfigs(_extends$1({}, bundleConfig, { | ||
buildOutputConfigs(pkg, _extends$1({}, bundleConfig, { | ||
format: 'es', | ||
useTypescript | ||
}), pkg, typescriptOptions) | ||
}), cwd, tsOptions, dtsOnly) | ||
]; | ||
@@ -480,7 +569,7 @@ } else { | ||
outputConfigs = outputExports.map((exportDist)=>{ | ||
return buildOutputConfigs(_extends$1({}, bundleConfig, { | ||
return buildOutputConfigs(pkg, _extends$1({}, bundleConfig, { | ||
file: exportDist.file, | ||
format: exportDist.format, | ||
useTypescript | ||
}), pkg, typescriptOptions); | ||
}), cwd, tsOptions, dtsOnly); | ||
}); | ||
@@ -492,7 +581,7 @@ // CLI output option is always prioritized | ||
outputConfigs = [ | ||
buildOutputConfigs(_extends$1({}, bundleConfig, { | ||
buildOutputConfigs(pkg, _extends$1({}, bundleConfig, { | ||
file, | ||
format: bundleConfig.format || fallbackFormat, | ||
useTypescript | ||
}), pkg, typescriptOptions) | ||
}), cwd, tsOptions, dtsOnly) | ||
]; | ||
@@ -504,7 +593,86 @@ } | ||
output: outputConfigs, | ||
exportName: ((_options_exportCondition = options.exportCondition) == null ? void 0 : _options_exportCondition.name) || '.', | ||
dtsOnly | ||
exportName: ((_options_exportCondition = options.exportCondition) == null ? void 0 : _options_exportCondition.name) || '.' | ||
}; | ||
} | ||
function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _async_to_generator$1(fn) { | ||
return function() { | ||
var self = this, args = arguments; | ||
return new Promise(function(resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
let hasLoggedTsWarning = false; | ||
function resolveTypescript(cwd) { | ||
let ts; | ||
const m = new module$1.Module('', undefined); | ||
m.paths = module$1.Module._nodeModulePaths(cwd); | ||
try { | ||
ts = m.require('typescript'); | ||
} catch (_) { | ||
console.error(_); | ||
if (!hasLoggedTsWarning) { | ||
hasLoggedTsWarning = true; | ||
exit('Could not load TypeScript compiler. Try to install `typescript` as dev dependency'); | ||
} | ||
} | ||
return ts; | ||
} | ||
function resolveTsConfig(cwd) { | ||
return _resolveTsConfig.apply(this, arguments); | ||
} | ||
function _resolveTsConfig() { | ||
_resolveTsConfig = _async_to_generator$1(function*(cwd) { | ||
let tsCompilerOptions = {}; | ||
let tsConfigPath; | ||
const ts = resolveTypescript(cwd); | ||
tsConfigPath = path.resolve(cwd, 'tsconfig.json'); | ||
if (yield fileExists(tsConfigPath)) { | ||
const basePath = tsConfigPath ? path.dirname(tsConfigPath) : cwd; | ||
const tsconfigJSON = ts.readConfigFile(tsConfigPath, ts.sys.readFile).config; | ||
tsCompilerOptions = ts.parseJsonConfigFileContent(tsconfigJSON, ts.sys, basePath).options; | ||
} else { | ||
tsConfigPath = undefined; | ||
return null; | ||
} | ||
return { | ||
tsCompilerOptions, | ||
tsConfigPath | ||
}; | ||
}); | ||
return _resolveTsConfig.apply(this, arguments); | ||
} | ||
function logSizeStats() { | ||
const stats = sizeCollector.getSizeStats(); | ||
const maxLength = Math.max(...stats.map(([filename])=>filename.length)); | ||
stats.forEach(([filename, prettiedSize])=>{ | ||
const padding = ' '.repeat(maxLength - filename.length); | ||
const action = filename.endsWith('.d.ts') ? 'Typed' : 'Built'; | ||
logger.log(` ✓ ${action} ${filename}${padding} - ${prettiedSize}`); | ||
}); | ||
} | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
@@ -565,7 +733,2 @@ try { | ||
} | ||
const SRC = 'src' // resolve from src/ directory | ||
; | ||
function logBuild(exportPath, dtsOnly, duration) { | ||
logger.log(` ✓ ${dtsOnly ? 'Typed' : 'Built'} ${exportPath} ${formatDuration(duration)}`); | ||
} | ||
function assignDefault(options, name, defaultValue) { | ||
@@ -576,34 +739,2 @@ if (!(name in options) || options[name] == null) { | ||
} | ||
function resolveSourceFile(cwd, filename) { | ||
return path.resolve(cwd, SRC, filename); | ||
} | ||
function getSourcePathFromExportPath(cwd, exportPath) { | ||
return _getSourcePathFromExportPath.apply(this, arguments); | ||
} | ||
function _getSourcePathFromExportPath() { | ||
_getSourcePathFromExportPath = // Map '.' -> './index.[ext]' | ||
// Map './lite' -> './lite.[ext]' | ||
// Return undefined if no match or if it's package.json exports | ||
_async_to_generator(function*(cwd, exportPath) { | ||
const exts = [ | ||
'js', | ||
'cjs', | ||
'mjs', | ||
'jsx', | ||
'ts', | ||
'tsx' | ||
]; | ||
for (const ext of exts){ | ||
// ignore package.json | ||
if (exportPath.endsWith('package.json')) return; | ||
if (exportPath === '.') exportPath = './index'; | ||
const filename = resolveSourceFile(cwd, `${exportPath}.${ext}`); | ||
if (yield fileExists(filename)) { | ||
return filename; | ||
} | ||
} | ||
return; | ||
}); | ||
return _getSourcePathFromExportPath.apply(this, arguments); | ||
} | ||
function hasMultiEntryExport(pkg) { | ||
@@ -620,12 +751,18 @@ const packageExportsField = pkg.exports || {}; | ||
_bundle = _async_to_generator(function*(entryPath, _param = {}) { | ||
var { cwd } = _param, options = _object_without_properties_loose(_param, [ | ||
var { cwd: _cwd } = _param, options = _object_without_properties_loose(_param, [ | ||
"cwd" | ||
]); | ||
config.rootDir = path.resolve(process.cwd(), cwd || ''); | ||
const cwd = path.resolve(process.cwd(), _cwd || ''); | ||
assignDefault(options, 'format', 'es'); | ||
assignDefault(options, 'minify', false); | ||
assignDefault(options, 'target', 'es2015'); | ||
const pkg = yield getPackageMeta(config.rootDir); | ||
const pkg = yield getPackageMeta(cwd); | ||
const packageExportsField = pkg.exports || {}; | ||
const isMultiEntries = hasMultiEntryExport(pkg); | ||
const tsConfig = yield resolveTsConfig(cwd); | ||
const hasTsConfig = Boolean(tsConfig == null ? void 0 : tsConfig.tsConfigPath); | ||
const defaultTsOptions = { | ||
tsConfigPath: tsConfig == null ? void 0 : tsConfig.tsConfigPath, | ||
tsCompilerOptions: (tsConfig == null ? void 0 : tsConfig.tsCompilerOptions) || {} | ||
}; | ||
// Handle single entry file | ||
@@ -635,31 +772,9 @@ if (!isMultiEntries) { | ||
// e.g. "exports": "./dist/index.js" -> use "./index.<ext>" as entry | ||
entryPath = entryPath || (yield getSourcePathFromExportPath(config.rootDir, '.')) || ''; | ||
entryPath = entryPath || (yield getSourcePathFromExportPath(cwd, '.')) || ''; | ||
} | ||
function buildEntryConfig(packageExports, dtsOnly) { | ||
return _buildEntryConfig.apply(this, arguments); | ||
} | ||
function _buildEntryConfig() { | ||
_buildEntryConfig = _async_to_generator(function*(packageExports, dtsOnly) { | ||
const configs = Object.keys(packageExports).map(/*#__PURE__*/ _async_to_generator(function*(entryExport) { | ||
const source = yield getSourcePathFromExportPath(config.rootDir, entryExport); | ||
if (!source) return undefined; | ||
if (dtsOnly && !isTypescript(source)) return; | ||
options.exportCondition = { | ||
source, | ||
name: entryExport, | ||
export: packageExports[entryExport] | ||
}; | ||
const entry = resolveSourceFile(cwd, source); | ||
const rollupConfig = buildConfig(entry, pkg, options, dtsOnly); | ||
return rollupConfig; | ||
})); | ||
return (yield Promise.all(configs)).filter((n)=>Boolean(n)); | ||
}); | ||
return _buildEntryConfig.apply(this, arguments); | ||
} | ||
const bundleOrWatch = (rollupConfig)=>{ | ||
const { input , exportName } = rollupConfig; | ||
const exportPath = getExportPath(pkg, exportName); | ||
const exportPath = getExportPath(pkg, cwd, exportName); | ||
// Log original entry file relative path | ||
const source = typeof input.input === 'string' ? path.relative(config.rootDir, input.input) : exportPath; | ||
const source = typeof input.input === 'string' ? path.relative(cwd, input.input) : exportPath; | ||
const buildMetadata = { | ||
@@ -671,3 +786,3 @@ source | ||
} | ||
return runBundle(rollupConfig, buildMetadata); | ||
return runBundle(rollupConfig); | ||
}; | ||
@@ -686,26 +801,22 @@ const hasSpecifiedEntryFile = entryPath ? (yield fileExists(entryPath)) && (yield fs.stat(entryPath)).isFile() : false; | ||
} | ||
let result; | ||
if (isMultiEntries) { | ||
const pkgExports = packageExportsField; | ||
const buildConfigs = yield buildEntryConfig(pkgExports, false); | ||
const buildConfigs = yield buildEntryConfig(pkg, options, cwd, defaultTsOptions, false); | ||
const assetsJobs = buildConfigs.map((rollupConfig)=>bundleOrWatch(rollupConfig)); | ||
const typesJobs = options.dts ? (yield buildEntryConfig(pkgExports, true)).map((rollupConfig)=>bundleOrWatch(rollupConfig)) : []; | ||
return yield Promise.all(assetsJobs.concat(typesJobs)); | ||
const typesJobs = options.dts ? (yield buildEntryConfig(pkg, options, cwd, defaultTsOptions, true)).map((rollupConfig)=>bundleOrWatch(rollupConfig)) : []; | ||
result = yield Promise.all(assetsJobs.concat(typesJobs)); | ||
} else { | ||
// Generate types | ||
if (hasTsConfig && options.dts) { | ||
yield bundleOrWatch(buildConfig(entryPath, pkg, options, cwd, defaultTsOptions, true)); | ||
} | ||
const rollupConfig = buildConfig(entryPath, pkg, options, cwd, defaultTsOptions, false); | ||
result = yield bundleOrWatch(rollupConfig); | ||
} | ||
// Generate types | ||
if (isTypescript(entryPath) && options.dts) { | ||
yield bundleOrWatch(buildConfig(entryPath, pkg, options, true)); | ||
} | ||
const rollupConfig = buildConfig(entryPath, pkg, options, false); | ||
return bundleOrWatch(rollupConfig); | ||
logSizeStats(); | ||
return result; | ||
}); | ||
return _bundle.apply(this, arguments); | ||
} | ||
// . -> pkg name | ||
// ./lite -> <pkg name>/lite | ||
function getExportPath(pkg, exportName) { | ||
const name = pkg.name || path.basename(config.rootDir); | ||
if (exportName === '.' || !exportName) return name; | ||
return path.join(name, exportName); | ||
} | ||
function runWatch({ input , output , dtsOnly }, metadata) { | ||
function runWatch({ input , output }, metadata) { | ||
const watchOptions = [ | ||
@@ -722,3 +833,2 @@ _extends({}, input, { | ||
const watcher = rollup.watch(watchOptions); | ||
let startTime = Date.now(); | ||
watcher.on('event', (event)=>{ | ||
@@ -732,3 +842,2 @@ switch(event.code){ | ||
{ | ||
startTime = Date.now(); | ||
logger.log(`Start building ${metadata.source} ...`); | ||
@@ -739,4 +848,2 @@ break; | ||
{ | ||
const duration = Date.now() - startTime; | ||
logBuild(metadata.source, dtsOnly, duration); | ||
break; | ||
@@ -750,11 +857,7 @@ } | ||
} | ||
function runBundle({ input , output , dtsOnly }, jobOptions) { | ||
const startTime = Date.now(); | ||
function runBundle({ input , output }) { | ||
return rollup.rollup(input).then((bundle)=>{ | ||
const writeJobs = output.map((options)=>bundle.write(options)); | ||
return Promise.all(writeJobs); | ||
}, onError).then(()=>{ | ||
const duration = Date.now() - startTime; | ||
logBuild(jobOptions.source, dtsOnly, duration); | ||
}); | ||
}, onError); | ||
} | ||
@@ -761,0 +864,0 @@ function onError(error) { |
{ | ||
"name": "bunchee", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "zero config bundler for js/ts/jsx libraries", | ||
@@ -34,2 +34,6 @@ "bin": { | ||
], | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true | ||
}, | ||
"engines": { | ||
@@ -52,2 +56,3 @@ "node": ">= 16" | ||
"arg": "5.0.2", | ||
"pretty-bytes": "5.6.0", | ||
"publint": "0.1.11", | ||
@@ -54,0 +59,0 @@ "rollup": "3.20.2", |
@@ -127,3 +127,3 @@ # bunchee | ||
While `exports` filed is becoming the standard of exporting in node.js, bunchee also supports to build multiple exports all in one command. | ||
While `exports` field is becoming the standard of exporting in node.js, bunchee also supports to build multiple exports all in one command. | ||
@@ -130,0 +130,0 @@ What you need to do is just add an entry file with the name (`[name].[ext]`) that matches the exported name from exports field in package.json. For instance: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
47665
1135
4
15
+ Addedpretty-bytes@5.6.0
+ Addedpretty-bytes@5.6.0(transitive)