microsite
Version:
<br /> <br />
112 lines (111 loc) • 6.37 kB
JavaScript
import { resolve, relative } from "path";
import module from "module";
const { createRequire } = module;
const require = createRequire(import.meta.url);
import { fileExists } from "./fs.js";
import { createConfiguration } from "snowpack";
import cc from "cosmiconfig";
import { yellow } from "kleur/colors";
const { cosmiconfig } = cc;
const _config = require("microsite/assets/snowpack.config.cjs");
const pkg = require(resolve(process.cwd(), "package.json"));
// const deps = Object.keys(
// pkg.dependencies || {}
// );
const DEFAULT_BASE_PATH = pkg.homepage || '/';
export function resolveNormalizedBasePath(args) {
var _a;
let basePath = (_a = args['--base-path']) !== null && _a !== void 0 ? _a : DEFAULT_BASE_PATH;
return basePath === '/' ? basePath : `/${basePath.replace(/^\//, '').replace(/\/$/, '')}/`;
}
async function hasPostCSSConfig() {
try {
const explorer = cosmiconfig("postcss");
const result = await explorer.search();
if (result.filepath)
return true;
}
catch (e) { }
return false;
}
export async function loadConfiguration(mode) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
const [snowpackconfigPath, tsconfigPath, usesPostCSS] = await Promise.all([
findSnowpackConfig(),
findTsOrJsConfig(),
hasPostCSSConfig(),
]);
const aliases = tsconfigPath
? resolveTsconfigPathsToAlias({ tsconfigPath })
: {};
const userConfig = snowpackconfigPath ? require(snowpackconfigPath) : {};
if (usesPostCSS) {
const missing = [];
const deps = ['@snowpack/plugin-postcss', 'postcss', 'postcss-cli'];
deps.forEach(dependency => {
try {
require.resolve(dependency);
}
catch (e) {
missing.push(dependency);
}
});
if (missing.length > 0) {
console.error(yellow(`It looks like you're trying to use PostCSS!\nMicrosite will automatically use your configuration, but requires some 'devDependencies' to do so.\n\nPlease run 'npm install --save-dev ${missing.join(' ')}'\n`));
process.exit(1);
}
}
const additionalPlugins = usesPostCSS ? ["@snowpack/plugin-postcss"] : [];
switch (mode) {
case "dev":
return createConfiguration(Object.assign(Object.assign(Object.assign({}, userConfig), _config), { buildOptions: Object.assign(Object.assign(Object.assign({}, userConfig.buildOptions), _config.buildOptions), { ssr: true }), packageOptions: Object.assign(Object.assign(Object.assign({}, userConfig.packageOptions), _config.packageOptions), { external: [
...((_b = (_a = userConfig.packageOptions) === null || _a === void 0 ? void 0 : _a.external) !== null && _b !== void 0 ? _b : []),
..._config.packageOptions.external,
].filter((v) => !v.startsWith('microsite')) }), plugins: [...additionalPlugins, ..._config.plugins, ...((_c = userConfig.plugins) !== null && _c !== void 0 ? _c : [])], alias: Object.assign(Object.assign(Object.assign(Object.assign({}, ((_d = userConfig.alias) !== null && _d !== void 0 ? _d : {})), aliases), ((_e = _config.alias) !== null && _e !== void 0 ? _e : {})), { "microsite/hydrate": "microsite/client/hydrate" }) }));
case "build":
return createConfiguration(Object.assign(Object.assign(Object.assign({}, userConfig), _config), { buildOptions: Object.assign(Object.assign(Object.assign({}, userConfig.buildOptions), _config.buildOptions), { ssr: true }), plugins: [...additionalPlugins, ..._config.plugins, ...((_f = userConfig.plugins) !== null && _f !== void 0 ? _f : [])], alias: Object.assign(Object.assign(Object.assign({}, ((_g = userConfig.alias) !== null && _g !== void 0 ? _g : {})), aliases), ((_h = _config.alias) !== null && _h !== void 0 ? _h : {})), packageOptions: Object.assign(Object.assign({}, _config.packageOptions), { external: [
...((_k = (_j = userConfig.packageOptions) === null || _j === void 0 ? void 0 : _j.external) !== null && _k !== void 0 ? _k : []),
..._config.packageOptions.external,
].filter((v) => v !== "preact"), rollup: Object.assign(Object.assign(Object.assign({}, ((_m = (_l = userConfig.installOptions) === null || _l === void 0 ? void 0 : _l.rollup) !== null && _m !== void 0 ? _m : {})), ((_p = (_o = _config.installOptions) === null || _o === void 0 ? void 0 : _o.rollup) !== null && _p !== void 0 ? _p : {})), { plugins: [
...((_s = (_r = (_q = userConfig.installOptions) === null || _q === void 0 ? void 0 : _q.rollup) === null || _r === void 0 ? void 0 : _r.plugins) !== null && _s !== void 0 ? _s : []),
{
name: '@microsite/auto-external',
options(opts) {
return Object.assign({}, opts, { external: (source) => source.startsWith('preact') });
}
}
] }) }) }));
}
}
const findSnowpackConfig = async () => {
const cwd = process.cwd();
const snowpack = resolve(cwd, './snowpack.config.cjs');
if (await fileExists(snowpack))
return snowpack;
return null;
};
const findTsOrJsConfig = async () => {
const cwd = process.cwd();
const tsconfig = resolve(cwd, "./tsconfig.json");
if (await fileExists(tsconfig))
return tsconfig;
const jsconfig = resolve(cwd, "./jsconfig.json");
if (await fileExists(jsconfig))
return jsconfig;
return null;
};
function resolveTsconfigPathsToAlias({ tsconfigPath = "./tsconfig.json", } = {}) {
var _a, _b;
let { baseUrl, paths } = (_b = (_a = require(tsconfigPath)) === null || _a === void 0 ? void 0 : _a.compilerOptions) !== null && _b !== void 0 ? _b : {};
if (!(baseUrl && paths))
return {};
baseUrl = resolve(process.cwd(), baseUrl);
const aliases = {};
Object.keys(paths).forEach((item) => {
const key = item.replace("/*", "");
const value = "./" +
relative(process.cwd(), resolve(baseUrl, paths[item][0].replace("/*", "").replace("*", "")));
aliases[key] = value;
});
return aliases;
}