diff --git a/README.md b/README.md index c5c8954..08e79be 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index ddf48c8..35103b2 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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))));