Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inline-style support #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ b.on('css stream', function (css) {
- `generateScopedName`: (API only) a function to override the default behaviour of creating locally scoped classnames.
- `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).
- `filePattern`: optional regular expression string to specify css file names. (default: `\.css$`)
- `inline`: auto create style tag and insert it into head tag.(default: `false`, need output to be `false`)

### Events
- `b.on('css stream', callback)` The callback is called with a readable stream containing the compiled CSS. You can write this to a file.
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports = function (browserify, options) {

var cssOutFilename = options.output || options.o;
var jsonOutFilename = options.json || options.jsonOutput;
var inlineStyle = options.inline && !cssOutFilename;
transformOpts.cssOutFilename = cssOutFilename;
transformOpts.cssFilePattern = options.filePattern;

Expand Down Expand Up @@ -228,6 +229,13 @@ module.exports = function (browserify, options) {
writes.push(writeFile(cssOutFilename, css));
}

// write the css into page style tag
if (inlineStyle) {
var func_start = ";(function() { var head = document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css';",
func_end = "if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } head.appendChild(style);}())";
self.push(func_start + "var css = " + JSON.stringify(loadersByFile[cssOutFilename].finalSource) + ";" + func_end);
}

// write the classname manifest
if (jsonOutFilename) {
writes.push(writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir))));
Expand Down