Skip to content

Commit

Permalink
fix: use correct module when using multiple modules versions (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver authored Jul 19, 2017
1 parent ed1e72a commit d3e2ea1
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export default class ModulesCdnWebpackPlugin {

this.disable = disable;
this.env = env || process.env.NODE_ENV || 'development';
this.urls = {};
this.exclude = exclude || [];
this.only = only || null;
this.verbose = verbose === true;

this.modulesFromCdn = {};
}

apply(compiler) {
Expand Down Expand Up @@ -72,6 +73,16 @@ export default class ModulesCdnWebpackPlugin {

const {version, peerDependencies} = readPkgUp({cwd: resolvePkg(modulePath, {cwd: contextPath})}).pkg;

const isModuleAlreadyLoaded = Boolean(this.modulesFromCdn[modulePath]);
if (isModuleAlreadyLoaded) {
const isSameVersion = this.modulesFromCdn[modulePath].version === version;
if (isSameVersion) {
return modulePath;
}

return false;
}

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

if (cdnConfig == null) {
Expand All @@ -93,7 +104,12 @@ export default class ModulesCdnWebpackPlugin {
}
}

this.urls[cdnConfig.name] = cdnConfig.url;
// TODO: on next breaking change, rely on module-to-cdn>=3.1.0 to get version
this.modulesFromCdn[modulePath] = Object.assign(
{},
cdnConfig,
{version}
);

return cdnConfig.var;
}
Expand All @@ -103,11 +119,11 @@ export default class ModulesCdnWebpackPlugin {
const entrypoint = compilation.entrypoints[Object.keys(compilation.entrypoints)[0]];
const parentChunk = entrypoint.chunks.find(x => x.isInitial());

for (const name of Object.keys(this.urls)) {
const url = this.urls[name];
for (const name of Object.keys(this.modulesFromCdn)) {
const cdnConfig = this.modulesFromCdn[name];

const chunk = compilation.addChunk(name);
chunk.files.push(url);
chunk.files.push(cdnConfig.url);

chunk.parents = [parentChunk];
parentChunk.addChunk(chunk);
Expand All @@ -128,7 +144,7 @@ export default class ModulesCdnWebpackPlugin {
includeAssetsPlugin.apply(compiler);

compiler.plugin('after-compile', (compilation, cb) => {
const assets = Object.values(this.urls);
const assets = Object.keys(this.modulesFromCdn).map(key => this.modulesFromCdn[key].url);

// HACK: Calling the constructor directly is not recomended
// But that's the only secure way to edit `assets` afterhand
Expand Down
35 changes: 35 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,38 @@ test('async loading', async t => {
const doesIncludeReact = outputs.some(output => includes(output, 'THIS IS REACT!'));
t.false(doesIncludeReact);
});

test('when using multiple versions of a module, make sure the right version is used for each', async t => {
await cleanDir(path.resolve(__dirname, './fixtures/output/multiple-versions'));

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

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

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

plugins: [
new ModulesCdnWebpackPlugin()
]
});

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

t.true(includes(files, 'app.js'));
t.true(includes(files, 'https://unpkg.com/[email protected]/dist/react.js'));

const output = await fs.readFile(path.resolve(__dirname, './fixtures/output/multiple-versions/app.js'));

// NOTE: not inside t.false to prevent ava to display whole file in console
const doesIncludeReact14 = includes(output, 'THIS IS [email protected]!');
t.true(doesIncludeReact14);

const doesIncludeReact = includes(output, 'THIS IS REACT!');
t.false(doesIncludeReact);
});
1 change: 1 addition & 0 deletions test/fixtures/app/dir/single.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import React from 'react';
2 changes: 2 additions & 0 deletions test/fixtures/app/mix.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import a from 'a';
import React from 'react';

import single from './dir/single';
2 changes: 2 additions & 0 deletions test/fixtures/app/node_modules/a/a.js

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

68 changes: 68 additions & 0 deletions test/fixtures/app/node_modules/a/node_modules/react/package.json

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

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

0 comments on commit d3e2ea1

Please sign in to comment.