@optimize-lodash/transform
Advanced tools
Comparing version 3.0.4 to 3.0.5
@@ -13,3 +13,3 @@ "use strict"; | ||
exports.UNCHANGED = null; | ||
function transform({ code, id, parse, warn, useLodashEs, appendDotJs = true, }) { | ||
function transform({ code, id, parse, warn = console.error, useLodashEs, appendDotJs = true, }) { | ||
// before parsing, check if we can skip the whole file | ||
@@ -47,20 +47,26 @@ if (!code.includes("lodash")) { | ||
} | ||
// transform specific "lodash" and "lodash/fp" imports such as: | ||
// import { isNil } from "lodash"; | ||
if ((0, guards_1.isImportSpecifierArray)(node.specifiers)) { | ||
magicString = magicString !== null && magicString !== void 0 ? magicString : new magic_string_1.default(code); | ||
// modify | ||
const imports = useLodashEs | ||
? (0, lodash_specifiers_to_es_1.lodashSpecifiersToEs)(node.source.value, node.specifiers) | ||
: (0, lodash_specifiers_to_cjs_1.lodashSpecifiersToCjs)(node.source.value, node.specifiers, appendDotJs); | ||
// write | ||
magicString.overwrite(node.start, node.end, imports.join("\n")); | ||
// no need to dig deeper | ||
// if it's not in the form `import { ... } from "lodash"`, we can't do anything | ||
if (!(0, guards_1.isImportSpecifierArray)(node.specifiers)) { | ||
// this behavior differs from babel-plugin-lodash | ||
// which does optimize non-specific imports | ||
warn(`Detected a default lodash or lodash/fp import within ${id} on line ${(_c = (_b = (_a = node.loc) === null || _a === void 0 ? void 0 : _a.start) === null || _b === void 0 ? void 0 : _b.line) !== null && _c !== void 0 ? _c : "unknown"}.\nThis import cannot be optimized by optimize-lodash-imports.`); | ||
this.skip(); | ||
return; | ||
} | ||
else if (warn !== undefined) { | ||
// help end-users benefit from this plugin (this behavior differs from | ||
// babel-plugin-lodash which does optimize non-specific imports) | ||
warn(`Detected a default lodash or lodash/fp import within ${id} on line ${(_c = (_b = (_a = node.loc) === null || _a === void 0 ? void 0 : _a.start) === null || _b === void 0 ? void 0 : _b.line) !== null && _c !== void 0 ? _c : "unknown"}.\nThis import cannot be optimized by optimize-lodash-imports.`); | ||
// we can't optimize chain() -- it relies on a bare import from `lodash` | ||
// to make various functions available | ||
if (hasChainImport(node.specifiers)) { | ||
warn(`Detected an import of chain() from lodash within ${id}\nchain() is incompatible with optimize-lodash-imports`); | ||
this.skip(); | ||
return; | ||
} | ||
magicString = magicString !== null && magicString !== void 0 ? magicString : new magic_string_1.default(code); | ||
// modify | ||
const imports = useLodashEs | ||
? (0, lodash_specifiers_to_es_1.lodashSpecifiersToEs)(node.source.value, node.specifiers) | ||
: (0, lodash_specifiers_to_cjs_1.lodashSpecifiersToCjs)(node.source.value, node.specifiers, appendDotJs); | ||
// write | ||
magicString.overwrite(node.start, node.end, imports.join("\n")); | ||
// no need to dig deeper | ||
this.skip(); | ||
}, | ||
@@ -81,2 +87,11 @@ }); | ||
exports.transform = transform; | ||
/** | ||
* Search the import for a reference to `chain()`. Does not check | ||
* if the import is from `lodash` or another package. | ||
* | ||
* @returns true if `chain()` is imported | ||
*/ | ||
function hasChainImport(specifiers) { | ||
return specifiers.some(({ imported }) => imported.name === "chain"); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@optimize-lodash/transform", | ||
"version": "3.0.4", | ||
"version": "3.0.5", | ||
"description": "Rewrites lodash imports in a given source file to be specific.", | ||
@@ -36,8 +36,8 @@ "keywords": [ | ||
"@types/jest": "29.5.4", | ||
"@types/lodash": "4.14.199", | ||
"@types/lodash": "4.14.202", | ||
"@types/node": "12.20.55", | ||
"@typescript-eslint/eslint-plugin": "6.7.0", | ||
"@typescript-eslint/parser": "6.7.0", | ||
"@typescript-eslint/eslint-plugin": "6.7.5", | ||
"@typescript-eslint/parser": "6.7.5", | ||
"acorn": "8.12.1", | ||
"depcheck": "1.4.6", | ||
"depcheck": "1.4.7", | ||
"eslint": "8.51.0", | ||
@@ -44,0 +44,0 @@ "eslint-config-prettier": "9.0.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17837
187