From 5847ae2d9c93a5e83a82d1a999ae1b09cd4019bd Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Sun, 28 May 2017 12:12:50 +0200 Subject: [PATCH] auto detect transform --- README.md | 14 +++++++------- index.js | 6 ++++++ test/import.js | 2 +- test/json.js | 4 ++-- test/plugins.js | 2 +- test/prefix.js | 5 ++--- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5ec2abd..fc50dcc 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,9 @@ const tree = html` document.body.appendChild(tree) ``` -Compile with browserify using `-t sheetify/transform`: +Compile with browserify using `-t sheetify`: ```sh -$ browserify -t sheetify/transform index.js > bundle.js +$ browserify -t sheetify index.js > bundle.js ``` CSS classes are namespaced based on the content hash: @@ -133,7 +133,7 @@ To write the compiled CSS to a separate file, rather than keep it in the bundle use [css-extract][2]: ```sh $ browserify index.js \ - -t [ sheetify/transform ] \ + -t [ sheetify ] \ -p [ css-extract -o bundle.css ] index.js \ -o bundle.js ``` @@ -164,9 +164,9 @@ const tree = html` document.body.appendChild(tree) ``` -Compile with browserify using `-t [ sheetify/transform -u sheetify-cssnext ]`: +Compile with browserify using `-t [ sheetify -u sheetify-cssnext ]`: ```sh -$ browserify -t [ sheetify/transform -u sheetify-cssnext ] index.js > bundle.js +$ browserify -t [ sheetify -u sheetify-cssnext ] index.js > bundle.js ``` Transforms the CSS into: @@ -185,7 +185,7 @@ The following plugins are available: Browserify transforms accept either flags from the command line using [subargs](https://github.com/substack/subarg): ```sh -$ browserify -t [ sheetify/transform -u sheetify-cssnext ] index.js > bundle.js +$ browserify -t [ sheetify -u sheetify-cssnext ] index.js > bundle.js ``` Or the equivalent options by passing in a configuration object in the @@ -194,7 +194,7 @@ JavaScript API: const browserify = require('browserify') const b = browserify(path.join(__dirname, 'transform/source.js')) -b.transform('sheetify/transform', { use: [ 'sheetify-cssnext' ] }) +b.transform('sheetify', { use: [ 'sheetify-cssnext' ] }) b.bundle().pipe(process.stdout) ``` diff --git a/index.js b/index.js index 7e4e0de..eb54d14 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,12 @@ module.exports.getPrefix = getPrefix // transform css // (str, str, obj?, fn) -> str function sheetify (src, filename, options, done) { + // browserify transform + if (typeof src === 'string' && !/\n/.test(src) && filename && filename._flags) { + var args = Array.prototype.slice.apply(arguments) + return require('./transform.js').apply(this, args) + } + // handle tagged template calls directly from Node const isTemplate = Array.isArray(src) if (isTemplate) src = src.join('') diff --git a/test/import.js b/test/import.js index 6e689eb..49a044f 100644 --- a/test/import.js +++ b/test/import.js @@ -5,7 +5,7 @@ const test = require('tape') const path = require('path') const fs = require('fs') -const sheetify = require(path.join(__dirname, '../transform')) +const sheetify = require('../') test('npm import', function (t) { t.test('should import npm packages', function (t) { diff --git a/test/json.js b/test/json.js index b818c46..9b99bca 100644 --- a/test/json.js +++ b/test/json.js @@ -2,14 +2,14 @@ const test = require('tape') const join = require('path').join const browserify = require('browserify') -const transform = require('../transform') +const sheetify = require('..') test('JSON files', function (t) { t.plan(2) browserify() .add(join(__dirname, 'fixtures', './example.json')) - .transform(transform) + .transform(sheetify) .bundle(function (err, code) { if (err) return t.end(err) code = String(code) diff --git a/test/plugins.js b/test/plugins.js index fd3e654..c9e2eaa 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -5,7 +5,7 @@ const test = require('tape') const path = require('path') const fs = require('fs') -const sheetify = require(path.join(__dirname, '../transform')) +const sheetify = require('../') test('plugins', function (t) { t.test('should transform CSS', function (t) { diff --git a/test/prefix.js b/test/prefix.js index 2b0c3c7..0a892ba 100644 --- a/test/prefix.js +++ b/test/prefix.js @@ -7,7 +7,6 @@ const fs = require('fs') const vm = require('vm') const cssResolve = require('style-resolve').sync -const transform = require('../transform') const sheetify = require('..') test('prefix', function (t) { @@ -48,7 +47,7 @@ test('prefix', function (t) { const bOpts = { browserField: false } const bpath = path.join(__dirname, 'fixtures/prefix-inline-source.js') browserify(bpath, bOpts) - .transform(transform) + .transform(sheetify) .transform(function (file) { return through(function (buf, enc, next) { const str = buf.toString('utf8') @@ -88,7 +87,7 @@ test('prefix', function (t) { const bOpts = { browserField: false } const bpath = path.join(__dirname, 'fixtures/prefix-import-source.js') browserify(bpath, bOpts) - .transform(transform) + .transform(sheetify) .transform(function (file) { return through(function (buf, enc, next) { const str = buf.toString('utf8')