Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
Implementation of options.merge (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
berlangadani01 authored and danikaze committed Sep 29, 2017
1 parent 8de6558 commit e9dc8cf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ new ExtractTextPlugin(options: filename | object)
|**`allChunks`**|`{Boolean}`|Extract from all additional chunks too (by default it extracts only from the initial chunk(s))<br />When using `CommonsChunkPlugin` and there are extracted chunks (from `ExtractTextPlugin.extract`) in the commons chunk, `allChunks` **must** be set to `true`|
|**`disable`**|`{Boolean}`|Disables the plugin|
|**`ignoreOrder`**|`{Boolean}`|Disables order check (useful for CSS Modules!), `false` by default|
|**`merge`**|`{Array}`|List of files to create merging other files. Each element should be like `{ test, filename, preventOriginalOutput}`|

* `[name]` name of the chunk
* `[id]` number of the chunk
Expand All @@ -84,7 +85,22 @@ new ExtractTextPlugin(options: filename | object)
* other `digestType`s, e.g. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
* and `length`, the length of the hash in chars

> :warning: `ExtractTextPlugin` generates a file **per entry**, so you must use `[name]`, `[id]` or `[contenthash]` when using multiple entries.
> :warning: `ExtractTextPlugin` generates a file **per entry**, so you must use `[name]`, `[id]` or `[contenthash]` when using multiple entries, or set the `merge` option.
```js
// Generate one styles.css from all the *.styles.css and one themes.css from all the *.themes.css files
new ExtractTextPlugin({
merge: [{
filename: 'styles.css', // name of the merged output file
test: /\.styles\.css$/, // files to include (*.styles.css)
preventOriginalOutput: true, // when true (by default), original files won't be generated
}, {
filename: 'themes.css',
test: /\.themes\.css$/,
preventOriginalOutput: true,
}]
})
```

#### `#extract`

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": "extract-text-webpack-plugin",
"version": "3.0.0",
"version": "3.1.0",
"author": "Tobias Koppers @sokra",
"description": "Extract text from bundle into a file.",
"license": "MIT",
Expand Down
5 changes: 4 additions & 1 deletion schema/loader.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
},
"publicPath": {
"type": "string"
}
},
"merge": {
"type": "array"
}
}
}
4 changes: 4 additions & 0 deletions schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"publicPath": {
"description": "",
"type": "string"
},
"merge": {
"description": "List of objects as { filename, test, preventOriginalOutput }",
"type": "array"
}
}
}
57 changes: 54 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class ExtractTextPlugin {
}
this.filename = options.filename;
this.id = options.id != null ? options.id : ++nextId;
this.options = {};
this.options = {
merge: [],
};
this.filesToMerge = {};
mergeOptions(this.options, options);
delete this.options.filename;
delete this.options.id;
Expand All @@ -55,6 +58,23 @@ class ExtractTextPlugin {
return ExtractTextPlugin.loader(mergeOptions({ id: this.id }, options));
}

mergeChunks(chunks, filename) {
const result = new Chunk();
// result.index = i;
// result.originalChunk = chunk;
result.name = filename;
result.entrypoints = [];

chunks.forEach((chunk) => {
// result.entrypoints = result.entrypoints.concat(chunk.entrypoints);
chunk.forEachModule((module) => {
result.addModule(module);
module.addChunk(result);
});
});
return result;
}

mergeNonInitialChunks(chunk, intoChunk, checkedChunks) {
if (!intoChunk) {
checkedChunks = [];
Expand Down Expand Up @@ -215,10 +235,41 @@ class ExtractTextPlugin {

const file = (isFunction(filename)) ? filename(getPath) : getPath(filename);

compilation.assets[file] = source;
chunk.files.push(file);
let preventOutput = false;
this.options.merge.forEach((mergeChunk) => {
if (mergeChunk.test.test(file)) {
if (!this.filesToMerge[mergeChunk.filename]) {
this.filesToMerge[mergeChunk.filename] = [];
}
this.filesToMerge[mergeChunk.filename].push(extractedChunk);
preventOutput = preventOutput || (mergeChunk.preventOriginalOutput !== false);
}
});

if (!preventOutput) {
compilation.assets[file] = source;
chunk.files.push(file);
}
}
}, this);

Object.keys(this.filesToMerge).forEach((filename) => {
const chunks = this.filesToMerge[filename];
const mergedChunk = this.mergeChunks(chunks);
const source = this.renderExtractedChunk(mergedChunk);

const getPath = format => compilation.getPath(format, {
mergedChunk,
}).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function () { // eslint-disable-line func-names
return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10));
});

const file = (isFunction(filename)) ? filename(getPath) : getPath(filename);

compilation.assets[file] = source;
mergedChunk.files.push(file);
});

callback();
});
});
Expand Down

0 comments on commit e9dc8cf

Please sign in to comment.