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

Resolve publicUrl and distDir from environment variables (.env) #8586

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

villermen
Copy link

(I couldn't find any open or closed PRs & issues related to this functionality.)

↪️ Pull Request

It's very difficult to provide the desired publicUrl or distDir arguments to parcel when they are sourced from environment variables. It's near-impossible (to my knowledge) to do so in a cross-platform compatible way from either the CLI or package.json. The environment variables are injected by Parcel and can be used in the build pipeline just fine, but Parcel itself will not be able to pick them up. So while your code might know the correct public URL based on the environment, you will still have to specify it manually to the build command.

This PR proposes a solution similar to how you can specify PORT as a fallback environment variable.

💻 Examples

// .env
PARCEL_PUBLIC_URL=/path
PARCEL_DIST_DIR=public

Running parcel build or parcel serve will default to a public URL of "/path" when not specified otherwise (via CLI or targets).

🚨 Test instructions

I was unable to test this behavior in practice yet. That will require me to dive a bit deeper into how this repository works,, but I'll be happy to do so once I know whether this is a welcome change or not.

✔️ PR Todo

  • Test and add automatic testing.

@villermen villermen mentioned this pull request Oct 30, 2022
4 tasks
@devongovett
Copy link
Member

devongovett commented Nov 4, 2022

You can't do something like this already?

parcel build --public-url $PUBLIC_URL

@villermen
Copy link
Author

villermen commented Nov 4, 2022

@devongovett Where would I source PUBLIC_URL from? It's stored in the .env file, but using that in a single command that is cross-platform compatible has proven to be very difficult.

  1. source .env && parcel build --public-url $PUBLIC_URL would not work because $PUBLIC_URL is interpreted before it is executed. It will not work outside of Linux systems either.
  2. source .env && bash - c 'parcel build --public-url \$PUBLIC_URL' would probably work but would definitely not work outside of systems without bash support.
  3. PUBLIC_URL=/foo npm run build would work but you would have to remember to set the envvar each time you build for a non-root domain.

I thought being able to leverage the already existing .env for this would be the better solution as it wouldn't introduce new concepts or possible room for user error while building. There might be a better solution out there that I'm just not seeing though.

Example for wanting this behavior is an application that configures <base href={{ process.env.BASE_URL }} because it can take an arbitrary amount of additional slugs. The Parcel references should still point to the location of the main index.html though.

@alexplummersky
Copy link

Any updates on this? I really need the public url to be accessible from an env var also.

@ppalmeida
Copy link

wow. Me as well. I've tried everything. I've tried many different solutions but none worked.

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

Successfully merging this pull request may close these issues.

4 participants