@equinor/fusion-framework-cli
Version:
--- title: Fusion Framework CLI ---
102 lines • 4.45 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { createFilter } from 'vite';
import { emitAssetSync } from './emit-asset.js';
import { resolveAssetId } from './resolve-asset-id.js';
import { createExtensionFilterPattern } from './extension-filter-pattern.js';
import { ASSET_EXTENSIONS, PLUGIN_NAME } from './static.js';
const defaultInclude = createExtensionFilterPattern(ASSET_EXTENSIONS);
/**
* A Vite plugin to handle external resources in a library build.
*
* @param options - Configuration options for the plugin.
* @param options.name - Optional name for the emitted assets.
* @param options.include - Filter pattern to include specific files.
* @param options.exclude - Filter pattern to exclude specific files.
* @returns A Vite plugin object.
*
* @remarks
* This plugin is intended to be used only during the Vite library build process.
* It resolves asset IDs, emits assets, and exports them as URLs.
*
* @example
* ```typescript
* // vite.config.ts
* import { ExternalAppAssetPlugin } from '@equinor/fusion-framework-cli/plugins/app-assets-plugin';
*
* export default {
* plugins: [
* ExternalAppAssetPlugin({
* name: 'my-asset',
* include: ['svg', 'png'],
* exclude: 'node_modules/**',
* }),
* ],
* };
* ```
*/
export const AppAssetExportPlugin = (options = {}) => {
const { name, include = defaultInclude, exclude } = options;
let viteConfig;
const assetsPathMap = new Map();
const filter = createFilter(include, exclude);
return {
name: PLUGIN_NAME,
enforce: 'pre',
apply: 'build',
configResolved(config) {
viteConfig = config;
},
resolveId(source, importer, opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (viteConfig.build.lib === false) {
this.warn('this plugin is only for vite build lib');
}
// skip resolves triggered by plugin self
if (((_a = opts.custom) === null || _a === void 0 ? void 0 : _a.caller) === PLUGIN_NAME) {
return null;
}
// resolve asset ID, the ID should refer to the actual asset file
const assetId = yield resolveAssetId(this, source, importer !== null && importer !== void 0 ? importer : '', Object.assign(Object.assign({}, opts), { custom: Object.assign(Object.assign({}, opts.custom), { caller: PLUGIN_NAME }) }));
// skip if asset is not found or filtered out
const { id } = assetId !== null && assetId !== void 0 ? assetId : {};
const shouldIncludeAsset = id && filter(id);
if (!shouldIncludeAsset) {
return null;
}
try {
// emit asset and index the asset path
const { outDir, assetsDir } = viteConfig.build;
const assetPath = emitAssetSync(this, id, {
name,
outDir,
assetsDir,
});
assetsPathMap.set(id, assetPath);
}
catch (err) {
this.warn(err.message);
return null;
}
});
},
load(id) {
// lookup asset path and export as URL
const assetPath = assetsPathMap.get(id);
if (assetPath) {
// ensure asset path is relative from the script load path
return `export default new URL(/* @vite-ignore */'${assetPath}', import.meta.url).href`;
}
},
};
};
export default AppAssetExportPlugin;
//# sourceMappingURL=app-asset-plugin.js.map