vuepress-plugin-lightgallery
Version:
Light Gallery plugin for VuePress
85 lines (79 loc) • 3.15 kB
JavaScript
import { Logger, addViteConfig, addViteOptimizeDepsExclude, chainWebpack, ensureEndingSlash, getModulePath } from "@vuepress/helper";
import { useSassPalettePlugin } from "@vuepress/plugin-sass-palette";
import { createConverter } from "vuepress-shared";
import { colors, getDirname, path } from "vuepress/utils";
//#region src/node/compact.ts
/**
* @deprecated You should use `defineLightGalleryConfig` from `vuepress-plugin-lightgallery/client`
* instead
* @param options - User options
*/
const convertOptions = (options) => {
const { droppedLogger } = createConverter("lightgallery");
droppedLogger({
options,
old: "options",
msg: `Please import and use ${colors.cyan("defineLightGalleryConfig")} from ${colors.magenta("vuepress-plugin-lightgallery/client")} instead.`
});
};
//#endregion
//#region src/node/prepareLightGalleryPlugins.ts
const prepareLightGalleryPlugins = async (app, pluginNames = [
"pager",
"share",
"zoom"
]) => {
const plugins = pluginNames.map((pluginName) => `import(/* webpackChunkName: "lightgallery" */ "${getModulePath(`lightgallery/plugins/${pluginName}/lg-${pluginName}.es5.js`, import.meta)}")`);
const pluginsStyles = pluginNames.map((pluginName) => `import "${getModulePath(`lightgallery/css/lg-${pluginName}.css`, import.meta)}";`);
await app.writeTemp("lightgallery/plugins.js", `\
${pluginsStyles.join("\n")}
export const useLightGalleryPlugins = () =>
Promise.all([
${plugins.map((item) => ` ${item}`).join(",\n")}
]);
`);
};
//#endregion
//#region src/node/utils.ts
const __dirname = getDirname(import.meta.url);
const PLUGIN_NAME = "vuepress-plugin-lightgallery";
const logger = new Logger(PLUGIN_NAME);
const CLIENT_FOLDER = ensureEndingSlash(path.resolve(__dirname, "../client"));
//#endregion
//#region src/node/lightgalleryPlugin.ts
const lightgalleryPlugin = (options = {}, legacy = true) => (app) => {
if (legacy) convertOptions(options);
if (app.env.isDebug) logger.info("Options:", options);
const plugins = options.plugins ?? [
"pager",
"share",
"zoom"
];
useSassPalettePlugin(app, {
id: "hope",
defaultConfig: getModulePath("vuepress-shared/scss/config.scss", import.meta)
});
return {
name: PLUGIN_NAME,
define: () => ({ __LG_SELECTOR__: options.selector ?? "[vp-content] :not(a) > img:not([no-view])" }),
extendsBundlerOptions: (bundlerOptions) => {
addViteOptimizeDepsExclude(bundlerOptions, app, ["lightgallery/lightgallery.es5.js", ...plugins.map((name) => `lightgallery/plugins/${name}/lg-${name}.es5.js`)]);
addViteOptimizeDepsExclude(bundlerOptions, app, ["lightgallery"]);
addViteConfig(bundlerOptions, app, { css: { preprocessorOptions: { scss: { quietDeps: true } } } });
chainWebpack(bundlerOptions, app, (config) => {
config.module.rule("scss").use("sass-loader").tap((loaderOptions) => ({
...loaderOptions,
sassOptions: {
quietDeps: true,
...loaderOptions.sassOptions
}
}));
});
},
onPrepared: () => prepareLightGalleryPlugins(app, options.plugins),
clientConfigFile: `${CLIENT_FOLDER}config.js`
};
};
//#endregion
export { lightgalleryPlugin };
//# sourceMappingURL=index.js.map