Skip to content

Commit

Permalink
feat(webpack): ignore chunks
Browse files Browse the repository at this point in the history
Allow the webpack plugin to ignore specific chunk names when writing the manifest file.
  • Loading branch information
adam-26 committed Feb 20, 2018
1 parent 027736f commit 0221236
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -1222,6 +1222,12 @@ export default {
This will create a file (`opts.filename`) which you can import to map modules
to bundles.

### `opts.filename`
Required, the destination file for writing react-loadable module data

### `opts.ignoreChunkNames`
Optional, an array of webpack chunk names to exclude from the module data

[Read more about mapping modules to bundles](#mapping-loaded-modules-to-bundles).

### `getBundles`
Expand Down
19 changes: 16 additions & 3 deletions src/webpack.js
Expand Up @@ -3,12 +3,23 @@ const fs = require('fs');
const path = require('path');
const url = require('url');

function buildManifest(compiler, compilation) {
function buildManifest(compiler, compilation, ignoreChunkNames) {
let context = compiler.options.context;
let manifest = {};

compilation.chunks.forEach(chunk => {
if (chunk.name !== 'main') {
// Determine if the chunk should be ignored
const chunkName = typeof chunk.name === 'undefined' ? 'undefined' : chunk.name === null ? 'null' : chunk.name;
const ignoreChunk = ignoreChunkNames.length === 0 ? false : ignoreChunkNames.some(chunkNameCondition => {
if (chunkNameCondition instanceof RegExp) {
chunkNameCondition.lastIndex = 0; // reset in-case its a global regexp
return chunkNameCondition.test(chunkName);
}

return chunkNameCondition === chunkName;
});

if (!ignoreChunk) {
chunk.files.forEach(file => {
chunk.forEachModule(module => {
let id = module.id;
Expand All @@ -35,11 +46,13 @@ function buildManifest(compiler, compilation) {
class ReactLoadablePlugin {
constructor(opts = {}) {
this.filename = opts.filename;
const ignoreChunkNames = opts.ignoreChunkNames || [];
this.ignoreChunkNames = Array.isArray(ignoreChunkNames) ? ignoreChunkNames : [ignoreChunkNames];
}

apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
const manifest = buildManifest(compiler, compilation);
const manifest = buildManifest(compiler, compilation, this.ignoreChunkNames);
var json = JSON.stringify(manifest, null, 2);
const outputDirectory = path.dirname(this.filename);
try {
Expand Down

0 comments on commit 0221236

Please sign in to comment.