-
Notifications
You must be signed in to change notification settings - Fork 8
/
jsx-pragmatic.js
56 lines (50 loc) · 1.5 KB
/
jsx-pragmatic.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
/**
* Insert code to load a module when JSX is detected.
* This is supposed to load a module corresponding to the `pragma` option of
* the JSX transform.
*/
module.exports = function jsxPragmatic (babel) {
var
t = babel.types,
visitor = {};
function getPragmaImport (state) {
return t.importDeclaration(
[t.importSpecifier(
t.identifier(state.opts.import),
t.identifier(state.opts.export || "default")
)],
t.stringLiteral(state.opts.module)
);
}
// getPragmaImport
visitor = {
Program: {
exit: function (path, state) {
if (! state.get('jsxDetected')) return;
// Apparently it's now safe to do this even if Program begins with
// directives.
path.unshiftContainer('body', getPragmaImport(state));
},
// exit
},
// Program
// It seems pretty hokey that this'll work even if JSX has already been
// transformed, but apparently that's the basis for the whole plugin
// architecture for babel@6, so I'm rolling with it and maybe it'll make
// more sense to me once I understand it better.
JSXElement: function (path, state) {
state.set('jsxDetected', true);
},
// JSXElement
};
return {
pre: function () {
if (! (this.opts.module && this.opts.import)) {
throw new Error("babel-plugin-jsx-pragmatic: You must specify `module` and `import`");
}
},
inherits: require("babel-plugin-syntax-jsx"),
visitor: visitor,
};
};
// jsxPragmatic