@stagewise/plugin-builder
Version:
Build stagewise plugins with ease.
135 lines (131 loc) • 3.63 kB
JavaScript
// src/index.ts
import { build } from "vite";
import react from "@vitejs/plugin-react-swc";
import preserveDirectives from "rollup-preserve-directives";
import { resolve } from "node:path";
import fs from "node:fs";
var mode = process.argv[2] || "production";
async function buildPlugin(projectDir, config) {
const __dirname = projectDir;
const baseConfig = {
mode,
resolve: {
alias: {
"@": resolve(__dirname, "src")
},
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"]
},
esbuild: {
minifyIdentifiers: mode === "production",
treeShaking: mode === "production"
},
build: {
rollupOptions: {
treeshake: mode === "production"
},
minify: mode === "production",
cssMinify: mode === "production"
}
};
const forcedConfig = {
plugins: [react(), preserveDirectives()],
// Cast to workaround Rollup version compatibility
resolve: {
mainFields: ["module", "main"]
},
build: {
outDir: "tmp/plugin",
lib: {
entry: resolve(process.cwd(), "src/index.tsx"),
name: "StagewisePluginExample",
fileName: (format) => `index.${format}.js`,
formats: ["es", "cjs"]
},
rollupOptions: {
output: {
manualChunks: void 0,
preserveModules: false
},
external: [
"react",
"react-dom",
"react-dom/client",
"react/jsx-runtime",
"@stagewise/toolbar",
"@stagewise/toolbar/plugin-ui"
]
}
},
optimizeDeps: {
esbuildOptions: {
mainFields: ["module", "main"]
}
}
};
const mergedConfig = config ? mergeDeep(config, baseConfig, forcedConfig) : mergeDeep(baseConfig, forcedConfig);
await build(mergedConfig);
const pluginContent = fs.readFileSync(
resolve(__dirname, "tmp/plugin/index.es.js"),
"utf8"
);
if (!fs.existsSync(resolve(__dirname, "dist"))) {
fs.mkdirSync(resolve(__dirname, "dist"), { recursive: true });
}
fs.writeFileSync(
resolve(__dirname, "dist/index.cjs.js"),
getLoaderContentCjs(pluginContent),
"utf8"
);
fs.writeFileSync(
resolve(__dirname, "dist/index.es.js"),
getLoaderContentEs(pluginContent),
"utf8"
);
fs.writeFileSync(
resolve(__dirname, "dist/index.d.ts"),
loaderTypeContent,
"utf8"
);
}
function isObject(item) {
return item && typeof item === "object" && !Array.isArray(item);
}
function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (Object.hasOwn(source, key)) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
}
return mergeDeep(target, ...sources);
}
var getLoaderContentEs = (pluginContent) => `'use client'
const plugin = {
mainPlugin: ${JSON.stringify(pluginContent)},
loader: true
}
export default plugin;
`;
var getLoaderContentCjs = (pluginContent) => `"use client";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const plugin = {
mainPlugin: ${JSON.stringify(pluginContent)},
loader: true
}
exports.default = plugin;
`;
var loaderTypeContent = `import type { ToolbarPluginLoader } from '@stagewise/toolbar'
declare const plugin: ToolbarPluginLoader;
export default plugin;
`;
export {
buildPlugin as default
};