Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detailed instructions for nodejs tailwindcss #88

Open
ngortheone opened this issue Jul 2, 2023 · 2 comments
Open

Detailed instructions for nodejs tailwindcss #88

ngortheone opened this issue Jul 2, 2023 · 2 comments

Comments

@ngortheone
Copy link

I apologize upfront for stupid question. I come from systems & backend engineering world, the world of frontend is new and frightening to me. I am trying to get started with Phoenix on FreeBSD.

I am aware of #49 , however this option has big drawback: there are are no official tailwind cli packages for FreeBSD and current package is outdated, maintained by a single person and relies on a complicated series of dirty hacks to get it working. (All thanks to convoluted build process of the tailwindcss CLI)

As I gather from the readme it is possible to use tailwindcss with nodejs:

For third-party Tailwind plugin support (e.g. DaisyUI), the node package must be used. See the Tailwind Node.js installation instructions if you require third-party plugin support.

However official tailwind documentation gives very generic guidance how to install it. Following those steps verbatim does not produce working result.

Can you please provide more detailed and tailored to phoenix steps?

I do have node and tailwind installed. npx tailwindcss works. As I understand tailwind mix dependency should not be needed, or at least should be reconfigured. I want to re-use auto-generated tailwind.config.js.

This snippet from config/config.ex hints on what input/output paths should be but I would appreciate official step-by-step getting started instruction for a web monkey like me.

# Configure tailwind (the version is required)
config :tailwind,
  version: "3.2.7",
  default: [
    args: ~w(
      --config=tailwind.config.js
      --input=css/app.css
      --output=../priv/static/assets/app.css
    ),
    cd: Path.expand("../assets", __DIR__)
  ]

P.S. It seems that this bit has to be adjusted to run npx tailwind instead...

  defp aliases do
    [
      setup: ["deps.get", "assets.setup", "assets.build"],
      "assets.setup": ["tailwind.install --if-missing", "esbuild.install --if-missing"],
      "assets.build": ["tailwind default", "esbuild default"],
      "assets.deploy": ["tailwind default --minify", "esbuild default --minify", "phx.digest"]
    ]
  end
@ngortheone
Copy link
Author

Partially answering my own question.

Some good hints were discovered here: https://github.com/phoenixframework/tailwind/blob/main/lib/tailwind.ex#L34
Later I discovered the same info here: https://hexdocs.pm/tailwind/Tailwind.html

Side note: It is a shame that github's readme and hexdocs have different content.

Nevertheless after installing tailwindcss module globally (npm install -g tailwindcss) and setting :path I was still getting an error:

Rebuilding...
Error: Cannot find module 'tailwindcss/plugin'
Require stack:
- /usr/home/ngor/tmp/guild/assets/tailwind.config.js
- /usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli/build/plugin.js
- /usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli/build/index.js
- /usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli/index.js
- /usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1070:15)
    at Module._load (node:internal/modules/cjs/loader:923:27)
    at Module.require (node:internal/modules/cjs/loader:1137:19)
    at require (node:internal/modules/helpers:121:18)
    at Object.<anonymous> (/usr/home/ngor/tmp/guild/assets/tailwind.config.js:6:16)
    at Module._compile (node:internal/modules/cjs/loader:1255:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1137:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/home/ngor/tmp/guild/assets/tailwind.config.js',
    '/usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli/build/plugin.js',
    '/usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli/build/index.js',
    '/usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli/index.js',
    '/usr/home/ngor/.local/lib/node_modules/tailwindcss/lib/cli.js'
  ]
}

The missing piece of the solution was found here:
https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package

Apparently node is not looking in global location on require, which may be obvious for node experts, but came as a surprise to me.

after setting export NODE_PATH=$(npm root -g) I finally was able to start phx.server

Question to phoenix experts: is it possible to incorporate this trick NODE_PATH=$(npm root -g) into the build somehow? (I.e. to avoid setting it manually, but have it as a part of mix.exs or something ...

cc @dch -> a working solution that does not require hackery and elf patching :P

@dch
Copy link
Contributor

dch commented Mar 13, 2024

@ngortheone this is what I used, with npm-node20 package:

$ npm install --prefix assets -D tailwindcss@latest postcss@latest autoprefixer@latest @tailwindcss/forms daisyui@latest && ln -s ../assets/node_modules/.bin/tailwind _build/tailwind-freebsd-x64

for aarch64 that would be probably arm64 at the end. Does that help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants