@hqjs/babel-plugin-support-nodejs-globals
Advanced tools
Comparing version 0.0.1 to 0.0.2
89
index.js
@@ -7,4 +7,6 @@ const path = require('path'); | ||
if (source.value === 'buffer') required.Buffer = false; | ||
else if (source.value === 'process') required.process = false; | ||
else if (source.value === 'setimmediate') required.setimmediate = false; | ||
else if (source.value === 'process' || source.value === 'process/browser.js') { | ||
source.value = 'process/browser.js'; | ||
required.process = false; | ||
} | ||
}, | ||
@@ -16,4 +18,6 @@ CallExpression(nodePath) { | ||
if (source.value === 'buffer') required.Buffer = false; | ||
else if (source.value === 'process') required.process = false; | ||
else if (source.value === 'setimmediate') required.setimmediate = false; | ||
else if (source.value === 'process' || source.value === 'process/browser.js') { | ||
source.value = 'process/browser.js'; | ||
required.process = false; | ||
} | ||
} | ||
@@ -27,3 +31,2 @@ }); | ||
process: true, | ||
setimmediate: true, | ||
}; | ||
@@ -35,3 +38,2 @@ return { | ||
required.process = true; | ||
required.setimmediate = true; | ||
programPath = nodePath; | ||
@@ -45,32 +47,59 @@ nodePath.traverse(importVisitor(t, required)); | ||
if (node.name === '__filename' && !nodePath.scope.hasBinding('__filename')) { | ||
if ( | ||
nodePath.parentPath.isMemberExpression() && | ||
(nodePath.parent.object.name === 'global' || nodePath.parent.object.name === 'globalThis') | ||
) nodePath.parentPath.replaceWith(nodePath); | ||
nodePath.replaceWith(t.stringLiteral(filename)); | ||
} else if (node.name === '__dirname' && !nodePath.scope.hasBinding('__dirname')) { | ||
if ( | ||
nodePath.parentPath.isMemberExpression() && | ||
(nodePath.parent.object.name === 'global' || nodePath.parent.object.name === 'globalThis') | ||
) nodePath.parentPath.replaceWith(nodePath); | ||
nodePath.replaceWith(t.stringLiteral(dirname)); | ||
} else if (node.name === 'Buffer' && !nodePath.scope.hasBinding('Buffer') && required.Buffer) { | ||
programPath.node.body.unshift(t.importDeclaration( | ||
[t.importSpecifier(node, node)], | ||
t.stringLiteral('buffer') | ||
)); | ||
required.Buffer = false; | ||
} else if (node.name === 'process' && !nodePath.scope.hasBinding('process') && required.process) { | ||
programPath.node.body.unshift(t.importDeclaration( | ||
[t.importDefaultSpecifier(node)], | ||
t.stringLiteral('process') | ||
)); | ||
required.process = false; | ||
} else if (node.name === 'Buffer' && !nodePath.scope.hasBinding('Buffer')) { | ||
const id = nodePath.scope.generateUidIdentifierBasedOnNode('globals'); | ||
if ( | ||
nodePath.parentPath.isMemberExpression() && | ||
(nodePath.parent.object.name === 'global' || nodePath.parent.object.name === 'globalThis') | ||
) nodePath.parentPath.replaceWith(nodePath); | ||
if ( | ||
required.Buffer && | ||
( | ||
!nodePath.parentPath.isMemberExpression() || | ||
nodePath.parent.object === node | ||
) | ||
) { | ||
programPath.node.body.unshift( | ||
t.importDeclaration( | ||
[t.importDefaultSpecifier(id)], | ||
t.stringLiteral('buffer') | ||
), | ||
t.variableDeclaration( | ||
'const', | ||
[t.variableDeclarator(node, id)] | ||
) | ||
); | ||
required.Buffer = false; | ||
} | ||
} else if (node.name === 'process' && !nodePath.scope.hasBinding('process')) { | ||
if ( | ||
nodePath.parentPath.isMemberExpression() && | ||
(nodePath.parent.object.name === 'global' || nodePath.parent.object.name === 'globalThis') | ||
) nodePath.parentPath.replaceWith(nodePath); | ||
if ( | ||
required.process && | ||
( | ||
!nodePath.parentPath.isMemberExpression() || | ||
nodePath.parent.object === node | ||
) | ||
) { | ||
programPath.node.body.unshift(t.importDeclaration( | ||
[t.importDefaultSpecifier(node)], | ||
t.stringLiteral('process/browser.js') | ||
)); | ||
required.process = false; | ||
} | ||
} else if (node.name === 'global' && !nodePath.scope.hasBinding('global')) { | ||
node.name = 'globalThis'; | ||
} | ||
}, | ||
CallExpression(nodePath) { | ||
const {node} = nodePath; | ||
if ( | ||
(t.isIdentifier(node.callee, {name: 'setImmediate'}) || t.isIdentifier(node.callee, {name: 'clearImmediate'})) && | ||
required.setimmediate | ||
) { | ||
programPath.node.body.unshift(t.importDeclaration( | ||
[], | ||
t.stringLiteral('setimmediate') | ||
)); | ||
} | ||
} | ||
@@ -77,0 +106,0 @@ }, |
{ | ||
"name": "@hqjs/babel-plugin-support-nodejs-globals", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Support nodejs globals", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# http://hqjs.org | ||
Support-nodejs globals | ||
* Buffer - adds `import Buffer from 'buffer';` on top of the program [npm buffer package](https://www.npmjs.com/package/buffer) should be installed as project dependency | ||
* process - adds `iport process from 'process';` on top of the program [npm process package](https://www.npmjs.com/package/process) should be installed as a project dependency | ||
* setImmediate/clearImmediate - adds `import 'setimmediate';` on top of the program [npm setimmediate package](https://www.npmjs.com/package/setimmediate) should be installed as a project dependency | ||
* global - changes to `globalThis` (which has polyfill in core-js) | ||
* Buffer - adds `import Buffer from 'buffer';` on top of the program [npm buffer package](https://www.npmjs.com/package/buffer) | ||
* process - adds `iport process from 'process';` on top of the program [npm process package](https://www.npmjs.com/package/process) | ||
* __dirname - substitutes exact value | ||
* __filename - substitutes exact value | ||
* global - changes to `globalThis` (which has polyfill in core-js) works in combination with globals above | ||
@@ -10,0 +9,0 @@ # Installation |
14671
101
20