assets-webpack-plugin
Advanced tools
Comparing version 2.2.1 to 2.2.2
159
index.js
@@ -1,12 +0,6 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var mkdirp = require('mkdirp'); | ||
var buildOutput = require('./lib/buildOutput'); | ||
var writeOutput = require('./lib/writeOutput'); | ||
var mkdirp = require('mkdirp'); | ||
var path = require('path'); | ||
// function extend(target, source) { | ||
// for(var k in source){ | ||
// target[k] = source[k]; | ||
// } | ||
// return target; | ||
// } | ||
function Plugin(options) { | ||
@@ -35,3 +29,3 @@ this.options = options || {}; | ||
var output = _this.buildOutput(compiler); | ||
var output = buildOutput(compiler); | ||
var outputFilename = _this.options.filename || 'webpack-assets.json'; | ||
@@ -43,3 +37,3 @@ var outputFullPath = path.join(outputDir, outputFilename); | ||
// } | ||
_this.writeOutput(compiler, output, outputFullPath); | ||
writeOutput(compiler, output, outputFullPath); | ||
@@ -50,143 +44,2 @@ callback(); | ||
/* | ||
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 ' + outputFullPath)); | ||
} | ||
}); | ||
}; | ||
Plugin.prototype.buildOutput = function(compiler) { | ||
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-42b6e1ec4fa8c5f0303e.js', | ||
// 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' ] | ||
// } | ||
var output = {}; | ||
// console.log(webpackStatsJson); | ||
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); | ||
output[chunkName] = chunkMap; | ||
} | ||
return output; | ||
}; | ||
Plugin.getMapSegment = function(compilerOptions) { | ||
// For source maps we care about: | ||
// compilerOptions.output.sourceMapFilename; | ||
// compiler.devtool; | ||
var sourceMapFilename = compilerOptions.output.sourceMapFilename; | ||
// e.g. '[file].map[query]' | ||
// e.g. 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' | ||
return sourceMapFilename | ||
.replace('[file]', '') | ||
.replace('[query]', '') | ||
.replace('[hash]', '') | ||
.replace(/\//, '\\/.*'); | ||
// .replace('.', ''); | ||
}; | ||
Plugin.isSourceMap = function(compilerOptions, asset) { | ||
var mapSegment = Plugin.getMapSegment(compilerOptions); | ||
var mapRegex = new RegExp(mapSegment); | ||
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 | ||
// 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); | ||
} | ||
var output = {}; | ||
var publicPath = compilerOptions.output.publicPath || ''; | ||
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' ] | ||
for (var a = 0; a < stringOrArray.length ; a++) { | ||
var asset = stringOrArray[a]; | ||
var kind = Plugin.getAssetKind(compilerOptions, asset); | ||
output[kind] = publicPath + asset; | ||
} | ||
} else { | ||
output.js = publicPath + stringOrArray; | ||
} | ||
return output; | ||
}; | ||
module.exports = Plugin; |
{ | ||
"name": "assets-webpack-plugin", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "Emits a json file with assets paths", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jshint *.js spec && ./node_modules/.bin/mocha spec" | ||
"test": "jshint *.js test && ./node_modules/.bin/mocha test" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
18273
18
549
1