@michijs/dev-server 0.7.17 → 0.7.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,9 +7,7 @@ import open from "open";
7
7
  import { context } from "esbuild";
8
8
  import { getHostURL } from "../utils/getHostURL.js";
9
9
  import { getLocalURL } from "../utils/getLocalURL.js";
10
- import { watchPublicFolderPlugin } from "../config/plugins/watchPublicFolder.js";
11
10
  export const start = async (callback) => {
12
- config.esbuildOptions.plugins?.push(watchPublicFolderPlugin);
13
11
  const buildContext = await context(config.esbuildOptions);
14
12
  const { host: esbuildHost, port: esbuildPort } = await buildContext.serve({
15
13
  servedir: config.esbuildOptions.outdir,
@@ -1,14 +1,13 @@
1
1
  import fs from "fs";
2
2
  import coloredString from "../utils/coloredString.js";
3
- import { copy } from "../utils/copy.js";
4
3
  import { getPath } from "../utils/getPath.js";
5
4
  import { userConfig } from "./userConfig.js";
6
5
  import { resolve } from "path";
7
- import { jsAndTsRegex, jsonTransformer, notJsAndTsRegex, transformers, } from "../actions/start/transformers.js";
8
6
  import { dirname } from "path";
9
7
  import { fileURLToPath } from "url";
10
8
  import { counterPlugin } from "./plugins/counter.js";
11
9
  import { getCurrentCommitSha } from "../utils/getCurrentCommitSha.js";
10
+ import { publicFolderPlugin } from "./plugins/publicFolder.js";
12
11
  const minify = process.env.NODE_ENV === "PRODUCTION";
13
12
  const devServerListener = process.env.NODE_ENV === "DEVELOPMENT"
14
13
  ? [getPath(`${dirname(fileURLToPath(import.meta.url))}/public/client.js`)]
@@ -88,45 +87,7 @@ const config = {
88
87
  // ],
89
88
  plugins: [
90
89
  ...(userConfig.esbuildOptions?.plugins ?? []),
91
- {
92
- name: "michijs-dev-server",
93
- setup(build) {
94
- // Clean outdir
95
- if (build.initialOptions.outdir) {
96
- if (fs.existsSync(build.initialOptions.outdir)) {
97
- fs.rmSync(build.initialOptions.outdir, { recursive: true });
98
- }
99
- fs.mkdirSync(build.initialOptions.outdir, { recursive: true });
100
- }
101
- if (config.public.manifest?.options && config.public.manifest.name) {
102
- const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.manifest.options, null, 2));
103
- fs.writeFileSync(getPath(`${build.initialOptions.outdir}/${config.public.manifest.name}`), transformedFile);
104
- }
105
- if (config.public.wellKnown) {
106
- const wellKnownDir = `${build.initialOptions.outdir}/.well-known`;
107
- if (!fs.existsSync(wellKnownDir))
108
- fs.mkdirSync(wellKnownDir);
109
- if (config.public.wellKnown.assetsLinks) {
110
- const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.wellKnown.assetsLinks));
111
- fs.writeFileSync(getPath(`${wellKnownDir}/assetlinks.json`), transformedFile);
112
- }
113
- if (config.public.wellKnown.webAppOriginAssociation) {
114
- const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.wellKnown.webAppOriginAssociation));
115
- fs.writeFileSync(getPath(`${wellKnownDir}/web-app-origin-association`), transformedFile);
116
- }
117
- }
118
- // Copy public path - Omit to copy service worker - will be transformed after
119
- if (config.public.path && build.initialOptions.outdir)
120
- copy(config.public.path, build.initialOptions.outdir, transformers, [jsAndTsRegex]);
121
- let firstLoad = true;
122
- build.onEnd(() => {
123
- // first-load sw - Omit to copy any other non-js file
124
- if (firstLoad && config.public.path && build.initialOptions.outdir)
125
- copy(config.public.path, build.initialOptions.outdir, transformers, [notJsAndTsRegex]);
126
- firstLoad = false;
127
- });
128
- },
129
- },
90
+ publicFolderPlugin,
130
91
  counterPlugin,
131
92
  ],
132
93
  define: {
@@ -0,0 +1,2 @@
1
+ import type { Plugin } from "esbuild";
2
+ export declare const publicFolderPlugin: Plugin;
@@ -0,0 +1,54 @@
1
+ import { config, connections } from "../config.js";
2
+ import { jsAndTsRegex, notJsAndTsRegex, transformers, } from "../../actions/start/transformers.js";
3
+ import { syncDirs } from "../../utils/syncDirs.js";
4
+ import { copy } from "../../utils/copy.js";
5
+ import fs from "fs";
6
+ import { jsonTransformer } from "../../actions/start/transformers.js";
7
+ import { getPath } from "../../utils/getPath.js";
8
+ export const publicFolderPlugin = {
9
+ name: "michijs-dev-server-public-folder",
10
+ setup(build) {
11
+ const outdir = build.initialOptions.outdir;
12
+ // Clean outdir
13
+ if (!outdir)
14
+ return;
15
+ if (fs.existsSync(outdir))
16
+ fs.rmSync(outdir, { recursive: true });
17
+ fs.mkdirSync(outdir, { recursive: true });
18
+ if (config.public.manifest?.options && config.public.manifest.name) {
19
+ const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.manifest.options, null, 2));
20
+ fs.writeFileSync(getPath(`${outdir}/${config.public.manifest.name}`), transformedFile);
21
+ }
22
+ if (config.public.wellKnown) {
23
+ const wellKnownDir = `${outdir}/.well-known`;
24
+ if (!fs.existsSync(wellKnownDir))
25
+ fs.mkdirSync(wellKnownDir);
26
+ if (config.public.wellKnown.assetsLinks) {
27
+ const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.wellKnown.assetsLinks));
28
+ fs.writeFileSync(getPath(`${wellKnownDir}/assetlinks.json`), transformedFile);
29
+ }
30
+ if (config.public.wellKnown.webAppOriginAssociation) {
31
+ const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.wellKnown.webAppOriginAssociation));
32
+ fs.writeFileSync(getPath(`${wellKnownDir}/web-app-origin-association`), transformedFile);
33
+ }
34
+ }
35
+ // Copy public path - Omit to copy service worker - will be transformed after
36
+ copy(config.public.path, outdir, transformers, [jsAndTsRegex]);
37
+ let firstLoad = true;
38
+ build.onEnd(() => {
39
+ // first-load sw - Omit to copy any other non-js file
40
+ if (firstLoad) {
41
+ copy(config.public.path, outdir, transformers, [notJsAndTsRegex]);
42
+ if (config.watch)
43
+ syncDirs(config.public.path, config.esbuildOptions.outdir, transformers, undefined, undefined, (event, fileChangedPath) => {
44
+ connections.forEach((x) => x.write(`event: change\ndata: ${JSON.stringify({
45
+ added: [],
46
+ removed: event === "remove" ? [fileChangedPath] : [],
47
+ updated: event === "update" ? [fileChangedPath] : [],
48
+ })}\n\n`));
49
+ });
50
+ firstLoad = false;
51
+ }
52
+ });
53
+ },
54
+ };
@@ -3,6 +3,7 @@ import path from "path";
3
3
  import { copyFile } from "../utils/copy.js";
4
4
  import watch from "node-watch";
5
5
  import { getPath } from "../utils/getPath.js";
6
+ import coloredString from "./coloredString.js";
6
7
  export const syncDirs = (srcDir, outDir, transformers, omit, onStartSync, onEndSync) => {
7
8
  watch.default(srcDir, {
8
9
  encoding: "utf-8",
@@ -14,6 +15,7 @@ export const syncDirs = (srcDir, outDir, transformers, omit, onStartSync, onEndS
14
15
  const fileName = path.basename(fileChangedPath);
15
16
  const fileOutDir = fileSrcDir.replace(srcDir, outDir);
16
17
  const pathToRemove = getPath(`${fileOutDir}/${fileName}`);
18
+ console.log(` ${coloredString(`File ${fileChangedPath} ${event}.`)}`);
17
19
  if (event === "remove")
18
20
  transformers.forEach((x) => {
19
21
  if (x.fileRegex.test(fileChangedPath)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@michijs/dev-server",
3
3
  "license": "MIT",
4
- "version": "0.7.17",
4
+ "version": "0.7.19",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/michijs/dev-server.git"
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "devDependencies": {
32
32
  "@michijs/tsconfig": "0.0.4",
33
- "@types/node": "22.6.0",
33
+ "@types/node": "22.7.4",
34
34
  "@types/yargs": "17.0.33",
35
35
  "typescript": "5.6.2"
36
36
  },
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "esbuild";
2
- export declare const watchPublicFolderPlugin: Plugin;
@@ -1,16 +0,0 @@
1
- import { config, connections } from "../../config/config.js";
2
- import { transformers } from "../../actions/start/transformers.js";
3
- import { syncDirs } from "../../utils/syncDirs.js";
4
- export const watchPublicFolderPlugin = {
5
- name: "michijs-dev-server-watch-public-folder",
6
- setup() {
7
- if (config.public.path && config.watch)
8
- syncDirs(config.public.path, config.esbuildOptions.outdir, transformers, undefined, undefined, (event, fileChangedPath) => {
9
- connections.forEach((x) => x.write(`event: change\ndata: ${JSON.stringify({
10
- added: [],
11
- removed: event === "remove" ? [fileChangedPath] : [],
12
- updated: event === "update" ? [fileChangedPath] : [],
13
- })}\n\n`));
14
- });
15
- },
16
- };