Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JamsonChan committed Oct 11, 2024
1 parent 178e8fe commit 8c6361d
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions babel-plugin-expose-private-functions-and-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ module.exports = function ({types: t}) {
}
}

function createCheckExistIfStatement(identifiers) {
const nestedExpression = createNestedMemberExpression(identifiers);
return t.ifStatement(
t.unaryExpression('!', nestedExpression),
t.expressionStatement(
t.assignmentExpression(
'=',
nestedExpression,
t.objectExpression([])
)
)
);
}

return {
visitor: {
Program: {
Expand Down Expand Up @@ -67,38 +81,12 @@ module.exports = function ({types: t}) {
});

// insert check-exist if statements in the start of the file
for (let i = 0; i < dir.length - 1; i++) {
const nestedExpression = createNestedMemberExpression(dir.slice(i));
path.unshiftContainer('body',
t.ifStatement(
t.unaryExpression('!', nestedExpression),
t.expressionStatement(
t.assignmentExpression(
'=',
nestedExpression,
t.objectExpression([])
)
)
)
);
}
for (let i = 0; i < dir.length - 1; i++)
path.unshiftContainer('body', createCheckExistIfStatement(dir.slice(i)));

// append check-exist if statements in the end of the file
for (let i = dir.length - 2; i >= 0; i--) {
const nestedExpression = createNestedMemberExpression(dir.slice(i));
path.pushContainer('body',
t.ifStatement(
t.unaryExpression('!', nestedExpression),
t.expressionStatement(
t.assignmentExpression(
'=',
nestedExpression,
t.objectExpression([])
)
)
)
);
}
for (let i = dir.length - 2; i >= 0; i--)
path.pushContainer('body', createCheckExistIfStatement(dir.slice(i)));

// export private variable to `window.PACKAGE._._var = _var`
privateVars.forEach(v => {
Expand Down

0 comments on commit 8c6361d

Please sign in to comment.