Skip to content

Commit

Permalink
fix: do not include module if peers deps failed
Browse files Browse the repository at this point in the history
closes #22
  • Loading branch information
mastilver committed Jul 24, 2017
1 parent a5b5cb4 commit 7257b5f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,17 @@ export default class ModulesCdnWebpackPlugin {
}

if (peerDependencies) {
let arePeerDependenciesLoaded = true;
for (const peerDependencyName in peerDependencies) {
if ({}.hasOwnProperty.call(peerDependencies, peerDependencyName)) {
this.addModule(contextPath, peerDependencyName, {env});
const isModuleLoaded = Boolean(this.addModule(contextPath, peerDependencyName, {env}));
arePeerDependenciesLoaded = arePeerDependenciesLoaded && isModuleLoaded;
}
}

if (!arePeerDependenciesLoaded) {
return false;
}
}

// TODO: on next breaking change, rely on module-to-cdn>=3.1.0 to get version
Expand Down
52 changes: 52 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,55 @@ test('when using a custom resolver', async t => {
const doesIncludeReact = includes(output, 'THIS IS REACT!');
t.false(doesIncludeReact);
});

test('when one peerDependency fails, do not load from cdn', async t => {
await cleanDir(path.resolve(__dirname, './fixtures/output/failing-peer-dependency'));

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

output: {
publicPath: '',
path: path.resolve(__dirname, './fixtures/output/failing-peer-dependency')
},

entry: {
app: './peer-dependencies.js'
},

plugins: [
new ModulesCdnWebpackPlugin({
resolver: name => {
return {
'@angular/core': {
var: 'ng',
name: 'angular',
url: 'https://unpkg.com/@angular/[email protected]/bundles/core.umd.js',
version: '4.2.4'
},
rxjs: {
var: 'Rx',
name: 'rxjs',
url: 'https://unpkg.com/[email protected]/bundles/Rx.js',
version: '5.4.1'
}
}[name];
}
})
]
});

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

t.is(files.length, 2);
t.true(includes(files, 'app.js'));
t.false(includes(files, 'https://unpkg.com/@angular/[email protected]/bundles/core.umd.js'));
t.true(includes(files, 'https://unpkg.com/[email protected]/bundles/Rx.js'));
t.false(includes(files, 'https://unpkg.com/[email protected]/dist/zone.js'));

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

const doesIncludeAngular = includes(output, 'console.log(\'THIS IS ANGULAR!\');');
t.true(doesIncludeAngular);
});
1 change: 1 addition & 0 deletions test/fixtures/app/node_modules/@angular/core/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/app/node_modules/@angular/core/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7257b5f

Please sign in to comment.