@michijs/dev-server 0.7.18 → 0.7.20
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.
- package/bin/config/config.js +0 -30
- package/bin/config/plugins/publicFolder.js +30 -8
- package/bin/utils/syncDirs.js +15 -9
- package/package.json +1 -1
package/bin/config/config.js
CHANGED
|
@@ -3,7 +3,6 @@ import coloredString from "../utils/coloredString.js";
|
|
|
3
3
|
import { getPath } from "../utils/getPath.js";
|
|
4
4
|
import { userConfig } from "./userConfig.js";
|
|
5
5
|
import { resolve } from "path";
|
|
6
|
-
import { jsonTransformer } from "../actions/start/transformers.js";
|
|
7
6
|
import { dirname } from "path";
|
|
8
7
|
import { fileURLToPath } from "url";
|
|
9
8
|
import { counterPlugin } from "./plugins/counter.js";
|
|
@@ -89,35 +88,6 @@ const config = {
|
|
|
89
88
|
plugins: [
|
|
90
89
|
...(userConfig.esbuildOptions?.plugins ?? []),
|
|
91
90
|
publicFolderPlugin,
|
|
92
|
-
{
|
|
93
|
-
name: "michijs-dev-server",
|
|
94
|
-
setup(build) {
|
|
95
|
-
// Clean outdir
|
|
96
|
-
if (build.initialOptions.outdir) {
|
|
97
|
-
if (fs.existsSync(build.initialOptions.outdir)) {
|
|
98
|
-
fs.rmSync(build.initialOptions.outdir, { recursive: true });
|
|
99
|
-
}
|
|
100
|
-
fs.mkdirSync(build.initialOptions.outdir, { recursive: true });
|
|
101
|
-
}
|
|
102
|
-
if (config.public.manifest?.options && config.public.manifest.name) {
|
|
103
|
-
const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.manifest.options, null, 2));
|
|
104
|
-
fs.writeFileSync(getPath(`${build.initialOptions.outdir}/${config.public.manifest.name}`), transformedFile);
|
|
105
|
-
}
|
|
106
|
-
if (config.public.wellKnown) {
|
|
107
|
-
const wellKnownDir = `${build.initialOptions.outdir}/.well-known`;
|
|
108
|
-
if (!fs.existsSync(wellKnownDir))
|
|
109
|
-
fs.mkdirSync(wellKnownDir);
|
|
110
|
-
if (config.public.wellKnown.assetsLinks) {
|
|
111
|
-
const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.wellKnown.assetsLinks));
|
|
112
|
-
fs.writeFileSync(getPath(`${wellKnownDir}/assetlinks.json`), transformedFile);
|
|
113
|
-
}
|
|
114
|
-
if (config.public.wellKnown.webAppOriginAssociation) {
|
|
115
|
-
const transformedFile = jsonTransformer.transformer(JSON.stringify(config.public.wellKnown.webAppOriginAssociation));
|
|
116
|
-
fs.writeFileSync(getPath(`${wellKnownDir}/web-app-origin-association`), transformedFile);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
91
|
counterPlugin,
|
|
122
92
|
],
|
|
123
93
|
define: {
|
|
@@ -2,21 +2,43 @@ import { config, connections } from "../config.js";
|
|
|
2
2
|
import { jsAndTsRegex, notJsAndTsRegex, transformers, } from "../../actions/start/transformers.js";
|
|
3
3
|
import { syncDirs } from "../../utils/syncDirs.js";
|
|
4
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";
|
|
5
8
|
export const publicFolderPlugin = {
|
|
6
9
|
name: "michijs-dev-server-public-folder",
|
|
7
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
|
+
}
|
|
8
35
|
// Copy public path - Omit to copy service worker - will be transformed after
|
|
9
|
-
|
|
10
|
-
copy(config.public.path, build.initialOptions.outdir, transformers, [
|
|
11
|
-
jsAndTsRegex,
|
|
12
|
-
]);
|
|
36
|
+
copy(config.public.path, outdir, transformers, [jsAndTsRegex]);
|
|
13
37
|
let firstLoad = true;
|
|
14
38
|
build.onEnd(() => {
|
|
15
39
|
// first-load sw - Omit to copy any other non-js file
|
|
16
|
-
if (firstLoad
|
|
17
|
-
copy(config.public.path,
|
|
18
|
-
notJsAndTsRegex,
|
|
19
|
-
]);
|
|
40
|
+
if (firstLoad) {
|
|
41
|
+
copy(config.public.path, outdir, transformers, [notJsAndTsRegex]);
|
|
20
42
|
if (config.watch)
|
|
21
43
|
syncDirs(config.public.path, config.esbuildOptions.outdir, transformers, undefined, undefined, (event, fileChangedPath) => {
|
|
22
44
|
connections.forEach((x) => x.write(`event: change\ndata: ${JSON.stringify({
|
package/bin/utils/syncDirs.js
CHANGED
|
@@ -13,16 +13,22 @@ export const syncDirs = (srcDir, outDir, transformers, omit, onStartSync, onEndS
|
|
|
13
13
|
onStartSync?.();
|
|
14
14
|
const fileSrcDir = path.dirname(fileChangedPath);
|
|
15
15
|
const fileName = path.basename(fileChangedPath);
|
|
16
|
-
const fileOutDir = fileSrcDir.replace(srcDir, outDir);
|
|
17
|
-
const pathToRemove = getPath(`${fileOutDir}/${fileName}`);
|
|
16
|
+
const fileOutDir = fileSrcDir.replace(path.normalize(srcDir), outDir);
|
|
18
17
|
console.log(` ${coloredString(`File ${fileChangedPath} ${event}.`)}`);
|
|
19
|
-
if (event === "remove")
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
if (event === "remove") {
|
|
19
|
+
const pathToRemove = getPath(`${fileOutDir}/${fileName}`);
|
|
20
|
+
const fileTransformer = transformers?.filter((x) => x.fileRegex.test(fileName));
|
|
21
|
+
if (fileTransformer.length > 0) {
|
|
22
|
+
fileTransformer.forEach(async (x) => {
|
|
23
|
+
if (x.fileRegex.test(fileChangedPath)) {
|
|
24
|
+
const finalPathToRemove = x.pathTransformer?.(pathToRemove) ?? pathToRemove;
|
|
25
|
+
fs.rmSync(finalPathToRemove, { force: true, recursive: true });
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else
|
|
30
|
+
fs.rmSync(pathToRemove, { force: true, recursive: true });
|
|
31
|
+
}
|
|
26
32
|
else {
|
|
27
33
|
copyFile(fileSrcDir, fileName, fileOutDir, transformers, omit);
|
|
28
34
|
}
|