chunk-rename.js 2.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.chunkRenamePlugin = void 0;
const chunkRenamePlugin = ({ pageChunkName = ({ title = "", key }) => {
    const chunkTitle = (title || "").replace(/[.&*?#\\/:"<>| ]/gu, "");
    return chunkTitle ? `page-${chunkTitle}` : `page-${key.slice(1)}`;
}, layoutChunkName = (layout) => `layout-${layout.componentName}`, }, context) => {
    // override internal plugins
    const plugins = [];
    if (pageChunkName) {
        plugins.push({
            name: "@vuepress/internal-page-components",
            extendPageData(page) {
                page._chunkName = pageChunkName(page);
            },
            clientDynamicModules() {
                const content = `export default {\n${context.pages
                    .filter(({ _filePath }) => _filePath)
                    .map((page) => {
                    const key = JSON.stringify(page.key);
                    const filePath = JSON.stringify(page._filePath);
                    const comment = page._chunkName
                        ? `/* webpackChunkName: ${JSON.stringify(page._chunkName)} */`
                        : "";
                    return `  ${key}: () => import(${comment}${filePath})`;
                })
                    .join(",\n")} \n}`;
                return {
                    dirname: "internal",
                    name: "page-components.js",
                    content,
                };
            },
        });
    }
    if (layoutChunkName) {
        const { layoutComponentMap } = context.themeAPI;
        for (const key in layoutComponentMap) {
            const component = layoutComponentMap[key];
            component._chunkName = layoutChunkName(component);
        }
        plugins.push({
            name: "@vuepress/internal-layout-components",
            clientDynamicModules() {
                const { layoutComponentMap } = context.themeAPI;
                const content = `export default {\n${Object.keys(layoutComponentMap)
                    .map((name) => {
                    const component = layoutComponentMap[name];
                    const key = JSON.stringify(name);
                    const filePath = JSON.stringify(component.path);
                    const comment = component._chunkName
                        ? `/* webpackChunkName: ${JSON.stringify(component._chunkName)} */`
                        : "";
                    return `  ${key}: () => import(${comment}${filePath})`;
                })
                    .join(",\n")} \n}`;
                return {
                    dirname: "internal",
                    name: "layout-components.js",
                    content,
                };
            },
        });
    }
    return {
        name: "chunk-rename",
        plugins,
    };
};
exports.chunkRenamePlugin = chunkRenamePlugin;
//# sourceMappingURL=chunk-rename.js.map