Scopedify: Browserify CSS Scoping
raw JSON →Scopedify is a CSS bundler designed specifically for the Browserify ecosystem, offering a solution for modular and scoped CSS. At version 1.1.1, released in 2016, and with no updates since, it is considered an abandoned project. It functions by transforming inline CSS or external `.css` files during the Browserify build process, applying unique attribute-based namespaces (e.g., `_scope_a68eaa6a`) to both CSS selectors and corresponding HTML elements. This mechanism aims to prevent style conflicts and pollution by isolating component-specific styles, conceptually similar to modern CSS Modules but tailored for Browserify. Key differentiators include its tight integration with Browserify transforms, support for nested and multiple scopes, and the ability to consume CSS from npm packages, albeit without default namespacing for those packages. Its reliance on Browserify and an older approach to CSS scoping means it is not directly compatible with contemporary bundlers like Webpack, Rollup, or Vite.
Common errors
error Error: Cannot find module 'scopedify/transform' from '...' or 'ReferenceError: css is not defined' ↓
-t scopedify/transform flag for CLI usage, or configure it correctly programmatically in your build script. Additionally, verify that your JavaScript file includes const css = require('scopedify') at the top. error My npm package CSS is not getting scoped, leading to visual inconsistencies. ↓
error My scoped styles are unexpectedly overridden by other styles on the page. ↓
scopedify where necessary to ensure they take precedence. Also, consider using initial or unset for inherited properties to explicitly reset them if they are causing unintended side effects. Warnings
breaking Scopedify is an abandoned project. Its last update was in 2016 (v1.1.1). It is not actively maintained and may have unaddressed bugs, security vulnerabilities, or compatibility issues with newer Node.js versions or browser features. ↓
gotcha CSS from npm packages will *not* be namespaced by default. This can lead to unexpected global styling or conflicts if those packages are not themselves designed for strict scoping, especially if they use common class names. ↓
gotcha Scopedify is tightly coupled with Browserify. It is not compatible with modern bundlers like Webpack, Rollup, or Vite without significant custom integration (e.g., using Browserify as a sub-process) or a complete change in build tooling. ↓
gotcha CSS inheritance functions normally within scoped elements. Properties like `color` or `font-size` will inherit from parent elements in the DOM, potentially overriding your scoped styles if not explicitly reset or overridden with higher specificity. ↓
gotcha Scopedify relies on attribute selectors (e.g., `[data-scope_a1b2c3d4]`) for its scoping mechanism. While effective, these can sometimes have minor performance implications on very large or deeply nested DOM trees compared to class-based selectors, or be overridden by more specific selectors (e.g., an `!important` rule, ID selectors) if not careful with CSS architecture. ↓
Install
npm install scopedify yarn add scopedify pnpm add scopedify Imports
- css (template literal) wrong
import css from 'scopedify'correctconst css = require('scopedify') - css (external file) wrong
import './my-styles.css' // does not apply scopedify transformcorrectconst css = require('scopedify')('./my-styles.css') - Browserify Transform wrong
require('scopedify/transform') // incorrect usage in application codecorrectbrowserify -t scopedify/transform index.js > bundle.js
Quickstart
const css = require('scopedify');
const html = require('bel'); // 'bel' is a common dependency for creating DOM elements in this ecosystem
// Define inline CSS using a tagged template literal
const scope = css`
.base h1 {
text-align: center;
color: #333;
background-color: #f9f9f9;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
}
.base p {
font-size: 1.1em;
line-height: 1.6;
margin-top: 10px;
}
.highlight {
font-weight: bold;
color: #007bff;
}
`;
// Apply the scoped CSS to an HTML tree constructed with 'bel'
const tree = scope(html`
<section class='base highlight'>
<h1>My beautiful, centered title</h1>
<p>This paragraph is part of the <span class="highlight">scoped section</span>. Its styles are isolated by Scopedify.</p>
<div>
<small>Additional content here also inherits the scope.</small>
</div>
</section>
`);
// Append the rendered HTML to the document body
document.body.appendChild(tree);
// To compile this with Browserify from the command line:
// $ browserify -t scopedify/transform index.js > bundle.js
// Then, include 'bundle.js' in your HTML file to see the effect.