babel-plugin-try-import
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -9,3 +9,17 @@ module.exports = function Plugin(babel) { | ||
visitor: { | ||
VariableDeclaration(path, state) { | ||
CallExpression(path, state) { | ||
// const hasModuleB = hasModule('moduleB'); | ||
// | | | ||
// | | | ||
// \ / | ||
// \ / | ||
// \/ | ||
// (function () { | ||
// let temp = false; | ||
// try { | ||
// require.resolveWeak('moduleB'); | ||
// temp = true; | ||
// } catch (error) { } | ||
// return temp; | ||
// }()) | ||
const options = state.opts; | ||
@@ -18,21 +32,17 @@ const tryImport = options.tryImport || 'tryImport'; | ||
if (node.declarations[0] && node.declarations[0].init && node.declarations[0].init.callee) { | ||
const funcName = node.declarations[0].init.callee.name; | ||
if (node.callee) { | ||
const funcName = node.callee.name; | ||
if (funcName === tryImport) { | ||
const { | ||
name | ||
} = node.declarations[0].id; | ||
const args = node.declarations[0].init.arguments; | ||
const left = t.identifier(name); // 不带default | ||
// const right = t.callExpression(t.identifier("require"), [args[0]]) | ||
const [moduleName, defaultValue] = node.arguments; | ||
const left = t.identifier('temp'); // 不带default | ||
// const right = t.callExpression(t.identifier("require"), [moduleName]) | ||
const right = t.memberExpression(t.callExpression(t.identifier('require'), [args[0]]), t.identifier('default')); | ||
path.replaceWithMultiple([t.variableDeclaration('let', [t.variableDeclarator(t.identifier(name), args[1])]), t.tryStatement(t.blockStatement([t.expressionStatement(t.assignmentExpression('=', left, right))]), t.catchClause(t.identifier('error'), t.blockStatement([])))]); | ||
const right = t.memberExpression(t.callExpression(t.identifier('require'), [moduleName]), t.identifier('default')); | ||
path.replaceWith(t.expressionStatement(t.callExpression(t.functionExpression(null, [], t.blockStatement([// 定义临时变量 | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('temp'), defaultValue)]), t.tryStatement(t.blockStatement([t.expressionStatement(t.assignmentExpression('=', left, right))]), t.catchClause(t.identifier('error'), t.blockStatement([]))), t.returnStatement(t.identifier('temp'))])), []))); | ||
} else if (funcName === hasModule) { | ||
const { | ||
name | ||
} = node.declarations[0].id; | ||
const args = node.declarations[0].init.arguments; | ||
path.replaceWithMultiple([t.variableDeclaration('let', [t.variableDeclarator(t.identifier(name), t.identifier('false'))]), t.tryStatement(t.blockStatement([t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('require'), t.identifier('resolveWeak')), [args[0]])), t.expressionStatement(t.assignmentExpression('=', t.identifier(name), t.identifier('true')))]), t.catchClause(t.identifier('error'), t.blockStatement([])))]); | ||
const [moduleName] = node.arguments; | ||
path.replaceWith(t.expressionStatement(t.callExpression(t.functionExpression(null, [], t.blockStatement([// 定义临时变量 | ||
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('temp'), t.identifier('false'))]), t.tryStatement(t.blockStatement([t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('require'), t.identifier('resolveWeak')), [moduleName])), t.expressionStatement(t.assignmentExpression('=', t.identifier('temp'), t.identifier('true')))]), t.catchClause(t.identifier('error'), t.blockStatement([]))), t.returnStatement(t.identifier('temp'))])), []))); | ||
} | ||
@@ -39,0 +49,0 @@ } |
{ | ||
"name": "babel-plugin-try-import", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "try import module", | ||
@@ -24,4 +24,3 @@ "keywords": [ | ||
"prepublishOnly": "npm run clean && npm run build", | ||
"test": "jest", | ||
"test-coverage": "jest --coverage" | ||
"test": "jest" | ||
}, | ||
@@ -31,8 +30,17 @@ "devDependencies": { | ||
"@babel/core": "^7.2.2", | ||
"@babel/helper-plugin-test-runner": "^7.8.0", | ||
"babel-jest": "^24.9.0", | ||
"eslint": "^5.12.1", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-plugin-import": "^2.15.0", | ||
"jest": "^24.0.0", | ||
"jest": "^24.9.0", | ||
"rimraf": "^2.5.4" | ||
}, | ||
"jest": { | ||
"modulePathIgnorePatterns": [ | ||
"/test/fixtures/" | ||
], | ||
"testEnvironment": "node", | ||
"testRegex": "./test/.+\\.js$" | ||
} | ||
} | ||
} |
# babel-plugin-try-import | ||
try import module in webpack | ||
#### what will this plugin do ? | ||
#### what does this plugin do | ||
convert | ||
``` | ||
convert | ||
```javascript | ||
let react = tryImport('react'); | ||
``` | ||
into | ||
```javascript | ||
let react = function () { | ||
let temp; | ||
try { | ||
temp = require('react').default; | ||
} catch (error) {} | ||
return temp; | ||
}() | ||
``` | ||
let react; | ||
try { | ||
react = require('react').default; | ||
} catch (error) {} | ||
``` | ||
so that webpack won't throw error while `react` is not exist | ||
convert | ||
``` | ||
```javascript | ||
let hasReact = hasModule('react'); | ||
``` | ||
into | ||
``` | ||
let hasReact = false; | ||
```javascript | ||
let hasReact = function () { | ||
let temp = false; | ||
try { | ||
require.resolveWeak('react'); | ||
hasReact = true; | ||
} catch (error) {} | ||
try { | ||
require.resolveWeak('react'); | ||
temp = true; | ||
} catch (error) {} | ||
return temp; | ||
}() | ||
``` | ||
so that you can check if a module exists | ||
so that you can check if a module exists |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5251
49
53
9