[d24f17c] | 1 | import { isEmpty, propEq } from 'ramda';
|
---|
| 2 | import File from "../util/File.mjs";
|
---|
| 3 | import * as plugins from "../util/plugins.mjs";
|
---|
| 4 | import UnmatchedBundleStrategyError from "../errors/UnmatchedBundleStrategyError.mjs";
|
---|
| 5 | import BundleError from "../errors/BundleError.mjs";
|
---|
| 6 | import parse from "../parse/index.mjs";
|
---|
| 7 | import { merge as mergeOptions } from "../options/util.mjs";
|
---|
| 8 | import * as url from "../util/url.mjs";
|
---|
| 9 | /**
|
---|
| 10 | * Bundle a file with all its external references to a compound document.
|
---|
| 11 | */
|
---|
| 12 | const bundle = async (uri, options) => {
|
---|
| 13 | const {
|
---|
| 14 | refSet
|
---|
| 15 | } = options.bundle;
|
---|
| 16 | const sanitizedURI = url.sanitize(uri);
|
---|
| 17 | const mergedOptions = mergeOptions(options, {
|
---|
| 18 | resolve: {
|
---|
| 19 | baseURI: sanitizedURI
|
---|
| 20 | }
|
---|
| 21 | });
|
---|
| 22 | let parseResult;
|
---|
| 23 |
|
---|
| 24 | // if refSet was provided, use it to avoid unnecessary parsing
|
---|
| 25 | if (refSet !== null && refSet.has(sanitizedURI)) {
|
---|
| 26 | // @ts-ignore
|
---|
| 27 | ({
|
---|
| 28 | value: parseResult
|
---|
| 29 | } = refSet.find(propEq(sanitizedURI, 'uri')));
|
---|
| 30 | } else {
|
---|
| 31 | parseResult = await parse(uri, mergedOptions);
|
---|
| 32 | }
|
---|
| 33 | const file = File({
|
---|
| 34 | uri: mergedOptions.resolve.baseURI,
|
---|
| 35 | parseResult,
|
---|
| 36 | mediaType: mergedOptions.parse.mediaType
|
---|
| 37 | });
|
---|
| 38 | const bundleStrategies = await plugins.filter('canBundle', file, mergedOptions.bundle.strategies);
|
---|
| 39 |
|
---|
| 40 | // we couldn't find any bundle strategy for this File
|
---|
| 41 | if (isEmpty(bundleStrategies)) {
|
---|
| 42 | throw new UnmatchedBundleStrategyError(file.uri);
|
---|
| 43 | }
|
---|
| 44 | try {
|
---|
| 45 | const {
|
---|
| 46 | result
|
---|
| 47 | } = await plugins.run('bundle', [file, mergedOptions], bundleStrategies);
|
---|
| 48 | return result;
|
---|
| 49 | } catch (error) {
|
---|
| 50 | throw new BundleError(`Error while bundling file "${file.uri}"`, {
|
---|
| 51 | cause: error
|
---|
| 52 | });
|
---|
| 53 | }
|
---|
| 54 | };
|
---|
| 55 | export default bundle; |
---|