@nooeh/cli 0.2.5 → 0.2.6

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/add.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { mkdir, readFile, writeFile } from "node:fs/promises";
2
- import { dirname, isAbsolute, join, relative, resolve } from "node:path";
2
+ import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
3
3
  import { createInterface } from "node:readline/promises";
4
4
  import { defaultConfig, hasConfig, readConfig, resolveConfigAlias } from "./config.js";
5
5
  import { installDependencies } from "./dependencies.js";
@@ -49,8 +49,11 @@ function resolveTargetPath(uiDirectory, filePath) {
49
49
  }
50
50
  return targetPath;
51
51
  }
52
- function replaceTokenImport(source, stylesAlias) {
53
- return source.replaceAll("@nooeh/tokens/semantic.stylex", `${stylesAlias}/semantic.stylex`);
52
+ function replaceTokenImport(source, targetPath, stylesDirectory) {
53
+ const tokenPath = join(stylesDirectory, "semantic.stylex");
54
+ const importPath = relative(dirname(targetPath), tokenPath).split(sep).join("/");
55
+ const relativeImportPath = importPath.startsWith(".") ? importPath : `./${importPath}`;
56
+ return source.replaceAll("@nooeh/tokens/semantic.stylex", relativeImportPath);
54
57
  }
55
58
  export async function add(projectDirectory, componentName, options = {}) {
56
59
  if (!componentName)
@@ -65,6 +68,7 @@ export async function add(projectDirectory, componentName, options = {}) {
65
68
  const config = options["dry-run"] && shouldInitialize ? defaultConfig : await readConfig(projectDirectory);
66
69
  const resolved = await resolveComponent(componentName);
67
70
  const uiDirectory = await resolveConfigAlias(projectDirectory, config.aliases.ui, "aliases.ui");
71
+ const stylesDirectory = await resolveConfigAlias(projectDirectory, config.aliases.styles, "aliases.styles");
68
72
  let isOverwriteConfirmed = false;
69
73
  async function confirmOverwrite() {
70
74
  if (isOverwriteConfirmed)
@@ -78,7 +82,7 @@ export async function add(projectDirectory, componentName, options = {}) {
78
82
  console.log(`Will add: ${targetPath}`);
79
83
  continue;
80
84
  }
81
- await writeSource(replaceTokenImport(file.content, config.aliases.styles), targetPath, confirmOverwrite);
85
+ await writeSource(replaceTokenImport(file.content, targetPath, stylesDirectory), targetPath, confirmOverwrite);
82
86
  }
83
87
  const shouldInstallDependencies = resolved.externalDependencies.length > 0 &&
84
88
  !options.skipDependencyInstall &&
package/dist/init.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { access, mkdir, readFile, writeFile } from "node:fs/promises";
2
- import { join, relative, sep } from "node:path";
2
+ import { join, relative } 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,40 +33,31 @@ async function readViteConfig(projectDirectory) {
33
33
  }
34
34
  throw new Error("Could not find a Vite config file.");
35
35
  }
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
- aliases: { ${JSON.stringify(aliasPattern)}: [${JSON.stringify(aliasTarget)}] },
42
- unstable_moduleResolution: {
43
- type: "commonJS",
44
- rootDir: new URL(".", import.meta.url).pathname,
45
- },
46
- })`;
47
- }
48
- function configureVite(source, stylesAlias, tokenDirectory, projectDirectory) {
36
+ function configureVite(source) {
49
37
  const importLine = 'import stylex from "@stylexjs/unplugin";';
50
38
  const pluginPattern = /plugins:\s*\[([^\]]*)\]/s;
51
- const configPattern = /defineConfig\(\{\s*/;
52
- const stylexPlugin = getStylexPlugin(stylesAlias, tokenDirectory, projectDirectory);
39
+ const stylexPlugin = "stylex.vite()";
53
40
  const legacyStylexPluginPattern = /stylex\.vite\(\{\s*useCSSLayers:\s*true\s*\}\)/;
54
41
  const emptyStylexPluginPattern = /stylex\.vite\(\)/;
42
+ const nooehStylexPluginPattern = /stylex\.vite\(\{\s*(?:\/\/[^\n]*\s*)?(?:useCSSLayers:\s*true,?\s*)?aliases:\s*\{[^}]*\},\s*unstable_moduleResolution:\s*\{\s*type:\s*["']commonJS["'],\s*rootDir:\s*new URL\(["']\.["'],\s*import\.meta\.url\)\.pathname,?\s*\},\s*\}\)/s;
55
43
  if (!source.includes("stylex.vite(") && !pluginPattern.test(source)) {
56
44
  throw new Error("Could not safely update the Vite plugins array. Add stylex.vite() manually.");
57
45
  }
58
- let withStylex = source;
46
+ if (nooehStylexPluginPattern.test(source)) {
47
+ return source.replace(nooehStylexPluginPattern, stylexPlugin);
48
+ }
59
49
  if (source.includes("unstable_moduleResolution")) {
60
- withStylex = source.replace(/\s*useCSSLayers:\s*true,?/, "");
50
+ throw new Error("Could not safely simplify the existing StyleX plugin. Remove Nooeh's aliases and unstable_moduleResolution manually, then use stylex.vite().");
61
51
  }
62
- else if (legacyStylexPluginPattern.test(source)) {
52
+ let withStylex = source;
53
+ if (legacyStylexPluginPattern.test(source)) {
63
54
  withStylex = source.replace(legacyStylexPluginPattern, stylexPlugin);
64
55
  }
65
56
  else if (emptyStylexPluginPattern.test(source)) {
66
57
  withStylex = source.replace(emptyStylexPluginPattern, stylexPlugin);
67
58
  }
68
59
  else if (source.includes("stylex.vite(")) {
69
- throw new Error("Could not safely update the existing StyleX plugin. Add Nooeh's aliases and unstable_moduleResolution manually.");
60
+ throw new Error("Could not safely update the existing StyleX plugin. Configure stylex.vite() manually.");
70
61
  }
71
62
  else {
72
63
  withStylex = (source.includes(importLine) ? source : `${importLine}\n${source}`).replace(pluginPattern, (_, plugins) => {
@@ -74,16 +65,11 @@ function configureVite(source, stylesAlias, tokenDirectory, projectDirectory) {
74
65
  return `plugins: [${prefix}]`;
75
66
  });
76
67
  }
77
- if (withStylex.includes("alias:"))
78
- return withStylex;
79
- if (!configPattern.test(withStylex)) {
80
- throw new Error('Could not safely add the "@" Vite alias. Add resolve.alias["@"] manually.');
81
- }
82
- return withStylex.replace(configPattern, 'defineConfig({\n resolve: { alias: { "@": new URL("./src", import.meta.url).pathname } },\n ');
68
+ return withStylex;
83
69
  }
84
- async function writeViteFiles(projectDirectory, tokenDirectory, stylesAlias, refreshLegacyTokens) {
70
+ async function writeViteFiles(projectDirectory, tokenDirectory, refreshLegacyTokens) {
85
71
  const { path: configPath, source: configSource } = await readViteConfig(projectDirectory);
86
- const configuredVite = configureVite(configSource, stylesAlias, tokenDirectory, projectDirectory);
72
+ const configuredVite = configureVite(configSource);
87
73
  await writeNooehFiles(projectDirectory, tokenDirectory, refreshLegacyTokens);
88
74
  await writeFile(configPath, configuredVite, "utf8");
89
75
  console.log(`Configured Vite and created ${relative(projectDirectory, tokenDirectory)}.`);
@@ -143,7 +129,7 @@ export async function init(projectDirectory, options) {
143
129
  }
144
130
  }
145
131
  if (options.framework === "vite") {
146
- await writeViteFiles(projectDirectory, tokenDirectory, stylesAlias, Boolean(options.force));
132
+ await writeViteFiles(projectDirectory, tokenDirectory, Boolean(options.force));
147
133
  }
148
134
  else {
149
135
  await writeNooehFiles(projectDirectory, tokenDirectory, Boolean(options.force));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nooeh/cli",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "private": false,
5
5
  "bin": {
6
6
  "nooeh": "./dist/cli.js"