{"id":19120,"library":"babel-plugin-optimize-objstr","title":"babel-plugin-optimize-obj-str","description":"Babel plugin that optimizes calls to `obj-str` by replacing them with equivalent unrolled string concatenation, resulting in ~1.8x performance improvement. Version 1.1.0 includes TypeScript definitions. Maintained by the same author as the `obj-str` library. Key differentiator: unlike other classname utilities, this plugin performs static analysis to inline only optimizable calls, preserving others by default. Released under MIT license.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/obj-str","tags":["javascript","babel","optimize","react","preact","classes","classname","classnames","object"],"install":[{"cmd":"npm install babel-plugin-optimize-objstr","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-optimize-objstr","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-optimize-objstr","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The plugin name in Babel config omits the 'babel-plugin-' prefix.","wrong":"plugins: ['babel-plugin-optimize-obj-str']","symbol":"plugin","correct":"plugins: ['optimize-obj-str']"},{"note":"Options must be wrapped in an array with the plugin name as the first element.","wrong":"plugins: ['optimize-obj-str', { strict: true }]","symbol":"options","correct":"plugins: [['optimize-obj-str', { strict: true }]]"}],"quickstart":{"code":"// babel.config.js\nmodule.exports = {\n  plugins: ['optimize-obj-str']\n};\n\n// input.js\nimport objstr from 'obj-str';\nconst classes = objstr({\n  'btn': true,\n  'btn-primary': isPrimary,\n  'disabled': isDisabled\n});\n\n// output.js\nconst classes = 'btn' + (isPrimary ? ' btn-primary' : '') + (isDisabled ? ' disabled' : '');","lang":"javascript","description":"Configures Babel to replace obj-str calls with optimized string concatenation for performance gains."},"warnings":[{"fix":"The leading space is intentional; most consumers like className work fine with it.","message":"Transformed output includes a leading space for truthy values, unlike raw objstr() which omits leading spaces. Ensure consumers handle leading spaces.","severity":"gotcha","affected_versions":"*"},{"fix":"Avoid duplicate keys in obj-str calls, or use a consistent condition for duplicate keys.","message":"Duplicate property names with identical keys are resolved by the last value, but the plugin uses the first key's literal string. If duplicate keys have different conditions, the behavior may be unexpected.","severity":"gotcha","affected_versions":"<1.1.0"},{"fix":"Consider the trade-off; for large number of properties, using obj-str directly may be smaller.","message":"The plugin may slightly increase bundle size if many (e.g., >100) conditional properties are used. It is intended for performance, not size reduction.","severity":"gotcha","affected_versions":"*"},{"fix":"Use strict mode only if you have full control over all obj-str calls and want to enforce optimization.","message":"The option 'strict' defaults to false. Setting strict: true makes the plugin throw errors for non-optimizable calls, which may break builds.","severity":"deprecated","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"npm install --save-dev babel-plugin-optimize-obj-str","cause":"Forgot to install the plugin.","error":"Module not found: Error: Can't resolve 'babel-plugin-optimize-obj-str'"},{"fix":"Ensure the first argument to objstr() is an object literal with string literal keys. Non-optimizable calls are preserved by default.","cause":"Passing an argument that is not an object literal (e.g., identifier, spread).","error":"TypeError: Cannot read property 'replace' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}