diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..93c10af --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +test/integration/scenarios/*/src/* +test/integration/scenarios/*/webpack.config.js + diff --git a/.gitignore b/.gitignore index 73ecdc8..95eb8b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ node_modules *.log *.swp +examples/*/build/* +/tmp/*/build/* +!/tmp/*/build/.keep diff --git a/.travis.yml b/.travis.yml index c6fa5b8..e912b52 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js node_js: + - 0.12 - 4 - 5 - 6 @@ -24,6 +25,4 @@ cache: directories: - node_modules - example/node_modules - -script: - - npm run test:ci + - tmp diff --git a/example/build/assets/26ae530c61260c72c117/main-26ae530c61260c72c117.js b/example/build/assets/26ae530c61260c72c117/main-26ae530c61260c72c117.js deleted file mode 100644 index 5ef5fa4..0000000 --- a/example/build/assets/26ae530c61260c72c117/main-26ae530c61260c72c117.js +++ /dev/null @@ -1 +0,0 @@ -!function(o){function e(t){if(r[t])return r[t].exports;var n=r[t]={exports:{},id:t,loaded:!1};return o[t].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var r={};return e.m=o,e.c=r,e.p="/assets/26ae530c61260c72c117/",e(0)}([function(o,e,r){o.exports=r(3)},function(o,e){},function(o,e,r){o.exports=r.p+"images/maru-6d18eff577039c3cd85aa19b82571845.jpg"},function(o,e,r){var t=r(2);r(1),console.log("hello world!",t)}]); \ No newline at end of file diff --git a/example/build/assets/26ae530c61260c72c117/main-f170406bbf80db11a0f7afdee504fa52.css b/example/build/assets/26ae530c61260c72c117/main-f170406bbf80db11a0f7afdee504fa52.css deleted file mode 100644 index 027717f..0000000 --- a/example/build/assets/26ae530c61260c72c117/main-f170406bbf80db11a0f7afdee504fa52.css +++ /dev/null @@ -1 +0,0 @@ -body{background:blue} \ No newline at end of file diff --git a/example/build/sprockets-manifest.json b/example/build/sprockets-manifest.json deleted file mode 100644 index a5d6558..0000000 --- a/example/build/sprockets-manifest.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "assets": { - "images/maru.jpg": "images/maru-6d18eff577039c3cd85aa19b82571845.jpg", - "main.js": "main-26ae530c61260c72c117.js", - "main.css": "main-f170406bbf80db11a0f7afdee504fa52.css" - }, - "files": { - "images/maru-6d18eff577039c3cd85aa19b82571845.jpg": { - "logical_path": "images/maru.jpg", - "size": 48416, - "digest": "6d18eff577039c3cd85aa19b82571845", - "mtime": "2016-06-21T08:40:31.275Z" - }, - "main-26ae530c61260c72c117.js": { - "integrity": "sha384-qNGoZqjcdOcN459HxyvHZ5iMTV7hA2akUt50kAsWfG2Kabx9EaFnyBagc7bKL/bn", - "size": 424, - "digest": "663749fe8d2219622d050e3ea2111ced", - "logical_path": "main.js", - "mtime": "2016-06-21T08:40:31.275Z" - }, - "main-f170406bbf80db11a0f7afdee504fa52.css": { - "integrity": "sha384-lkGaxwmX5qUkXJGM8wX4uMLH+AO5QZKlhbZdtQS0niV3w5EeOnJ0HsNDSEcvFFu1", - "size": 21, - "digest": "f170406bbf80db11a0f7afdee504fa52", - "logical_path": "main.css", - "mtime": "2016-06-21T08:40:31.275Z" - } - }, - "hash": "26ae530c61260c72c117", - "publicPath": "/assets/26ae530c61260c72c117/" -} \ No newline at end of file diff --git a/example/test/fixtures/sprockets-manifest.json b/example/test/fixtures/sprockets-manifest.json deleted file mode 100644 index 81d98fc..0000000 --- a/example/test/fixtures/sprockets-manifest.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "assets": { - "images/maru.jpg": "images/maru-6d18eff577039c3cd85aa19b82571845.jpg", - "main.js": "main-26ae530c61260c72c117.js", - "main.css": "main-f170406bbf80db11a0f7afdee504fa52.css" - }, - "files": { - "images/maru-6d18eff577039c3cd85aa19b82571845.jpg": { - "logical_path": "images/maru.jpg", - "size": 48416, - "digest": "6d18eff577039c3cd85aa19b82571845", - "mtime": "2016-06-21T07:42:09.623Z" - }, - "main-26ae530c61260c72c117.js": { - "integrity": "sha384-qNGoZqjcdOcN459HxyvHZ5iMTV7hA2akUt50kAsWfG2Kabx9EaFnyBagc7bKL/bn", - "size": 424, - "digest": "663749fe8d2219622d050e3ea2111ced", - "logical_path": "main.js", - "mtime": "2016-06-21T07:42:09.623Z" - }, - "main-f170406bbf80db11a0f7afdee504fa52.css": { - "integrity": "sha384-lkGaxwmX5qUkXJGM8wX4uMLH+AO5QZKlhbZdtQS0niV3w5EeOnJ0HsNDSEcvFFu1", - "size": 21, - "digest": "f170406bbf80db11a0f7afdee504fa52", - "logical_path": "main.css", - "mtime": "2016-06-21T07:42:09.623Z" - } - }, - "hash": "26ae530c61260c72c117", - "publicPath": "/assets/26ae530c61260c72c117/" -} diff --git a/example/package.json b/examples/01_simple/package.json similarity index 76% rename from example/package.json rename to examples/01_simple/package.json index 2ae4682..d80b79e 100644 --- a/example/package.json +++ b/examples/01_simple/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "build": "NODE_ENV=production webpack --config webpack.config.js --progress --profile --colors", + "build": "npm run reinstall && NODE_ENV=production webpack --config webpack.config.js --progress --profile --colors", "reinstall": "npm uninstall sprockets-stats-webpack-plugin && npm install" }, "author": "", @@ -14,7 +14,7 @@ "css-loader": "^0.23.1", "extract-text-webpack-plugin": "^0.9.1", "file-loader": "^0.8.5", - "sprockets-stats-webpack-plugin": "file:../", + "sprockets-stats-webpack-plugin": "file:../../", "sri-stats-webpack-plugin": "^0.7.0", "style-loader": "^0.13.0", "url-loader": "^0.5.7", diff --git a/example/src/assets/images/maru.jpg b/examples/01_simple/src/assets/images/maru.jpg similarity index 100% rename from example/src/assets/images/maru.jpg rename to examples/01_simple/src/assets/images/maru.jpg diff --git a/example/src/assets/stylesheets/application.css b/examples/01_simple/src/assets/stylesheets/application.css similarity index 100% rename from example/src/assets/stylesheets/application.css rename to examples/01_simple/src/assets/stylesheets/application.css diff --git a/example/src/index.js b/examples/01_simple/src/index.js similarity index 100% rename from example/src/index.js rename to examples/01_simple/src/index.js diff --git a/example/test/SprocketsManifest.test.js b/examples/01_simple/test/SprocketsManifest.test.js similarity index 86% rename from example/test/SprocketsManifest.test.js rename to examples/01_simple/test/SprocketsManifest.test.js index 30fd37d..77d0d90 100644 --- a/example/test/SprocketsManifest.test.js +++ b/examples/01_simple/test/SprocketsManifest.test.js @@ -12,8 +12,10 @@ test('generated sprockets manifest matches expected', function(t) { t.truthy(Date.parse(file.mtime), 'not a valid date'); - file.mtime = '2016-06-21T07:42:09.623Z'; + file.mtime = '2016-06-27T20:53:11.389Z'; }); + actualManifest.hash = 'test'; + t.deepEqual(actualManifest, expectedManifest, 'manifests not equal'); }); diff --git a/examples/01_simple/test/fixtures/sprockets-manifest.json b/examples/01_simple/test/fixtures/sprockets-manifest.json new file mode 100644 index 0000000..9a142f7 --- /dev/null +++ b/examples/01_simple/test/fixtures/sprockets-manifest.json @@ -0,0 +1,31 @@ +{ + "assets": { + "images/maru.jpg": "images/maru-01-test.jpg", + "main.js": "main-01-test.js", + "main.css": "main-01-test.css" + }, + "files": { + "images/maru-01-test.jpg": { + "logical_path": "images/maru.jpg", + "size": 48416, + "digest": "6d18eff577039c3cd85aa19b82571845", + "mtime": "2016-06-27T20:53:11.389Z" + }, + "main-01-test.js": { + "integrity": "sha384-9WCBflot2kBNysprVPr2Cspm01ivh4eg4OOLQPBOFPA/V96H3eWOFreCuQedp892", + "size": 386, + "digest": "bfe15df886795b3cfdba39725fe0761a", + "logical_path": "main.js", + "mtime": "2016-06-27T20:53:11.389Z" + }, + "main-01-test.css": { + "integrity": "sha384-lkGaxwmX5qUkXJGM8wX4uMLH+AO5QZKlhbZdtQS0niV3w5EeOnJ0HsNDSEcvFFu1", + "size": 21, + "digest": "f170406bbf80db11a0f7afdee504fa52", + "logical_path": "main.css", + "mtime": "2016-06-27T20:53:11.389Z" + } + }, + "hash": "test", + "publicPath": "/assets/01-test/" +} diff --git a/example/webpack.config.js b/examples/01_simple/webpack.config.js similarity index 80% rename from example/webpack.config.js rename to examples/01_simple/webpack.config.js index 34a650e..51ba1f2 100644 --- a/example/webpack.config.js +++ b/examples/01_simple/webpack.config.js @@ -21,11 +21,11 @@ module.exports = { }, "output": { - "path": path.join(PROJECT_DIR, "build", "assets", "[hash]"), - "publicPath": "/assets/[hash]/", - "filename": "[name]-[hash].js", - "chunkFilename": "chunk-[id].[name]-[hash].js", - "sourceMapFilename": "debug/[file]-[hash].id-[id].map", + "path": path.join(PROJECT_DIR, "build", "assets", "01-test"), + "publicPath": "/assets/01-test/", + "filename": "[name]-01-test.js", + "chunkFilename": "chunk-[id].[name]-01-test.js", + "sourceMapFilename": "debug/[file]-01-test.map", "pathInfo": "false" }, @@ -51,7 +51,7 @@ module.exports = { }, { "test": /\.jpg$/, - "loader": "url?limit=10&name=images/[name]-[hash].[ext]" + "loader": "url?limit=10&name=images/[name]-01-test.[ext]" } ] }, @@ -59,6 +59,8 @@ module.exports = { "plugins": [ new webpack.NoErrorsPlugin(), new CleanPlugin(["build"]), + new webpack.optimize.OccurenceOrderPlugin(), + new webpack.optimize.AggressiveMergingPlugin(), new webpack.optimize.DedupePlugin(), new webpack.optimize.UglifyJsPlugin({ output: { @@ -68,9 +70,7 @@ module.exports = { warnings: false } }), - new webpack.optimize.OccurenceOrderPlugin(), - new webpack.optimize.AggressiveMergingPlugin(), - new ExtractTextPlugin("[name]-[contenthash].css"), + new ExtractTextPlugin("[name]-01-test.css"), new SriStatsPlugin({ customStatsKey: "rails" }), diff --git a/index.js b/index.js index 1e7d89f..e33df0a 100644 --- a/index.js +++ b/index.js @@ -71,19 +71,25 @@ SprocketsStatsWebpackPlugin.prototype.apply = function(compiler) { }); compilation.plugin('module-asset', function(mod, filename) { - var logicalPath = ""; + var logicalPath = ''; var filenameKey = Object.keys(mod.assets).slice(-1)[0]; if (mappings.length > 0) { + /* eslint-disable vars-on-top */ // loaderUtils.interpolatePath takes a loader context but only uses the // resourcePath property, so let's create a stub - var loaderContextStub = {resourcePath: mod.userRequest}; + var loaderContextStub = { resourcePath: mod.userRequest }; + var i; + /* eslint-enable vars-on-top */ - for (let mapping of mappings) { + for (i = 0; i < mappings.length; i++) { + /* eslint-disable vars-on-top */ + var mapping = mappings[i]; var re = new RegExp(mapping.test); var match = mod.userRequest.match(re); + /* eslint-enable vars-on-top */ - if(match) { + if (match) { logicalPath = loaderUtils.interpolateName( loaderContextStub, mapping.logicalPath, @@ -111,6 +117,24 @@ SprocketsStatsWebpackPlugin.prototype.apply = function(compiler) { var walker = walk.walk(outputAssetsPath); var assets = stats.toJson().assets; + assets.forEach(function(asset) { + var hashedAssetName = asset.name; + var assetName; + var assetExt; + var filename; + + if ((asset.chunks && asset.chunks.length > 0) && + (asset.chunkNames && asset.chunkNames.length > 0) + ) { + assetName = asset.chunkNames.slice(-1)[0]; + assetExt = hashedAssetName.split('.').pop(); + + filename = assetName + '.' + assetExt; + + sprockets[hashedAssetName].logical_path = filename; + } + }); + walker.on('file', function(rootPath, fileStat, next) { var fullPath = path.join(rootPath, fileStat.name); var filename = (path.relative(outputPath, fullPath)); @@ -140,6 +164,7 @@ SprocketsStatsWebpackPlugin.prototype.apply = function(compiler) { Object.keys(output.files).forEach(function(filename) { var asset = output.files[filename]; + output.assets[asset.logical_path] = filename; }); diff --git a/package.json b/package.json index f583563..ba7b62f 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,6 @@ ], "scripts": { "test": "ava", - "build:integration": "cd example && npm run reinstall && npm run build", - "test:all": "npm run test && npm run build:integration && ava ./example/test/SprocketsManifest.test.js", - "test:ci": "npm run test && ava ./example/test/SprocketsManifest.test.js", "lint": "eslint ./test ./lib index.js", "check": "npm run lint && npm run test" }, @@ -35,10 +32,18 @@ "homepage": "https://github.com/mikechau/sprockets-stats-webpack-plugin#readme", "devDependencies": { "ava": "^0.15.2", + "css-loader": "^0.23.1", "eslint": "^2.13.1", "eslint-config-mc": "^0.6.5", "eslint-plugin-ava": "^2.5.0", "eslint-plugin-import": "^1.8.1", + "extract-text-webpack-plugin": "^1.0.1", + "file-loader": "^0.9.0", + "memory-fs": "^0.3.0", + "remove-webpack-plugin": "^1.1.1", + "sri-stats-webpack-plugin": "^0.7.3", + "style-loader": "^0.13.1", + "url-loader": "^0.5.7", "webpack": "^1.13.1" }, "dependencies": { @@ -49,7 +54,8 @@ }, "ava": { "files": [ - "test/**/*.js" + "test/**/*.test.js", + "!test/integration/scenarios/**/*" ], "failFast": true, "verbose": true diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 0ac65bd..a33d0cc 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -1,6 +1,7 @@ { "extends": "eslint-config-mc/ava", "rules": { - "func-names": 0 + "func-names": 0, + "consistent-return": 0 } } diff --git a/test/integration/scenarios/rails/app/assets/images/maru-2.jpg b/test/integration/scenarios/rails/app/assets/images/maru-2.jpg new file mode 100644 index 0000000..5ed00e4 Binary files /dev/null and b/test/integration/scenarios/rails/app/assets/images/maru-2.jpg differ diff --git a/test/integration/scenarios/rails/app/assets/javascripts/application.js b/test/integration/scenarios/rails/app/assets/javascripts/application.js new file mode 100644 index 0000000..8339a13 --- /dev/null +++ b/test/integration/scenarios/rails/app/assets/javascripts/application.js @@ -0,0 +1 @@ +console.log('hi2u'); diff --git a/test/integration/scenarios/rails/client/src/assets/fonts/lato-regular-webfont.eot b/test/integration/scenarios/rails/client/src/assets/fonts/lato-regular-webfont.eot new file mode 100644 index 0000000..519ae5d Binary files /dev/null and b/test/integration/scenarios/rails/client/src/assets/fonts/lato-regular-webfont.eot differ diff --git a/test/integration/scenarios/rails/client/src/index.js b/test/integration/scenarios/rails/client/src/index.js new file mode 100644 index 0000000..45962f5 --- /dev/null +++ b/test/integration/scenarios/rails/client/src/index.js @@ -0,0 +1,3 @@ +var font = require('./src/assets/fonts'); + +console.log(font); diff --git a/test/integration/scenarios/rails/client/webpack.config.js b/test/integration/scenarios/rails/client/webpack.config.js new file mode 100644 index 0000000..a0a2898 --- /dev/null +++ b/test/integration/scenarios/rails/client/webpack.config.js @@ -0,0 +1,106 @@ +/* eslint quotes: [2, "double", "avoid-escape"] */ + +"use strict"; + +var webpack = require("webpack"); +var path = require("path"); +var RemoveWebpackPlugin = require("remove-webpack-plugin"); +var ExtractTextPlugin = require("extract-text-webpack-plugin"); +var SriStatsPlugin = require("sri-stats-webpack-plugin"); +var SprocketsStatsPlugin = require("../../../../../"); +var merge = require('lodash.merge'); + +module.exports = function(webpackDir, params) { + var opts = merge({ + sprockets: { + write: false + }, + sri: { + runAfterEmit: false + } + }, params); + + return { + "devtool": "false", + + "entry": { + "main": [ + path.resolve(__dirname, "./src/index.js") + ] + }, + + "output": { + "path": path.resolve(webpackDir, "build", "assets", "02-test"), + "publicPath": "/assets/02-test/", + "filename": "[name]-02-test.js", + "chunkFilename": "chunk-[id].[name]-02-test.js", + "sourceMapFilename": "debug/[file]-02-test.map", + "pathInfo": "false" + }, + + "debug": false, + + "resolve": { + "root": path.resolve(__dirname, "./src"), + "extensions": ["", ".js"], + "alias": { + "app": path.resolve(__dirname, "./src"), + "railsAssets": path.resolve(__dirname, "../app/assets") + } + }, + + "resolveLoader": { + "root": path.resolve(__dirname, "../../../../../node_modules") + }, + + "module": { + "loaders": [ + { + "test": /\.css$/, + "loader": ExtractTextPlugin.extract("style-loader", "css") + }, + { + "test": /\.jpg$/, + "loader": "url?limit=10&name=images/[name]-02-test.[ext]" + }, + { + "test": /\.eot$/, + "loader": "url?limit=10&name=fonts/[name]-02-test.[ext]" + } + ] + }, + + "plugins": [ + new webpack.NoErrorsPlugin(), + function() { + if (opts.sprockets.write) { + return new RemoveWebpackPlugin(path.resolve(webpackDir, "build", "sprockets-manifest.json")); + } + }, + new webpack.optimize.OccurenceOrderPlugin(), + new webpack.optimize.AggressiveMergingPlugin(), + new webpack.optimize.DedupePlugin(), + new webpack.optimize.UglifyJsPlugin({ + output: { + comments: false + }, + compress: { + warnings: false + } + }), + new ExtractTextPlugin("[name]-02-test.css"), + new SriStatsPlugin({ + customStatsKey: "rails", + runAfterEmit: opts.sri.runAfterEmit + }), + new SprocketsStatsPlugin({ + customStatsKey: "rails", + saveAs: path.resolve(webpackDir, "build", "sprockets-manifest.json"), + write: opts.sprockets.write, + outputAssetsPath: path.resolve(webpackDir, "build", "assets", "02-test"), + sourceAssetsPath: path.resolve(__dirname, "./src", "assets"), + mappings: [] + }) + ] + }; +}; diff --git a/test/integration/scenarios/simple/fixtures/01-sprockets-manifest.json b/test/integration/scenarios/simple/fixtures/01-sprockets-manifest.json new file mode 100644 index 0000000..36fb750 --- /dev/null +++ b/test/integration/scenarios/simple/fixtures/01-sprockets-manifest.json @@ -0,0 +1,28 @@ +{ + "assets": { + "images/maru.jpg": "images/maru-01-test.jpg", + "main.js": "main-01-test.js", + "main.css": "main-01-test.css" + }, + "files": { + "images/maru-01-test.jpg": { + "logical_path": "images/maru.jpg", + "size": 48416, + "digest": "6d18eff577039c3cd85aa19b82571845" + }, + "main-01-test.js": { + "integrity": "sha384-9WCBflot2kBNysprVPr2Cspm01ivh4eg4OOLQPBOFPA/V96H3eWOFreCuQedp892", + "size": 386, + "digest": "bfe15df886795b3cfdba39725fe0761a", + "logical_path": "main.js" + }, + "main-01-test.css": { + "integrity": "sha384-lkGaxwmX5qUkXJGM8wX4uMLH+AO5QZKlhbZdtQS0niV3w5EeOnJ0HsNDSEcvFFu1", + "size": 21, + "digest": "f170406bbf80db11a0f7afdee504fa52", + "logical_path": "main.css" + } + }, + "hash": "85acf47b215574899a78", + "publicPath": "/assets/01-test/" +} diff --git a/example/build/assets/26ae530c61260c72c117/images/maru-6d18eff577039c3cd85aa19b82571845.jpg b/test/integration/scenarios/simple/src/assets/images/maru.jpg similarity index 100% rename from example/build/assets/26ae530c61260c72c117/images/maru-6d18eff577039c3cd85aa19b82571845.jpg rename to test/integration/scenarios/simple/src/assets/images/maru.jpg diff --git a/test/integration/scenarios/simple/src/assets/stylesheets/application.css b/test/integration/scenarios/simple/src/assets/stylesheets/application.css new file mode 100644 index 0000000..ae844dc --- /dev/null +++ b/test/integration/scenarios/simple/src/assets/stylesheets/application.css @@ -0,0 +1,3 @@ +body { + background: blue; +} diff --git a/test/integration/scenarios/simple/src/index.js b/test/integration/scenarios/simple/src/index.js new file mode 100644 index 0000000..39271f0 --- /dev/null +++ b/test/integration/scenarios/simple/src/index.js @@ -0,0 +1,5 @@ +var maru = require('./assets/images/maru.jpg'); +require('./assets/stylesheets/application.css'); + +console.log('hello world!', maru); + diff --git a/test/integration/scenarios/simple/webpack.config.js b/test/integration/scenarios/simple/webpack.config.js new file mode 100644 index 0000000..a15c2dc --- /dev/null +++ b/test/integration/scenarios/simple/webpack.config.js @@ -0,0 +1,100 @@ +/* eslint quotes: [2, "double", "avoid-escape"] */ + +"use strict"; + +var webpack = require("webpack"); +var path = require("path"); +var RemoveWebpackPlugin = require("remove-webpack-plugin"); +var ExtractTextPlugin = require("extract-text-webpack-plugin"); +var SriStatsPlugin = require("sri-stats-webpack-plugin"); +var SprocketsStatsPlugin = require("../../../../"); +var merge = require('lodash.merge'); + +module.exports = function(webpackDir, params) { + var opts = merge({ + sprockets: { + write: false + }, + sri: { + runAfterEmit: false + } + }, params); + + return { + "devtool": "false", + + "entry": { + "main": [ + path.resolve(__dirname, "./src/index.js") + ] + }, + + "output": { + "path": path.resolve(webpackDir, "build", "assets", "01-test"), + "publicPath": "/assets/01-test/", + "filename": "[name]-01-test.js", + "chunkFilename": "chunk-[id].[name]-01-test.js", + "sourceMapFilename": "debug/[file]-01-test.map", + "pathInfo": "false" + }, + + "debug": false, + + "resolve": { + "root": path.resolve(__dirname, "./src"), + "extensions": ["", ".js"], + "alias": { + "app": path.resolve(__dirname, "./src") + } + }, + + "resolveLoader": { + "root": path.resolve(__dirname, "../../../../node_modules") + }, + + "module": { + "loaders": [ + { + "test": /\.css$/, + "loader": ExtractTextPlugin.extract("style-loader", "css") + }, + { + "test": /\.jpg$/, + "loader": "url?limit=10&name=images/[name]-01-test.[ext]" + } + ] + }, + + "plugins": [ + new webpack.NoErrorsPlugin(), + function() { + if (opts.sprockets.write) { + return new RemoveWebpackPlugin(path.resolve(webpackDir, "build", "sprockets-manifest.json")); + } + }, + new webpack.optimize.OccurenceOrderPlugin(), + new webpack.optimize.AggressiveMergingPlugin(), + new webpack.optimize.DedupePlugin(), + new webpack.optimize.UglifyJsPlugin({ + output: { + comments: false + }, + compress: { + warnings: false + } + }), + new ExtractTextPlugin("[name]-01-test.css"), + new SriStatsPlugin({ + customStatsKey: "rails", + runAfterEmit: opts.sri.runAfterEmit + }), + new SprocketsStatsPlugin({ + customStatsKey: "rails", + saveAs: path.resolve(webpackDir, "build", "sprockets-manifest.json"), + write: opts.sprockets.write, + outputAssetsPath: path.resolve(webpackDir, "build", "assets", "01-test"), + sourceAssetsPath: path.resolve(__dirname, "./src", "assets") + }) + ] + }; +}; diff --git a/test/integration/simple.test.js b/test/integration/simple.test.js new file mode 100644 index 0000000..3689ada --- /dev/null +++ b/test/integration/simple.test.js @@ -0,0 +1,85 @@ +'use strict'; + +var test = require('ava'); +var webpack = require('webpack'); +var path = require('path'); +var MemoryFS = require('memory-fs'); +var fs = require('fs'); + +var config = require('./scenarios/simple/webpack.config'); +var tmpDir = path.resolve(__dirname, '../../tmp/01-test'); + +function buildCompiler(t, fsys, opts, callback) { + var compilier = webpack(config(tmpDir, opts)); + + if (fsys) { + compilier.outputFileSystem = fsys; + } + + compilier.run(function(err, stats) { + if (err) { + t.fail(err); + } + + var jsonStats = stats.toJson(); // eslint-disable-line vars-on-top + + if (jsonStats.errors.length > 0) { + return t.fail(jsonStats.errors); + } + + if (jsonStats.warnings.length > 0) { + return t.fail(jsonStats.warnings); + } + + callback(jsonStats, stats); + }); +} + +test.cb('generates the expected stats', function(t) { + // eslint-disable-next-line global-require + var expectedManifest = require('./scenarios/simple/fixtures/01-sprockets-manifest'); + + expectedManifest.hash = 'test'; + + var counter = 0; + + [true, false].forEach(function(runAfterEmit) { + var memFs = new MemoryFS(); + + buildCompiler(t, memFs, { + sprockets: { + write: false + }, + sri: { + runAfterEmit: runAfterEmit + } + }, function(jsonStats) { + var sprockets = jsonStats.__RESULTS_SPROCKETS; // eslint-disable-line no-underscore-dangle + sprockets.hash = 'test'; + + t.deepEqual(sprockets, expectedManifest); + + counter++; + + if (counter === 2) { + t.end(); + } + }); + }); +}); + +test.cb('writes sprockets-manifest.json', function(t) { + var memFs = new MemoryFS(); + + buildCompiler(t, memFs, { + sprockets: { + write: true + }, + sri: { + runAfterEmit: false + } + }, function() { + var actualManifestPath = path.join(tmpDir, 'build', 'sprockets-manifest.json'); + fs.stat(actualManifestPath, t.end); + }); +}); diff --git a/tmp/01-test/build/.keep b/tmp/01-test/build/.keep new file mode 100644 index 0000000..e69de29