Skip to content

Commit

Permalink
ZK-4928: expose widget private functions and variables as public ones
Browse files Browse the repository at this point in the history
  • Loading branch information
JamsonChan committed Aug 25, 2023
1 parent 054d7e4 commit 3e1b0e8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .browserslistrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
defaults
ie >= 11
not ie > 0
not ie_mob > 0
not dead
87 changes: 87 additions & 0 deletions babel-plugin-expose-private-functions-and-variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// eslint-disable-next-line no-undef
module.exports = function ({types: t}) {

function createNestedMemberExpression(identifiers) {
if (identifiers.length === 1) {
return t.identifier(identifiers[0]);
} else {
const [head, ...tail] = identifiers;
return t.memberExpression(
createNestedMemberExpression(tail),
t.identifier(head)
);
}
}

return {
visitor: {
Program: {
exit(path) {
let dir = this.file.opts.filename.replace(/-/g, '_').split('/');
const jsLoc = dir.findIndex(x => x === 'js'),
file = dir[dir.length - 1],
exports = {};

// pass if [not in js folder] or [not ts file] or [is global.d.ts] or [is index.ts]
if (jsLoc === -1 || !file.endsWith('ts') || file === 'global.d.ts' || file === 'index.ts') return;

// simplify whole dir
dir = dir.slice(jsLoc + 1);
dir[dir.length - 1] = file.replace('.ts', '');

// sort order for follow-up
dir.unshift('window');
dir.push('_');
dir.reverse();

// visit all nodes
path.node.body.forEach((node, index) => {
if (t.isVariableDeclaration(node)) {
node.declarations.forEach((declaration) => {
if (t.isIdentifier(declaration.id)) {
exports[declaration.id.name] = declaration.id.name;
}
});
} else if (t.isFunctionDeclaration(node) && t.isIdentifier(node.id)) {
exports[node.id.name] = node.id.name;
}
});

// add check-exist if statements
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([])
)
)
)
);
}

// export all global variables and functions
path.pushContainer('body',
// window.x.x.x._ = {...}
t.expressionStatement(
t.assignmentExpression(
'=',
createNestedMemberExpression(dir),
t.objectExpression(
Object.entries(exports).map(([k, v]) => {
return t.objectProperty(t.identifier(k), t.identifier(v));
})
)
)
)
);

}
}
}
};
};
29 changes: 15 additions & 14 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
module.exports = function (api) {
api.cache(true);

return {
presets: [
['@babel/preset-env', {'modules': 'commonjs',
"exclude": ['@babel/plugin-transform-typeof-symbol']}],
],
plugins: [
return {
presets: [
['@babel/preset-env', {'modules': 'commonjs',
"exclude": ['@babel/plugin-transform-typeof-symbol']}],
],
plugins: [
'./babel-plugin-expose-private-functions-and-variables',
['@babel/plugin-transform-modules-commonjs', {
loose: true,
strictMode: false,
}],
['@babel/plugin-transform-runtime', {'corejs': 3}],
["@babel/plugin-proposal-decorators", { "legacy": true }],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
],
loose: true,
strictMode: false,
}],
['@babel/plugin-transform-runtime', {'corejs': 3}],
["@babel/plugin-proposal-decorators", { "legacy": true }],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
],
overrides: [{
test: /[\\/]ext[\\/]//* treat as script for 3rd-party library */,
sourceType: 'script'
Expand Down
1 change: 1 addition & 0 deletions zkdoc/release-note
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ZK 10.0.0
ZK-5533: Add all new HTML5 tags in ZK 10 ZHTML
ZK-5049: Deprecate org.zkoss.zk.ui.uuidRecycle.disabled in ZK10
ZK-5437: Upgrade WCAG with the lighthouse 10.x detection tools
ZK-4928: expose widget private functions and variables as public ones

* Bugs
ZK-5089: AfterSizeEvent doesn't return a correct size of a Window component
Expand Down

0 comments on commit 3e1b0e8

Please sign in to comment.