@michijs/dev-server 0.8.4 → 0.8.5-beta.1
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
CHANGED
|
@@ -8,6 +8,7 @@ import { fileURLToPath } from "url";
|
|
|
8
8
|
import { counterPlugin } from "./plugins/counter.js";
|
|
9
9
|
import { getCurrentCommitSha } from "../utils/getCurrentCommitSha.js";
|
|
10
10
|
import { publicFolderPlugin } from "./plugins/publicFolder.js";
|
|
11
|
+
import { cssPlugin } from "./plugins/css.js";
|
|
11
12
|
const minify = process.env.NODE_ENV === "PRODUCTION";
|
|
12
13
|
const devServerListener = process.env.NODE_ENV === "DEVELOPMENT"
|
|
13
14
|
? [getPath(`${dirname(fileURLToPath(import.meta.url))}/public/client.js`)]
|
|
@@ -88,6 +89,7 @@ const config = {
|
|
|
88
89
|
// ],
|
|
89
90
|
plugins: [
|
|
90
91
|
...(userConfig.esbuildOptions?.plugins ?? []),
|
|
92
|
+
cssPlugin,
|
|
91
93
|
publicFolderPlugin,
|
|
92
94
|
counterPlugin,
|
|
93
95
|
],
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { build as esbuild } from "esbuild";
|
|
2
|
+
import { config } from "../config.js";
|
|
3
|
+
import path from "path";
|
|
4
|
+
export const cssPlugin = {
|
|
5
|
+
name: 'css import assertions',
|
|
6
|
+
setup(build) {
|
|
7
|
+
build.onLoad({ filter: /\.css$/ }, async (args) => {
|
|
8
|
+
try {
|
|
9
|
+
// Check if CSS is from a library (inside node_modules)
|
|
10
|
+
let layerName;
|
|
11
|
+
const parts = args.path.split(path.sep);
|
|
12
|
+
const nodeModulesIndex = parts.lastIndexOf('node_modules');
|
|
13
|
+
if (nodeModulesIndex !== -1 && parts[nodeModulesIndex + 1]) {
|
|
14
|
+
layerName = parts[nodeModulesIndex + 1].replace(/[^a-zA-Z0-9_-]/g, '-'); // Sanitize name
|
|
15
|
+
}
|
|
16
|
+
const result = await esbuild({
|
|
17
|
+
...config.esbuildOptions,
|
|
18
|
+
splitting: false,
|
|
19
|
+
outdir: undefined,
|
|
20
|
+
inject: undefined,
|
|
21
|
+
plugins: undefined,
|
|
22
|
+
write: false,
|
|
23
|
+
entryPoints: [args.path],
|
|
24
|
+
legalComments: "inline",
|
|
25
|
+
// TODO: Add other image formats
|
|
26
|
+
loader: { '.svg': 'dataurl', '.gif': 'dataurl', '.png': 'dataurl', '.webp': 'dataurl' },
|
|
27
|
+
define: undefined,
|
|
28
|
+
});
|
|
29
|
+
const processedCss = result.outputFiles?.[0].text ?? "";
|
|
30
|
+
const contents = `
|
|
31
|
+
const styles = new CSSStyleSheet();
|
|
32
|
+
styles.replaceSync(\`
|
|
33
|
+
${layerName ? `@layer ${layerName} {\n${processedCss}\n}` : processedCss}
|
|
34
|
+
\`);
|
|
35
|
+
export default styles;`;
|
|
36
|
+
return { contents };
|
|
37
|
+
}
|
|
38
|
+
catch (ex) {
|
|
39
|
+
console.error(ex);
|
|
40
|
+
throw ex;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
};
|