{"id":13991770,"url":"https://github.com/css-modules/css-modulesify","last_synced_at":"2025-04-13T00:44:41.614Z","repository":{"id":33147073,"uuid":"36786893","full_name":"css-modules/css-modulesify","owner":"css-modules","description":"A browserify plugin to load CSS Modules","archived":false,"fork":false,"pushed_at":"2019-11-02T09:18:06.000Z","size":123,"stargazers_count":402,"open_issues_count":30,"forks_count":47,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-13T00:44:35.981Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/css-modules.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-03T07:22:26.000Z","updated_at":"2025-03-30T00:41:55.000Z","dependencies_parsed_at":"2022-08-17T20:55:24.051Z","dependency_job_id":null,"html_url":"https://github.com/css-modules/css-modulesify","commit_stats":null,"previous_names":[],"tags_count":46,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/css-modules%2Fcss-modulesify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/css-modules%2Fcss-modulesify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/css-modules%2Fcss-modulesify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/css-modules%2Fcss-modulesify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/css-modules","download_url":"https://codeload.github.com/css-modules/css-modulesify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650419,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-09T14:01:34.590Z","updated_at":"2025-04-13T00:44:41.592Z","avatar_url":"https://github.com/css-modules.png","language":"JavaScript","readme":"# css-modulesify\n\n[![Build Status](https://travis-ci.org/css-modules/css-modulesify.svg?branch=master)](https://travis-ci.org/css-modules/css-modulesify)\n\nA browserify plugin to load [CSS Modules].\n\n[CSS Modules]: https://github.com/css-modules/css-modules\n\n**Please note that this is still highly experimental.**\n\n## Why CSS Modules?\n\nNormally you need to use a strict naming convention like BEM to ensure that one component's CSS doesn't collide with another's. CSS Modules are locally scoped, which allows you to use names that are meaningful within the context of the component, without any danger of name collision.\n\nRead Mark Dalgleish's excellent [\"End of Global CSS\"](https://medium.com/seek-ui-engineering/the-end-of-global-css-90d2a4a06284) and check out [css-modules](https://github.com/css-modules/css-modules) for more context.\n\n## Getting started\n\nFirst install the package: `npm install --save css-modulesify`\n\nThen you can use it as a browserify plugin, eg: `browserify -p [ css-modulesify -o dist/main.css ] example/index.js`\n\nInside `example/index.js` you can now load css into your scripts.  When you do `var box1 = require('./box1.css')`, `box1` will be an object to lookup the localized classname for one of the selectors in that file.\n\nSo to apply a class to an element you can do something like:\n\n```js\nvar styles = require('./styles.css');\nvar div = `\u003cdiv class=\"${styles.inner}\"\u003e...\u003c/div\u003e`;\n```\n\nThe generated css will contain locally-scoped versions of any css you have `require`'d, and will be written out to the file you specify in the `--output` or `-o` option.\n\n## API Usage\n\n```js\nvar b = require('browserify')();\n\nb.add('./main.js');\nb.plugin(require('css-modulesify'), {\n  rootDir: __dirname,\n  output: './path/to/my.css'\n});\n\nb.bundle();\n```\n\n```js\n// or, get the output as a stream\nvar b = require('browserify')();\nvar fs = require('fs');\n\nb.add('./main.js');\nb.plugin(require('css-modulesify'), {\n  rootDir: __dirname\n});\n\nvar bundle = b.bundle()\nb.on('css stream', function (css) {\n  css.pipe(fs.createWriteStream('mycss.css'));\n});\n```\n\n### Options:\n\n- `rootDir`: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames. css-modulesify will try to use the browserify `basedir` if `rootDir` is not specified, if both are not specified it will use the location from which the command was executed. \n- `output`: path to write the generated css. If not provided, you'll need to listen to the `'css stream'` event on the bundle to get the output.\n- `jsonOutput`: optional path to write a json manifest of classnames.\n- `use`: optional array of postcss plugins (by default we use the css-modules core plugins). NOTE: it's safer to use `after`\n- `before`:  optional array of postcss plugins to run before the required css-modules core plugins are run.\n- `after`:  optional array of postcss plugins to run after the required css-modules core plugins are run.\n- `generateScopedName`: (API only) a function to override the default behaviour of creating locally scoped classnames.\n- `global`: optional boolean. Set to `true` if you want `css-modulesify` to apply to `node_modules` as well as local files. You can read more about it in the [browserify docs](https://github.com/substack/node-browserify/#btransformtr-opts).\n- `filePattern`: optional regular expression string to specify css file names. (default: `\\.css$`)\n- `cache`: optional object to persist cache between runs.\n\n### Events\n- `b.on('css stream', callback)` The callback is called with a readable stream containing the compiled CSS. You can write this to a file.\n\n## Using CSS Modules on the backend\n\nIf you want to use CSS Modules in server-generated templates there are a couple of options:\n\n- Option A (nodejs only): register the [require-hook](https://github.com/css-modules/css-modules-require-hook) so that `var styles = require('./foo.css')` operates the same way as on the frontend. Make sure that the `rootDir` option matches to guarantee that the classnames are the same.\n\n- Option B: configure the `jsonOutput` option with a file path and `css-modulesify` will generate a JSON manifest of classnames.\n\n\n## PostCSS Plugins\n\nThe following PostCSS plugins are enabled by default:\n\n  * [postcss-modules-local-by-default]\n  * [postcss-modules-extract-imports]\n  * [postcss-modules-scope]\n  * [postcss-modules-values]\n\n(i.e. the [CSS Modules] specification).\n\nYou can override the default PostCSS Plugins (and add your own) by passing `--use|-u` to `css-modulesify`.\n\nOr if you just want to add some extra plugins to run after the default, add them to the `postcssAfter` array option (API only at this time). In the same way, add extra plugins to `postcssBefore` to run the before the defaults.\n\nIn addition you may also wish to configure defined PostCSS plugins by passing `--plugin.option true`.\n\nAn example of this would be:\n\n```\nbrowserify -p [css-modulesify \\\n  --after autoprefixer --autoprefixer.browsers '\u003e 5%' \\\n  -o dist/main.css] -o dist/index.js src/index.js\n```\n\n[postcss-modules-local-by-default]: https://github.com/css-modules/postcss-modules-local-by-default\n[postcss-modules-extract-imports]: https://github.com/css-modules/postcss-modules-extract-imports\n[postcss-modules-scope]: https://github.com/css-modules/postcss-modules-scope\n[postcss-modules-values]: https://github.com/css-modules/postcss-modules-values\n\n## Building for production\n\nIf you set `NODE_ENV=production` then `css-modulesify` will generate shorter (though less useful) classnames.\n\nYou can also manually switch to short names by setting the `generateScopedName` option. Eg:\n\n```\nbrowserify.plugin(cssModulesify, {\n  rootDir: __dirname,\n  output: './dist/main.css',\n  generateScopedName: cssModulesify.generateShortName\n})\n```\n\n## Example\n\nAn example implementation can be found [here](https://github.com/css-modules/browserify-demo).\n\n## Licence\n\nMIT\n\n## With thanks\n\n - Tobias Koppers\n - Mark Dalgleish\n - Glen Maddern\n\n----\nJosh Johnston, 2015.\n","funding_links":[],"categories":["Tools","JavaScript","0. 前端自动化(Workflow)"],"sub_categories":["CSS bundlers"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcss-modules%2Fcss-modulesify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcss-modules%2Fcss-modulesify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcss-modules%2Fcss-modulesify/lists"}