vite-plugin-react18-pages
Version:
<p> <a href="https://www.npmjs.com/package/vite-plugin-react-pages" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/v/vite-plugin-react-pages.svg" alt="npm package" /></a> </p>
51 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPagePublicPath = exports.defaultFileHandler = exports.DefaultPageStrategy = void 0;
const __1 = require("..");
const virtual_module_1 = require("../../utils/virtual-module");
class DefaultPageStrategy extends __1.PageStrategy {
constructor(opts = {}) {
const { extraFindPages, fileHandler = exports.defaultFileHandler } = opts;
// pass a wrapped findPages function to super class
super((pagesDir, helpersFromParent) => {
// we can create our own helpers, providing a default fileHandler
// and not using helpersFromParent
const helpers = this.createHelpers(fileHandler);
helpers.watchFiles(pagesDir, '**/*$.{md,mdx,js,jsx,ts,tsx}');
if (typeof extraFindPages === 'function') {
extraFindPages(pagesDir, helpers);
}
});
}
}
exports.DefaultPageStrategy = DefaultPageStrategy;
/**
* The defaultFileHandler return the result to caller,
* instead of directly setting the pageData object.
* so that it is more useful to users.
*/
const defaultFileHandler = async (file, api) => {
const pagePublicPath = getPagePublicPath(file.relative);
return {
pageId: pagePublicPath,
dataPath: file.path,
staticData: await (0, virtual_module_1.extractStaticData)(file),
};
};
exports.defaultFileHandler = defaultFileHandler;
/**
* turn `sub-path/page2/index.tsx` into `/sub-path/page2`
*/
function getPagePublicPath(relativePageFilePath) {
let pagePublicPath = relativePageFilePath.replace(/\$\.(md|mdx|js|jsx|ts|tsx)$/, '');
pagePublicPath = pagePublicPath.replace(/index$/, '');
// ensure starting slash
pagePublicPath = pagePublicPath.replace(/\/$/, '');
pagePublicPath = `/${pagePublicPath}`;
// turn [id] into :id
// so that react-router can recognize it as url params
pagePublicPath = pagePublicPath.replace(/\[(.*?)\]/g, (_, paramName) => `:${paramName}`);
return pagePublicPath;
}
exports.getPagePublicPath = getPagePublicPath;
//# sourceMappingURL=index.js.map