Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
benurb committed Aug 2, 2017
2 parents 3f10dbb + 35ccc28 commit 8e2b546
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class StatsReplacePlugin {
*/
constructor(configArray) {
this.configArray = configArray;

// We store a list of assets this plugin has already "seen" in a compilation. This is used for
// webpack-dev-server as some loaders (e.g. the css-loader) do not remove assets from previous builds from the
// compilation, but just add the newest
this.knownAssets = [];
}

apply(compiler) {
Expand All @@ -40,6 +45,7 @@ class StatsReplacePlugin {

process(compilation) {
const assetNames = Object.keys(compilation.assets);
const newAssetNames = assetNames.filter((assetName) => this.knownAssets.indexOf(assetName) === -1);

for (const config of this.configArray) {
for (const assetName of assetNames) {
Expand All @@ -62,7 +68,18 @@ class StatsReplacePlugin {
let content = asset.source().toString("utf8");

for (const replacer of config.replacers) {
let relatedAsset = assetNames.find((name) => replacer.asset.test(name));
let relatedAsset = newAssetNames.find((name) => replacer.asset.test(name));

// Only check all assets if there is no matching new one
if (!relatedAsset) {
relatedAsset = assetNames.find((name) => replacer.asset.test(name));
}

if (!relatedAsset) {
const noMatchErr = new Error(`stats-replace-webpack-plugin: No matching asset for ${replacer}`);
compilation.errors.push(noMatchErr);
return;
}

if (config.basename) {
relatedAsset = path.basename(relatedAsset);
Expand All @@ -72,6 +89,8 @@ class StatsReplacePlugin {
console.log(`[${assetName}] ${match} => ${relatedAsset}`);
return relatedAsset;
});

this.knownAssets.push(relatedAsset);
}

compilation.assets[assetName] = new RawSource(Buffer.from(content, "utf8"));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stats-replace-webpack-plugin",
"version": "2.0.0",
"version": "2.1.0",
"description": "Fills placeholders in text files with the final name of compiled assets",
"main": "lib/index.js",
"directories": {
Expand Down

0 comments on commit 8e2b546

Please sign in to comment.