michaelficarra / commonjs-everywhere

:rainbow: minimal CommonJS browser bundler with aliasing, extensibility, and source maps
BSD 3-Clause "New" or "Revised" License
158 stars 21 forks source link

Alias on windows #88

Closed albertosantini closed 6 years ago

albertosantini commented 10 years ago

cjsify --verbose --alias bar.js:foo.js main.js > p.js

required "bar.js" from "/main.js"
required "fs" from "/bar.js"

c:\My\Dev\snippets\commonjs-everywhere\lib\command.js:167
        throw e;
              ^
Error: Core module "fs" has not yet been ported to the browser
    at resolvePath (c:\My\Dev\snippets\commonjs-everywhere\lib\relative-resolve.
js:29:15)
    at module.exports (c:\My\Dev\snippets\commonjs-everywhere\lib\relative-resol
ve.js:61:16)
    at Controller.estraverse.replace.enter (c:\My\Dev\snippets\commonjs-everywhere\lib\traverse-dependencies.js:109:24)
    at Controller.__execute (c:\My\Dev\snippets\commonjs-everywhere\node_modules\estraverse\estraverse.js:313:31)
    at Controller.replace (c:\My\Dev\snippets\commonjs-everywhere\node_modules\estraverse\estraverse.js:488:27)
    at Object.replace (c:\My\Dev\snippets\commonjs-everywhere\node_modules\estraverse\estraverse.js:556:27)
    at Object.module.exports [as traverseDependencies] (c:\My\Dev\snippets\commonjs-everywhere\lib\traverse-dependencies.js:94:18)
    at build (c:\My\Dev\snippets\commonjs-everywhere\lib\command.js:149:31)
    at startBuild (c:\My\Dev\snippets\commonjs-everywhere\lib\command.js:220:17)

    at Object.<anonymous> (c:\My\Dev\snippets\commonjseverywhere\lib\command.js
:271:5)

bar.js

var fs = require("fs");

exports.PI = 42;

foo.js

exports.PI = 3.14;

main.js

var foo = require("bar.js");

console.log(foo.PI);

The alias is not used.

Furthermore, if you remove the offending line (the required fs module) and execute node p.js is printed 42 and not 3.14. Another confirm the alias is not used.

This is issue is related to https://github.com/nzakas/eslint/issues/119.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/1957813-alias-on-windows?utm_campaign=plugin&utm_content=tracker%2F297897&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F297897&utm_medium=issues&utm_source=github).
michaelficarra commented 10 years ago

@albertosantini: The alias should actually be --alias /bar.js:/foo.js, but judging by your comments in nzakas/eslint#119, that will not be sufficient.

@Nami-Doc: You're on Windows, right? Mind looking into this?

vendethiel commented 10 years ago

Sure !

albertosantini commented 10 years ago

I repeated the test using --alias /bar.js:/foo.js and I confirm the results are the same.

vendethiel commented 10 years ago

Yes, I can confirm the bug. I'll look into it.

vendethiel commented 10 years ago

When I dump CORE_MODULES then corePath before throwing, I get this

{ buffer: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\buffer-browserify\\index.js',
  constants: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\constants-browserify\\constants.json',
  crypto: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\crypto-browserify\\index.js',
  events: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\events-browserify\\events.js',
  http: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\http-browserify\\index.js',
  punycode: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\punycode\\punycode.js',
  querystring: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\querystring\\index.js',
  vm: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\vm-browserify\\index.js',
  zlib: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node_modules\\zlib-browserify\\index.js',
  _stream_duplex: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\_stream_duplex.js',
  _stream_passthrough: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\_stream_passthrough.js',
  _stream_readable: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\_stream_readable.js',
  _stream_transform: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\_stream_transform.js',
  _stream_writable: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\_stream_writable.js',
  assert: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\assert.js',
  console: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\console.js',
  domain: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\domain.js',
  freelist: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\freelist.js',
  path: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\path.js',
  readline: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\readline.js',
  stream: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\stream.js',
  string_decoder: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\string_decoder.js',
  sys: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\sys.js',
  url: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\url.js',
  util: '%%AppData%\\npm\\node_modules\\commonjs-everywhere\\node\\lib\\util.js' } undefined
vendethiel commented 10 years ago

Actually, whatever are the params I pass it, I always get the same exception (which seems expected since core-modules doesn't export fs).

aelr commented 10 years ago

Any progress on this? I just ran into this issue. Any pointers on where to look to investigate where this is falling down?

In my case, I'm not trying to alias a Node.JS core module but a CommonJS package name (and providing a path to a shim that grabs the global var).

cjsify --verbose --no-node -w -s test/all-tests-built.map -a lazy.js:/shims/lazy.js test/all-tests.js -o test/all-tests-built.js

Output:

required "lazy.js" from "/js/<file>"
ERROR: Cannot find module "lazy.js" in "<project root>"

I tried:

-a lazy.js:/shims/lazy.js
-a lazy.js:shims/lazy.js
--amend lazy.js:shims/lazy.js
michaelficarra commented 10 years ago

Sorry, I just don't have the ability to test on a Windows machine, and frankly this issue is not enough of an incentive to go out and acquire a Windows license. I'd happily review a PR though if someone wants to take it on.

aelr commented 10 years ago

Understandable. I had switched to this from watchify as it had stopped working recently. I was able to spot the breakage on that project and get a fix accepted, so I've moved back.

albertosantini commented 6 years ago

Thanks for the ride, but the time is over. :)