Skip to content

Commit 868cd23

Browse files
Add ESLint & clean-up code style
1 parent c74e89f commit 868cd23

4 files changed

Lines changed: 163 additions & 92 deletions

File tree

.eslintrc.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module.exports = {
2+
'env': {
3+
'browser': true,
4+
'es6': true
5+
},
6+
'extends': [
7+
'eslint:recommended',
8+
'plugin:@typescript-eslint/eslint-recommended'
9+
],
10+
'globals': {
11+
'Atomics': 'readonly',
12+
'SharedArrayBuffer': 'readonly'
13+
},
14+
'parser': '@typescript-eslint/parser',
15+
'parserOptions': {
16+
'ecmaFeatures': {
17+
'jsx': true
18+
},
19+
'ecmaVersion': 2019,
20+
'sourceType': 'module'
21+
},
22+
'plugins': [
23+
'@typescript-eslint'
24+
],
25+
'rules': {
26+
"comma-dangle": ["error", "always-multiline"],
27+
'indent': [
28+
'error',
29+
4,
30+
{ "SwitchCase": 1 }
31+
],
32+
'linebreak-style': [
33+
'error',
34+
'unix'
35+
],
36+
"object-curly-spacing": [
37+
"error",
38+
"always"
39+
],
40+
"prefer-arrow-callback": "error",
41+
'quotes': [
42+
'error',
43+
'single'
44+
],
45+
'semi': [
46+
'error',
47+
'always'
48+
]
49+
},
50+
"settings": {
51+
"propWrapperFunctions": [
52+
"forbidExtraProps",
53+
{"property": "freeze", "object": "Object"}
54+
],
55+
"linkComponents": [
56+
"Hyperlink",
57+
{"name": "Link", "linkAttribute": "to"}
58+
]
59+
}
60+
};

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('./lib/plugin')
1+
module.exports = require('./lib/plugin');

lib/plugin.js

Lines changed: 92 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,109 @@
1-
const _ = require('lodash')
2-
require('require-safe')('html-webpack-plugin')
1+
const _ = require('lodash');
2+
require('require-safe')('html-webpack-plugin');
33

4-
function ChunkHashExclude (options) {
4+
function ChunkHashExclude(options) {
55
this.options = _.assign({
66
excludeJs: [],
77
excludeCss: [],
88
publicCssPath: '',
9-
cancelHtmlHash: true
10-
}, options)
9+
cancelHtmlHash: true,
10+
}, options);
1111
}
12-
ChunkHashExclude.prototype.cancelAssetsHash = function(compilation, callback) {
12+
13+
ChunkHashExclude.prototype.cancelAssetsHash = (compilation, callback) => {
1314
compilation.chunks.forEach((chunk, i) => {
1415
chunk.files.forEach((filename, j) => {
15-
if (this.options.excludeJs.indexOf(chunk.name) > -1 && filename.indexOf('js') > -1) {
16-
let reg = new RegExp(`(js/)(${chunk.name})(.*)(.js|.js.map)`)
17-
if (filename.replace(reg, '$1$2$4') !== filename) {
18-
compilation.chunks[i].files.splice(j, 1, filename.replace(reg, '$1$2$4'))
19-
compilation.assets[filename.replace(reg, '$1$2$4')] = compilation.assets[filename]
20-
delete compilation.assets[filename]
16+
if (this.options.excludeJs.indexOf(chunk.name) > -1 && filename.indexOf('js') > -1) {
17+
let reg = new RegExp(`(js/)(${chunk.name})(.*)(.js|.js.map)`);
18+
if (filename.replace(reg, '$1$2$4') !== filename) {
19+
compilation.chunks[i].files.splice(j, 1, filename.replace(reg, '$1$2$4'));
20+
compilation.assets[filename.replace(reg, '$1$2$4')] = compilation.assets[filename];
21+
delete compilation.assets[filename];
22+
}
2123
}
22-
}
23-
if (filename.indexOf('css') > -1) {
24-
let reg = new RegExp(`(css/)(${chunk.name})(.*)(.css|.css.map)`)
25-
let newFilename = filename.replace(reg, '$1$2$4')
26-
if (this.options.excludeCss.indexOf(chunk.name) > -1) {
27-
if (newFilename !== filename) {
28-
compilation.chunks[i].files.splice(j, 1, newFilename)
29-
compilation.assets[newFilename] = compilation.assets[filename]
30-
delete compilation.assets[filename]
31-
}
24+
if (filename.indexOf('css') > -1) {
25+
let reg = new RegExp(`(css/)(${chunk.name})(.*)(.css|.css.map)`);
26+
let newFilename = filename.replace(reg, '$1$2$4');
27+
if (this.options.excludeCss.indexOf(chunk.name) > -1) {
28+
if (newFilename !== filename) {
29+
compilation.chunks[i].files.splice(j, 1, newFilename);
30+
compilation.assets[newFilename] = compilation.assets[filename];
31+
delete compilation.assets[filename];
32+
}
33+
}
3234
}
33-
}
34-
})
35-
})
36-
callback()
37-
}
35+
});
36+
});
37+
callback();
38+
};
3839

39-
ChunkHashExclude.prototype.cancelHtmlHash = function (compilation, callback) {
40+
ChunkHashExclude.prototype.cancelHtmlHash = (compilation, callback) => {
4041
compilation.chunks.map(chunk => {
41-
if (this.options.excludeJs.indexOf(chunk.names[0]) > -1) {
42-
let reg = new RegExp(`(js/)(${chunk.names[0]})(.*)(.js)`)
43-
let index = -1
44-
compilation.body.some((item, i) => {
45-
if (item.attributes.src.indexOf(chunk.names[0]) > -1) {
46-
index = i
47-
return true
48-
}
49-
return false
50-
})
51-
if (index > -1) compilation.body[index].attributes.src = compilation.body[index].attributes.src.replace(reg, '$1$2$4')
52-
}
53-
if (this.options.excludeCss.indexOf(chunk.names[0]) > -1) {
54-
let reg = new RegExp(`(css/)(${chunk.names[0]})(.*)(.css)`)
55-
let index = -1
56-
compilation.head.some((item, i) => {
57-
if (item.attributes.href.indexOf(chunk.names[0]) > -1) {
58-
index = i
59-
return true
60-
}
61-
return false
62-
})
63-
if (index > -1) compilation.head[index].attributes.href = compilation.head[index].attributes.href.replace(reg, '$1$2$4')
64-
}
65-
})
66-
callback()
67-
}
68-
// 去掉html模版里面对应chunk资源文件的hash值
69-
ChunkHashExclude.prototype.apply = function (compiler) {
42+
if (this.options.excludeJs.indexOf(chunk.names[0]) > -1) {
43+
let reg = new RegExp(`(js/)(${chunk.names[0]})(.*)(.js)`);
44+
let index = -1;
45+
compilation.body.some((item, i) => {
46+
if (item.attributes.src.indexOf(chunk.names[0]) > -1) {
47+
index = i;
48+
return true;
49+
}
50+
return false;
51+
});
52+
if (index > -1) compilation.body[index].attributes.src = compilation.body[index].attributes.src.replace(reg, '$1$2$4');
53+
}
54+
if (this.options.excludeCss.indexOf(chunk.names[0]) > -1) {
55+
let reg = new RegExp(`(css/)(${chunk.names[0]})(.*)(.css)`);
56+
let index = -1;
57+
compilation.head.some((item, i) => {
58+
if (item.attributes.href.indexOf(chunk.names[0]) > -1) {
59+
index = i;
60+
return true;
61+
}
62+
return false;
63+
});
64+
if (index > -1) compilation.head[index].attributes.href = compilation.head[index].attributes.href.replace(reg, '$1$2$4');
65+
}
66+
});
67+
callback();
68+
};
69+
// Remove the hash value of the corresponding chunk resource file in the html template
70+
ChunkHashExclude.prototype.apply = (compiler) => {
7071
if (compiler.hooks) {
71-
if (this.options.publicCssPath) {
72-
compiler.hooks.thisCompilation.tap('thisCompilation', compilation => {
73-
compilation.mainTemplate.hooks.requireEnsure.tap('requireEnsure', (source, chunk, hash) => {
74-
return source.replace('__webpack_require__.p', "'" + this.options.publicCssPath + "'")
75-
})
76-
})
77-
}
72+
if (this.options.publicCssPath) {
73+
compiler.hooks.thisCompilation.tap('thisCompilation', compilation => {
74+
compilation.mainTemplate.hooks.requireEnsure.tap('requireEnsure', (source, chunk, hash) => {
75+
return source.replace('__webpack_require__.p', `'${this.options.publicCssPath}'`);
76+
});
77+
});
78+
}
7879
compiler.hooks.emit.tapAsync('emit', (compilation, callback) => {
79-
this.cancelAssetsHash(compilation, callback)
80-
})
81-
// 替换html静态资源的hash值
80+
this.cancelAssetsHash(compilation, callback);
81+
});
82+
// Replace the hash value of the html static resource
8283
if (this.options.cancelHtmlHash) {
83-
compiler.hooks.compilation.tap('compilation', (compilation) => {
84-
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('compilation', (compilation, callback) => {
85-
this.cancelHtmlHash(compilation, callback)
86-
})
87-
})
88-
}
84+
compiler.hooks.compilation.tap('compilation', (compilation) => {
85+
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('compilation', (compilation, callback) => {
86+
this.cancelHtmlHash(compilation, callback);
87+
});
88+
});
89+
}
8990
} else {
90-
compiler.plugin('emit', (compilation, callback) => {
91-
cancelAssetsHash(compilation, callback)
92-
})
93-
if (this.options.cancelHtmlHash) {
94-
compiler.plugin('html-webpack-plugin-alter-asset-tags', (compilation, callback) => {
95-
cancelHtmlHash(compilation, callback)
96-
})
97-
}
98-
if (this.options.publicCssPath) {
99-
compiler.plugin('this-compilation', compilation => {
100-
compilation.mainTemplate.plugin('require-ensure', (source, chunk, hash) => {
101-
return source.replace('__webpack_require__.p', "'" + this.options.publicCssPath + "'")
102-
})
103-
})
104-
}
91+
compiler.plugin('emit', (compilation, callback) => {
92+
cancelAssetsHash(compilation, callback);
93+
});
94+
if (this.options.cancelHtmlHash) {
95+
compiler.plugin('html-webpack-plugin-alter-asset-tags', (compilation, callback) => {
96+
cancelHtmlHash(compilation, callback);
97+
});
98+
}
99+
if (this.options.publicCssPath) {
100+
compiler.plugin('this-compilation', compilation => {
101+
compilation.mainTemplate.plugin('require-ensure', (source, chunk, hash) => {
102+
return source.replace('__webpack_require__.p', `'${this.options.publicCssPath}'`);
103+
});
104+
});
105+
}
105106
}
106-
}
107-
108-
module.exports = ChunkHashExclude
107+
};
108+
109+
module.exports = ChunkHashExclude;

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,15 @@
2020
"html-webpack-plugin": "^3.2.0",
2121
"lodash": "^4.17.11",
2222
"require-safe": "^1.1.0"
23+
},
24+
"devDependencies": {
25+
"@typescript-eslint/eslint-plugin": "^2.10.0",
26+
"@typescript-eslint/parser": "^2.10.0",
27+
"babel-eslint": "10.0.3",
28+
"eslint": "^6.6.0",
29+
"eslint-loader": "3.0.3",
30+
"eslint-plugin-flowtype": "4.6.0",
31+
"eslint-plugin-import": "2.20.0",
32+
"typescript": "^3.7.5"
2333
}
2434
}

0 commit comments

Comments
 (0)