Skip to content

Commit

Permalink
support custom function importType
Browse files Browse the repository at this point in the history
  • Loading branch information
liady committed Apr 1, 2018
1 parent b9ee53b commit bfc6e83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Webpack node modules externals
[![Downloads](https://img.shields.io/npm/dm/webpack-node-externals.svg)](https://www.npmjs.org/package/webpack-node-externals)
[![Build Status](https://travis-ci.org/liady/webpack-node-externals.svg?branch=master)](https://travis-ci.org/liady/webpack-node-externals)

Webpack allows you to define [*externals*](https://webpack.github.io/docs/configuration.html#externals) - modules that should not be bundled.
Webpack allows you to define [*externals*](https://webpack.js.org/configuration/externals) - modules that should not be bundled.

When bundling with Webpack for the backend - you usually don't want to bundle its `node_modules` dependencies.
This library creates an *externals* function that ignores `node_modules` when bundling in Webpack.<br/>(Inspired by the great [Backend apps with Webpack](http://jlongster.com/Backend-Apps-with-Webpack--Part-I) series)
Expand Down Expand Up @@ -42,6 +42,12 @@ An array for the `externals` to whitelist, so they **will** be included in the b

#### `options.importType (='commonjs')`
The method in which unbundled modules will be required in the code. Best to leave as `commonjs` for node modules.
May be one of [documented options](https://webpack.js.org/configuration/externals/#externals) or function `callback(moduleName)` which returns custom code to be returned as import type, eg.:
```js
options.importType = function (moduleName) {
return 'amd ' + moduleName;
}
```

#### `options.modulesDir (='node_modules')`
The folder in which to search for the node modules.
Expand All @@ -68,7 +74,7 @@ For most use cases, the defaults of `importType` and `modulesDir` should be used

## Q&A
#### Why not just use a regex in the Webpack config?
Webpack allows inserting [regex](https://webpack.github.io/docs/configuration.html#externals) in the *externals* array, to capture non-relative modules:
Webpack allows inserting [regex](https://webpack.js.org/configuration/externals/#regex) in the *externals* array, to capture non-relative modules:
```js
{
externals: [
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ module.exports = function nodeExternals(options) {
return function(context, request, callback){
var moduleName = getModuleName(request, includeAbsolutePaths);
if (utils.contains(nodeModules, moduleName) && !utils.containsPattern(whitelist, request)) {
if (typeof importType === 'function') {
return callback(null, importType(request));
}
// mark this module as external
// https://webpack.js.org/configuration/externals/
return callback(null, importType + " " + request);
};
callback();
}
}
};
11 changes: 11 additions & 0 deletions test/library.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ describe('invocation with a different importType', function() {
it('when given a relative path', assertResult('./src/index.js', undefined));
});

describe('should invoke a custom function', function(){
before(function(){
context.instance = nodeExternals({ importType: function(moduleName) {
return 'commonjs ' + moduleName;
}});
});

it('when given an existing module', assertResult('moduleA', 'commonjs moduleA'));
it('when given a non-node module', assertResult('non-node-module', undefined));
});

after(function(){
restoreMock()
});
Expand Down

0 comments on commit bfc6e83

Please sign in to comment.