🐛 bug report
When running parcel watch / serve and even when I add NODE_ENV=development variable before running command it still reads process.env.NODE_ENV is production.
I have console log in my .postcss and it prints production no matter what I do.
🎛 Configuration (.babelrc, package.json, cli command)
"build": "parcel build '.tmp/parcel/**/index.html' --cache-dir .tmp/.cache",
"build:dev": "NODE_ENV=development parcel build '.tmp/parcel/**/*.html' --cache-dir .tmp/.cache",
"watch": "parcel watch '.tmp/parcel/**/*.html' --cache-dir .tmp/.cache",
"watch:dev": "NODE_ENV=development parcel watch '.tmp/parcel/**/*.html' --cache-dir .tmp/.cache",
"serve": "parcel serve --port 3000 '.tmp/parcel/**/*.html' --cache-dir .tmp/.cache",
"serve:dev": "NODE_ENV=development parcel serve --port 3000 '.tmp/parcel/**/*.html' --cache-dir .tmp/.cache",
{
"presets": [
"@babel/preset-env", {
"include": ["@babel/plugin-transform-strict-mode" ]
}]
}
console.log('.postcssrc', process.env.NODE_ENV)
const purgecss = require('@fullhuman/postcss-purgecss')({
content: ['.tmp/parcel/**/*.html'],
// Include any special characters you're using in this regular expression
defaultExtractor: (content) => content.match(/[A-Za-z0-9-_:\.\/]+/g) || [],
})
module.exports = {
plugins: [
require('postcss-easy-import'),
require('tailwindcss')('./pages/assets/css/tailwind.config.js'),
require('autoprefixer'),
...(process.env.NODE_ENV === 'production' ? [purgecss] : []),
],
}
check npm scripts
https://github.com/danielstaleiny/minimal-example-parcel2
🤔 Expected Behavior
parcel should have NODE_ENV ‘development’ when running watch / serve mode and when setting up in cli NODE_ENV environment.
😯 Current Behavior
parcel build -> nothing is printed from .postcss??
NODE_ENV=development -> nothing is printed .postcss??
parcel watch -> .postcss production
NODE_ENV=development parcel watch -> .postcss production
parcel serve -> .postcss production
NODE_ENV=development parcel serve -> .postcss production
💁 Possible Solution
Propagate NODE_ENV variable correctly to postcss
🔦 Context
runs purge when it is not needed.
💻 Code Sample
https://github.com/danielstaleiny/minimal-example-parcel2
🌍 Your Environment
Software | Version(s) |
---|---|
Parcel | “parcel”: “^2.0.0-nightly.240”, |
Node | v10.17.0 |
npm/Yarn | 6.11.3 |
Operating System | NixOS unstable (20.03) |
The JS config is “executed” here:
parcel/packages/core/utils/src/config.js
Line 87
in
d19bee1
Problems I’ve discovered just now:
require()
returns the same result. The cache needs to be cleared with something likedelete require.cache[configFile]
)require()
, the value inoptions.env.NODE_ENV
should be loaded intoprocess.env
(and reverted?))parcel/packages/transformers/postcss/src/loadConfig.js
Line 86
in
d19bee1
The config is indeed reevaluated (and it is different), but the asset isn’t transformed again (so somehow Parcel has decided that the config didn’t change at all?):
parcel/packages/transformers/postcss/src/PostCSSTransformer.js
Line 50
in
d19bee1
require
s in the JS config to missing modules should throw an error, but these are swallowed here:parcel/packages/core/utils/src/config.js
Line 110
in
d19bee1
cc @DeMoorJasper @padmaia
Any workarounds for this issue? I don’t fancy turning purge on/off and deleting cache manually whenever I need to publish.😭
I’m experiencing this issue with Tailwind CSS, which also does a purge when NODE_ENV is production
I had issues with tailwind as well for the longest time. I decided to do a diff between
process.env
in build & dev mode. Turns out there’s a variable callednpm_lifecycle_event
that seems to contain the name of the npm script that’s running. You can essentially use this as your hook to know whether you are in dev mode or not.For instance say you have the following scripts in your
package.json
:and the following in your
tailwind.config.js
This will build with purge mode when you execute the
build
command and leave as is withdev
script.More information can be found here: https://medium.com/@brianhan/use-this-npm-variable-as-a-flag-for-your-build-scripts-31069f5e2e57
Hope that helps!