Skip to content

Commit

Permalink
respond to PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
byteme980 committed Apr 10, 2018
1 parent c9d7d66 commit e6e4e98
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
10 changes: 5 additions & 5 deletions docs/rules/dynamic-import-chunkname.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

This rule reports any dynamic imports without a webpackChunkName specified in a leading block comment in the proper format.

This is a useful rule because if the webpackChunkName is not defined in a dynamic import, Webpack will autogenerate the chunk name.
This rule enforces naming of webpack chunks in dynamic imports. When you don't explicitly name chunks, webpack will autogenerate chunk names that are not consistent across builds, which prevents long-term browser caching.

## Rule Details
This rule runs against `import` by default, but can be configured to also run against an alternative dynamic-import function, e.g. 'dynamicImport.'
You can also configure the regex format you'd like to accept for the webpackChunkName - for example, we don't want the number 6 to show up in our chunk names.
This rule runs against `import()` by default, but can be configured to also run against an alternative dynamic-import function, e.g. 'dynamicImport.'
You can also configure the regex format you'd like to accept for the webpackChunkName - for example, if we don't want the number 6 to show up in our chunk names:
```javascript
{
"dynamic-import-chunkname": [2, {
importFunction: "dynamicImport",
importFunctions: ["dynamicImport"],
webpackChunknameFormat: "[a-zA-Z0-57-9-/_]"
}]
}
Expand Down Expand Up @@ -63,4 +63,4 @@ The following patterns are valid:

## When Not To Use It

If you don't care that Webpack will autogenerate chunk names and may blow up browser caches and bundle size reports.
If you don't care that webpack will autogenerate chunk names and may blow up browser caches and bundle size reports.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const rules = {
'no-unassigned-import': require('./rules/no-unassigned-import'),
'no-useless-path-segments': require('./rules/no-useless-path-segments'),
'dynamic-import-chunkname': require('./rules/dynamic-import-chunkname'),

// export
'exports-last': require('./rules/exports-last'),

Expand Down
23 changes: 11 additions & 12 deletions src/rules/dynamic-import-chunkname.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ module.exports = {
schema: [{
type: 'object',
properties: {
importFunction: {
type: 'string',
importFunctions: {
type: 'array',
items: {
type: 'string',
},
},
webpackChunknameFormat: {
type: 'string',
Expand All @@ -20,23 +23,19 @@ module.exports = {

create: function (context) {
const config = context.options[0]
let importFunction
if (config) {
({ importFunction } = config)
}
const { importFunctions = [] } = config || {}
const { webpackChunknameFormat = '[0-9a-zA-Z-_/.]+' } = config || {}

let webpackChunknameFormat = '[0-9a-zA-Z-_/.]+'
if (config && config.webpackChunknameFormat) {
({ webpackChunknameFormat } = config)
}
const commentFormat = ` webpackChunkName: "${webpackChunknameFormat}" `
const commentRegex = new RegExp(commentFormat)

return {
CallExpression(node) {
if (node.callee.name !== importFunction && node.callee.type !== 'Import') {
[`CallExpression[callee.type="Import"],CallExpression[callee.name]`](node) {
const { callee: { name }} = node
if (name && !importFunctions.includes(name)) {
return
}

const sourceCode = context.getSourceCode()
const arg = node.arguments[0]
const leadingComments = sourceCode.getComments(arg).leading
Expand Down
4 changes: 2 additions & 2 deletions tests/src/rules/dynamic-import-chunkname.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const ruleTester = new RuleTester()

const commentFormat = '[0-9a-zA-Z-_/.]+'
const pickyCommentFormat = '[a-zA-Z-_/.]+'
const options = [{ importFunction: 'dynamicImport' }]
const options = [{ importFunctions: ['dynamicImport'] }]
const pickyCommentOptions = [{
importFunction: 'dynamicImport',
importFunctions: ['dynamicImport'],
webpackChunknameFormat: pickyCommentFormat,
}]
const parser = 'babel-eslint'
Expand Down

0 comments on commit e6e4e98

Please sign in to comment.