Skip to content

Commit

Permalink
feat: able to use a custom resolver (mastilver#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver authored Jul 24, 2017
1 parent 4d63ed2 commit a5b5cb4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ Default: `null`

List the only modules that should be served by the cdn


#### options.exclude

Type: `Array<string>`
Expand All @@ -176,6 +175,14 @@ Default: `false`

Log whether the library is being served by the cdn or is bundled

#### options.resolver
Type: `string`, `function`<br>
Default: `'module-to-cdn'`

Allow you to define a custom module resolver, it can either be a `function` or an npm module
The resolver should return either `null` or an `object` with the keys: `name`, `var`, `url`, `version`


## Related

- [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin)
Expand Down
7 changes: 7 additions & 0 deletions src/get-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function getResolver(resolver = 'module-to-cdn') {
if (typeof resolver === 'function') {
return resolver;
}

return require(resolver);
}
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import moduleToCdn from 'module-to-cdn';
import {sync as readPkgUp} from 'read-pkg-up';
import HtmlWebpackIncludeAssetsPlugin from 'html-webpack-include-assets-plugin';
import ExternalModule from 'webpack/lib/ExternalModule';
import resolvePkg from 'resolve-pkg';
import includes from 'babel-runtime/core-js/array/includes';

import getResolver from './get-resolver';

let HtmlWebpackPlugin;
try {
// eslint-disable-next-line import/no-extraneous-dependencies
Expand All @@ -14,7 +15,7 @@ try {
}

export default class ModulesCdnWebpackPlugin {
constructor({disable = false, env, exclude, only, verbose} = {}) {
constructor({disable = false, env, exclude, only, verbose, resolver} = {}) {
if (exclude && only) {
throw new Error('You can\'t use \'exclude\' and \'only\' at the same time');
}
Expand All @@ -24,6 +25,7 @@ export default class ModulesCdnWebpackPlugin {
this.exclude = exclude || [];
this.only = only || null;
this.verbose = verbose === true;
this.resolver = getResolver(resolver);

this.modulesFromCdn = {};
}
Expand Down Expand Up @@ -83,7 +85,7 @@ export default class ModulesCdnWebpackPlugin {
return false;
}

const cdnConfig = moduleToCdn(modulePath, version, {env});
const cdnConfig = this.resolver(modulePath, version, {env});

if (cdnConfig == null) {
if (this.verbose) {
Expand Down
44 changes: 44 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,47 @@ test('when using multiple versions of a module, make sure the right version is u
const doesIncludeReact = includes(output, 'THIS IS REACT!');
t.false(doesIncludeReact);
});

test('when using a custom resolver', async t => {
await cleanDir(path.resolve(__dirname, './fixtures/output/custom-resolver'));

const stats = await runWebpack({
context: path.resolve(__dirname, './fixtures/app'),

output: {
publicPath: '',
path: path.resolve(__dirname, './fixtures/output/custom-resolver')
},

entry: {
app: './single.js'
},

plugins: [
new ModulesCdnWebpackPlugin({
resolver: () => {
return {
var: 'CustomReact',
name: 'react',
url: 'https://my-cdn.com/react.js',
version: '15.0.0'
};
}
})
]
});

const files = stats.compilation.chunks.reduce((files, x) => files.concat(x.files), []);

t.true(includes(files, 'app.js'));
t.true(includes(files, 'https://my-cdn.com/react.js'));

let output = await fs.readFile(path.resolve(__dirname, './fixtures/output/custom-resolver/app.js'));
output = output.toString();

const doesExportCustomReact = includes(output, 'module.exports = CustomReact');
t.true(doesExportCustomReact);

const doesIncludeReact = includes(output, 'THIS IS REACT!');
t.false(doesIncludeReact);
});

0 comments on commit a5b5cb4

Please sign in to comment.