frostming / lektor-tailwind

A Lektor plugin that adds Tailwind CSS to your project seamlessly
MIT License
12 stars 3 forks source link

Minimize CSS when running `lektor server` with NODE_ENV=production #5

Closed dairiki closed 10 months ago

dairiki commented 1 year ago

Currently, the generated CSS is minimized when running lektor build, but not when running lektor server.

If one publishes their site using the "Publish" button in the Lektor GUI, one publishes un-minimized CSS.

This PR minimizes the CSS in lektor server mode if the $NODE_ENV environment variable is set to "production".


NOTE: This PR is stacked on PR #4. It should be merged after that one. (It may also require rebasing, depending on how #4 is merged.)

frostming commented 10 months ago

This PR minimizes the CSS in lektor server mode if the $NODE_ENV environment variable is set to "production".

When will that be set? Can it be set from any tool are must be set by user manually?

dairiki commented 10 months ago

This PR minimizes the CSS in lektor server mode if the $NODE_ENV environment variable is set to "production".

When will that be set? Can it be set from any tool are must be set by user manually?

For lack of better ideas, yes, I was envisioning it being set manually, or in some external way.

E.g. I now use pdm (thank you) to manage my Lektor projects' virtual environments. Configuring a pdm script to run lektor server makes it easy to configure per-script environment variables).


I'm open to alternative ideas for switch modes. Some ideas:

frostming commented 10 months ago
  • Instead of keying off an environment variable, we could Lektor's _extraflags (controlled by the --build-flag or -f command-line switch) to control behavior. E.g. lektor server -f production .

I prefer this method. Let's do it.

dairiki commented 10 months ago

The more I think about this, the more I prefer using $NODE_ENV. As far as I can tell, its used for exactly this purpose is common in JS-land.

Refs:

Various magics whereby JS transpilers treat NODE_ENV as a compile-type constant, thus removing debug code (upon minimization) from code transpiled with NODE_ENV="production":


Also, I think it would minimize confusion if lektor build and lektor server behaved the same. So, I suggest:

Note that Lektor flags can take an argument, so we could do something like

# minimize CSS
lektor server -f production:true
# or
lektor build -f production:true

# non-mimize CSS
lektor server -f production:false
# or
lektor build -f production:no

(and the default can be whatever we decide...)

frostming commented 10 months ago

Also, I think it would minimize confusion if lektor build and lektor server behaved the same.

But it's also a common practice in JS world that commands for different purpose set NODE_ENV to different values. For example, vite dev implies NODE_ENV=development while vite build implies NODE_ENV=production.

lektor server is more a development command to me(starting a dev server like vite dev), so non-minify makes more sense to me.

dairiki commented 10 months ago

@frostming Good enough! Thank you!