@nooeh/cli 0.2.1 → 0.2.2
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/dist/init.js +38 -11
- package/package.json +1 -1
package/dist/init.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { join, relative } from "node:path";
|
|
2
|
+
import { join, relative, sep } from "node:path";
|
|
3
3
|
import { createInterface } from "node:readline/promises";
|
|
4
4
|
import { configFileName, defaultConfig, hasConfig, resolveConfigAlias, writeConfig, } from "./config.js";
|
|
5
5
|
import { installDependencies } from "./dependencies.js";
|
|
@@ -33,21 +33,48 @@ async function readViteConfig(projectDirectory) {
|
|
|
33
33
|
}
|
|
34
34
|
throw new Error("Could not find a Vite config file.");
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
function getStylexPlugin(stylesAlias, tokenDirectory, projectDirectory) {
|
|
37
|
+
const tokenPath = relative(projectDirectory, tokenDirectory).split(sep).join("/");
|
|
38
|
+
const aliasPattern = `${stylesAlias}/*`;
|
|
39
|
+
const aliasTarget = `/ROOT/${tokenPath}/*`;
|
|
40
|
+
return `stylex.vite({
|
|
41
|
+
useCSSLayers: true,
|
|
42
|
+
aliases: { ${JSON.stringify(aliasPattern)}: [${JSON.stringify(aliasTarget)}] },
|
|
43
|
+
unstable_moduleResolution: {
|
|
44
|
+
type: "commonJS",
|
|
45
|
+
rootDir: new URL(".", import.meta.url).pathname,
|
|
46
|
+
},
|
|
47
|
+
})`;
|
|
48
|
+
}
|
|
49
|
+
function configureVite(source, stylesAlias, tokenDirectory, projectDirectory) {
|
|
37
50
|
const importLine = 'import stylex from "@stylexjs/unplugin";';
|
|
38
51
|
const pluginPattern = /plugins:\s*\[([^\]]*)\]/s;
|
|
39
52
|
const configPattern = /defineConfig\(\{\s*/;
|
|
53
|
+
const stylexPlugin = getStylexPlugin(stylesAlias, tokenDirectory, projectDirectory);
|
|
54
|
+
const legacyStylexPluginPattern = /stylex\.vite\(\{\s*useCSSLayers:\s*true\s*\}\)/;
|
|
55
|
+
const emptyStylexPluginPattern = /stylex\.vite\(\)/;
|
|
40
56
|
if (!source.includes("stylex.vite(") && !pluginPattern.test(source)) {
|
|
41
57
|
throw new Error("Could not safely update the Vite plugins array. Add stylex.vite({ useCSSLayers: true }) manually.");
|
|
42
58
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
59
|
+
let withStylex = source;
|
|
60
|
+
if (source.includes("unstable_moduleResolution")) {
|
|
61
|
+
withStylex = source;
|
|
62
|
+
}
|
|
63
|
+
else if (legacyStylexPluginPattern.test(source)) {
|
|
64
|
+
withStylex = source.replace(legacyStylexPluginPattern, stylexPlugin);
|
|
65
|
+
}
|
|
66
|
+
else if (emptyStylexPluginPattern.test(source)) {
|
|
67
|
+
withStylex = source.replace(emptyStylexPluginPattern, stylexPlugin);
|
|
68
|
+
}
|
|
69
|
+
else if (source.includes("stylex.vite(")) {
|
|
70
|
+
throw new Error("Could not safely update the existing StyleX plugin. Add Nooeh's aliases and unstable_moduleResolution manually.");
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
withStylex = (source.includes(importLine) ? source : `${importLine}\n${source}`).replace(pluginPattern, (_, plugins) => {
|
|
74
|
+
const prefix = plugins.trim() ? `${stylexPlugin}, ${plugins}` : stylexPlugin;
|
|
49
75
|
return `plugins: [${prefix}]`;
|
|
50
76
|
});
|
|
77
|
+
}
|
|
51
78
|
if (withStylex.includes("alias:"))
|
|
52
79
|
return withStylex;
|
|
53
80
|
if (!configPattern.test(withStylex)) {
|
|
@@ -55,9 +82,9 @@ function configureVite(source) {
|
|
|
55
82
|
}
|
|
56
83
|
return withStylex.replace(configPattern, 'defineConfig({\n resolve: { alias: { "@": new URL("./src", import.meta.url).pathname } },\n ');
|
|
57
84
|
}
|
|
58
|
-
async function writeViteFiles(projectDirectory, tokenDirectory) {
|
|
85
|
+
async function writeViteFiles(projectDirectory, tokenDirectory, stylesAlias) {
|
|
59
86
|
const { path: configPath, source: configSource } = await readViteConfig(projectDirectory);
|
|
60
|
-
const configuredVite = configureVite(configSource);
|
|
87
|
+
const configuredVite = configureVite(configSource, stylesAlias, tokenDirectory, projectDirectory);
|
|
61
88
|
await writeNooehFiles(projectDirectory, tokenDirectory);
|
|
62
89
|
await writeFile(configPath, configuredVite, "utf8");
|
|
63
90
|
console.log(`Configured Vite and created ${relative(projectDirectory, tokenDirectory)}.`);
|
|
@@ -106,7 +133,7 @@ export async function init(projectDirectory, options) {
|
|
|
106
133
|
}
|
|
107
134
|
}
|
|
108
135
|
if (options.framework === "vite") {
|
|
109
|
-
await writeViteFiles(projectDirectory, tokenDirectory);
|
|
136
|
+
await writeViteFiles(projectDirectory, tokenDirectory, stylesAlias);
|
|
110
137
|
}
|
|
111
138
|
else {
|
|
112
139
|
await writeNooehFiles(projectDirectory, tokenDirectory);
|