Skip to content

Commit

Permalink
Moved everything related with public folder to publicFolderPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lsegurado committed Oct 11, 2024
1 parent 29c9e96 commit 5e95c7c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 58 deletions.
3 changes: 0 additions & 3 deletions src/actions/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import open from "open";
import { context } from "esbuild";
import { getHostURL } from "../utils/getHostURL.js";
import { getLocalURL } from "../utils/getLocalURL.js";
import { watchPublicFolderPlugin } from "../config/plugins/watchPublicFolder.js";

export const start = async (callback: (selectedPort: number) => void) => {
config.esbuildOptions.plugins?.push(watchPublicFolderPlugin);

const buildContext = await context(config.esbuildOptions);
const { host: esbuildHost, port: esbuildPort } = await buildContext.serve({
servedir: config.esbuildOptions.outdir,
Expand Down
28 changes: 2 additions & 26 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import fs from "fs";
import type { Config } from "../types.js";
import coloredString from "../utils/coloredString.js";
import { copy } from "../utils/copy.js";
import { getPath } from "../utils/getPath.js";
import { userConfig } from "./userConfig.js";
import type http from "http";
import { resolve } from "path";
import {
jsAndTsRegex,
jsonTransformer,
notJsAndTsRegex,
transformers,
} from "../actions/start/transformers.js";
import { dirname } from "path";
import { fileURLToPath } from "url";
import { counterPlugin } from "./plugins/counter.js";
import { getCurrentCommitSha } from "../utils/getCurrentCommitSha.js";
import { publicFolderPlugin } from "./plugins/publicFolder.js";

const minify = process.env.NODE_ENV === "PRODUCTION";
const devServerListener =
Expand Down Expand Up @@ -102,6 +99,7 @@ const config = {
// ],
plugins: [
...(userConfig.esbuildOptions?.plugins ?? []),
publicFolderPlugin,
{
name: "michijs-dev-server",
setup(build) {
Expand Down Expand Up @@ -148,28 +146,6 @@ const config = {
);
}
}

// Copy public path - Omit to copy service worker - will be transformed after
if (config.public.path && build.initialOptions.outdir)
copy(
config.public.path,
build.initialOptions.outdir,
transformers,
[jsAndTsRegex],
);

let firstLoad = true;
build.onEnd(() => {
// first-load sw - Omit to copy any other non-js file
if (firstLoad && config.public.path && build.initialOptions.outdir)
copy(
config.public.path,
build.initialOptions.outdir,
transformers,
[notJsAndTsRegex],
);
firstLoad = false;
});
},
},
counterPlugin,
Expand Down
52 changes: 52 additions & 0 deletions src/config/plugins/publicFolder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Plugin } from "esbuild";
import { config, connections } from "../config.js";
import { jsAndTsRegex, notJsAndTsRegex, transformers } from "../../actions/start/transformers.js";
import { syncDirs } from "../../utils/syncDirs.js";
import { copy } from "../../utils/copy.js";

export const publicFolderPlugin: Plugin = {
name: "michijs-dev-server-public-folder",
setup(build) {
// Copy public path - Omit to copy service worker - will be transformed after
if (build.initialOptions.outdir)
copy(
config.public.path,
build.initialOptions.outdir,
transformers,
[jsAndTsRegex],
);

let firstLoad = true;
build.onEnd(() => {
// first-load sw - Omit to copy any other non-js file
if (firstLoad && build.initialOptions.outdir) {
copy(
config.public.path,
build.initialOptions.outdir,
transformers,
[notJsAndTsRegex],
);
if (config.watch)
syncDirs(
config.public.path,
config.esbuildOptions.outdir,
transformers,
undefined,
undefined,
(event, fileChangedPath) => {
connections.forEach((x) =>
x.write(
`event: change\ndata: ${JSON.stringify({
added: [],
removed: event === "remove" ? [fileChangedPath] : [],
updated: event === "update" ? [fileChangedPath] : [],
})}\n\n`,
),
);
},
);
firstLoad = false;
}
});
},
};
29 changes: 0 additions & 29 deletions src/config/plugins/watchPublicFolder.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/utils/syncDirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path";
import { type Transformer, copyFile } from "../utils/copy.js";
import watch from "node-watch";
import { getPath } from "../utils/getPath.js";
import coloredString from "./coloredString.js";

export const syncDirs = (
srcDir: string,
Expand All @@ -25,6 +26,7 @@ export const syncDirs = (
const fileName = path.basename(fileChangedPath);
const fileOutDir = fileSrcDir.replace(srcDir, outDir);
const pathToRemove = getPath(`${fileOutDir}/${fileName}`);
console.log(` ${coloredString(`File ${fileChangedPath} ${event}.`)}`);
if (event === "remove")
transformers.forEach((x) => {
if (x.fileRegex.test(fileChangedPath)) {
Expand Down

0 comments on commit 5e95c7c

Please sign in to comment.