1
0
mirror of https://github.com/koloml/furbooru-tagging-assistant.git synced 2025-12-24 07:12:57 +00:00
Files
furbooru-tagging-assistant/.vite/pack-extension.js
KoloMl 7cf2730402 Reworked build step for the content scripts
Main changes:

- Scripts are now built in 2 steps instead of building every script and
style one at a time;
- Scripts are built as AMD modules;
- Dependencies are automatically injected into resulting manifest.json
file.
2025-04-06 15:12:12 +04:00

82 lines
2.3 KiB
JavaScript

import { loadManifest } from "./lib/manifest.js";
import path from "path";
import { buildScriptsAndStyles } from "./lib/content-scripts.js";
import { extractInlineScriptsFromIndex } from "./lib/index-file.js";
import { normalizePath } from "vite";
/**
* Build addition assets required for the extension and pack it into the directory.
* @param {PackExtensionSettings} settings Build settings.
*/
export async function packExtension(settings) {
const manifest = loadManifest(path.resolve(settings.rootDir, 'manifest.json'));
const replacementMapping = await buildScriptsAndStyles({
inputs: manifest.collectContentScripts(),
outputDir: settings.contentScriptsDir,
rootDir: settings.rootDir,
});
await manifest.mapContentScripts(async entry => {
if (entry.js) {
entry.js = entry.js
.map(jsSourcePath => {
if (!replacementMapping.has(jsSourcePath)) {
return [];
}
return replacementMapping.get(jsSourcePath);
})
.flat(1)
.map(pathName => {
return normalizePath(
path.relative(
settings.exportDir,
path.join(
settings.contentScriptsDir,
pathName
)
)
)
});
}
if (entry.css) {
entry.css = entry.css
.map(jsSourcePath => {
if (!replacementMapping.has(jsSourcePath)) {
return [];
}
return replacementMapping.get(jsSourcePath);
})
.flat(1)
.map(pathName => {
return normalizePath(
path.relative(
settings.exportDir,
path.join(
settings.contentScriptsDir,
pathName
)
)
)
})
}
return entry;
})
manifest.passVersionFromPackage(path.resolve(settings.rootDir, 'package.json'));
manifest.saveTo(path.resolve(settings.exportDir, 'manifest.json'));
extractInlineScriptsFromIndex(path.resolve(settings.exportDir, 'index.html'));
}
/**
* @typedef {Object} PackExtensionSettings
* @property {string} rootDir Root directory of the repository. Required for properly fetching source files.
* @property {string} exportDir Directory of the built extension.
* @property {string} contentScriptsDir Directory specifically for content scripts entries.
*/