You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
// src/index.ts
|
|
import { join } from "path";
|
|
import { existsSync, promises as fs } from "fs";
|
|
import _debug from "debug";
|
|
var debug = _debug("vite-plugin-package-config");
|
|
function VitePluginPackageConfig(options = {}) {
|
|
const {
|
|
packageJsonPath = join(process.cwd(), "package.json"),
|
|
field = "vite"
|
|
} = options;
|
|
return {
|
|
name: "vite-plugin-package-config",
|
|
async config() {
|
|
if (!existsSync(packageJsonPath)) {
|
|
debug("package.json not found at %s", packageJsonPath);
|
|
return;
|
|
}
|
|
debug("loading package.json at %s", packageJsonPath);
|
|
try {
|
|
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
|
|
const extend = packageJson[field];
|
|
if (!extend) {
|
|
debug("no %s field found in package.json, skip", field);
|
|
return;
|
|
}
|
|
debug("merging config with %o", extend);
|
|
return extend;
|
|
} catch (e) {
|
|
debug("parse error: %o", e);
|
|
debug("error on loading package.json at %s, skip", packageJsonPath);
|
|
}
|
|
},
|
|
api: {
|
|
options: {
|
|
packageJsonPath,
|
|
field
|
|
}
|
|
}
|
|
};
|
|
}
|
|
var src_default = VitePluginPackageConfig;
|
|
export {
|
|
src_default as default
|
|
};
|