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

Insert bin prefix for script packages #338

Open
joshferrell opened this issue Nov 16, 2019 · 8 comments
Open

Insert bin prefix for script packages #338

joshferrell opened this issue Nov 16, 2019 · 8 comments
Assignees
Labels
kind: bug Something isn't working solution: workaround available There is a workaround available for this issue

Comments

@joshferrell
Copy link

Current Behavior

The output of index.js begins with use strict which if the package is intended to be a script, an error outputs.

Desired Behavior

When a package.json includes a bin parameter, prefix the index.js file with #!/usr/bin/env node

Who does this impact? Who is this for?

This is for users who wish to use tsdx for creating packages that are intended to be used in the command line.

Additional context

If it doesn't make sense to base the behavior on the bin parameter in the package.json, it might make sense instead to make this a new template type, but it's so similar to node, that this might be more effort than necessary.

@swyxio
Copy link
Collaborator

swyxio commented Dec 4, 2019

thanks for your idea! i'd say this is beyond project goals - if you're making a CLI, you'll know to make those two changes.

@swyxio swyxio closed this as completed Dec 4, 2019
@joshferrell
Copy link
Author

Is it possible to allow us to modify the index file using rollup in that case?

Currently the rollup extensions do not allow for modifications to the index.js file generated in /dist. So while I can write a script to after build prefix the index file, since npm start is a watch script, I can't manipulate it except manually (unless I've already ran build and it's cached).

@swyxio swyxio reopened this Dec 5, 2019
@swyxio
Copy link
Collaborator

swyxio commented Dec 5, 2019

ah you’re right. i see. will leave this one to @jaredpalmer

@joshferrell
Copy link
Author

@jaredpalmer I'm happy to contribute to this if you have any ideas on how you think this should be configured from the end users perspective.

@jaredpalmer
Copy link
Owner

Ahh. My preserve shebang Rollup hack is totally undone by dev/prod entry. Shoot! I wonder if we can sniff it out somehow. I also wonder if we have a flag to turn off Dev/prod since a cli can’t really run in DEV

@agilgur5
Copy link
Collaborator

agilgur5 commented Oct 1, 2020

So I'm working on a fix to this by making the dev/prod entry an actual Rollup plugin per #793 (comment) (it being out-of-band causes some issues like #351 (comment), also other folks could potentially use it) and recently had a breakthrough on how to make it work with multiple inputs (mostly by heavily reading the Rollup docs again, which I've been doing for upstream fixes in rollup-plugin-typescript2 like ezolenko/rollup-plugin-typescript2#221 anyway). I feel pretty close but still need to do some testing, particularly against #367

In the meantime, here's a one-liner workaround in Bash for this that one can use in a post-build script:

echo -e '#!/usr/bin/env node\n' "$(cat dist/index.js)" > dist/index.js

@agilgur5 agilgur5 added the solution: workaround available There is a workaround available for this issue label Oct 1, 2020
@agilgur5 agilgur5 added this to the Future Non-breaking milestone Oct 22, 2020
@karlhorky
Copy link
Contributor

We currently use TSDX to build Preflight, a command-line tool for the @upleveled students to check their project work before submitting.

The way that we're dealing with it is to have a separate file in a bin folder, similar to the code below (although ours uses ES Modules):

#!/usr/bin/env node

'use strict';

require('../src/cli').run(process.argv.slice(2));

This is also how Prettier handles it - see also their package.json

Maybe this should be the recommendation from TSDX until there are other viable options?

@huozhi
Copy link

huozhi commented Jan 18, 2021

I used rollup.output.banner to add shebang header in my bundler lib https://github.com/huozhi/bunchee/blob/073ae571604a2a75a2b54d6a510b6aad39e5b0ef/src/rollup-config.ts#L74

also filed a PR #957 with the similar approach, trying to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug Something isn't working solution: workaround available There is a workaround available for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants