babel-plugin-define-variables
Advanced tools
+32
-10
| "use strict"; | ||
| var hash = require('hash-sum'); | ||
| var _require = require('./utils'), | ||
@@ -7,6 +9,23 @@ getConstCache = _require.getConstCache, | ||
| expr2str = _require.expr2str, | ||
| getPackage = _require.getPackage; | ||
| getPackage = _require.getPackage, | ||
| mergeObject = _require.mergeObject; | ||
| function createHash(filename, pkg) { | ||
| if (pkg) filename = "".concat(pkg.name, "!").concat(filename); | ||
| return "".concat(hash(filename.replace(/\\/g, '/'))); | ||
| } | ||
| module.exports = function (_ref) { | ||
| var t = _ref.types; | ||
| var options = { | ||
| builtIns: { | ||
| filename: true, | ||
| filehash: true, | ||
| dirname: true, | ||
| now: true, | ||
| timestamp: true, | ||
| packagename: true, | ||
| packageversion: true | ||
| } | ||
| }; | ||
@@ -26,4 +45,4 @@ function IdentifierVisitor(path, _ref2) { | ||
| while (parentPath && parentPath.node.type === 'MemberExpression') { | ||
| if (path.node === parentPath.node.property) identifier = "".concat(expr2str(parentPath.node.object), ".").concat(identifier); | ||
| while (parentPath && parentPath.node.type === 'MemberExpression' && path.node === parentPath.node.property) { | ||
| identifier = "".concat(expr2str(parentPath.node.object), ".").concat(identifier); | ||
| path = parentPath; | ||
@@ -33,3 +52,3 @@ parentPath = parentPath.parentPath; | ||
| } else if (parent.type === 'CallExpression' && expr2str(parent.callee) === identifier) { | ||
| if (identifier !== '__packageversion' || !parent.arguments.length || !t.isStringLiteral(parent.arguments[0])) return; | ||
| if (identifier !== '__packageversion__' || !parent.arguments.length || !t.isStringLiteral(parent.arguments[0])) return; | ||
| path = path.parentPath; | ||
@@ -47,16 +66,18 @@ var packageName = expr2str(path.node.arguments[0]); | ||
| if (identifier === '__filename') { | ||
| if (options.builtIns.filename && identifier === '__filename__') { | ||
| path.replaceWith(t.stringLiteral(cache.filename)); | ||
| } else if (identifier === '__dirname') { | ||
| } else if (options.builtIns.dirname && identifier === '__dirname__') { | ||
| path.replaceWith(t.stringLiteral(cache.dirname)); | ||
| } else if (identifier === '__now') { | ||
| } else if (options.builtIns.now && identifier === '__now__') { | ||
| path.replaceWith(t.stringLiteral(cache.now)); | ||
| } else if (identifier === '__timestamp') { | ||
| } else if (options.builtIns.timestamp && identifier === '__timestamp__') { | ||
| path.replaceWith(t.numericLiteral(Date.now())); | ||
| } else if (options.builtIns.filehash && identifier === '__filehash__') { | ||
| path.replaceWith(t.stringLiteral(createHash(cache.filename, cache.pkg))); | ||
| } else if (defines[identifier] !== undefined) { | ||
| path.replaceWith(var2Expression(defines[identifier])); | ||
| } else if (cache.pkg) { | ||
| if (identifier === '__packagename') { | ||
| if (options.builtIns.packagename && identifier === '__packagename__') { | ||
| path.replaceWith(t.stringLiteral(cache.pkg.name)); | ||
| } else if (identifier === '__packageversion') { | ||
| } else if (options.builtIns.packageversion && identifier === '__packageversion__') { | ||
| path.replaceWith(t.stringLiteral(cache.pkg.version)); | ||
@@ -74,2 +95,3 @@ } | ||
| var cache = getConstCache(filename); | ||
| mergeObject(options, opts); | ||
| path.traverse({ | ||
@@ -76,0 +98,0 @@ Identifier: IdentifierVisitor |
+44
-1
@@ -285,2 +285,44 @@ "use strict"; | ||
| var _toString = Object.prototype.toString; | ||
| function isPlainObject(obj) { | ||
| return _toString.call(obj) === '[object Object]'; | ||
| } | ||
| function mergeObject(target) { | ||
| function _mergeObject(target, source, copiedObjects) { | ||
| if (!target) return target; | ||
| if (!isPlainObject(source)) return target; | ||
| copiedObjects.push({ | ||
| source: source, | ||
| target: target | ||
| }); | ||
| Object.keys(source).forEach(function (key) { | ||
| var v = source[key]; | ||
| if (isPlainObject(v)) { | ||
| var copied = copiedObjects.find(function (c) { | ||
| return c.target === v; | ||
| }); | ||
| if (copied) target[key] = copied.target;else { | ||
| var w = target[key]; | ||
| if (!isPlainObject(w)) w = target[key] = {}; | ||
| _mergeObject(w, v, copiedObjects); | ||
| } | ||
| } else target[key] = v; | ||
| }); | ||
| return target; | ||
| } | ||
| var ret = target; | ||
| var copiedObjects = []; | ||
| for (var i = 1; i < arguments.length; i++) { | ||
| _mergeObject(ret, arguments[i], copiedObjects); | ||
| } | ||
| return ret; | ||
| } | ||
| module.exports = { | ||
@@ -293,3 +335,4 @@ getConstCache: getConstCache, | ||
| getConfigPath: getConfigPath, | ||
| getPackage: getPackage | ||
| getPackage: getPackage, | ||
| mergeObject: mergeObject | ||
| }; |
+3
-3
| { | ||
| "name": "babel-plugin-define-variables", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "description": "a babel define vars that like webpack.DefinePlugin", | ||
@@ -30,5 +30,5 @@ "keywords": [ | ||
| "dependencies": { | ||
| "hash-sum": "^2.0.0" | ||
| }, | ||
| "peerDependencies": { | ||
| }, | ||
| "peerDependencies": {}, | ||
| "devDependencies": { | ||
@@ -35,0 +35,0 @@ "@babel/cli": "^7.2.3", |
+60
-9
@@ -28,2 +28,11 @@ # babel-plugin-define-variables | ||
| 'process.env.NODE_ENV': process.env.NODE_ENV, | ||
| }, | ||
| builtIns: { | ||
| // filename: false, | ||
| // filehash: false, | ||
| // dirname: false, | ||
| // now: false, | ||
| // timestamp: false, | ||
| // packagename: false, | ||
| // packageversion: false | ||
| } | ||
@@ -39,23 +48,27 @@ } | ||
| - __filename | ||
| - __filename__ | ||
| the filename of code file than relative of `package.json` path that current project. | ||
| the filename of code file that relative of `package.json` path that current project. | ||
| - __dirname | ||
| - __filehash__ | ||
| the filename of code file | ||
| the dirname of code file than relative of `package.json` path that current project. | ||
| - __dirname__ | ||
| - __now | ||
| the dirname of code file that relative of `package.json` path that current project. | ||
| - __now__ | ||
| the time that build moment. format: 'yyyy-MM-dd hh:mm:ss' | ||
| - __timestamp | ||
| - __timestamp__ | ||
| the timestamp that build moment. | ||
| - __packagename | ||
| - __packagename__ | ||
| the package name of this project. | ||
| - __packageversion | ||
| - __packageversion__ | ||
@@ -65,4 +78,42 @@ the package version of this project. you can also use like this: | ||
| ```js | ||
| __packageversion('react'); | ||
| __packageversion__('react'); | ||
| ``` | ||
| that you will get version of react; | ||
| ## demo | ||
| src/index.js: | ||
| ```js | ||
| function test() { | ||
| console.log('__filename__', __filename__); | ||
| console.log('__filehash__', __filehash__); | ||
| console.log('__dirname__', __dirname__); | ||
| console.log('__now__', __now__); | ||
| console.log('__timestamp__', __timestamp__); | ||
| console.log('__packagename__', __packagename__); | ||
| console.log('__packageversion__', __packageversion__); | ||
| console.log('__packageversion__', __packageversion__('')); | ||
| console.log('__packageversion__', __packageversion__('@babel/cli')); | ||
| console.log('process.env.BUILD_ENV', process.env.BUILD_ENV); | ||
| __packageversion__.split('.'); | ||
| } | ||
| ``` | ||
| output/index.js: | ||
| ```js | ||
| function test() { | ||
| console.log('__filename__', "/demo/src/test1.js"); | ||
| console.log('__filehash__', "d7bfcc4a"); | ||
| console.log('__dirname__', "/demo/src"); | ||
| console.log('__now__', "2020-12-03 18:43:46"); | ||
| console.log('__timestamp__', 1606992227198); | ||
| console.log('__packagename__', "babel-plugin-define-variables"); | ||
| console.log('__packageversion__', "0.0.2"); | ||
| console.log('__packageversion__', ""); | ||
| console.log('__packageversion__', "7.6.4"); | ||
| console.log('process.env.BUILD_ENV', "test"); | ||
| "0.0.2".split('.'); | ||
| } | ||
| ``` |
+34
-11
| const hash = require('hash-sum'); | ||
| const { | ||
@@ -6,6 +7,23 @@ getConstCache, | ||
| expr2str, | ||
| getPackage | ||
| getPackage, | ||
| mergeObject | ||
| } = require('./utils'); | ||
| function createHash(filename, pkg) { | ||
| if (pkg) filename = `${pkg.name}!${filename}`; | ||
| return `${hash(filename.replace(/\\/g, '/'))}`; | ||
| } | ||
| module.exports = function ({ types: t }) { | ||
| const options = { | ||
| builtIns: { | ||
| filename: true, | ||
| filehash: true, | ||
| dirname: true, | ||
| now: true, | ||
| timestamp: true, | ||
| packagename: true, | ||
| packageversion: true | ||
| } | ||
| }; | ||
| function IdentifierVisitor(path, { opts, cache }) { | ||
@@ -21,4 +39,5 @@ let parent = path.parent; | ||
| let parentPath = path.parentPath; | ||
| while (parentPath && parentPath.node.type === 'MemberExpression') { | ||
| if (path.node === parentPath.node.property) identifier = `${expr2str(parentPath.node.object)}.${identifier}`; | ||
| while (parentPath && parentPath.node.type === 'MemberExpression' | ||
| && path.node === parentPath.node.property) { | ||
| identifier = `${expr2str(parentPath.node.object)}.${identifier}`; | ||
| path = parentPath; | ||
@@ -28,3 +47,3 @@ parentPath = parentPath.parentPath; | ||
| } else if (parent.type === 'CallExpression' && expr2str(parent.callee) === identifier) { | ||
| if (identifier !== '__packageversion' | ||
| if (identifier !== '__packageversion__' | ||
| || !parent.arguments.length | ||
@@ -41,16 +60,18 @@ || !t.isStringLiteral(parent.arguments[0])) return; | ||
| const defines = (opts && opts.defines) || {}; | ||
| if (identifier === '__filename') { | ||
| if (options.builtIns.filename && identifier === '__filename__') { | ||
| path.replaceWith(t.stringLiteral(cache.filename)); | ||
| } else if (identifier === '__dirname') { | ||
| } else if (options.builtIns.dirname && identifier === '__dirname__') { | ||
| path.replaceWith(t.stringLiteral(cache.dirname)); | ||
| } else if (identifier === '__now') { | ||
| } else if (options.builtIns.now && identifier === '__now__') { | ||
| path.replaceWith(t.stringLiteral(cache.now)); | ||
| } else if (identifier === '__timestamp') { | ||
| } else if (options.builtIns.timestamp && identifier === '__timestamp__') { | ||
| path.replaceWith(t.numericLiteral(Date.now())); | ||
| } else if (defines[identifier] !== undefined) { | ||
| } else if (options.builtIns.filehash && identifier === '__filehash__') { | ||
| path.replaceWith(t.stringLiteral(createHash(cache.filename, cache.pkg))); | ||
| } else if (defines[identifier] !== undefined) { | ||
| path.replaceWith(var2Expression(defines[identifier])); | ||
| } else if (cache.pkg) { | ||
| if (identifier === '__packagename') { | ||
| if (options.builtIns.packagename && identifier === '__packagename__') { | ||
| path.replaceWith(t.stringLiteral(cache.pkg.name)); | ||
| } else if (identifier === '__packageversion') { | ||
| } else if (options.builtIns.packageversion && identifier === '__packageversion__') { | ||
| path.replaceWith(t.stringLiteral(cache.pkg.version)); | ||
@@ -73,2 +94,4 @@ } | ||
| mergeObject(options, opts); | ||
| path.traverse({ | ||
@@ -75,0 +98,0 @@ Identifier: IdentifierVisitor |
+34
-1
@@ -214,3 +214,35 @@ const t = require('@babel/types'); | ||
| const _toString = Object.prototype.toString; | ||
| function isPlainObject(obj) { | ||
| return _toString.call(obj) === '[object Object]'; | ||
| } | ||
| function mergeObject(target) { | ||
| function _mergeObject(target, source, copiedObjects) { | ||
| if (!target) return target; | ||
| if (!isPlainObject(source)) return target; | ||
| copiedObjects.push({ source, target }); | ||
| Object.keys(source).forEach(key => { | ||
| let v = source[key]; | ||
| if (isPlainObject(v)) { | ||
| let copied = copiedObjects.find(c => c.target === v); | ||
| if (copied) target[key] = copied.target; | ||
| else { | ||
| let w = target[key]; | ||
| if (!isPlainObject(w)) w = target[key] = {}; | ||
| _mergeObject(w, v, copiedObjects); | ||
| } | ||
| } else target[key] = v; | ||
| }); | ||
| return target; | ||
| } | ||
| let ret = target; | ||
| let copiedObjects = []; | ||
| for (let i = 1; i < arguments.length; i++) _mergeObject(ret, arguments[i], copiedObjects); | ||
| return ret; | ||
| } | ||
| module.exports = { | ||
@@ -223,3 +255,4 @@ getConstCache, | ||
| getConfigPath, | ||
| getPackage | ||
| getPackage, | ||
| mergeObject | ||
| }; |
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
30697
19.77%680
18.26%117
77.27%6
-14.29%1
Infinity%+ Added
+ Added