diff --git a/.eslintrc b/.eslintrc index 71199da..ef37a41 100755 --- a/.eslintrc +++ b/.eslintrc @@ -24,7 +24,6 @@ "no-negated-in-lhs": 2, "no-obj-calls": 2, "no-regex-spaces": 2, - "quote-props": [1, "consistent-as-needed"], "no-sparse-arrays": 2, "no-unreachable": 2, "use-isnan": 2, @@ -92,7 +91,7 @@ 2, "all" ], - "no-use-before-define": 2, + "no-use-before-define": 0, "handle-callback-err": 2, "no-mixed-requires": 0, "no-new-require": 2, @@ -219,7 +218,6 @@ ] } ], - "array-bracket-spacing": [1, "never"], "wrap-regex": 0, "constructor-super": 2, "no-this-before-super": 2, diff --git a/.gitignore b/.gitignore index 8f5c351..ce25fd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/* !node_modules/cool-styles +npm-debug.log \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 9ccb608..1d0ad98 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ sudo: false language: node_js node_js: - - "0.10" - - "0.12" - - "iojs" + - "4" + - "6" diff --git a/README.md b/README.md index 41e6f2e..f806a7a 100644 --- a/README.md +++ b/README.md @@ -63,12 +63,16 @@ b.on('css stream', function (css) { ### Options: -- `rootDir`: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames. +- `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. - `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. - `jsonOutput`: optional path to write a json manifest of classnames. - `use`: optional array of postcss plugins (by default we use the css-modules core plugins). NOTE: it's safer to use `after` +- `before`: optional array of postcss plugins to run before the required css-modules core plugins are run. - `after`: optional array of postcss plugins to run after the required css-modules core plugins are run. - `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$`) +- `cache`: optional object to persist cache between runs. ### 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. @@ -89,12 +93,13 @@ The following PostCSS plugins are enabled by default: * [postcss-modules-local-by-default] * [postcss-modules-extract-imports] * [postcss-modules-scope] + * [postcss-modules-values] (i.e. the [CSS Modules] specification). You can override the default PostCSS Plugins (and add your own) by passing `--use|-u` to `css-modulesify`. -Or 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). +Or 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. In addition you may also wish to configure defined PostCSS plugins by passing `--plugin.option true`. @@ -109,6 +114,7 @@ browserify -p [css-modulesify \ [postcss-modules-local-by-default]: https://github.com/css-modules/postcss-modules-local-by-default [postcss-modules-extract-imports]: https://github.com/css-modules/postcss-modules-extract-imports [postcss-modules-scope]: https://github.com/css-modules/postcss-modules-scope +[postcss-modules-values]: https://github.com/css-modules/postcss-modules-values ## Building for production diff --git a/cmify.js b/cmify.js new file mode 100644 index 0000000..a8dec5a --- /dev/null +++ b/cmify.js @@ -0,0 +1,87 @@ +var stream = require('stream'); +var util = require('util'); +var assign = require('object-assign'); +var path = require('path'); + +util.inherits(Cmify, stream.Transform); +function Cmify (filename, opts) { + if (!(this instanceof Cmify)) { + return new Cmify(filename, opts); + } + + stream.Transform.call(this); + + this.cssFilePattern = new RegExp(opts.cssFilePattern || '\.css$'); + this._data = ''; + this._filename = filename; + this._cssOutFilename = opts.cssOutFilename; + this._loader = opts.loader; + this._tokensByFile = opts.tokensByFile; + this._rootDir = opts.rootDir; + opts.cssFiles.push(filename); +} + +Cmify.prototype.isCssFile = function (filename) { + return this.cssFilePattern.test(filename); +}; + +Cmify.prototype._transform = function (buf, enc, callback) { + // only handle .css files + if (!this.isCssFile(this._filename)) { + this.push(buf); + return callback(); + } + + this._data += buf; + callback(); +}; + +Cmify.prototype._flush = function (callback) { + var self = this; + var filename = this._filename; + + // only handle .css files + if (!this.isCssFile(filename)) { return callback(); } + + // grab the correct loader + var loader = this._loader; + var tokensByFile = this._tokensByFile; + + // convert css to js before pushing + // reset the `tokensByFile` state + var relFilename = path.relative(this._rootDir, filename); + tokensByFile[filename] = loader.tokensByFile[filename] = null; + + loader.fetch(relFilename, '/').then(function (tokens) { + var deps = loader.deps.dependenciesOf(filename); + var output = deps.map(function (f) { + return 'require("' + f + '")'; + }); + output.push('module.exports = ' + JSON.stringify(tokens)); + + var isValid = true; + var isUndefined = /\bundefined\b/; + Object.keys(tokens).forEach(function (k) { + if (isUndefined.test(tokens[k])) { + isValid = false; + } + }); + + if (!isValid) { + var err = 'Composition in ' + filename + ' contains an undefined reference'; + console.error(err); + output.push('console.error("' + err + '");'); + } + + assign(tokensByFile, loader.tokensByFile); + + self.push(output.join('\n')); + return callback(); + }).catch(function (err) { + self.push('console.error("' + err + '");'); + self.emit('error', err); + return callback(); + }); +}; + +module.exports = Cmify; diff --git a/file-system-loader.js b/file-system-loader.js new file mode 100644 index 0000000..4e033fc --- /dev/null +++ b/file-system-loader.js @@ -0,0 +1,143 @@ +'use strict'; + +var DepGraph = require('dependency-graph').DepGraph; +var nodeResolve = require('resolve'); + +Object.defineProperty(exports, '__esModule', { + value: true +}); + +var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } + +var _indexJs = require('css-modules-loader-core/lib/index.js'); + +var _indexJs2 = _interopRequireDefault(_indexJs); + +var _fs = require('fs'); + +var _fs2 = _interopRequireDefault(_fs); + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +// Sorts dependencies in the following way: +// AAA comes before AA and A +// AB comes after AA and before A +// All Bs come after all As +// This ensures that the files are always returned in the following order: +// - In the order they were required, except +// - After all their dependencies +var traceKeySorter = function traceKeySorter(a, b) { + if (a.length < b.length) { + return a < b.substring(0, a.length) ? -1 : 1; + } else if (a.length > b.length) { + return a.substring(0, b.length) <= b ? -1 : 1; + } else { + return a < b ? -1 : 1; + } +}; + +var FileSystemLoader = (function () { + function FileSystemLoader(root, plugins) { + _classCallCheck(this, FileSystemLoader); + + this.root = root; + this.sources = {}; + this.importNr = 0; + this.core = new _indexJs2['default'](plugins); + this.tokensByFile = {}; + this.deps = new DepGraph(); + } + + _createClass(FileSystemLoader, [{ + key: 'fetch', + value: function fetch(_newPath, relativeTo, _trace) { + var _this = this; + + var newPath = _newPath.replace(/^["']|["']$/g, ''), + trace = _trace || String.fromCharCode(this.importNr++); + return new Promise(function (resolve, reject) { + var relativeDir = _path2['default'].dirname(relativeTo), + rootRelativePath = _path2['default'].resolve(relativeDir, newPath), + rootRelativeDir = _path2['default'].join(_this.root, relativeDir), + fileRelativePath = _path2['default'].resolve(rootRelativeDir, newPath); + + // if the path is not relative or absolute, try to resolve it in node_modules + if (newPath[0] !== '.' && newPath[0] !== '/') { + var paths; + if (process.env.NODE_PATH) { + paths = process.env.NODE_PATH.split(_path2['default'].delimiter); + } + try { + fileRelativePath = nodeResolve.sync(newPath, { + basedir: rootRelativeDir, + paths: paths + }); + // in this case we need to actualize rootRelativePath too + rootRelativePath = _path2['default'].relative(_this.root, fileRelativePath); + } catch (e) {} + } + + // first time? add a node + if (_trace === undefined) { + if (!_this.deps.hasNode(fileRelativePath)) { + _this.deps.addNode(fileRelativePath); + } + } + // otherwise add a dependency + else { + var parentFilePath = _path2['default'].join(_this.root, relativeTo); + if (!_this.deps.hasNode(parentFilePath)) { + console.error('NO NODE', parentFilePath, fileRelativePath) + } + if (!_this.deps.hasNode(fileRelativePath)) { + _this.deps.addNode(fileRelativePath); + } + _this.deps.addDependency(parentFilePath, fileRelativePath); + } + + var tokens = _this.tokensByFile[fileRelativePath]; + if (tokens) { + return resolve(tokens); + } + + _fs2['default'].readFile(fileRelativePath, 'utf-8', function (err, source) { + if (err) reject(err); + _this.core.load(source, rootRelativePath, trace, _this.fetch.bind(_this)).then(function (_ref) { + var injectableSource = _ref.injectableSource; + var exportTokens = _ref.exportTokens; + + _this.sources[fileRelativePath] = injectableSource; + _this.tokensByFile[fileRelativePath] = exportTokens; + resolve(exportTokens); + }, reject); + }); + }); + } + }, { + key: 'finalSource', + get: function () { + var sources = this.sources; + var written = {}; + + return this.deps.overallOrder().map(function (filename) { + if (written[filename] === true) { + return null; + } + written[filename] = true; + + return sources[filename]; + }).join(''); + } + }]); + + return FileSystemLoader; +})(); + +exports['default'] = FileSystemLoader; +module.exports = exports['default']; diff --git a/index.js b/index.js index e967841..d71525e 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,14 @@ +// Some css-modules-loader-code dependencies use Promise so we'll provide it for older node versions +if (!global.Promise) { global.Promise = require('promise-polyfill'); } + var fs = require('fs'); var path = require('path'); -var through = require('through'); +var Cmify = require('./cmify'); var Core = require('css-modules-loader-core'); -var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader'); -var assign = require('object-assign'); +var FileSystemLoader = require('./file-system-loader'); var stringHash = require('string-hash'); var ReadableStream = require('stream').Readable; - +var through = require('through2'); /* Custom `generateScopedName` function for `postcss-modules-scope`. @@ -14,7 +16,7 @@ var ReadableStream = require('stream').Readable; */ function generateShortName (name, filename, css) { // first occurrence of the name - // TOOD: better match with regex + // TODO: better match with regex var i = css.indexOf('.' + name); var numLines = css.substr(0, i).split(/[\r\n]/).length; @@ -47,7 +49,8 @@ function getDefaultPlugins (options) { scope.generateScopedName = customNameFunc || defaultNameFunc; return [ - Core.localByDefault + Core.values + , Core.localByDefault , Core.extractImports , scope ]; @@ -71,35 +74,8 @@ function normalizeManifestPaths (tokensByFile, rootDir) { return output; } -var cssExt = /\.css$/; - -// caches -// -// persist these for as long as the process is running. #32 - -// keep track of css files visited -var filenames = []; - -// keep track of all tokens so we can avoid duplicates -var tokensByFile = {}; - -// keep track of all source files for later builds: when -// using watchify, not all files will be caught on subsequent -// bundles -var sourceByFile = {}; - -module.exports = function (browserify, options) { - options = options || {}; - - // if no root directory is specified, assume the cwd - var rootDir = options.rootDir || options.d; - if (rootDir) { rootDir = path.resolve(rootDir); } - if (!rootDir) { rootDir = process.cwd(); } - - var cssOutFilename = options.output || options.o; - var jsonOutFilename = options.json || options.jsonOutput; - - // PostCSS plugins passed to FileSystemLoader +// PostCSS plugins passed to FileSystemLoader +function getPlugins (options) { var plugins = options.use || options.u; if (!plugins) { plugins = getDefaultPlugins(options); @@ -110,17 +86,18 @@ module.exports = function (browserify, options) { } } + var postcssBefore = options.postcssBefore || options.before || []; var postcssAfter = options.postcssAfter || options.after || []; - plugins = plugins.concat(postcssAfter); + plugins = (Array.isArray(postcssBefore) ? postcssBefore : [postcssBefore]).concat(plugins).concat(postcssAfter); // load plugins by name (if a string is used) - plugins = plugins.map(function requirePlugin (name) { - // assume functions are already required plugins - if (typeof name === 'function') { + return plugins.map(function requirePlugin (name) { + // assume not strings are already required plugins + if (typeof name !== 'string') { return name; } - var plugin = require(require.resolve(name)); + var plugin = module.parent.require(name); // custom scoped name generation if (name === 'postcss-modules-scope') { @@ -139,90 +116,109 @@ module.exports = function (browserify, options) { return plugin; }); +} - // the compiled CSS stream needs to be avalible to the transform, - // but re-created on each bundle call. - var compiledCssStream; - - function transform (filename) { - // only handle .css files - if (!cssExt.test(filename)) { - return through(); - } - - // collect visited filenames - filenames.push(filename); +module.exports = function (browserify, options) { + options = options || {}; - var loader = new FileSystemLoader(rootDir, plugins); - return through(function noop () {}, function end () { - var self = this; + // if no root directory is specified, assume the cwd + var rootDir = options.rootDir || options.d || browserify._options.basedir; + if (rootDir) { + rootDir = path.resolve(rootDir); + } + if (!rootDir) { + rootDir = process.cwd(); + } - loader.fetch(path.relative(rootDir, filename), '/').then(function (tokens) { - var output = 'module.exports = ' + JSON.stringify(tokens); + var cssOutFilename = options.output || options.o; + var jsonOutFilename = options.json || options.jsonOutput; + var loader; + // keep track of all tokens so we can avoid duplicates + var tokensByFile; + if (options.cache) { + if (options.cache.loaders) { + loader = options.cache.loaders[cssOutFilename]; + } else { + options.cache.loaders = {}; + } + if (options.cache.tokens) { + tokensByFile = options.cache.tokens; + } else { + options.cache.tokens = {}; + } + } - assign(tokensByFile, loader.tokensByFile); + loader = loader || new FileSystemLoader(rootDir, getPlugins(options)); + tokensByFile = tokensByFile || {}; - // store this file's source to be written out to disk later - sourceByFile[filename] = loader.finalSource; + if (options.cache) { + options.cache.loaders[cssOutFilename] = loader; + options.cache.tokens = tokensByFile; + } - compiledCssStream.push(loader.finalSource); + var transformOpts = { + cssFilePattern: options.filePattern + , cssFiles: [] + , cssOutFilename: cssOutFilename + , global: options.global || options.g + , loader: loader + , rootDir: rootDir + , tokensByFile: tokensByFile + }; - self.queue(output); - self.queue(null); - }, function (err) { - self.emit('error', err); - }); - }); - } + browserify.transform(Cmify, transformOpts); - browserify.transform(transform, { - global: true - }); + // ---- - browserify.on('bundle', function (bundle) { - // on each bundle, create a new stream b/c the old one might have ended - compiledCssStream = new ReadableStream(); - compiledCssStream._read = function () {}; + function addHooks () { + browserify.pipeline.get('pack').push(through(function write (row, enc, next) { + next(null, row); + }, function end (cb) { + // on each bundle, create a new stream b/c the old one might have ended + var compiledCssStream = new ReadableStream(); + compiledCssStream._read = function () {}; - browserify.emit('css stream', compiledCssStream); + browserify.emit('css stream', compiledCssStream); - bundle.on('end', function () { - // Combine the collected sources into a single CSS file - var files = Object.keys(sourceByFile); - var css; + // Combine the collected sources for a single bundle into a single CSS file + var self = this; + var css = loader.finalSource; // end the output stream + compiledCssStream.push(css); compiledCssStream.push(null); + var writes = []; + // write the css file if (cssOutFilename) { - css = files.map(function (file) { - return sourceByFile[file]; - }).join('\n'); - - fs.writeFile(cssOutFilename, css, function (err) { - if (err) { - browserify.emit('error', err); - } - }); + writes.push(writeFile(cssOutFilename, css)); } // write the classname manifest if (jsonOutFilename) { - fs.writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir)), function (err) { - if (err) { - browserify.emit('error', err); - } - }); + writes.push(writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir)))); } + Promise.all(writes) + .then(function () { cb(); }) + .catch(function (err) { self.emit('error', err); cb(); }); + })); + } - // reset the `tokensByFile` cache - tokensByFile = {}; - }); - }); + browserify.on('reset', addHooks); + addHooks(); return browserify; }; +function writeFile (filename, content) { + return new Promise(function (resolve, reject) { + fs.writeFile(filename, content, function (err) { + if (err) reject(err); + else resolve(); + }); + }); +} + module.exports.generateShortName = generateShortName; module.exports.generateLongName = generateLongName; diff --git a/package.json b/package.json index 51a9bcd..bba0ee2 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,16 @@ { "name": "css-modulesify", - "version": "0.10.0", + "version": "0.28.0", "description": "A browserify transform to load CSS Modules", "main": "index.js", "dependencies": { - "css-modules-loader-core": "0.0.12", + "css-modules-loader-core": "^1.1.0", + "dependency-graph": "^0.4.1", "object-assign": "^3.0.0", + "promise-polyfill": "^2.1.0", + "resolve": "^1.1.7", "string-hash": "^1.1.0", - "through": "^2.3.7" + "through2": "^2.0.1" }, "devDependencies": { "browserify": "^11.0.1", diff --git a/tests/cache.js b/tests/cache.js index 45c3c59..3f59240 100644 --- a/tests/cache.js +++ b/tests/cache.js @@ -25,6 +25,7 @@ tape('multiple builds', function (t) { fs: fakeFs }); + var cssModulesifyCache = {}; var getBundler = rebundler(function (cache, packageCache) { return browserify(path.join(simpleCaseDir, 'main.js'), { cache: cache @@ -34,6 +35,7 @@ tape('multiple builds', function (t) { .plugin(cssModulesify, { rootDir: path.join(simpleCaseDir) , output: cssOutFilename + , cache: cssModulesifyCache }); }); diff --git a/tests/cases/compose-from-shared/expected.css b/tests/cases/compose-from-shared/expected.css new file mode 100644 index 0000000..9c004b1 --- /dev/null +++ b/tests/cases/compose-from-shared/expected.css @@ -0,0 +1,9 @@ +._shared__shared { + background: #000; +} +._styles_1__foo { + color: #F00; +} +._styles_2__bar { + background: #BAA; +} diff --git a/tests/cases/compose-from-shared/main.js b/tests/cases/compose-from-shared/main.js new file mode 100644 index 0000000..14cc992 --- /dev/null +++ b/tests/cases/compose-from-shared/main.js @@ -0,0 +1,2 @@ +require('./styles-1.css'); +require('./styles-2.css'); diff --git a/tests/cases/compose-from-shared/shared.css b/tests/cases/compose-from-shared/shared.css new file mode 100644 index 0000000..d6fc168 --- /dev/null +++ b/tests/cases/compose-from-shared/shared.css @@ -0,0 +1,3 @@ +.shared { + background: #000; +} diff --git a/tests/cases/compose-from-shared/styles-1.css b/tests/cases/compose-from-shared/styles-1.css new file mode 100644 index 0000000..2ffd802 --- /dev/null +++ b/tests/cases/compose-from-shared/styles-1.css @@ -0,0 +1,4 @@ +.foo { + composes: shared from "./shared.css"; + color: #F00; +} diff --git a/tests/cases/compose-from-shared/styles-2.css b/tests/cases/compose-from-shared/styles-2.css new file mode 100644 index 0000000..f9d32d8 --- /dev/null +++ b/tests/cases/compose-from-shared/styles-2.css @@ -0,0 +1,4 @@ +.bar { + composes: shared from "./shared.css"; + background: #BAA; +} diff --git a/tests/cases/compose-local-node-module/custom.js b/tests/cases/compose-local-node-module/custom.js new file mode 100644 index 0000000..7e10afd --- /dev/null +++ b/tests/cases/compose-local-node-module/custom.js @@ -0,0 +1,3 @@ +module.exports = { + global: true +} diff --git a/tests/cases/compose-local-node-module/expected.css b/tests/cases/compose-local-node-module/expected.css new file mode 100644 index 0000000..fa249e5 --- /dev/null +++ b/tests/cases/compose-local-node-module/expected.css @@ -0,0 +1,6 @@ +._node_modules_cool_local_styles_styles__foo { + color: #F00; +} +._styles__foo { + background: black; +} diff --git a/tests/cases/compose-local-node-module/main.js b/tests/cases/compose-local-node-module/main.js new file mode 100644 index 0000000..922a6aa --- /dev/null +++ b/tests/cases/compose-local-node-module/main.js @@ -0,0 +1,2 @@ +var styles = require('./styles.css'); +module.exports = styles; diff --git a/tests/cases/compose-local-node-module/node_modules/cool-local-styles/styles.css b/tests/cases/compose-local-node-module/node_modules/cool-local-styles/styles.css new file mode 100644 index 0000000..092b8ad --- /dev/null +++ b/tests/cases/compose-local-node-module/node_modules/cool-local-styles/styles.css @@ -0,0 +1,3 @@ +.foo { + color: #F00; +} diff --git a/tests/cases/compose-local-node-module/styles.css b/tests/cases/compose-local-node-module/styles.css new file mode 100644 index 0000000..2f17694 --- /dev/null +++ b/tests/cases/compose-local-node-module/styles.css @@ -0,0 +1,4 @@ +.foo { + composes: foo from "cool-local-styles/styles.css"; + background: black; +} diff --git a/tests/cases/compose-node-module/custom.js b/tests/cases/compose-node-module/custom.js new file mode 100644 index 0000000..7e10afd --- /dev/null +++ b/tests/cases/compose-node-module/custom.js @@ -0,0 +1,3 @@ +module.exports = { + global: true +} diff --git a/tests/cases/compose-node-module/expected.css b/tests/cases/compose-node-module/expected.css index ddd63b1..492da06 100644 --- a/tests/cases/compose-node-module/expected.css +++ b/tests/cases/compose-node-module/expected.css @@ -1,4 +1,4 @@ -._cool_styles_styles__foo { +._node_modules_cool_styles_styles__foo { color: #F00; } ._styles__foo { diff --git a/tests/cases/custom-ext/custom.js b/tests/cases/custom-ext/custom.js new file mode 100644 index 0000000..548c51d --- /dev/null +++ b/tests/cases/custom-ext/custom.js @@ -0,0 +1,3 @@ +module.exports = { + filePattern: /\.cssmodule$/ +} diff --git a/tests/cases/custom-ext/expected.css b/tests/cases/custom-ext/expected.css new file mode 100644 index 0000000..f0a88f9 --- /dev/null +++ b/tests/cases/custom-ext/expected.css @@ -0,0 +1,3 @@ +._styles__foo { + color: #F00; +} diff --git a/tests/cases/custom-ext/main.js b/tests/cases/custom-ext/main.js new file mode 100644 index 0000000..96280a2 --- /dev/null +++ b/tests/cases/custom-ext/main.js @@ -0,0 +1,3 @@ +// test using a custom `filePattern` option +var styles = require('./styles.cssmodule'); +module.exports = styles; diff --git a/tests/cases/custom-ext/styles.cssmodule b/tests/cases/custom-ext/styles.cssmodule new file mode 100644 index 0000000..092b8ad --- /dev/null +++ b/tests/cases/custom-ext/styles.cssmodule @@ -0,0 +1,3 @@ +.foo { + color: #F00; +} diff --git a/tests/cases/import-node-module/custom.js b/tests/cases/import-node-module/custom.js new file mode 100644 index 0000000..7e10afd --- /dev/null +++ b/tests/cases/import-node-module/custom.js @@ -0,0 +1,3 @@ +module.exports = { + global: true +} diff --git a/tests/index.js b/tests/index.js index f651e55..0270550 100644 --- a/tests/index.js +++ b/tests/index.js @@ -10,6 +10,16 @@ var cssOutFilename = 'out.css'; function runTestCase (dir) { tape('case: ' + dir, function (t) { + // load (optional) custom setup for this testcase + var customPath = path.join(casesDir, dir, 'custom.js'); + var customOpts; + try { + fs.accessSync(customPath); + customOpts = require(customPath); + } catch (e) { + customOpts = {}; + } + var fakeFs = { writeFile: function (filename, content, cb) { var expected = fs.readFileSync(path.join(casesDir, dir, 'expected.css'), 'utf8'); @@ -25,12 +35,13 @@ function runTestCase (dir) { }); var b = browserify(); + b.add(path.join(casesDir, dir, 'main.js')); - b.plugin(cssModulesify, { + b.plugin(cssModulesify, Object.assign({}, { rootDir: path.join(casesDir, dir) , output: cssOutFilename , generateScopedName: cssModulesify.generateLongName - }); + }, customOpts)); b.bundle(function (err) { if (err) { @@ -45,4 +56,7 @@ function runTestCase (dir) { // test cases are expected to have: // - main.js (entry point) // - expected.css (what to expect from css-modulesify output) +// optional: +// - custom.js (module that exports an object of custom settings) + fs.readdirSync(path.join(__dirname, 'cases')).forEach(runTestCase); diff --git a/tests/stream-output.js b/tests/stream-output.js index d33ff1a..48fc861 100644 --- a/tests/stream-output.js +++ b/tests/stream-output.js @@ -25,7 +25,7 @@ tape('stream output', function (t) { fs: fakeFs }); - t.plan(cssFilesTotal * 2 + 1); + t.plan(cssFilesTotal * 2 + 2); var cssFilesCount = 0; var b = browserify(path.join(simpleCaseDir, 'main.js'));