/
index.js
152 lines (126 loc) · 4.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var stream = require("stream");
var util = require("util");
var path = require("path");
let babel;
try {
babel = require("@babel/core");
} catch (err) {
if (err.code === "MODULE_NOT_FOUND") {
err.message +=
"\n babelify@9 requires Babel 7.x (the package '@babel/core'). " +
"If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.";
}
throw err;
}
// Since we've got the reverse bridge package at @babel/core@6.x, give
// people useful feedback if they try to use it alongside babel-loader.
if (/^6\./.test(babel.version)) {
throw new Error(
"\n babel-loader@8 will not work with the '@babel/core@6' bridge package. " +
"If you want to use Babel 6.x, install 'babel-loader@7'."
);
}
module.exports = Babelify;
util.inherits(Babelify, stream.Transform);
function Babelify(filename, opts) {
if (!(this instanceof Babelify)) {
return babelify(filename, opts);
}
stream.Transform.call(this);
this._data = [];
this._filename = filename;
this._opts = opts;
}
Babelify.prototype._transform = function (buf, enc, callback) {
this._data.push(buf);
callback();
};
Babelify.prototype._flush = function (callback) {
// Merge the buffer pieces after all are available, instead of one at a time,
// to avoid corrupting multibyte characters.
const data = Buffer.concat(this._data).toString();
babel.transform(data, this._opts, (err, result) => {
if (err) {
this.emit("error", err);
} else {
this.emit("babelify", result, this._filename);
var code = result !== null ? result.code : data;
this.push(code);
callback();
}
});
};
Babelify.configure = buildTransform;
const babelify = buildTransform();
function buildTransform(opts) {
return function (filename, transformOpts) {
const babelOpts = normalizeOptions(opts, transformOpts, filename);
if (babelOpts === null) {
return stream.PassThrough();
}
return new Babelify(filename, babelOpts);
};
}
function normalizeOptions(preconfiguredOpts, transformOpts, filename) {
const basedir = normalizeTransformBasedir(transformOpts);
const opts = normalizeTransformOpts(transformOpts);
// Transform options override preconfigured options unless they are undefined.
if (preconfiguredOpts) {
for (const key of Object.keys(preconfiguredOpts)) {
if (opts[key] === undefined) {
opts[key] = preconfiguredOpts[key];
}
}
}
// babelify specific options
var extensions = opts.extensions || babel.DEFAULT_EXTENSIONS;
var sourceMapsAbsolute = opts.sourceMapsAbsolute;
delete opts.sourceMapsAbsolute;
delete opts.extensions;
var extname = path.extname(filename);
if (extensions.indexOf(extname) === -1) {
return null;
}
// Browserify doesn't actually always normalize the filename passed
// to transforms, so we manually ensure that the filename is relative
const absoluteFilename = path.resolve(basedir, filename);
Object.assign(opts, {
cwd: opts.cwd === undefined ? basedir : opts.cwd,
caller: Object.assign(
{
name: "babelify",
},
opts.caller
),
filename: absoluteFilename,
// Since Browserify can only handle inline sourcemaps, we override any other
// values to force inline sourcemaps unless they've been disabled.
sourceMaps: opts.sourceMaps === false ? false : "inline",
// The default sourcemap path is the path of the file relative to the
// basedir. This should mirror Browserify's internal behavior when
// 'debug' is enabled.
sourceFileName:
sourceMapsAbsolute
? absoluteFilename
: path.relative(basedir, absoluteFilename),
});
return opts;
}
function normalizeTransformBasedir(opts) {
return path.resolve(opts._flags && opts._flags.basedir || ".");
}
function normalizeTransformOpts(opts) {
opts = Object.assign({}, opts);
// browserify cli options
delete opts._;
// "--opt [ a b ]" and "--opt a --opt b" are allowed:
if (opts.ignore && opts.ignore._) opts.ignore = opts.ignore._;
if (opts.only && opts.only._) opts.only = opts.only._;
if (opts.plugins && opts.plugins._) opts.plugins = opts.plugins._;
if (opts.presets && opts.presets._) opts.presets = opts.presets._;
// browserify specific options
delete opts._flags;
delete opts.basedir;
delete opts.global;
return opts;
}