@pika/plugin-build-web
Version:
A @pika/pack plugin: Adds an ESM distribution to your package, built & optimized to run in most web browsers (and bundlers).
74 lines (62 loc) • 2.01 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var path = _interopDefault(require('path'));
var fs = _interopDefault(require('fs'));
var types = require('@pika/types');
var rollup = require('rollup');
const DEFAULT_ENTRYPOINT = 'module';
async function beforeJob({
out
}) {
const srcDirectory = path.join(out, 'dist-src/');
if (!fs.existsSync(srcDirectory)) {
throw new types.MessageError('"dist-src/" does not exist, or was not yet created in the pipeline.');
}
const srcEntrypoint = path.join(out, 'dist-src/index.js');
if (!fs.existsSync(srcEntrypoint)) {
throw new types.MessageError('"dist-src/index.js" is the expected standard entrypoint, but it does not exist.');
}
}
function manifest(manifest, {
options
}) {
if (options.entrypoint !== null) {
let keys = options.entrypoint || [DEFAULT_ENTRYPOINT];
if (typeof keys === 'string') {
keys = [keys];
}
for (const key of keys) {
manifest[key] = manifest[key] || 'dist-web/index.js';
}
}
}
async function build({
out,
options,
reporter
}) {
const writeToWeb = path.join(out, 'dist-web', 'index.js');
const result = await rollup.rollup({
input: path.join(out, 'dist-src/index.js'),
plugins: [],
onwarn: (warning, defaultOnWarnHandler) => {
// // Unresolved external imports are expected
if (warning.code === 'UNRESOLVED_IMPORT' && !(warning.source.startsWith('./') || warning.source.startsWith('../'))) {
return;
}
defaultOnWarnHandler(warning);
}
});
await result.write({
file: writeToWeb,
format: 'esm',
exports: 'named',
sourcemap: options.sourcemap === undefined ? true : options.sourcemap
});
reporter.created(writeToWeb, 'module');
}
exports.beforeJob = beforeJob;
exports.build = build;
exports.manifest = manifest;
//# sourceMappingURL=index.js.map
;