astro-parcel
Version:
Build and optimize your Astro project using Parcel
83 lines • 3.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.serve = exports.dev = exports.build = exports.getAstroBinPath = exports.getParcelBinPath = exports.makeHTMLFilesRelative = exports.getHTMLFiles = void 0;
const fast_glob_1 = __importDefault(require("fast-glob"));
const posthtml_1 = __importDefault(require("posthtml"));
const posthtml_relative_paths_1 = __importDefault(require("posthtml-relative-paths"));
const resolve_1 = __importDefault(require("resolve"));
const path_1 = require("path");
const execa_1 = __importDefault(require("execa"));
const options_1 = require("./options");
const fs_extra_1 = require("fs-extra");
function getHTMLFiles(astroDist) {
return (0, fast_glob_1.default)([`${astroDist}/**/*.html`], {
dot: true,
cwd: process.cwd(),
onlyFiles: true,
absolute: true,
});
}
exports.getHTMLFiles = getHTMLFiles;
async function makeHTMLFilesRelative(astroDist, srcDir) {
const htmlFiles = await getHTMLFiles(astroDist);
if (htmlFiles.length === 0) {
throw new Error(`No HTML files were found in ${astroDist}. If you changed the Astro's outDir, you have to pass it to astro-parcel`);
}
await Promise.all(htmlFiles.map(async (htmlFile) => {
const htmlString = await (0, fs_extra_1.readFile)(htmlFile, "utf8");
const { html } = await (0, posthtml_1.default)([(0, posthtml_relative_paths_1.default)(htmlFile, astroDist, srcDir)]).process(htmlString);
await (0, fs_extra_1.writeFile)(htmlFile, html);
}));
}
exports.makeHTMLFilesRelative = makeHTMLFilesRelative;
function getParcelBinPath() {
return resolve_1.default.sync("parcel");
}
exports.getParcelBinPath = getParcelBinPath;
function getAstroBinPath() {
const astroPackageJsonPath = resolve_1.default.sync("astro/package.json");
const astroDir = (0, path_1.dirname)(astroPackageJsonPath);
const astroPackageJson = JSON.parse((0, fs_extra_1.readFileSync)(astroPackageJsonPath, "utf8"));
const astroBin = (0, path_1.join)(astroDir, astroPackageJson.bin.astro);
return astroBin;
}
exports.getAstroBinPath = getAstroBinPath;
async function build(options) {
const { astroDist, parcelDist, publicDir, srcDir, extraArgs, nodeBin, astroJs, parcelJs } = {
...options_1.optionsDefaults,
...options,
};
await (0, execa_1.default)(nodeBin, [astroJs, "build", ...extraArgs], { stdio: "inherit" });
await (0, fs_extra_1.copy)(srcDir, astroDist, {
filter: (file) => !file.endsWith(".astro"),
overwrite: false,
});
await makeHTMLFilesRelative(astroDist, srcDir);
await (0, execa_1.default)(nodeBin, [parcelJs, "build", "--dist-dir", parcelDist, ...(0, options_1.filterParcelBuildArgs)(extraArgs)], {
stdio: "inherit",
});
await (0, fs_extra_1.copy)(publicDir, parcelDist);
}
exports.build = build;
async function dev(options) {
const { extraArgs, nodeBin, astroJs } = {
...options_1.optionsDefaults,
...options,
};
await (0, execa_1.default)(nodeBin, [astroJs, "dev", ...extraArgs], { stdio: "inherit" });
}
exports.dev = dev;
async function serve(options) {
const { extraArgs } = {
...options_1.optionsDefaults,
...options,
};
await (0, execa_1.default)(options.nodeBin, [options.parcelJs, "serve", "--dist-dir", options.parcelDist, ...extraArgs], {
stdio: "inherit",
});
}
exports.serve = serve;
//# sourceMappingURL=lib.js.map