Skip to content

Commit

Permalink
Merge pull request #72 from mkobayashime/script-name
Browse files Browse the repository at this point in the history
Pass `scriptName` as a prop to the plugin instead of calculating it in place
  • Loading branch information
mkobayashime authored Mar 7, 2025
2 parents 82ddd8e + fdb79d1 commit 1e11ab7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
51 changes: 32 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@ export const main = async ({ mode }: { mode: Mode }) => {
in: filepath,
}));

const getCommonOptions = (): esbuild.BuildOptions => ({
bundle: true,
write: false,
charset: "utf8",
format: "esm",
legalComments: "inline",
plugins: [
userscriptsPlugin({
mode,
defaultMeta: config.defaultMeta,
}),
],
});
const getCommonOptions = (entryPoint: {
in: string;
out: string;
}): esbuild.BuildOptions => {
const scriptName = path.basename(path.dirname(entryPoint.in));

if (!scriptName) {
throw new Error(
`Failed to get scriptName from entryPoint path: ${entryPoint.in}`,
);
}

return {
entryPoints: [entryPoint],
bundle: true,
write: false,
charset: "utf8",
format: "esm",
legalComments: "inline",
plugins: [
userscriptsPlugin({
defaultMeta: config.defaultMeta,
mode,
scriptName,
}),
],
};
};

console.log(styleText("blue", `Bundlemonkey started in ${mode} mode\n`));

Expand All @@ -38,10 +53,9 @@ export const main = async ({ mode }: { mode: Mode }) => {
await mkdir(config.dist.dev, { recursive: true });

await Promise.all(
entryPoints.map(async (entrypoint) => {
entryPoints.map(async (entryPoint) => {
const context = await esbuild.context({
...getCommonOptions(),
entryPoints: [entrypoint],
...getCommonOptions(entryPoint),
outdir: config.dist.dev,
});

Expand All @@ -54,10 +68,9 @@ export const main = async ({ mode }: { mode: Mode }) => {
await mkdir(config.dist.production, { recursive: true });

await Promise.all(
entryPoints.map(async (entrypoint) => {
entryPoints.map(async (entryPoint) => {
await esbuild.build({
...getCommonOptions(),
entryPoints: [entrypoint],
...getCommonOptions(entryPoint),
outdir: config.dist.production,
});
}),
Expand Down
21 changes: 4 additions & 17 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export type Mode = "production" | "watch" | "watchRemote";
const jiti = createJiti(import.meta.url, { moduleCache: false });

export const userscriptsPlugin = ({
mode,
defaultMeta,
mode,
scriptName,
}: {
mode: Mode;
defaultMeta: ParsedConfig["defaultMeta"];
mode: Mode;
scriptName: string;
}): esbuild.Plugin => {
const metaStore: Record<string, ParsedMeta | null> = {};

Expand All @@ -31,14 +33,6 @@ export const userscriptsPlugin = ({
name: "userscripts",
setup: (build) => {
build.onLoad({ filter: /\.user\.(t|j)s$/ }, async (args) => {
const scriptName = path.basename(path.dirname(args.path));

if (!scriptName) {
throw new Error(
`Failed to get name of the script from path: ${args.path}`,
);
}

try {
const sourceWithMainExtracted = await extractMain(args.path);

Expand Down Expand Up @@ -100,13 +94,6 @@ export const userscriptsPlugin = ({

build.onEnd(async (result) => {
for (const file of result.outputFiles ?? []) {
const scriptName = path
.basename(file.path)
.replace(/\.user\.(j|t)s$/, "");
if (!scriptName) {
throw new Error(`Failed to get scriptName from path: ${file.path}`);
}

const meta = metaStore[scriptName];

if (meta === null) {
Expand Down

0 comments on commit 1e11ab7

Please sign in to comment.