assets-webpack-plugin
Advanced tools
Comparing version 0.3.1 to 2.0.0
175
index.js
var fs = require('fs'); | ||
var path = require('path'); | ||
var assets = {}; | ||
// function extend(target, source) { | ||
// for(var k in source){ | ||
// target[k] = source[k]; | ||
// } | ||
// return target; | ||
// } | ||
function extend(target, source) { | ||
for(var k in source){ | ||
target[k] = source[k]; | ||
} | ||
return target; | ||
} | ||
function Plugin(options) { | ||
@@ -18,7 +16,9 @@ this.options = options || {}; | ||
Plugin.prototype.apply = function(compiler) { | ||
var self = this; | ||
var _this = this; | ||
// var assets = {}; | ||
compiler.plugin('emit', function(compiler, callback) { | ||
var hashes = self.getHashes(compiler); | ||
var outputDir = self.options.path || '.'; | ||
// console.log('emit'); | ||
// console.log(assets); | ||
var outputDir = _this.options.path || '.'; | ||
@@ -33,5 +33,11 @@ try { | ||
var outputFilename = self.options.filename || 'webpack-assets.json'; | ||
var outputFull = path.join(outputDir, outputFilename); | ||
self.writeOutput(compiler, self.options.multiCompiler ? extend(assets,hashes) : hashes, outputFull); | ||
var output = _this.buildOutput(compiler); | ||
var outputFilename = _this.options.filename || 'webpack-assets.json'; | ||
var outputFullPath = path.join(outputDir, outputFilename); | ||
// if (_this.options.multiCompiler) { | ||
// output = extend(assets, output); | ||
// } | ||
_this.writeOutput(compiler, output, outputFullPath); | ||
callback(); | ||
@@ -41,47 +47,50 @@ }); | ||
Plugin.prototype.writeOutput = function(compiler, hashes, outputFull) { | ||
var json = JSON.stringify(hashes); | ||
fs.writeFile(outputFull, json, function(err) { | ||
/* | ||
Write output to file system | ||
*/ | ||
Plugin.prototype.writeOutput = function(compiler, output, outputFullPath) { | ||
var json = JSON.stringify(output); | ||
fs.writeFile(outputFullPath, json, function(err) { | ||
if (err) { | ||
compiler.errors.push(new Error('Plugin: Unable to save to ' + outputFull)); | ||
compiler.errors.push(new Error('Plugin: Unable to save to ' + outputFullPath)); | ||
} | ||
}); | ||
// compiler.assets[outputFilename] = { | ||
// source: function() { | ||
// return json; | ||
// }, | ||
// size: function() { | ||
// return json.length; | ||
// } | ||
// }; | ||
}; | ||
Plugin.prototype.getHashes = function(compiler) { | ||
var webpackStatsJson = compiler.getStats().toJson(); | ||
var assets = {}; | ||
Plugin.prototype.buildOutput = function(compiler) { | ||
// webpackStatsJson.assetsByChunkName contains a hash with the bundle names and the produced files | ||
var webpackStatsJson = compiler.getStats().toJson(); | ||
var assetsByChunkName = webpackStatsJson.assetsByChunkName; | ||
// assetsByChunkName contains a hash with the bundle names and the produced files | ||
// e.g. { one: 'one-bundle.js', two: 'two-bundle.js' } | ||
// in some cases (when using a plugin or source maps) it might contain an array of produced files | ||
// e.g. { main: [ 'index-bundle.js', 'index-bundle.js.map' ] } | ||
for (var chunk in webpackStatsJson.assetsByChunkName) { | ||
var chunkValue = webpackStatsJson.assetsByChunkName[chunk]; | ||
// Webpack outputs an array for each chunk when using sourcemaps and some plugins | ||
// e.g. { | ||
// main: | ||
// [ 'index-bundle-42b6e1ec4fa8c5f0303e.js', | ||
// 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' ] | ||
// } | ||
chunkValue = Plugin.getAssetChunk(chunkValue, compiler.options); | ||
var output = {}; | ||
if (compiler.options.output.publicPath) { | ||
chunkValue = compiler.options.output.publicPath + chunkValue; | ||
} | ||
// console.log(webpackStatsJson); | ||
assets[chunk] = chunkValue; | ||
for (var chunkName in assetsByChunkName) { | ||
var chunkValue = assetsByChunkName[chunkName]; | ||
// Webpack outputs an array for each chunkName when using sourcemaps and some plugins | ||
var chunkMap = Plugin.getChunkMap(compiler.options, chunkValue); | ||
// if (compiler.options.output.publicPath) { | ||
// chunkMap = compiler.options.output.publicPath + chunkMap; | ||
// } | ||
output[chunkName] = chunkMap; | ||
} | ||
return assets; | ||
return output; | ||
}; | ||
Plugin.getAssetChunk = function (stringOrArray, compilerOptions) { | ||
if (!stringOrArray) throw new Error('stringOrArray required'); | ||
if (!compilerOptions) throw new Error('compilerOptions required'); | ||
Plugin.getMapSegment = function(compilerOptions) { | ||
// For source maps we care about: | ||
@@ -93,17 +102,65 @@ // compilerOptions.output.sourceMapFilename; | ||
// e.g. '[file].map[query]' | ||
var mapSegment = sourceMapFilename | ||
// e.g. 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' | ||
return sourceMapFilename | ||
.replace('[file]', '') | ||
.replace('[query]', '') | ||
.replace('[hash]', ''); | ||
.replace('[hash]', '') | ||
.replace(/\//, '\\/.*'); | ||
// .replace('.', ''); | ||
}; | ||
// mapSegment e.g. ".map" | ||
Plugin.isSourceMap = function(compilerOptions, asset) { | ||
var mapSegment = Plugin.getMapSegment(compilerOptions); | ||
var mapRegex = new RegExp(mapSegment); | ||
// value e.g. | ||
// desktop.js.map | ||
// desktop.js.map?9b913c8594ce98e06b21 | ||
function isSourceMap(value) { | ||
return mapRegex.test(value); | ||
return mapRegex.test(asset); | ||
}; | ||
Plugin.getFileExt = function(compilerOptions, asset) { | ||
var isSourceMap = Plugin.isSourceMap(compilerOptions, asset); | ||
if (isSourceMap) { | ||
// remove the map segment | ||
var mapSegment = Plugin.getMapSegment(compilerOptions); | ||
asset = asset.replace(mapSegment, ''); | ||
// remove queries | ||
var queryIx = asset.indexOf('?'); | ||
if (queryIx > -1) asset = asset.slice(0, queryIx); | ||
} | ||
var extIx = asset.lastIndexOf('.'); | ||
return asset.slice(extIx + 1); | ||
}; | ||
Plugin.getAssetKind = function(compilerOptions, asset) { | ||
// console.log('compilerOptions', compilerOptions); | ||
// console.log(asset); | ||
var isSourceMap = Plugin.isSourceMap(compilerOptions, asset); | ||
var ext = Plugin.getFileExt(compilerOptions, asset); | ||
if (isSourceMap) { | ||
return ext + 'Map'; | ||
} else { | ||
return ext; | ||
} | ||
}; | ||
/* | ||
Create a map with information about this chunk | ||
@param stringOrArray | ||
String or an array of strings | ||
@return | ||
{ | ||
js: 'source.js' | ||
} | ||
*/ | ||
Plugin.getChunkMap = function (compilerOptions, stringOrArray) { | ||
if (!stringOrArray) throw new Error('stringOrArray required'); | ||
if (!compilerOptions) throw new Error('compilerOptions required'); | ||
// isAsset | ||
@@ -116,2 +173,3 @@ // Return true if a chunk is not a source map | ||
} | ||
var output = {}; | ||
@@ -123,10 +181,15 @@ if (stringOrArray instanceof Array) { | ||
// e.g. [ 'styles-bundle.js', 'styles-bundle.css' ] | ||
return stringOrArray | ||
.filter(isAsset) | ||
.pop(); | ||
for (var a = 0; a < stringOrArray.length ; a++) { | ||
var asset = stringOrArray[a]; | ||
var kind = Plugin.getAssetKind(compilerOptions, asset); | ||
output[kind] = asset; | ||
} | ||
} else { | ||
return stringOrArray; | ||
output.js = stringOrArray; | ||
} | ||
return output; | ||
}; | ||
module.exports = Plugin; |
{ | ||
"name": "assets-webpack-plugin", | ||
"version": "0.3.1", | ||
"version": "2.0.0", | ||
"description": "Emits a json file with assets paths", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,10 +8,26 @@ assets-webpack-plugin | ||
When working with Webpack will probably want to generate you bundles with a generate hash in them (for cache busting). | ||
When working with Webpack you might want to generate you bundles with a generated hash in them (for cache busting). | ||
This plug-in generates a json file with the generated assets to be used somewhere else, I use this with Rails so it can find the assets. | ||
This plug-in outputs a json file with the generated assets so you can find the assets from somewhere else. | ||
### Example output: | ||
As of version 2.0 the output now includes keys for different assets kinds. | ||
``` | ||
{ | ||
"one": { | ||
"js": "one-bundle.js", | ||
"jsMap": "one-bundle.js.map" | ||
}, | ||
"two": { | ||
"js": "two-bundle.js" | ||
} | ||
} | ||
``` | ||
## Install | ||
```sh | ||
npm install assets-webpack-plugin --save | ||
npm install assets-webpack-plugin --save-dev | ||
``` | ||
@@ -24,4 +40,5 @@ | ||
```js | ||
var path = require("path"); | ||
var SaveAssetsJson = require('assets-webpack-plugin'); | ||
var path = require('path'); | ||
var AssetsPlugin = require('assets-webpack-plugin'); | ||
var assetsPluginInstance = new AssetsPlugin(); | ||
@@ -31,8 +48,8 @@ module.exports = { | ||
output: { | ||
path: path.join(__dirname, "public", "js"), | ||
filename: "[name]-bundle-[hash].js", | ||
publicPath: "/js/" | ||
path: path.join(__dirname, "public", "js"), | ||
filename: "[name]-bundle-[hash].js", | ||
publicPath: "/js/" | ||
}, | ||
.... | ||
plugins: [new SaveAssetsJson()] | ||
plugins: [assetsPluginInstance] | ||
}; | ||
@@ -50,3 +67,3 @@ ``` | ||
```js | ||
new SaveHashes({path: path.join(__dirname, 'app', 'views')}) | ||
new AssetsPlugin({path: path.join(__dirname, 'app', 'views')}) | ||
``` | ||
@@ -59,3 +76,3 @@ | ||
```js | ||
new SaveHashes({filename: 'assets.json'}) | ||
new AssetsPlugin({filename: 'assets.json'}) | ||
``` | ||
@@ -65,3 +82,3 @@ | ||
I use this with Rails, so it can find my Webpack assets. In my application controller I have: | ||
You can use this with Rails to find the bundled Webpack assets via sprockets. In `ApplicationController` you might have: | ||
@@ -73,3 +90,3 @@ ```ruby | ||
json = JSON.parse(file) | ||
json[bundle] | ||
json[bundle]['js'] | ||
end | ||
@@ -82,3 +99,3 @@ ``` | ||
def show | ||
@script = script_for('clients') # this will retrive the bundle named 'clients' | ||
@script = script_for('clients') # this will retrieve the bundle named 'clients' | ||
end | ||
@@ -85,0 +102,0 @@ ``` |
@@ -11,3 +11,11 @@ var path = require('path'); | ||
function testPlugin(webpackConfig, expectedResults, outputFile, done) { | ||
function expectOutput(args, done) { | ||
if (!args.config) throw new Error('Expected args.config'); | ||
if (!args.expected) throw new Error('Expected args.expected'); | ||
if (!done) throw new Error('Expected done'); | ||
var webpackConfig = args.config; | ||
var expectedResult = args.expected; | ||
var outputFile = args.outputFile; | ||
// Create output folder | ||
@@ -25,12 +33,8 @@ mkdirp(OUTPUT_DIR, function(err) { | ||
for (var i = 0; i < expectedResults.length; i++) { | ||
var expectedResult = expectedResults[i]; | ||
if (expectedResult instanceof RegExp) { | ||
expect(content).toMatch(expectedResult); | ||
} else { | ||
var json = JSON.parse(content); | ||
var expectedJson = JSON.parse(expectedResult); | ||
expect(json).toEqual(expectedJson); | ||
} | ||
if (expectedResult instanceof RegExp) { | ||
expect(content).toMatch(expectedResult); | ||
} else { | ||
expect(content).toContain(expectedResult); | ||
} | ||
done(); | ||
@@ -42,27 +46,2 @@ }); | ||
describe('getAssetChunk', function() { | ||
var webpackConfig; | ||
beforeEach(function () { | ||
webpackConfig = { | ||
output: { | ||
sourceMapFilename: '[file].map[query]' | ||
}, | ||
devtool: 'sourcemap' | ||
}; | ||
}); | ||
it('returns the string when given just a string', function () { | ||
var input = 'desktop.js'; | ||
var res = Plugin.getAssetChunk(input, webpackConfig); | ||
expect(res).toBe(input); | ||
}); | ||
it('returns the assets when given an array', function() { | ||
var input = ['desktop.js?9b913c8594ce98e06b21', 'desktop.js.map?9b913c8594ce98e06b21']; | ||
var res = Plugin.getAssetChunk(input, webpackConfig); | ||
expect(res).toBe('desktop.js?9b913c8594ce98e06b21'); | ||
}); | ||
}); | ||
describe('Plugin', function() { | ||
@@ -85,5 +64,15 @@ beforeEach(function(done) { | ||
var expected = ['{"main":"index-bundle.js"}']; | ||
var expected = { | ||
main: { | ||
js: 'index-bundle.js' | ||
} | ||
}; | ||
expected = JSON.stringify(expected); | ||
testPlugin(webpackConfig, expected, null, done); | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected | ||
}; | ||
expectOutput(args, done); | ||
}); | ||
@@ -106,40 +95,18 @@ | ||
var expected = ['{"one":"one-bundle.js","two":"two-bundle.js"}']; | ||
testPlugin(webpackConfig, expected, null, done); | ||
}); | ||
it('generates a default file with multiple compilers', function(done) { | ||
var webpackConfig = [ | ||
{ | ||
entry: { | ||
one: path.join(__dirname, 'fixtures/one.js') | ||
}, | ||
output: { | ||
path: OUTPUT_DIR, | ||
filename: 'one-bundle.js' | ||
}, | ||
plugins: [new Plugin({ | ||
multiCompiler: true, | ||
path: 'dist' | ||
})] | ||
var expected = { | ||
one: { | ||
js: 'one-bundle.js' | ||
}, | ||
{ | ||
entry: { | ||
two: path.join(__dirname, 'fixtures/two.js') | ||
}, | ||
output: { | ||
path: OUTPUT_DIR, | ||
filename: 'two-bundle.js' | ||
}, | ||
plugins: [new Plugin({ | ||
multiCompiler: true, | ||
path: 'dist' | ||
})] | ||
two: { | ||
js: 'two-bundle.js' | ||
} | ||
]; | ||
}; | ||
expected = JSON.stringify(expected); | ||
var expected = ['{"one":"one-bundle.js","two":"two-bundle.js"}']; | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected | ||
}; | ||
testPlugin(webpackConfig, expected, null, done); | ||
expectOutput(args, done); | ||
}); | ||
@@ -161,5 +128,16 @@ | ||
var expected = ['{"main":"index-bundle.js"}']; | ||
var expected = { | ||
main: { | ||
js: 'index-bundle.js' | ||
} | ||
}; | ||
expected = JSON.stringify(expected); | ||
testPlugin(webpackConfig, expected, 'foo.json', done); | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected, | ||
outputFile: 'foo.json' | ||
}; | ||
expectOutput(args, done); | ||
}); | ||
@@ -200,5 +178,17 @@ | ||
var expected = ['{"main":"index-bundle.js"}']; | ||
var expected = { | ||
main: { | ||
js: 'index-bundle.js', | ||
jsMap: 'index-bundle.js.map' | ||
} | ||
}; | ||
testPlugin(webpackConfig, expected, null, done); | ||
expected = JSON.stringify(expected); | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected | ||
}; | ||
expectOutput(args, done); | ||
}); | ||
@@ -219,5 +209,10 @@ | ||
var expected = [/{"main":"index-bundle-[0-9a-f]+\.js"}/]; | ||
var expected = /{"main":{"js":"index-bundle-[0-9a-f]+\.js","jsMap":"index-bundle-[0-9a-f]+\.js\.map"}}/; | ||
testPlugin(webpackConfig, expected, null, done); | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected | ||
}; | ||
expectOutput(args, done); | ||
}); | ||
@@ -238,5 +233,17 @@ | ||
var expected = [/{"main":"index-bundle-[0-9a-f]+\.js"}/]; | ||
// var expected = { | ||
// main: { | ||
// js: "index-bundle-[0-9a-f]+\.js" | ||
// } | ||
// }; | ||
// expected = JSON.stringify(expected); | ||
testPlugin(webpackConfig, expected, null, done); | ||
var expected = /{"main":{"js":"index-bundle-[0-9a-f]+\.js"}}/; | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected | ||
}; | ||
expectOutput(args, done); | ||
}); | ||
@@ -269,7 +276,73 @@ | ||
var expected = ['{"one":"one-bundle.js","two":"two-bundle.js","styles":"styles-bundle.css"}']; | ||
var expected = { | ||
one: { | ||
js: "one-bundle.js" | ||
}, | ||
two: { | ||
js: "two-bundle.js" | ||
}, | ||
styles: { | ||
js: "styles-bundle.js", | ||
css: "styles-bundle.css" | ||
} | ||
}; | ||
expected = JSON.stringify(expected); | ||
testPlugin(webpackConfig, expected, null, done); | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected | ||
}; | ||
expectOutput(args, done); | ||
}); | ||
xit('generates a default file with multiple compilers', function(done) { | ||
var webpackConfig = [ | ||
{ | ||
entry: { | ||
one: path.join(__dirname, 'fixtures/one.js') | ||
}, | ||
output: { | ||
path: OUTPUT_DIR, | ||
filename: 'one-bundle.js' | ||
}, | ||
plugins: [new Plugin({ | ||
multiCompiler: true, | ||
path: 'dist' | ||
})] | ||
}, | ||
{ | ||
entry: { | ||
two: path.join(__dirname, 'fixtures/two.js') | ||
}, | ||
output: { | ||
path: OUTPUT_DIR, | ||
filename: 'two-bundle.js' | ||
}, | ||
plugins: [new Plugin({ | ||
multiCompiler: true, | ||
path: 'dist' | ||
})] | ||
} | ||
]; | ||
var expected = { | ||
one: { | ||
js: "one-bundle.js" | ||
}, | ||
two: { | ||
js: "two-bundle.js" | ||
} | ||
}; | ||
expected = JSON.stringify(expected); | ||
var args = { | ||
config: webpackConfig, | ||
expected: expected | ||
}; | ||
expectOutput(args, done); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15510
10
468
0
109