UNPKG

3.83 kBJavaScriptView Raw
1import { resolve, relative } from "path";
2import module from "module";
3const { createRequire } = module;
4const require = createRequire(import.meta.url);
5import { fileExists } from "./fs.js";
6import { loadConfiguration as loadUserConfiguration } from "snowpack";
7const pkg = require(resolve(process.cwd(), "package.json"));
8const DEFAULT_BASE_PATH = pkg.homepage || "/";
9const _config = require("microsite/assets/snowpack.config.cjs");
10export function resolveNormalizedBasePath(args) {
11 var _a;
12 let basePath = (_a = args["--base-path"]) !== null && _a !== void 0 ? _a : DEFAULT_BASE_PATH;
13 return basePath === "/"
14 ? basePath
15 : `/${basePath.replace(/^\//, "").replace(/\/$/, "")}/`;
16}
17export async function loadConfiguration(mode) {
18 const overrides = await getOverrides(mode);
19 return loadUserConfiguration(overrides);
20}
21export async function getOverrides(mode) {
22 var _a, _b, _c, _d;
23 const [tsconfigPath] = await Promise.all([findTsOrJsConfig()]);
24 const aliases = tsconfigPath
25 ? resolveTsconfigPathsToAlias({ tsconfigPath })
26 : {};
27 switch (mode) {
28 case "dev":
29 return Object.assign(Object.assign({}, _config), { buildOptions: Object.assign(Object.assign({}, _config.buildOptions), { ssr: true }), packageOptions: Object.assign(Object.assign({}, _config.packageOptions), { external: [..._config.packageOptions.external].filter((v) => !v.startsWith("microsite")) }), plugins: [..._config.plugins], alias: Object.assign(Object.assign(Object.assign({}, aliases), ((_a = _config.alias) !== null && _a !== void 0 ? _a : {})), { "microsite/hydrate": "microsite/client/hydrate" }) });
30 case "build":
31 return Object.assign(Object.assign({}, _config), { devOptions: Object.assign(Object.assign({}, _config.devOptions), { hmr: false, port: 0, hmrPort: 0 }), buildOptions: Object.assign(Object.assign({}, _config.buildOptions), { ssr: true }), plugins: [..._config.plugins], alias: Object.assign(Object.assign({}, aliases), ((_b = _config.alias) !== null && _b !== void 0 ? _b : {})), packageOptions: Object.assign(Object.assign({}, _config.packageOptions), { external: [..._config.packageOptions.external].filter((v) => v !== "preact"), rollup: Object.assign(Object.assign({}, ((_d = (_c = _config.installOptions) === null || _c === void 0 ? void 0 : _c.rollup) !== null && _d !== void 0 ? _d : {})), { plugins: [
32 {
33 name: "@microsite/auto-external",
34 options(opts) {
35 return Object.assign({}, opts, {
36 external: (source) => source.startsWith("preact"),
37 });
38 },
39 },
40 ] }) }) });
41 }
42}
43const findTsOrJsConfig = async () => {
44 const cwd = process.cwd();
45 const tsconfig = resolve(cwd, "./tsconfig.json");
46 if (await fileExists(tsconfig))
47 return tsconfig;
48 const jsconfig = resolve(cwd, "./jsconfig.json");
49 if (await fileExists(jsconfig))
50 return jsconfig;
51 return null;
52};
53function resolveTsconfigPathsToAlias({ tsconfigPath = "./tsconfig.json", } = {}) {
54 var _a, _b;
55 let { baseUrl, paths } = (_b = (_a = require(tsconfigPath)) === null || _a === void 0 ? void 0 : _a.compilerOptions) !== null && _b !== void 0 ? _b : {};
56 if (!(baseUrl && paths))
57 return {};
58 baseUrl = resolve(process.cwd(), baseUrl);
59 const aliases = {};
60 Object.keys(paths).forEach((item) => {
61 const key = item.replace("/*", "");
62 const value = "./" +
63 relative(process.cwd(), resolve(baseUrl, paths[item][0].replace("/*", "").replace("*", "")));
64 aliases[key] = value;
65 });
66 return aliases;
67}