Skip to content

Commit 588bde8

Browse files
committed
Standardize sourcemap basedir processing to get relative paths.
1 parent 9c2f6bb commit 588bde8

File tree

1 file changed

+78
-30
lines changed

1 file changed

+78
-30
lines changed

index.js

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@ util.inherits(Babelify, stream.Transform);
2828

2929
function Babelify(filename, opts) {
3030
if (!(this instanceof Babelify)) {
31-
return Babelify.configure(opts)(filename);
31+
return babelify(filename, opts);
3232
}
3333

3434
stream.Transform.call(this);
3535
this._data = [];
3636
this._filename = filename;
37-
this._opts = Object.assign({filename: filename}, opts, {
38-
caller: Object.assign({
39-
name: "babelify",
40-
}, opts.caller),
41-
});
37+
this._opts = opts;
4238
}
4339

4440
Babelify.prototype._transform = function (buf, enc, callback) {
@@ -63,21 +59,81 @@ Babelify.prototype._flush = function (callback) {
6359
});
6460
};
6561

66-
Babelify.configure = function (opts) {
67-
opts = Object.assign({}, opts);
68-
var extensions = opts.extensions || babel.DEFAULT_EXTENSIONS;
69-
var sourceMapsAbsolute = opts.sourceMapsAbsolute;
70-
if (opts.sourceMaps !== false) opts.sourceMaps = "inline";
62+
Babelify.configure = buildTransform;
63+
64+
const babelify = buildTransform();
65+
66+
function buildTransform(opts) {
67+
return function (filename, transformOpts) {
68+
const babelOpts = normalizeOptions(opts, transformOpts, filename);
69+
if (babelOpts === null) {
70+
return stream.PassThrough();
71+
}
72+
73+
return new Babelify(filename, babelOpts);
74+
};
75+
}
76+
77+
function normalizeOptions(preconfiguredOpts, transformOpts, filename) {
78+
const basedir = normalizeTransformBasedir(transformOpts);
79+
const opts = normalizeTransformOpts(transformOpts);
80+
81+
// Transform options override preconfigured options unless they are undefined.
82+
if (preconfiguredOpts) {
83+
for (const key of Object.keys(preconfiguredOpts)) {
84+
if (opts[key] === undefined) {
85+
opts[key] = preconfiguredOpts[key];
86+
}
87+
}
88+
}
7189

7290
// babelify specific options
91+
var extensions = opts.extensions || babel.DEFAULT_EXTENSIONS;
92+
var sourceMapsAbsolute = opts.sourceMapsAbsolute;
7393
delete opts.sourceMapsAbsolute;
7494
delete opts.extensions;
75-
delete opts.filename;
7695

77-
// browserify specific options
78-
delete opts._flags;
79-
delete opts.basedir;
80-
delete opts.global;
96+
var extname = path.extname(filename);
97+
if (extensions.indexOf(extname) === -1) {
98+
return null;
99+
}
100+
101+
// Browserify doesn't actually always normalize the filename passed
102+
// to transforms, so we manually ensure that the filename is relative
103+
const absoluteFilename = path.resolve(basedir, filename);
104+
105+
Object.assign(opts, {
106+
cwd: opts.cwd === undefined ? basedir : opts.cwd,
107+
caller: Object.assign(
108+
{
109+
name: "babelify",
110+
},
111+
opts.caller
112+
),
113+
filename: absoluteFilename,
114+
115+
// Since Browserify can only handle inline sourcemaps, we override any other
116+
// values to force inline sourcemaps unless they've been disabled.
117+
sourceMaps: opts.sourceMaps === false ? false : "inline",
118+
119+
// The default sourcemap path is the path of the file relative to the
120+
// basedir. This should mirror Browserify's internal behavior when
121+
// 'debug' is enabled.
122+
sourceFileName:
123+
sourceMapsAbsolute
124+
? absoluteFilename
125+
: path.relative(basedir, absoluteFilename),
126+
});
127+
128+
return opts;
129+
}
130+
131+
function normalizeTransformBasedir(opts) {
132+
return path.resolve(opts._flags && opts._flags.basedir || ".");
133+
}
134+
135+
function normalizeTransformOpts(opts) {
136+
opts = Object.assign({}, opts);
81137

82138
// browserify cli options
83139
delete opts._;
@@ -87,18 +143,10 @@ Babelify.configure = function (opts) {
87143
if (opts.plugins && opts.plugins._) opts.plugins = opts.plugins._;
88144
if (opts.presets && opts.presets._) opts.presets = opts.presets._;
89145

90-
return function (filename, topts) {
91-
var extname = path.extname(filename);
92-
if (extensions.indexOf(extname) === -1) {
93-
return stream.PassThrough();
94-
}
95-
96-
var _opts = sourceMapsAbsolute
97-
? Object.assign({sourceFileName: filename}, opts)
98-
: opts;
99-
100-
if (topts && topts._flags && topts._flags.basedir) _opts.cwd = topts._flags.basedir;
146+
// browserify specific options
147+
delete opts._flags;
148+
delete opts.basedir;
149+
delete opts.global;
101150

102-
return new Babelify(filename, _opts);
103-
};
104-
};
151+
return opts;
152+
}

0 commit comments

Comments
 (0)