Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(migrate): CommonChunksPlugin to SplitChunksPlugin #558

Merged
merged 16 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" data 1`] = `
"module.exports = {
entry: {
app: './src/app.js',
vendor: './src/vendors.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
app: {
name: 'app',
chunks: 'initial',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add minChunks: 2 here

enforce: true,
test: 'app'
},

vendor: {
name: 'vendor',
chunks: 'initial',
enforce: true,
test: 'vendor'
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case runtimeChunk: { name: "vendor" } makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input webpack config doesn't have runtime anywhere. How to identify when to apply runtimeChunk?

This is the input case for this output:

module.exports = {
	entry: {
		app: './src/app.js',
		vendor: './src/vendors.js',
	},
	plugins: [
		new webpack.optimize.CommonsChunkPlugin({
			names: ["app", "vendor"],
			minChunks: 2
		})
	]
}

}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" data 1`] = `
"module.exports = {
entry: {
vendor: './src/vendors.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
common: {
name: 'common',
chunks: 'initial',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default minChunks for the CCP is the number of selected chunks. So you may count the number of entrypoints here.

enforce: true
},

vendor: {
name: 'vendor',
chunks: 'initial',
enforce: true,
test: 'vendor'
}
}
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" data 1`] = `
"module.exports = {
entry: {
vendor: './src/vendors.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
vendor: {
name: 'vendor',
chunks: 'initial',
enforce: true,
test: 'vendor'
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks correct to me

}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" data 1`] = `
"module.exports = {
optimizations: {
splitChunks: {
cacheGroups: {
commons: {
name: 'commons',
chunks: 'initial',

This comment was marked as resolved.

enforce: true,
filename: 'commons.js'
}
}
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" data 1`] = `
"module.exports = {
entry: {
main: './src/index.js',
},

optimizations: {
splitChunks: {
chunks: 'async',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do it globally

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would CCP async: true migrate to, for SCP?

Copy link
Contributor

@ematipico ematipico Aug 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think inside the cacheGroups

splitChunks: {
   cacheGroups: {
      chunks: 'async'
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the docs:

js

module.exports = {
  //...
  optimization: {
    splitChunks: {
      // include all types of chunks
      chunks: 'async'
    }
  }
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know, but in cacheGroups you can override almost everything. As per docs

minSize: 2000,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this into the cacheGroups.main

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not resolved.

This case is not 100% map-able to splitChunks. This might be the best option:

            cacheGroups: {
                main: {
                    minSize: 2000,
                    chunks: 'async'
                }
            }


cacheGroups: {
main: {
name: 'main',
test: 'main'
}
}
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" data 1`] = `
"module.exports = {
entry: {
main: './src/index.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
main: {
name: 'main',
chunks: 'initial',
enforce: true,
test: 'main'
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks correct

}
},

runtimeChunk: {
name: 'runtime'
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" data 1`] = `
"module.exports = {
entry: {
main: './src/index.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
main: {
name: 'main',
chunks: 'initial',
enforce: true,

test: module => {
if (module.getChunks().some(chunk => chunk.name === 'main'))
return true;

const fn = ({ resource }) => /node_modules/.test(resource);
return fn(module);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me

}
}
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" data 1`] = `
"module.exports = {
entry: {
main: './src/index.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
main: {
name: 'main',
chunks: 'initial',
enforce: true,

test: module => {
if (module.getChunks().some(chunk => chunk.name === 'main'))
return true;

const fn = ({ resource }) => {
// some code
return /node_modules/.test(resource);
};

return fn(module);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me

}
}
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" data 1`] = `
"module.exports = {
entry: {
main: './src/index.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
main: {
name: 'main',
chunks: 'initial',
enforce: true,

test: module => {
if (module.getChunks().some(chunk => chunk.name === 'main'))
return true;

const fn = function ({ resource }) {
// some code
return /node_modules/.test(resource);
};

return fn(module);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me

}
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" data 1`] = `
"module.exports = {
entry: {
main: './src/index.js',
},

optimizations: {
splitChunks: {
cacheGroups: {
main: {
name: 'main',
chunks: 'initial',
enforce: true,

test: module => {
if (module.getChunks().some(chunk => chunk.name === 'main'))
return true;

const fn = function ({ resource }) {
if (foo) {
return /node_modulesfoo/.test(resource);
} else {
return /node_modulesbaz/.test(resource);
}
};

return fn(module);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me

}
}
}
}
}
"
`;

exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" data 1`] = `
"module.exports = {
entry: {
main: './src/index.js',
},

optimizations: {
splitChunks: {
minSize: 3000,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This need to be moved into cacheGroups.main

Copy link
Member Author

@dhruvdutt dhruvdutt Aug 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved.


cacheGroups: {
main: {
name: 'main',
chunks: 'initial',
enforce: true,
test: 'main'
}
}
}
}
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
entry: {
app: './src/app.js',
vendor: './src/vendors.js',
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["app", "vendor"],
minChunks: 2
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
entry: {
vendor: './src/vendors.js',
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["common", "vendor"]
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
entry: {
vendor: './src/vendors.js',
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["vendor"]
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "commons",
filename: "commons.js",
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
entry: {
main: './src/index.js',
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "main",
async: true,
minSize: 2000,
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
entry: {
main: './src/index.js',
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["main", "runtime"],
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
entry: {
main: './src/index.js',
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "main",
minChunks: ({ resource }) => /node_modules/.test(resource),
})
]
}
Loading