From d7dcb7ab55fc4c874f8f143d62fc84a7a6e1b981 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Thu, 9 Apr 2015 17:54:44 +0200 Subject: [PATCH 1/2] fix tests --- Gruntfile.js | 6 +-- npm-debug.log | 110 ------------------------------------------------ tasks/inline.js | 14 ++++-- 3 files changed, 13 insertions(+), 117 deletions(-) delete mode 100644 npm-debug.log diff --git a/Gruntfile.js b/Gruntfile.js index 59e6e32..c255ecf 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,13 +4,13 @@ module.exports = function(grunt) { grunt.initConfig({ inline: { dist: { - src: ['test/dist/*.html'], - dest: ['tmp/'] + src: 'test/dist/*.html', + dest: 'tmp/' } }, clean: { - tests: ['tmp/'] + tests: 'tmp/' }, htmlmin: { diff --git a/npm-debug.log b/npm-debug.log deleted file mode 100644 index eeac3e8..0000000 --- a/npm-debug.log +++ /dev/null @@ -1,110 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ 'node', '/usr/local/bin/npm', 'publish' ] -2 info using npm@1.4.14 -3 info using node@v0.10.29 -4 verbose publish [ '.' ] -5 verbose cache add [ '.', null ] -6 verbose cache add name=undefined spec="." args=[".",null] -7 verbose parsed url { protocol: null, -7 verbose parsed url slashes: null, -7 verbose parsed url auth: null, -7 verbose parsed url host: null, -7 verbose parsed url port: null, -7 verbose parsed url hostname: null, -7 verbose parsed url hash: null, -7 verbose parsed url search: null, -7 verbose parsed url query: null, -7 verbose parsed url pathname: '.', -7 verbose parsed url path: '.', -7 verbose parsed url href: '.' } -8 silly lockFile 3a52ce78- . -9 verbose lock . /Users/a/.npm/3a52ce78-.lock -10 verbose tar pack [ '/Users/a/.npm/grunt-inline/0.3.3/package.tgz', '.' ] -11 verbose tarball /Users/a/.npm/grunt-inline/0.3.3/package.tgz -12 verbose folder . -13 info prepublish grunt-inline@0.3.3 -14 silly lockFile 1f1177db-tar tar://. -15 verbose lock tar://. /Users/a/.npm/1f1177db-tar.lock -16 silly lockFile 9f87bd96-m-grunt-inline-0-3-3-package-tgz tar:///Users/a/.npm/grunt-inline/0.3.3/package.tgz -17 verbose lock tar:///Users/a/.npm/grunt-inline/0.3.3/package.tgz /Users/a/.npm/9f87bd96-m-grunt-inline-0-3-3-package-tgz.lock -18 silly lockFile 1f1177db-tar tar://. -19 silly lockFile 1f1177db-tar tar://. -20 silly lockFile 9f87bd96-m-grunt-inline-0-3-3-package-tgz tar:///Users/a/.npm/grunt-inline/0.3.3/package.tgz -21 silly lockFile 9f87bd96-m-grunt-inline-0-3-3-package-tgz tar:///Users/a/.npm/grunt-inline/0.3.3/package.tgz -22 silly lockFile f04d4788-a-npm-grunt-inline-0-3-3-package /Users/a/.npm/grunt-inline/0.3.3/package -23 verbose lock /Users/a/.npm/grunt-inline/0.3.3/package /Users/a/.npm/f04d4788-a-npm-grunt-inline-0-3-3-package.lock -24 silly lockFile f04d4788-a-npm-grunt-inline-0-3-3-package /Users/a/.npm/grunt-inline/0.3.3/package -25 silly lockFile f04d4788-a-npm-grunt-inline-0-3-3-package /Users/a/.npm/grunt-inline/0.3.3/package -26 silly lockFile 3a52ce78- . -27 silly lockFile 3a52ce78- . -28 silly publish { author: -28 silly publish { name: '程序猿小卡', -28 silly publish email: 'chyingp@gmail.com', -28 silly publish url: 'https://github.com/chyingp' }, -28 silly publish name: 'grunt-inline', -28 silly publish description: 'Inlines img, script and link tags into the same file.', -28 silly publish version: '0.3.3', -28 silly publish keywords: [ 'gruntplugin', 'inline', 'js', 'css' ], -28 silly publish main: 'inline', -28 silly publish repository: -28 silly publish { type: 'git', -28 silly publish url: 'https://github.com/chyingp/grunt-inline.git' }, -28 silly publish engines: { node: '>=0.8.0' }, -28 silly publish devDependencies: -28 silly publish { 'grunt-contrib-nodeunit': '~0.1.2', -28 silly publish 'grunt-contrib-clean': '~0.4.0', -28 silly publish 'grunt-contrib-htmlmin': '~0.1.3', -28 silly publish grunt: '~0.4.0' }, -28 silly publish dependencies: { 'uglify-js': '2.4.1', datauri: '~0.2.0', 'clean-css': '1.1.7' }, -28 silly publish scripts: { test: 'grunt test' }, -28 silly publish _id: 'grunt-inline@0.3.3', -28 silly publish _from: '.', -28 silly publish readme: '# grunt-inline[![build status](https://secure.travis-ci.org/miniflycn/grunt-inline.png)](http://travis-ci.org/miniflycn/grunt-inline)\r\n\r\nBrings externally referenced resources, such as js, css and images, into\r\na single file.\r\n\r\nFor exmample:\r\n\r\n````\r\n\r\n````\r\nis replaced with\r\n````\r\n\r\n```\r\n\r\nJavascript references are brought inline, and images in the html\r\nand css blocks are converted to base-64 data: urls.\r\n\r\nBy default, only urls marked with `__inline` are converted, however this\r\nbehavior can be overrided via the `tag:` option.\r\n\r\n\r\n## Getting Started\r\nThis plugin requires Grunt `~0.4.1`\r\n\r\nIf you haven\'t used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you\'re familiar with that process, you may install this plugin with this command:\r\n\r\n\tnpm install grunt-inline --save-dev\r\n\r\nOnce the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:\r\n\r\n\tgrunt.loadNpmTasks(\'grunt-inline\');\r\n\r\n## The "grunt-inline" task\r\n\r\n### Overview\r\nIn your project\'s Gruntfile, add a section named `inline` to the data object passed into `grunt.initConfig()`.\r\n\r\n\tgrunt.initConfig({\r\n\t inline: {\r\n\t dist: {\r\n\t src: \'src/index.html\',\r\n\t dest: \'dist/index.html\'\r\n\t }\r\n\t }\r\n\t})\r\n### Options\r\n\r\n#### dest\r\nIf dest is assigned, the the source file will be copied to the destination path. eg:\r\n`src/index.html` will be processed and then copied to `dist/index.html`\r\n\r\n```\r\ngrunt.initConfig({\r\n\tinline: {\r\n\t\tdist: {\r\n\t\t\tsrc: \'src/index.html\',\r\n\t\t\tdest: \'dist/index.html\'\r\n\t\t}\r\n\t}\r\n});\r\n```\r\n\r\n### cssmin\r\nIf cssmin is assigned true, `.css` will be minified before inlined.\r\n\r\n```\r\ngrunt.initConfig({\r\n\tinline: {\r\n\t\tdist: {\r\n\t\t\toptions:{\r\n\t\t\t\tcssmin: true\r\n\t\t\t},\r\n\t\t\tsrc: \'src/index.html\',\r\n\t\t\tdest: \'dist/index.html\'\r\n\t\t}\r\n\t}\r\n});\r\n```\r\n\r\n### tag (defaults to ```__inline```)\r\nOnly URLs that contain the value for ```tag``` will be inlined.\r\nSpecify ```tag: \'\'``` to include all urls.\r\n\r\n```\r\ngrunt.initConfig({\r\n\tinline: {\r\n\t\tdist: {\r\n\t\t\toptions:{\r\n\t\t\t\ttag: \'\'\r\n\t\t\t},\r\n\t\t\tsrc: \'src/index.html\',\r\n\t\t\tdest: \'dist/index.html\']\r\n\t\t}\r\n\t}\r\n});\r\n```\r\n\r\n\r\n### inlineTagAttributes\r\nAbility to add attributes string to inline tag.\r\n\r\n```\r\ngrunt.initConfig({\r\n\tinline: {\r\n\t\tdist: {\r\n\t\t\toptions:{\r\n\t\t\t\tinlineTagAttributes: {\r\n\t\t\t\t\tjs: \'data-inlined="true"\',\t// Adds ``````\r\n\t\t\t\t\tcss: \'data-inlined="true"\'\t// Adds ``````\r\n\t\t\t},\r\n\t\t\tsrc: \'src/index.html\',\r\n\t\t\tdest: \'dist/index.html\'\r\n\t\t}\r\n\t}\r\n});\r\n```\r\n\r\n\r\n### uglify\r\nIf uglify is assigned true, `.js` file will be minified before inlined.\r\n\r\n```\r\ngrunt.initConfig({\r\n\tinline: {\r\n\t\tdist: {\r\n\t\t\toptions:{\r\n\t\t\t\tuglify: true\r\n\t\t\t},\r\n\t\t\tsrc: \'src/index.html\',\r\n\t\t\tdest: \'dist/index.html\'\r\n\t\t}\r\n\t}\r\n});\r\n```\r\n\r\n### exts\r\nSetting an exts array allows multiple file extensions to be processed as\r\nhtml.\r\n\r\n```\r\ngrunt.initConfig({\r\n\tinline: {\r\n\t\tdist: {\r\n\t\t\toptions:{\r\n\t\t\t exts: [\'jade\'],\r\n\t\t\t\tuglify: true\r\n\t\t\t},\r\n\t\t\tsrc: \'src/index.jade\',\r\n\t\t\tdest: \'dist/index.jade\'\r\n\t\t}\r\n\t}\r\n});\r\n```\r\n\r\n### Usage Examples\r\n\r\n> config\r\n\r\n\tgrunt.initConfig({\r\n\t inline: {\r\n\t dist: {\r\n\t src: \'src/index.html\'\r\n\t }\r\n\t }\r\n\t})\r\n\r\n> src/index.html\r\n\r\n\t\r\n\t\t\r\n\t\t\tdemo\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t\r\n\r\n\t\t\t\r\n\t\t\r\n\t\r\n\r\n> after `grunt inline` was run, it will be something like\r\n\r\n\t\r\n\t\t\r\n\t\t\tdemo\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t\r\n\t\t\t\r\n\r\n\t\t\t\r\n\t\t\r\n\t\r\n\r\n#### inline tag\r\nSuppose there is an `` tag in `index.html` like bellow\r\n\r\n```\r\n\r\n\r\n```\r\n\r\nThe content of `test.html` is\r\n\r\n```\r\n

I\'m inline html

\r\nhello world!\r\n```\r\n\r\nThen, after the `inline` task is run, the original content in `index.html` will be replaced with\r\n\r\n```\r\n

I\'m inline html

\r\nhello world!\r\n```\r\n\r\n## Contributing\r\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).\r\n\r\n## Release History\r\n* 2015-01-09 v0.3.3 bug fix: when processing files of a folder and then copy the processed content to another destination, the original files are changed unexpectedly, as mentioned in this issue [Support file globbing for input and output](https://github.com/chyingp/grunt-inline/issues/35)\r\n* 2014-06-16 v0.3.1 bug fix: protocol-relative urls in css are messed up\r\n* 2014-06-15 v0.3.1 bug fix: when options.tag is \'\', then all img tags, whose src attribute has already been inlined will be matched.\r\n* 2014-05-19 v0.3.0 support for new options.exts\r\n* 2014-05-19 v0.2.9 bug fix: options.tag is assigned \'\', bug image url in css are not converted to base64 formate\r\n* 2014-03-06 v0.2.6 bug fix: script tags like were not inlined\r\n* 2014-01-31 v0.2.3 radded tag option, encode url(..) images.\r\n* 2013-10-31  v0.2.2 bug fix: img urls like \'background: url(http://www.example.com/img/bg.png)\' will be transformed to \'background: url(url(http://www.example.com/img/bg.png))\'\r\n* 2013-10-30  v0.2.1 bug fix: when processing relative file path of img url in css stylesheet, forgot to transform "\\" to "/" for windows users\r\n* 2013-10-30  v0.2.0 new feature: Support for minifing js、css when they ar inlined into html.\r\n* 2013-08-30  v0.1.9 bug fix: stylesheets ended with ">" cannot be inlined\r\n* 2013-09-02  v0.1.9 add feature: add options.dest to assign a destination path where the source file will be copied\r\n* 2013-09-02  v0.1.8 add feature: support for `` tag\r\n', -28 silly publish readmeFilename: 'README.md', -28 silly publish gitHead: '497e222702d0c6564d3a9d8d1f7979cad803b08f', -28 silly publish bugs: { url: 'https://github.com/chyingp/grunt-inline/issues' }, -28 silly publish homepage: 'https://github.com/chyingp/grunt-inline', -28 silly publish _shasum: '0ae2fb5c1b1569bbea0003db01c015f4371f662d' } -29 verbose url raw grunt-inline -30 verbose url resolving [ 'https://registry.npmjs.org/', './grunt-inline' ] -31 verbose url resolved https://registry.npmjs.org/grunt-inline -32 info trying registry request attempt 1 at 02:27:12 -33 http PUT https://registry.npmjs.org/grunt-inline -34 http 403 https://registry.npmjs.org/grunt-inline -35 verbose headers { date: 'Thu, 08 Jan 2015 18:27:14 GMT', -35 verbose headers server: 'CouchDB/1.5.0 (Erlang OTP/R16B03)', -35 verbose headers 'content-type': 'application/json', -35 verbose headers 'cache-control': 'max-age=60', -35 verbose headers 'content-length': '230', -35 verbose headers 'accept-ranges': 'bytes', -35 verbose headers via: '1.1 varnish', -35 verbose headers 'x-served-by': 'cache-ty66-TYO', -35 verbose headers 'x-cache': 'MISS', -35 verbose headers 'x-cache-hits': '0', -35 verbose headers 'x-timer': 'S1420741634.005146,VS0,VE239', -35 verbose headers 'keep-alive': 'timeout=10, max=50', -35 verbose headers connection: 'Keep-Alive' } -36 error publish Failed PUT 403 -37 error Error: forbidden Attempting to modify version 0.3.3, -37 error which was previously published on 2015-01-08T17:00:33.229Z. -37 error This is forbidden, to maintain package integrity. -37 error Please update the version number and try again.: grunt-inline -37 error at RegClient. (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:274:14) -37 error at Request._callback (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:212:65) -37 error at Request.self.callback (/usr/local/lib/node_modules/npm/node_modules/request/request.js:123:22) -37 error at Request.emit (events.js:98:17) -37 error at Request. (/usr/local/lib/node_modules/npm/node_modules/request/request.js:893:14) -37 error at Request.emit (events.js:117:20) -37 error at IncomingMessage. (/usr/local/lib/node_modules/npm/node_modules/request/request.js:844:12) -37 error at IncomingMessage.emit (events.js:117:20) -37 error at _stream_readable.js:929:16 -37 error at process._tickCallback (node.js:419:13) -38 error If you need help, you may report this *entire* log, -38 error including the npm and node versions, at: -38 error -39 error System Darwin 13.4.0 -40 error command "node" "/usr/local/bin/npm" "publish" -41 error cwd /Users/a/Documents/git-code/grunt-inline -42 error node -v v0.10.29 -43 error npm -v 1.4.14 -44 verbose exit [ 1, true ] diff --git a/tasks/inline.js b/tasks/inline.js index 556077d..a053102 100644 --- a/tasks/inline.js +++ b/tasks/inline.js @@ -34,7 +34,7 @@ module.exports = function(grunt) { var fileContent = grunt.file.read(filepath); var destFilepath = ''; - grunt.log.write('Processing ' + filepath + '...') + grunt.log.write('Processing ' + filepath + '... '); if(fileType==='html' || (exts && exts.indexOf(fileType) > -1)){ fileContent = html(filepath, fileContent, relativeTo, options); @@ -43,17 +43,21 @@ module.exports = function(grunt) { } if(detectDestType(filePair.dest) === 'directory') { - destFilepath = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, filepath)); + destFilepath = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, fileName(filepath))); }else{ destFilepath = filePair.dest || filepath; } grunt.file.write(destFilepath, fileContent); - grunt.log.ok() + grunt.log.ok(); }); }); }); + function fileName(filePath) { + return filePath.replace(/^.*[\\\/]/, ''); + } + function isRemotePath( url ){ return url.match(/^'?https?:\/\//) || url.match(/^\/\//); } @@ -69,7 +73,7 @@ module.exports = function(grunt) { } else { return 'file'; } - } + } function unixifyPath(filepath) { if (process.platform === 'win32') { @@ -84,11 +88,13 @@ module.exports = function(grunt) { var isDestinationDirectory = (/\/$/).test(pathToDestinationFile); var fileName = path.basename(pathToSource); var newPathToDestination; + if (typeof pathToDestinationFile === 'undefined') { newPathToDestination = pathToSource; } else { newPathToDestination = pathToDestinationFile + (isDestinationDirectory ? fileName : ''); } + return newPathToDestination; } From c3de4686d7a6f8c83a76be5aff8bfae812df0aa0 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Thu, 9 Apr 2015 18:05:57 +0200 Subject: [PATCH 2/2] perform some trivial code cleanups --- tasks/inline.js | 127 ++++++++++++++++++------------------------------ 1 file changed, 46 insertions(+), 81 deletions(-) diff --git a/tasks/inline.js b/tasks/inline.js index a053102..9a72a5f 100644 --- a/tasks/inline.js +++ b/tasks/inline.js @@ -17,19 +17,15 @@ module.exports = function(grunt) { grunt.registerMultiTask('inline', "Replaces , '; - }else{ - grunt.log.error("Couldn't find " + inlineFilePath + '!'); + return ''; } - } - grunt.log.debug('ret = : ' + ret +'\n'); - - return ret; - }).replace(//g, function(matchedWord, src){ - var ret = matchedWord; + grunt.log.error("Couldn't find " + inlineFilePath + '!'); + } - if(!isRemotePath(src) && src.indexOf(options.tag)!=-1){ + return matchedWord; + }).replace(//g, function(matchedWord, src){ + if(!isRemotePath(src) && src.indexOf(options.tag)!=-1) { var inlineFilePath = path.resolve( path.dirname(filepath), src ).replace(/\?.*$/, ''); // 将参数去掉 if( grunt.file.exists(inlineFilePath) ){ var styleSheetContent = grunt.file.read( inlineFilePath ); - ret = ''; - }else{ - grunt.log.error("Couldn't find " + inlineFilePath + '!'); + + return ''; } + + grunt.log.error("Couldn't find " + inlineFilePath + '!'); } - grunt.log.debug('ret = : ' + ret +'\n'); - return ret; + return matchedWord; }).replace(//g, function(matchedWord, src){ - var ret = matchedWord; - if(!grunt.file.isPathAbsolute(src) && src.indexOf(options.tag)!=-1){ var inlineFilePath = path.resolve( path.dirname(filepath), src ).replace(/\?.*$/, ''); // 将参数去掉 if( grunt.file.exists(inlineFilePath) ){ - ret = matchedWord.replace(src, (new datauri(inlineFilePath)).content); - }else{ + return matchedWord.replace(src, (new datauri(inlineFilePath)).content); + } else { grunt.log.error("Couldn't find " + inlineFilePath + '!'); } - } - grunt.log.debug('ret = : ' + ret +'\n'); + } - return ret; + return matchedWord; }); return fileContent; } function css(filepath, fileContent, relativeTo, options) { - if(relativeTo){ + if(relativeTo) { filepath = filepath.replace(/[^\/]+\//g, relativeTo); } - fileContent = fileContent.replace(/url\(["']*([^)'"]+)["']*\)/g, function(matchedWord, imgUrl){ - var newUrl = imgUrl; + fileContent = fileContent.replace(/url\(["']*([^)'"]+)["']*\)/g, function(matchedWord, imgUrl) { var flag = imgUrl.indexOf(options.tag)!=-1; // urls like "img/bg.png?__inline" will be transformed to base64 + if(isBase64Path(imgUrl) || isRemotePath(imgUrl)){ return matchedWord; } - grunt.log.debug( 'imgUrl: '+imgUrl); - grunt.log.debug( 'filepath: '+filepath); + var absoluteImgurl = path.resolve( path.dirname(filepath),imgUrl ); - grunt.log.debug( 'absoluteImgurl: '+absoluteImgurl); - newUrl = path.relative( path.dirname(filepath), absoluteImgurl ); - grunt.log.debug( 'newUrl: '+newUrl); + var newUrl = path.relative( path.dirname(filepath), absoluteImgurl ); absoluteImgurl = absoluteImgurl.replace(/\?.*$/, ''); - if(flag && grunt.file.exists(absoluteImgurl)){ + + if(flag && grunt.file.exists(absoluteImgurl)) { newUrl = datauri(absoluteImgurl); - }else{ + } else { newUrl = newUrl.replace(/\\/g, '/'); } return matchedWord.replace(imgUrl, newUrl); }); - fileContent = options.cssmin ? CleanCSS.process(fileContent) : fileContent; - return fileContent; + return options.cssmin ? CleanCSS.process(fileContent) : fileContent; } function cssInlineToHtml(htmlFilepath, filepath, fileContent, relativeTo, options) { @@ -223,31 +193,26 @@ module.exports = function(grunt) { } fileContent = fileContent.replace(/url\(["']*([^)'"]+)["']*\)/g, function(matchedWord, imgUrl){ - var newUrl = imgUrl; var flag = !!imgUrl.match(/\?__inline/); // urls like "img/bg.png?__inline" will be transformed to base64 - grunt.log.debug('flag:'+flag); + if(isBase64Path(imgUrl) || isRemotePath(imgUrl)){ return matchedWord; } - grunt.log.debug( 'imgUrl: '+imgUrl); - grunt.log.debug( 'filepath: '+filepath); - var absoluteImgurl = path.resolve( path.dirname(filepath),imgUrl ); // img url relative to project root - grunt.log.debug( 'absoluteImgurl: '+absoluteImgurl); - newUrl = path.relative( path.dirname(htmlFilepath), absoluteImgurl ); // img url relative to the html file - grunt.log.debug([htmlFilepath, filepath, absoluteImgurl, imgUrl]); - grunt.log.debug( 'newUrl: '+newUrl); + + var absoluteImgurl = path.resolve( path.dirname(filepath), imgUrl ); // img url relative to project root + var newUrl = path.relative( path.dirname(htmlFilepath), absoluteImgurl ); // img url relative to the html file absoluteImgurl = absoluteImgurl.replace(/\?.*$/, ''); + if(flag && grunt.file.exists(absoluteImgurl)){ newUrl = datauri(absoluteImgurl); - }else{ + } else { newUrl = newUrl.replace(/\\/g, '/'); } return matchedWord.replace(imgUrl, newUrl); }); - fileContent = options.cssmin ? CleanCSS.process(fileContent) : fileContent; - return fileContent; + return options.cssmin ? CleanCSS.process(fileContent) : fileContent; } };