presetter-kernel-module-preset/src/index.ts

112 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-06-27 05:59:03 +00:00
import type { PresetAsset } from "presetter-types";
import { PresetContext } from "presetter-types";
2023-07-08 06:07:37 +00:00
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { loadFile, resolveDirective, template } from "presetter";
const DIR = fileURLToPath(dirname(import.meta.url));
// paths to the template directory
const TEMPLATES = resolve(DIR, "..", "templates");
/** List of configurable variables */
export type Variable = {
/** the directory containing all source code (default: source) */
source: string;
/** the directory containing all the compiled files (default: lib) */
output: string;
buildSource: string;
};
export const DEFAULT_VARIABLE: Variable = {
source: "build",
output: "lib",
buildSource: "src",
};
function buildOptions(context: PresetContext) {
const opts = context.custom.config?.vite as any;
if (!opts) {
throw new Error("vite options missing!");
}
const build = opts.build;
const resolve = opts.resolve;
const optimize = opts.optimize;
const polyfill = opts.polyfill;
return {
viteBuild: resolveDirective(build, context).stringifiedConfig,
viteResolve: resolveDirective(resolve, context).stringifiedConfig,
viteOptimize: resolveDirective(optimize, context).stringifiedConfig,
vitePolyfill: resolveDirective(polyfill, context).stringifiedConfig,
};
}
2023-06-27 05:59:03 +00:00
/**
* get the list of templates provided by this preset
* @returns list of preset templates
*/
export default async function (context: PresetContext): Promise<PresetAsset> {
const ignores = !context.custom.config?.official
? [".github/workflows/ci.yml"]
: [];
return {
2023-07-08 06:07:37 +00:00
extends: ["@lumeweb/node-library-preset"],
template: {
/* eslint-disable @typescript-eslint/naming-convention */
"vite.config.js": (context) => {
const content = loadFile(resolve(TEMPLATES, "vite.config.js"), "text");
const variable = buildOptions(context);
return template(content, variable);
/* eslint-enable @typescript-eslint/naming-convention */
},
},
2023-06-27 05:59:03 +00:00
supplementaryIgnores: ignores,
2023-06-29 21:57:25 +00:00
supplementaryConfig: {
2023-07-08 07:01:30 +00:00
"release": {
plugins: {
2023-06-30 10:07:28 +00:00
"3": ["@semantic-release/npm", { npmPublish: false }],
},
},
2023-07-08 07:01:30 +00:00
"vite": {
2023-07-08 06:07:37 +00:00
build: {
outDir: "{output}",
lib: {
// Could also be a dictionary or array of multiple entry points
entry: "{source}/index.js",
name: "main",
formats: ["cjs"],
fileName: "index",
},
2023-07-08 06:07:37 +00:00
minify: false,
},
2023-07-08 06:07:37 +00:00
resolve: {},
optimize: {
"node-fetch": "export default undefined;",
},
2023-07-08 06:10:33 +00:00
polyfill: {
exclude: ["fs"],
globals: {
Buffer: true,
global: true,
process: true,
},
},
},
2023-07-08 07:01:30 +00:00
"tsconfig.build": {
include: ["{buildSource}"],
compilerOptions: {
outDir: "{source}",
},
},
2023-06-29 21:57:25 +00:00
},
2023-07-08 06:07:37 +00:00
scripts: resolve(TEMPLATES, "scripts.yaml"),
variable: DEFAULT_VARIABLE,
2023-06-27 05:59:03 +00:00
};
}