/
index.js
35 lines (29 loc) · 869 Bytes
/
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
const template = require('babel-template');
const drupalBehavior = template(`Drupal.behaviors.NAME = {attach: function (context, settings) {BODY}};`);
module.exports = function (babel) {
const t = babel.types;
return {
inherits: require("babel-plugin-transform-strict-mode"),
visitor: {
Program: {
exit (path) {
if (!this.drupalBehavior) {
this.drupalBehavior = true;
function getRandomNumber(max) {
return Math.floor(Math.random() * Math.floor(max));
}
const randomNum = getRandomNumber(1000).toString();
const addBehavior = drupalBehavior({
NAME: t.identifier('name' + randomNum),
BODY: path.node.body
});
path.replaceWith(
t.program([addBehavior])
);
}
path.node.directives = [];
}
}
}
};
};