Skip to content

Commit

Permalink
Chunks filtering (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
devpreview authored Jun 1, 2018
1 parent 4df7fd3 commit b404f02
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ You can pass a hash of configuration options to `all-chunks-loaded-webpack-plugi
|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**`callback`**|`{String}`|`undefined`|Your callback called after all chunks loaded|
|`chunks`|`{String[]}`|`null`|Allows you to callback called only some chunks loaded|
|`excludeChunks`|`{String[]}`|`null`|Allows you to skip callback called some chunks loaded|

Here's an example `webpack` config illustrating how to use these options:

Expand Down Expand Up @@ -103,6 +105,3 @@ module.exports = {

## Need a feature?
Welcome to [issues](https://github.com/devpreview/all-chunks-loaded-webpack-plugin/issues)!

## To do...
* Chunks filtering.
40 changes: 39 additions & 1 deletion src/main/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,35 @@ export class Callback {
public constructor(
protected options: AllChunksLoadedWebpackPluginOptions,
protected publicPath: string
) {}
) {
}

public makeLoadedCallback(data: any): any {
for (let chunk of data.chunks) {
for (let name of chunk.names) {
if (this.options.chunks && this.options.chunks.indexOf(name) == -1) {
continue;
}
if (this.options.excludeChunks && this.options.excludeChunks.indexOf(name) > -1) {
continue;
}
if (!this.chunks.has(name)) {
this.chunks.set(name, []);
}
this.chunks.set(name, this.chunks.get(name).concat(chunk.files));
}
}
for (let tag of [].concat(data.head, data.body)) {
let chunk = this.getChunk(data.chunks, tag);
if (chunk == null) {
continue;
}
if (this.options.chunks && this.options.chunks.indexOf(chunk) == -1) {
continue;
}
if (this.options.excludeChunks && this.options.excludeChunks.indexOf(chunk) > -1) {
continue;
}
if (tag.tagName === 'script' && tag.attributes && tag.attributes.src) {
this.addOnload(tag.attributes.src, tag);
}
Expand Down Expand Up @@ -78,4 +95,25 @@ export class Callback {
return loadedScript;
}

protected getChunk(chunks: any[], tag: any): string | null {
let src: string = null;
if (tag.tagName === 'script' && tag.attributes && tag.attributes.src) {
src = tag.attributes.src;
}
if (tag.tagName === 'link' && tag.attributes && tag.attributes.href) {
src = tag.attributes.href;
}
if (!src) {
return null;
}
for (let chunk of chunks) {
for (let file of chunk.files) {
if (src.endsWith(file)) {
return chunk.id;
}
}
}
return null;
}

}
2 changes: 2 additions & 0 deletions src/main/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Callback } from "./callback";

export interface AllChunksLoadedWebpackPluginOptions {
callback: string;
chunks?: string[];
excludeChunks?: string[];
}

export default class AllChunksLoadedWebpackPlugin {
Expand Down

0 comments on commit b404f02

Please sign in to comment.