diff --git a/README.md b/README.md index e538b2b..2367f05 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,18 @@ usemin src/index.html -d dist -o dist/index.html --htmlmin true -c config.js ``` ### Example HTML +#### Blocks +Blocks are expressed as: +```html + +... HTML Markup, list of script / link tags. + +``` + +- **pipelineId**: pipeline id for options or remove to remove a section +- **alternate search path**: (optional) By default the input files are relative to the treated file. Alternate search path allows one to change that +- **path**: the file path of the optimized file, the target output + ```html @@ -65,6 +77,19 @@ Running the command with `--rmlr true` will output: ``` +#### Alternate search path +```html + + + + + + + + + +``` + ### Config file Please check the relevant documentations for the available options: [ UglifyJS](https://github.com/mishoo/UglifyJS2), [CleanCSS](https://github.com/jakubpawlowicz/clean-css) and [HTML minifier](https://github.com/kangax/html-minifier). diff --git a/lib/getBlocks.js b/lib/getBlocks.js index eea7f01..035eacc 100644 --- a/lib/getBlocks.js +++ b/lib/getBlocks.js @@ -1,5 +1,6 @@ 'use strict'; var path = require('path'); +var fs = require('fs'); module.exports = function (src, content, removeLivereload) { var buildReg = //; @@ -24,9 +25,14 @@ module.exports = function (src, content, removeLivereload) { type: build[1], dest: build[3], indent: indent, + searchPath: [''], src: [], raw: [] }; + + if (build[2]) { + block.searchPath = build[2].split(','); + } } if (removeLivereload && livereload) { @@ -47,7 +53,19 @@ module.exports = function (src, content, removeLivereload) { var asset = l.match(/(href|src)=["']([^'"]+)["']/); if (asset && asset[2]) { - block.src.push(path.join(srcDir, asset[2])); + var numSearchPath = block.searchPath.length; + + for (var i = 0; i < numSearchPath; i++) { + var assetPath = path.join(srcDir, block.searchPath[i], asset[2]); + + try { + fs.accessSync(assetPath, fs.F_OK); + block.src.push(assetPath); + break; + } catch (e) { + continue; + } + } // Preserve defer attribute var defer = / defer/.test(l); diff --git a/package.json b/package.json index 7fda9da..a1e998f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "usemin-cli", - "version": "0.1.0", + "version": "0.2.0", "description": "Replaces references to non-optimized scripts or stylesheets into a set of HTML files (or any templates/views).", "main": "index.js", "bin": { diff --git a/test/fixtures/alt-search-path.html b/test/fixtures/alt-search-path.html new file mode 100644 index 0000000..88f71ed --- /dev/null +++ b/test/fixtures/alt-search-path.html @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/test/fixtures/alt/bar.js b/test/fixtures/alt/bar.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/alt/foo.js b/test/fixtures/alt/foo.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/alt2/foobar.js b/test/fixtures/alt2/foobar.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/css.html b/test/fixtures/css.html index 8d4fd06..2feb4cc 100644 --- a/test/fixtures/css.html +++ b/test/fixtures/css.html @@ -5,8 +5,8 @@ - - + + diff --git a/test/fixtures/css/bar.css b/test/fixtures/css/bar.css new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/css/foo.css b/test/fixtures/css/foo.css new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/defer-async.html b/test/fixtures/defer-async.html index 8d59189..eb71831 100644 --- a/test/fixtures/defer-async.html +++ b/test/fixtures/defer-async.html @@ -7,10 +7,8 @@ - - - - + + diff --git a/test/fixtures/index.html b/test/fixtures/index.html index 092c4d5..faa1a96 100644 --- a/test/fixtures/index.html +++ b/test/fixtures/index.html @@ -5,24 +5,20 @@ - - + + - - - - + + - - - - + + diff --git a/test/fixtures/js.html b/test/fixtures/js.html index a1f98f2..2dcc7a6 100644 --- a/test/fixtures/js.html +++ b/test/fixtures/js.html @@ -7,10 +7,8 @@ - - - - + + diff --git a/test/fixtures/js/bar.js b/test/fixtures/js/bar.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/js/config.js b/test/fixtures/js/config.js new file mode 100644 index 0000000..17c618d --- /dev/null +++ b/test/fixtures/js/config.js @@ -0,0 +1,25 @@ +module.exports = { + uglifyjs: { + outSourceMap: 'minified.js.map', + warnings: true, + mangle: true, + compress: { + loops: true, + unused: true + } + }, + cleancss: { + advanced: true, + keepBreaks: true, + rebase: false + }, + htmlminifier: { + removeComments: true, + collapseWhitespace: true, + removeEmptyAttributes: true, + removeScriptTypeAttributes: true, + removeStyleLinkTypeAttributes: true, + minifyJS: false, + minifyCSS: false + } +}; diff --git a/test/fixtures/js/foo.js b/test/fixtures/js/foo.js new file mode 100644 index 0000000..e69de29 diff --git a/test/getBlocksSpec.js b/test/getBlocksSpec.js index 87b76c3..c432e28 100644 --- a/test/getBlocksSpec.js +++ b/test/getBlocksSpec.js @@ -16,18 +16,15 @@ describe('Get Blocks', function () { type: 'js', dest: 'js/main.js', indent: '\t', + searchPath: [''], src: [ - inputDir + 'js/app.js', - inputDir + 'js/models.js', - inputDir + 'js/views.js', - inputDir + 'js/controllers.js' + inputDir + 'js/foo.js', + inputDir + 'js/bar.js', ], raw: [ '\t', - '\t', - '\t', - '\t', - '\t', + '\t', + '\t', '\t' ] } @@ -47,18 +44,15 @@ describe('Get Blocks', function () { type: 'js', dest: 'js/main.js', indent: '\t', + searchPath: [''], src: [ - inputDir + 'js/app.js', - inputDir + 'js/models.js', - inputDir + 'js/views.js', - inputDir + 'js/controllers.js' + inputDir + 'js/foo.js', + inputDir + 'js/bar.js', ], raw: [ '\t', - '\t', - '\t', - '\t', - '\t', + '\t', + '\t', '\t' ] } @@ -78,14 +72,15 @@ describe('Get Blocks', function () { type: 'css', dest: 'css/main.css', indent: '\t', + searchPath: [''], src: [ - inputDir + 'css/main.css', - inputDir + 'css/test.css' + inputDir + 'css/foo.css', + inputDir + 'css/bar.css' ], raw: [ '\t', - '\t', - '\t', + '\t', + '\t', '\t' ] } @@ -109,4 +104,34 @@ describe('Get Blocks', function () { expect(blocks).to.eql(outcome); }); + + it('should get JS block with alternate search path', function () { + var src = inputDir + 'alt-search-path.html'; + var content = fs.readFileSync(src).toString(); + var blocks = getBlocks(src, content); + var outcome = [ + { + async: false, + defer: false, + type: 'js', + dest: 'js/main.js', + indent: '\t', + searchPath: ['alt', 'alt2'], + src: [ + inputDir + 'alt/foo.js', + inputDir + 'alt/bar.js', + inputDir + 'alt2/foobar.js', + ], + raw: [ + '\t', + '\t', + '\t', + '\t', + '\t' + ] + } + ]; + + expect(blocks).to.eql(outcome); + }); });