assets-webpack-plugin
Advanced tools
Comparing version 0.2.0 to 0.3.0
64
index.js
@@ -51,13 +51,2 @@ var fs = require('fs'); | ||
// filterDevChunks | ||
// Return true if a chunk is not a source map | ||
// @param {String} chunk value e.g. index-bundle.js.map | ||
// @return {Boolean} | ||
function filterDevChunks(value) { | ||
return !( | ||
/source-?map/.test(compiler.options.devtool) && | ||
/\.map$/.test(value) | ||
); | ||
} | ||
// webpackStatsJson.assetsByChunkName contains a hash with the bundle names and the produced files | ||
@@ -69,12 +58,6 @@ // e.g. { one: 'one-bundle.js', two: 'two-bundle.js' } | ||
var chunkValue = webpackStatsJson.assetsByChunkName[chunk]; | ||
// Webpack outputs an array for each chunk when using sourcemaps and some plugins | ||
if (chunkValue instanceof Array) { | ||
// When using plugins like 'extract-text', for extracting CSS from JS, webpack | ||
// will push the new bundle to the array, so the last item will be the correct | ||
// chunk | ||
// e.g. [ 'styles-bundle.js', 'styles-bundle.css' ] | ||
chunkValue = chunkValue.filter(filterDevChunks).pop(); | ||
} | ||
chunkValue = Plugin.getAssetChunk(chunkValue, compiler.options); | ||
if (compiler.options.output.publicPath) { | ||
@@ -90,2 +73,45 @@ chunkValue = compiler.options.output.publicPath + chunkValue; | ||
Plugin.getAssetChunk = function (stringOrArray, compilerOptions) { | ||
if (!stringOrArray) throw new Error('stringOrArray required'); | ||
if (!compilerOptions) throw new Error('compilerOptions required'); | ||
// For source maps we care about: | ||
// compilerOptions.output.sourceMapFilename; | ||
// compiler.devtool; | ||
var sourceMapFilename = compilerOptions.output.sourceMapFilename; | ||
// e.g. '[file].map[query]' | ||
var mapSegment = sourceMapFilename | ||
.replace('[file]', '') | ||
.replace('[query]', '') | ||
.replace('[hash]', '') | ||
.replace('.', ''); | ||
var mapRegex = new RegExp(mapSegment); | ||
function isSourceMap(value) { | ||
return mapRegex.test(value); | ||
} | ||
// isAsset | ||
// Return true if a chunk is not a source map | ||
// @param {String} chunk value e.g. index-bundle.js.map | ||
// @return {Boolean} | ||
function isAsset(value) { | ||
return !isSourceMap(value); | ||
} | ||
if (stringOrArray instanceof Array) { | ||
// When using plugins like 'extract-text', for extracting CSS from JS, webpack | ||
// will push the new bundle to the array, so the last item will be the correct | ||
// chunk | ||
// e.g. [ 'styles-bundle.js', 'styles-bundle.css' ] | ||
return stringOrArray | ||
.filter(isAsset) | ||
.pop(); | ||
} else { | ||
return stringOrArray; | ||
} | ||
}; | ||
module.exports = Plugin; |
{ | ||
"name": "assets-webpack-plugin", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Emits a json file with assets paths", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,3 +14,3 @@ assets-webpack-plugin | ||
``` | ||
```sh | ||
npm install assets-webpack-plugin --save | ||
@@ -23,3 +23,3 @@ ``` | ||
``` | ||
```js | ||
var path = require("path"); | ||
@@ -48,3 +48,3 @@ var SaveAssetsJson = require('assets-webpack-plugin'); | ||
``` | ||
```js | ||
new SaveHashes({path: path.join(__dirname, 'app', 'views')}) | ||
@@ -57,3 +57,3 @@ ``` | ||
``` | ||
```js | ||
new SaveHashes({filename: 'assets.json'}) | ||
@@ -85,3 +85,3 @@ ``` | ||
``` | ||
```erb | ||
<div id="app"> | ||
@@ -94,5 +94,4 @@ <script src="<%= @script %>"></script> | ||
``` | ||
```sh | ||
npm test | ||
``` | ||
@@ -38,2 +38,27 @@ var path = require('path'); | ||
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() { | ||
@@ -98,6 +123,4 @@ beforeEach(function(done) { | ||
testPlugin(webpackConfig, expected, 'foo.json', done); | ||
}); | ||
it('registers a webpack error when output folder doesnt exists', function(done) { | ||
@@ -141,2 +164,20 @@ var webpackConfig = { | ||
it('works with source maps and hash', function(done) { | ||
var webpackConfig = { | ||
devtool: 'sourcemap', | ||
entry: path.join(__dirname, 'fixtures/one.js'), | ||
output: { | ||
path: OUTPUT_DIR, | ||
filename: 'index-bundle-[hash].js' | ||
}, | ||
plugins: [new Plugin({ | ||
path: 'dist' | ||
})] | ||
}; | ||
var expected = [/{"main":"index-bundle-[0-9a-f]+\.js"}/]; | ||
testPlugin(webpackConfig, expected, null, done); | ||
}); | ||
it('handles hashes in bundle filenames', function(done) { | ||
@@ -143,0 +184,0 @@ |
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
11459
284
92