@michijs/dev-server 0.7.10 → 0.7.12

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.
@@ -1,14 +1,27 @@
1
1
  import { getPath } from "../utils/getPath.js";
2
- import fs from "fs";
2
+ import { readdirSync, rmSync, statSync } from "fs";
3
3
  import { basename, dirname } from "path";
4
+ async function minifyImage(src) {
5
+ if (!src.endsWith("webp")) {
6
+ const { default: sharp } = await import("sharp");
7
+ const image = sharp(src);
8
+ const fileNameWithoutExtension = basename(src).split(".")[0];
9
+ const newDirname = dirname(src);
10
+ await image
11
+ .webp()
12
+ .toFile(getPath(`${newDirname}/${fileNameWithoutExtension}.webp`));
13
+ rmSync(src);
14
+ }
15
+ }
4
16
  export async function minifyAsset(callback, src) {
5
- const { default: sharp } = await import("sharp");
6
- const image = sharp(src);
7
- const fileNameWithoutExtension = basename(src).split(".")[0];
8
- const newDirname = dirname(src);
9
- await image
10
- .webp()
11
- .toFile(getPath(`${newDirname}${fileNameWithoutExtension}.webp`));
12
- fs.rmdirSync(src);
17
+ const stat = await statSync(src);
18
+ if (stat.isDirectory()) {
19
+ await Promise.all(readdirSync(src).map(async (x) => {
20
+ await minifyImage(getPath(`${src}/${x}`));
21
+ }));
22
+ }
23
+ else {
24
+ await minifyImage(src);
25
+ }
13
26
  callback();
14
27
  }
@@ -5,4 +5,8 @@ export declare const jsonTransformer: {
5
5
  fileRegex: RegExp;
6
6
  transformer: (fileContent: string) => string;
7
7
  };
8
+ export declare const cssTransformer: {
9
+ fileRegex: RegExp;
10
+ transformer: (fileContent: string) => string;
11
+ };
8
12
  export declare const transformers: Transformer[];
@@ -1,6 +1,7 @@
1
1
  import { config } from "../../config/config.js";
2
2
  import { minifyXMLLike } from "../../utils/minify.js";
3
3
  import { serviceWorkerTransformer } from "../../utils/serviceWorkerTransformer.js";
4
+ import { transformSync as esbuild } from "esbuild";
4
5
  export const jsAndTsRegex = /.*\.(?:ts|js)/;
5
6
  export const notJsAndTsRegex = /.*\.(?!ts|js)/;
6
7
  export const jsonTransformer = {
@@ -9,6 +10,13 @@ export const jsonTransformer = {
9
10
  ? JSON.stringify(JSON.parse(fileContent))
10
11
  : fileContent,
11
12
  };
13
+ export const cssTransformer = {
14
+ fileRegex: /.*\.(?:css)/,
15
+ transformer: (fileContent) => esbuild(fileContent, {
16
+ loader: "css",
17
+ minify: config.public.minify,
18
+ }).code,
19
+ };
12
20
  export const transformers = [
13
21
  jsonTransformer,
14
22
  {
@@ -20,4 +28,5 @@ export const transformers = [
20
28
  transformer: serviceWorkerTransformer,
21
29
  pathTransformer: (destPath) => destPath.replace(".ts", ".js"),
22
30
  },
31
+ cssTransformer,
23
32
  ];
@@ -14,14 +14,19 @@ const devServerListener = process.env.NODE_ENV === "DEVELOPMENT"
14
14
  : [];
15
15
  export const connections = [];
16
16
  const getCurrentCommitSha = () => {
17
- const headPath = join(".git", "HEAD");
18
- const headContent = fs.readFileSync(headPath, "utf-8").trim();
19
- if (headContent.startsWith("ref:")) {
20
- const refPath = join(".git", headContent.split(" ")[1]);
21
- return fs.readFileSync(refPath, "utf-8").trim();
17
+ try {
18
+ const headPath = join(".git", "HEAD");
19
+ const headContent = fs.readFileSync(headPath, "utf-8").trim();
20
+ if (headContent.startsWith("ref:")) {
21
+ const refPath = join(".git", headContent.split(" ")[1]);
22
+ return fs.readFileSync(refPath, "utf-8").trim();
23
+ }
24
+ else {
25
+ return headContent;
26
+ }
22
27
  }
23
- else {
24
- return headContent;
28
+ catch {
29
+ return "";
25
30
  }
26
31
  };
27
32
  const commitSha = getCurrentCommitSha();
@@ -40,7 +45,7 @@ const config = {
40
45
  public: {
41
46
  path: "public",
42
47
  indexName: "index.html",
43
- minify: minify,
48
+ minify,
44
49
  ...(userConfig.public ?? {}),
45
50
  assets: {
46
51
  path: "assets",
@@ -13,14 +13,17 @@ export const syncDirs = (srcDir, outDir, transformers, omit, onStartSync, onEndS
13
13
  const fileSrcDir = path.dirname(fileChangedPath);
14
14
  const fileName = path.basename(fileChangedPath);
15
15
  const fileOutDir = fileSrcDir.replace(srcDir, outDir);
16
+ const pathToRemove = getPath(`${fileOutDir}/${fileName}`);
16
17
  if (event === "remove")
17
18
  transformers.forEach((x) => {
18
19
  if (x.fileRegex.test(fileChangedPath)) {
19
- fs.rmSync(getPath(`${fileOutDir}/${x.pathTransformer?.(fileName) ?? fileName}`), { force: true, recursive: true });
20
+ const finalPathToRemove = x.pathTransformer?.(pathToRemove) ?? pathToRemove;
21
+ fs.rmSync(finalPathToRemove, { force: true, recursive: true });
20
22
  }
21
23
  });
22
- else
24
+ else {
23
25
  copyFile(fileSrcDir, fileName, fileOutDir, transformers, omit);
26
+ }
24
27
  onEndSync?.(event, fileChangedPath);
25
28
  });
26
29
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@michijs/dev-server",
3
3
  "license": "MIT",
4
- "version": "0.7.10",
4
+ "version": "0.7.12",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/michijs/dev-server.git"