vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
9.13k stars 287 forks source link

Weird behavior with `path.resolve` #444

Open alexindigo opened 5 years ago

alexindigo commented 5 years ago

Switched from @zeit/ncc@^0.11.0 to @zeit/ncc@0.20.2 and observing weird behavior with path.resolve:

js file

var fileWithExt = filename + '.' + ext;
console.log('- 1:', path.resolve(dir, fileWithExt));
console.log('- 2:', path.resolve(dir, filename + '.' + ext));

Run it with ./node_modules/.bin/ncc run test.js

Output:

- 1: /Users/alex/Projects/configly/test/fixtures/config/basic/runtime.js
- 2: /private/var/folders/zb/mp_ksxdx4p14t518_6d523lr0000gn/T/e022a7461cd2d3e99b8a466628324dbc/configly/./test/fixtures/config/basic/runtime.js

And if I run ./node_modules/.bin/ncc build test.js, this is what I get:

var fileWithExt = filename + '.' + ext;
console.log('- 1:', path.resolve(dir, fileWithExt));
console.log('- 2:', __webpack_require__.ab + "configly/" + dir + '/' + filename + '.' + ext);

Above are not full files, but lines of code in question.

Thank you.

guybedford commented 5 years ago

This is by design - assets are emitted wherever referenced as file paths will not work out otherwise.

Can you perhaps share where dir and filename are being assigned here, as that will affect the emission analysis considerably and determine whether this is a bug or expected behaviour?

alexindigo commented 5 years ago

it's the same reference both times, so the difference is in variables names? :)

And this is the code in question https://github.com/alexindigo/configly/blob/master/lib/load_files.js#L39-L44

Thank you.

guybedford commented 5 years ago

I tried to replicate this with the following code:

git clone git@github.com:alexindigo/configly
cd configly
yarn install
sudo npm install -g @zeit/ncc
ncc build lib/load_files.js -o lf
cat lf/index.js

when observing the outputted source I don't see the rewriting you are referring to at all here.

Can you perhaps clarify how I could replicate your case.

alexindigo commented 5 years ago

@guybedford Yes, because I added workaround in the latest version, you can try to check out v5.0.1 tag, where it works with 0.11 version of ncc, but breaks in 0.20 version – https://github.com/alexindigo/configly/tree/v5.0.1 Thank you.

guybedford commented 5 years ago

Ah perfect, yes I can confirm this is a bug thanks.

alexindigo commented 5 years ago

Thank you @guybedford

Probably don't need that "need more info" label :)