From 2f84cba915b5571ed896d06e92783c5bab0bde9d Mon Sep 17 00:00:00 2001 From: SevereOverfl0w Date: Sun, 20 Sep 2015 14:29:02 +0100 Subject: [PATCH 1/4] add tests for browserify func --- test/fixtures/path-prefix/app.coffee | 5 +++++ test/fixtures/path-prefix/index.coffee | 1 + test/fixtures/path-prefix/index.jade | 1 + test/fixtures/path-prefix/package.json | 7 +++++++ test/test.coffee | 8 ++++++++ 5 files changed, 22 insertions(+) create mode 100644 test/fixtures/path-prefix/app.coffee create mode 100644 test/fixtures/path-prefix/index.coffee create mode 100644 test/fixtures/path-prefix/index.jade create mode 100644 test/fixtures/path-prefix/package.json diff --git a/test/fixtures/path-prefix/app.coffee b/test/fixtures/path-prefix/app.coffee new file mode 100644 index 0000000..fc24eca --- /dev/null +++ b/test/fixtures/path-prefix/app.coffee @@ -0,0 +1,5 @@ +browserify = require '../../..' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [browserify(files: "index.coffee", out: "build.js")] diff --git a/test/fixtures/path-prefix/index.coffee b/test/fixtures/path-prefix/index.coffee new file mode 100644 index 0000000..06d7abf --- /dev/null +++ b/test/fixtures/path-prefix/index.coffee @@ -0,0 +1 @@ +console.log 'wowe, such test.' diff --git a/test/fixtures/path-prefix/index.jade b/test/fixtures/path-prefix/index.jade new file mode 100644 index 0000000..542ff48 --- /dev/null +++ b/test/fixtures/path-prefix/index.jade @@ -0,0 +1 @@ +!= browserify('../') diff --git a/test/fixtures/path-prefix/package.json b/test/fixtures/path-prefix/package.json new file mode 100644 index 0000000..8c98bd1 --- /dev/null +++ b/test/fixtures/path-prefix/package.json @@ -0,0 +1,7 @@ +{ + "name": "test", + "dependencies": { + "coffee-script": "^1.10.0", + "jade": "^1.11.0" + } +} diff --git a/test/test.coffee b/test/test.coffee index 686bd87..eff99f4 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -184,3 +184,11 @@ describe 'cacheing in watch mode', -> , 3000 it 'invalidates the cache when a file changes' + +describe 'path-prefix', -> + + before (done) -> compile_fixture.call(@, 'path-prefix', -> done()) + + it 'should include the build.js', -> + out = path.join(@public, 'index.html') + h.file.contains(out, "").should.be.ok From 98b12906c0ee323563bcaf9a137d2d38359ffe73 Mon Sep 17 00:00:00 2001 From: SevereOverfl0w Date: Sun, 20 Sep 2015 14:40:28 +0100 Subject: [PATCH 2/4] Add a browserify local function This function auto-injects the file built by the browserify extension. --- lib/index.coffee | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/index.coffee b/lib/index.coffee index b7838f0..1036d72 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -52,9 +52,16 @@ module.exports = (opts) -> @b.on 'package', (pkg) => @pkg_cache[path.join(pkg.__dirname, 'package.json')] = pkg + @roots.config.locals ?= {} + @roots.config.locals.browserify = (prefix = '') -> + "\n" + + @b.transform(t) for t in opts.transforms if opts.minify then @b.transform(uglifyify, { global: true }) + + ###* * Gets the dependency graph of required files so we can ignore them * from the compile process. From ed5bf22015a74399152a98bc21f1afb7e99c7025 Mon Sep 17 00:00:00 2001 From: SevereOverfl0w Date: Mon, 21 Sep 2015 18:38:10 +0100 Subject: [PATCH 3/4] document new browserify() function. --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index db79bac..4fc64b6 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,36 @@ Roots browserify is an alternate javascript pipeline that uses commonjs and [bro This extension very directly uses browserify's javascript API under the hood. For basic usage, pass either a string with a file path or an array of file path strings as entry points for browserify, and an output path where all the concatenated scripts should be written, as shown in the example above. +##### Injecting script into views +When you use this extension, it wille expose a function called `browserify` to all your view files. When you call this function, the extension will drop in one script tag pointing to your script. + +The `browserify` function accepts one optional argument, which is a path to prefix any injected scripts with. So for example if you wanted to have the script load from the root of the site, you could pass in `/`. By default it would be the relative path `js/build.js`, but calling with `/` would make it `/js/build.js`. + +Here's an example of using the `browserify` function. This example uses [jade](http://jade-lang.com/) but this will also work with any other templating lagnuage. + +```jade +//- index.jade +p here's my great website +!= browserify() +``` + +Now let's take a look at some sample output. With this configuration: + +```coffee +# app.coffee +browserify = require 'roots-browserify' + +module.exports = + extensions: [browserify(files: 'assets/js/main.coffee', out: 'js/build.js')] +``` + +You would see this output. +```html + +

here's my great website

+ +``` + ### Options ##### files From 3af54f89b9827700bea026656ee19da4441e7804 Mon Sep 17 00:00:00 2001 From: SevereOverfl0w Date: Mon, 21 Sep 2015 18:51:47 +0100 Subject: [PATCH 4/4] test for both prefixed & unprefixed paths in the output. --- test/fixtures/path-prefix/index.jade | 1 + test/test.coffee | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/fixtures/path-prefix/index.jade b/test/fixtures/path-prefix/index.jade index 542ff48..055bea3 100644 --- a/test/fixtures/path-prefix/index.jade +++ b/test/fixtures/path-prefix/index.jade @@ -1 +1,2 @@ != browserify('../') +!= browserify() diff --git a/test/test.coffee b/test/test.coffee index eff99f4..6ce4375 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -189,6 +189,7 @@ describe 'path-prefix', -> before (done) -> compile_fixture.call(@, 'path-prefix', -> done()) - it 'should include the build.js', -> + it 'should include the build.js with & without prefix', -> out = path.join(@public, 'index.html') h.file.contains(out, "").should.be.ok + h.file.contains(out, "").should.be.ok