-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (52 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Modified from `babel-plugin-dynamic-import-chunk-name` as it used a slightly
// different naming system than what we wanted. Using the plugin and correcting
// the names after the fact would be more effort than forking. Additionally, the
// plugin does not have source code available online. While we're unlikely to run
// into issues, having a way to create fixes ourselves would be beneficial.
//
// https://www.npmjs.im/babel-plugin-dynamic-import-chunk-name
// MIT Licensed: https://www.runpkg.com/[email protected]/LICENSE
function removeTrailingIndex(string) {
return string.split('-').length - 1 > 1
? string.replace(/-index$/, '')
: string;
}
function convertToKebabCase(string) {
return string
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase();
}
function getChunkName(filename) {
filename = filename
.split('/')
.map((part) => part.replace(/\..*$/, ''))
.filter(Boolean)
.map(convertToKebabCase)
.join('-');
return removeTrailingIndex(filename);
}
function hasComment(comment) {
return comment && comment.value.replace(/\*+/g, '').trim().startsWith('webpackChunkName');
}
/**
* @param {import('@babel/core')} babel
* @returns {import('@babel/core').PluginObj}
*/
export default function webpackChunkNameCommentsPlugin({ types: t }) {
return {
name: 'webpack-chunk-name-comments',
visitor: {
CallExpression(path) {
if (path.node.callee.type !== 'Import') {
return;
}
const [arg] = path.node.arguments;
const [comment] = arg.leadingComments || [];
if (!hasComment(comment)) {
t.addComment(arg, 'leading', `webpackChunkName: "${getChunkName(arg.value)}"`);
}
},
},
};
};