@@ -28,17 +28,13 @@ util.inherits(Babelify, stream.Transform);
28
28
29
29
function Babelify ( filename , opts ) {
30
30
if ( ! ( this instanceof Babelify ) ) {
31
- return Babelify . configure ( opts ) ( filename ) ;
31
+ return babelify ( filename , opts ) ;
32
32
}
33
33
34
34
stream . Transform . call ( this ) ;
35
35
this . _data = [ ] ;
36
36
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 ;
42
38
}
43
39
44
40
Babelify . prototype . _transform = function ( buf , enc , callback ) {
@@ -63,21 +59,81 @@ Babelify.prototype._flush = function (callback) {
63
59
} ) ;
64
60
} ;
65
61
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
+ }
71
89
72
90
// babelify specific options
91
+ var extensions = opts . extensions || babel . DEFAULT_EXTENSIONS ;
92
+ var sourceMapsAbsolute = opts . sourceMapsAbsolute ;
73
93
delete opts . sourceMapsAbsolute ;
74
94
delete opts . extensions ;
75
- delete opts . filename ;
76
95
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 ) ;
81
137
82
138
// browserify cli options
83
139
delete opts . _ ;
@@ -87,18 +143,10 @@ Babelify.configure = function (opts) {
87
143
if ( opts . plugins && opts . plugins . _ ) opts . plugins = opts . plugins . _ ;
88
144
if ( opts . presets && opts . presets . _ ) opts . presets = opts . presets . _ ;
89
145
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 ;
101
150
102
- return new Babelify ( filename , _opts ) ;
103
- } ;
104
- } ;
151
+ return opts ;
152
+ }
0 commit comments