Skip to content

Commit

Permalink
feat: add ExcludeModulePlugin (#381)
Browse files Browse the repository at this point in the history
* feat: add ExcludeModulePlugin

* fix: add ExcludeModulePlugin info to README
  • Loading branch information
mzgoddard authored Jun 14, 2018
1 parent dda7593 commit 074b5d9
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 6 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ new HardSourceWebpackPlugin({
}),
```

Some further configuration is possible through provided plugins.

```js
plugins: [
new HardSourceWebpackPlugin(),

// You can optionally exclude items that may not be working with HardSource
// or items with custom loaders while you are actively developing the
// loader.
new HardSourceWebpackPlugin.ExcludeModulePlugin([
{
// HardSource works with mini-css-extract-plugin but due to how
// mini-css emits assets, assets are not emitted on repeated builds with
// mini-css and hard-source together. Ignoring the mini-css loader
// modules, but not the other css loader modules, excludes the modules
// that mini-css needs rebuilt to output assets every time.
test: /mini-css-extract-plugin[\\/]dist[\\/]loader/,
},
{
test: /my-loader/,
include: path.join(__dirname, 'vendor'),
},
]),
]
```


## Options

### `cacheDirectory`
Expand Down
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ const LoggerFactory = require('./lib/loggerFactory');
const cachePrefix = require('./lib/util').cachePrefix;

const CacheSerializerFactory = require('./lib/CacheSerializerFactory');
const SerializerAppendPlugin = require('./lib/SerializerAppendPlugin');
const ExcludeModulePlugin = require('./lib/ExcludeModulePlugin');
const HardSourceLevelDbSerializerPlugin = require('./lib/SerializerLeveldbPlugin');
const SerializerAppend2Plugin = require('./lib/SerializerAppend2Plugin');
const SerializerAppendPlugin = require('./lib/SerializerAppendPlugin');
const SerializerCacachePlugin = require('./lib/SerializerCacachePlugin');
const SerializerJsonPlugin = require('./lib/SerializerJsonPlugin');
const HardSourceLevelDbSerializerPlugin = require('./lib/SerializerLeveldbPlugin');

const hardSourceVersion = require('./package.json').version;

Expand Down Expand Up @@ -557,8 +558,10 @@ class HardSourceWebpackPlugin {

module.exports = HardSourceWebpackPlugin;

HardSourceWebpackPlugin.SerializerAppendPlugin = SerializerAppendPlugin;
HardSourceWebpackPlugin.ExcludeModulePlugin = ExcludeModulePlugin;
HardSourceWebpackPlugin.HardSourceLevelDbSerializerPlugin = HardSourceLevelDbSerializerPlugin;
HardSourceWebpackPlugin.LevelDbSerializerPlugin = HardSourceLevelDbSerializerPlugin;
HardSourceWebpackPlugin.SerializerAppend2Plugin = SerializerAppend2Plugin;
HardSourceWebpackPlugin.SerializerAppendPlugin = SerializerAppendPlugin;
HardSourceWebpackPlugin.SerializerCacachePlugin = SerializerCacachePlugin;
HardSourceWebpackPlugin.SerializerJsonPlugin = SerializerJsonPlugin;
HardSourceWebpackPlugin.HardSourceLevelDbSerializerPlugin = HardSourceLevelDbSerializerPlugin;
53 changes: 53 additions & 0 deletions lib/ExcludeModulePlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const pluginCompat = require('./util/plugin-compat');

const matchTest = (test, source) => {
if (Array.isArray(test)) {
return test.some(subtest => matchTest(subtest, source));
} else if (test instanceof RegExp) {
return test.test(source);
} else if (typeof test === 'string') {
return source.startsWith(test);
} else if (typeof test === 'function') {
return test(source);
}
return false;
};

const matchOne = ({ test, include, exclude }, source) => {
return (
(test ? matchTest(test, source) : true) &&
(include ? matchTest(include, source) : true) &&
(exclude ? !matchTest(exclude, source) : true)
);
};

const matchAny = (test, source) => {
if (Array.isArray(test)) {
return test.some(subtest => matchOne(subtest, source));
}
return matchOne(test, source);
};

class ExcludeModulePlugin {
constructor(match) {
this.match = match;
}

apply(compiler) {
const compilerHooks = pluginCompat.hooks(compiler);

compilerHooks.afterPlugins.tap('HardSource - ExcludeModulePlugin', () => {
compilerHooks._hardSourceAfterFreezeModule.tap(
'HardSource - ExcludeModulePlugin',
(frozen, module, extra) => {
if (matchAny(this.match, module.identifier())) {
return null;
}
return frozen;
},
);
});
}
}

module.exports = ExcludeModulePlugin;
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-exclude-plugin/fib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(n) {
return n + (n > 0 ? n - 1 : 0);
};
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-exclude-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var fib = require('./fib');

console.log(fib(3));
21 changes: 21 additions & 0 deletions tests/fixtures/hard-source-exclude-plugin/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var HardSourceWebpackPlugin = require('../../..');

module.exports = {
context: __dirname,
entry: './index.js',
output: {
path: __dirname + '/tmp',
filename: 'main.js',
},
plugins: [
new HardSourceWebpackPlugin({
cacheDirectory: 'cache',
environmentHash: {
root: __dirname + '/../../..',
},
}),
new HardSourceWebpackPlugin.ExcludeModulePlugin({
test: /fib\.js$/,
}),
],
};
7 changes: 6 additions & 1 deletion tests/hard-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ var fs = require('fs');

var expect = require('chai').expect;

var itCompilesChange = require('./util').itCompilesChange;
var itCompiles = require('./util').itCompiles;
var itCompilesChange = require('./util').itCompilesChange;
var itCompilesHardModules = require('./util').itCompilesHardModules;
var itCompilesTwice = require('./util').itCompilesTwice;
var itCompilesWithCache = require('./util').itCompilesWithCache;
var writeFiles = require('./util').writeFiles;

Expand Down Expand Up @@ -273,4 +275,7 @@ describe('hard-source features', function() {
'yarn.lock': 'b',
});

itCompilesTwice('hard-source-exclude-plugin');
itCompilesHardModules('hard-source-exclude-plugin', ['./index.js', '!./fib.js']);

});
6 changes: 5 additions & 1 deletion tests/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ exports.itCompilesHardModules = function(fixturePath, filesA, filesB, expectHand
});
}
else {
expect(hardModules).to.include(handle);
if (handle.startsWith('!')) {
expect(hardModules).to.not.include(handle.substring(1));
} else {
expect(hardModules).to.include(handle);
}
}
});
}
Expand Down

0 comments on commit 074b5d9

Please sign in to comment.