Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamically calculate __dirname and __filename when --node is passed
When `--no-commondir` is set (as a consequence of `--node` for example), it will cause the final bundle to hardcode absolute values of the machine that generated the bundle in order to resolve `__dirname` and `__filename`. For example, consider a `foo.js` file containing: ```js console.log(__dirname); console.log(__filename); ``` Calling `browserify --node` on it results in: ```js (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ (function (__filename,__dirname){ console.log(__dirname); console.log(__filename); }).call(this,"/Users/jviotti/Projects/playground/node-browserify/foo.js","/Users/jviotti/Projects/playground/node-browserify") },{}]},{},[1]); ``` Notice the absolute paths at the end of the bundle. This means that Browserify node users can't generate a bundle in one machine, and expect it to run without issues on another machine. As a solution, we can use the final bundle's `__dirname` to dynamically resolve every file's `__dirname` and `__filename`. This change only takes place when `--node` is set, and keeps Browserify backwards compatible. For example, the above output might contain the following line: ``` }).call(this,require("path").join(__dirname,"foo.js"),require("path").join(__dirname,".")) ``` Fixes: #1723 See: balena-io/etcher#1429 See: balena-io/etcher#1409 Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org> Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
- Loading branch information
Showing
4 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
console.log([ | ||
__dirname, | ||
__filename | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters