website icon indicating copy to clipboard operation
website copied to clipboard

Extensions does not work with a quote delimited string.

Open ryanpcmcquen opened this issue 5 years ago • 6 comments

As I discovered in my own project. When using .ts files alongside .js files, passing a quote delimited string to --extensions, causes Babel to ignore plain .js files:

extensions_string_issue

Commands are as follows:

"build": "babel src/ -s --extensions .js,.jsx,.ts,.tsx -d lib/",
"build-broken": "babel src/ -s --extensions '.js,.jsx,.ts,.tsx' -d lib/",

There may be other areas that need to be updated ... or perhaps this is just a bug?

ryanpcmcquen avatar Jan 03 '21 09:01 ryanpcmcquen

:heavy_check_mark: Deploy preview for babel ready! Built without sensitive environment variables

:hammer: Explore the source changes: 46b65b0ebae00f1a960ee4828830187035a6069d

:mag: Inspect the deploy logs: https://app.netlify.com/sites/babel/deploys/5ff18b73d474df00076cae31

:sunglasses: Browse the preview: https://deploy-preview-2436--babel.netlify.app

netlify[bot] avatar Jan 03 '21 09:01 netlify[bot]

Here is an exact commit where this can be repro'd: https://github.com/ryanpcmcquen/inkdrop_wiki_links/commit/1428c56aa21933f3e80a2c90d695c74146439d9a

ryanpcmcquen avatar Jan 03 '21 09:01 ryanpcmcquen

I don't think we have to change --extensions ".ts" to --extensions .ts

@babel/cli does not support comma-separated --extensions string, if you want to provide variadic options

--extensions .ts .js

or

--extensions .ts --extensions .js

See also https://github.com/tj/commander.js/#variadic-option

JLHwung avatar Jan 14 '21 16:01 JLHwung

Well, this works:

--extensions .js,.jsx,.ts,.tsx

This does not:

--extensions '.js,.jsx,.ts,.tsx'

And this does not:

--extensions ".js,.jsx,.ts,.tsx"

Despite comma separated strings not being supported, they have propagated well across the internet, even landing in an official Microsoft project promoting Babel with TypeScript:

https://github.com/microsoft/TypeScript-Babel-Starter/blob/d7f039380418fcd4d59e6bd4ab3fd8e405d07196/package.json#L10

    "build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline"

They are on Microsoft's site as well:

https://devblogs.microsoft.com/typescript/typescript-and-babel-7/

Other various sites have picked up on this syntax now for it to become somewhat idiomatic ... since the quote delimiting is superfluous, is there any reason to keep it in the documentation?

ryanpcmcquen avatar Jan 14 '21 17:01 ryanpcmcquen

We'll then need a new tests to make sure that comma separated extensions don't accidentally stop working.

nicolo-ribaudo avatar Jan 14 '21 18:01 nicolo-ribaudo

Here is an exact commit where this can be repro'd:

Thanks for the reproduction repo. However, I can not reproduce this issue: yarn build-broken works for me. It seems that there are inconsistent behaviours between macOS and Windows.

yarn run v1.22.10
warning [email protected]: The engine "inkdrop" appears to be invalid.
$ babel src/ -s --extensions '.js,.jsx,.ts,.tsx' -d lib/
Successfully compiled 4 files with Babel (654ms).
✨  Done in 0.95s.

In your case, the '.js,.jsx,.ts,.tsx' might be passed down to process.argv as "'.js,.jsx,.ts,.tsx'". After it is split by ,, it becomes an array of '.js, .jsx, .ts and .tsx'. So from your side, adding quotes to comma-separated string does not work.

@babel/cli does not support comma-separated --extensions string

I was wrong. Babel does support comma separated extensions: https://github.com/babel/babel/blob/d3a7cd5e8d5f99fb0b465a5c1d7209d5e1e6ab03/packages/babel-cli/src/babel/options.ts#L354

JLHwung avatar Jul 30 '21 21:07 JLHwung