Skip to content

Commit

Permalink
refactor: enhance lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-zone committed Dec 3, 2018
1 parent 11a5379 commit 0017bc9
Show file tree
Hide file tree
Showing 27 changed files with 354 additions and 316 deletions.
36 changes: 25 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,45 @@ module.exports = {
"node": true,
"es6": true
},
"extends": "eslint:recommended",
extends: [
"eslint:recommended",
"plugin:prettier/recommended"
],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017
"ecmaVersion": 8
},
"rules": {
"prettier/prettier": "error",
"no-extra-semi": "error",
"no-template-curly-in-string": "error",
"no-caller": "error",
"no-control-regex": "off",
"yoda": "error",
"eqeqeq": "error",
"global-require": "off",
"brace-style": "off",
"eol-last": "error",
"no-extra-bind": "warn",
"no-process-exit": "warn",
"no-use-before-define": "off",
"no-unused-vars": ["error", { args: "none" }],
"no-unsafe-negation": "error",
"no-loop-func": "warn",
"indent": "off",
"no-console": "off",
"no-unused-vars": ["error", { "args": "none" }],
"no-empty": "off",
"no-multi-spaces": "error",
"no-multiple-empty-lines": "error",
"brace-style":"error",
"arrow-parens": ["error", "as-needed"],
"indent": ["error", 2, { "SwitchCase": 1 }],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
"double",
{ avoidEscape: true, allowTemplateLiterals: false }
],
"semi": [
"error",
"always"
]
"prettier/prettier": ["error", { "singleQuote": false }]
}
};
4 changes: 2 additions & 2 deletions examples/entry1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var $ = require("jQuery");
var lib = require("./lib");
const $ = require("jQuery");
const lib = require("./lib");

console.log("entry1");
console.log(lib);
Expand Down
10 changes: 10 additions & 0 deletions lib/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
extends: ["plugin:node/recommended"],
rules: {
"no-undef": "error",
"no-var": "error",
"node/no-unsupported-features": "error",
"node/no-deprecated-api": "error",
"node/no-missing-import": "error"
}
};
38 changes: 19 additions & 19 deletions lib/createStartup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var fs = require("fs");
var path = require("path");
var UglifyJS = require("uglify-js");
var asString = require("./util/template").asString;
var indent = require("./util/template").indent;
const fs = require("fs");
const path = require("path");
const UglifyJS = require("uglify-js");
const asString = require("./util/template").asString;
const indent = require("./util/template").indent;

var libraryTargetWarn = fs.readFileSync(
const libraryTargetWarn = fs.readFileSync(
path.resolve(__dirname, "../template/startup/libraryTargetWarn.js"),
"utf8"
);
Expand All @@ -26,14 +26,14 @@ module.exports = function(
polyfill
} = {}
) {
var source = asString([
const source = asString([
"(function(manifest, __retry_function__, window, global){",
polyfill ? polyfill : "", // an opportunity to insert polyfills
enableLog
? indent([
"var emptyFunc = function(){};",
"var log = typeof console !== 'undefined' ? console : { warn:emptyFunc, log:emptyFunc, error:emptyFunc };"
])
"var emptyFunc = function(){};",
"var log = typeof console !== 'undefined' ? console : { warn:emptyFunc, log:emptyFunc, error:emptyFunc };"
])
: "",
indent([
"/*",
Expand All @@ -60,11 +60,11 @@ module.exports = function(
indent([
"switch(item.libraryTarget){",
indent([
"case \"var\":",
"case \"this\":",
"case \"window\":",
'case "var":',
'case "this":',
'case "window":',
indent(["if(window[item.name]) return true;", "break;"]),
"case \"global\":",
'case "global":',
indent(["if(global[item.name]) return true;", "break;"]),
"default:",
indent([enableLog ? libraryTargetWarn : "", "return true;"])
Expand Down Expand Up @@ -167,10 +167,10 @@ module.exports = function(
"if(notFoundItemsName.length){",
enableLog
? indent([
"log.warn('[Bundle Ensure Plugin] WARNING: Some bundles still not found after reload: ' + ",
"notFoundItemsName.join(', ')",
");"
])
"log.warn('[Bundle Ensure Plugin] WARNING: Some bundles still not found after reload: ' + ",
"notFoundItemsName.join(', ')",
");"
])
: "",
"}else{",
enableLog
Expand Down Expand Up @@ -202,7 +202,7 @@ module.exports = function(
]);

if (minify) {
var result = UglifyJS.minify(source);
const result = UglifyJS.minify(source);
if (result.error) {
throw result.error;
}
Expand Down
48 changes: 24 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ class BundleEnsureWebpackPlugin extends Tapable {
}

findEntrypointsInHtml(chunks, compilation) {
var includeEntryChunksId = new Set();
const includeEntryChunksId = new Set();
chunks.forEach(chunk => {
if (this.entryChunks.has(chunk.id)) {
includeEntryChunksId.add(chunk.id);
}
});
var entrypoints = new Map();
const entrypoints = new Map();
compilation.entrypoints.forEach((entrypoint, entryName) => {
var entrypointChunksId = entrypoint.chunks.map(chunk => chunk.id);
const entrypointChunksId = entrypoint.chunks.map(chunk => chunk.id);
if (
entrypointChunksId.find(id => includeEntryChunksId.has(id)) !==
undefined
Expand All @@ -95,10 +95,10 @@ class BundleEnsureWebpackPlugin extends Tapable {
}

getManifestForEntry(entrypoint, compilation) {
var manifest = [];
const manifest = [];

//collect all externals
var externals = util.getUsedExternals(compilation);
const externals = util.getUsedExternals(compilation);
externals.forEach((external, name) => {
manifest.push({
isExternal: true,
Expand All @@ -110,10 +110,10 @@ class BundleEnsureWebpackPlugin extends Tapable {
});

//collect chunks
var needChunksId = new Set(entrypoint.chunks.map(chunk => chunk.id));
const needChunksId = new Set(entrypoint.chunks.map(chunk => chunk.id));
this.prepareChunks.forEach((chunk, id) => {
if (!needChunksId.has(id)) return;
var filename = util.getChunkMainAssetFilename(chunk, compilation);
const filename = util.getChunkMainAssetFilename(chunk, compilation);
manifest.push({
isChunk: true,
id: id,
Expand All @@ -124,7 +124,7 @@ class BundleEnsureWebpackPlugin extends Tapable {
});
this.entryChunks.forEach((chunk, id) => {
if (!needChunksId.has(id)) return;
var filename = util.getChunkMainAssetFilename(chunk, compilation);
const filename = util.getChunkMainAssetFilename(chunk, compilation);
manifest.push({
isChunk: true,
id: id,
Expand All @@ -138,7 +138,7 @@ class BundleEnsureWebpackPlugin extends Tapable {
}

mergeManifest(manifestList) {
var mergeMap = new Map();
const mergeMap = new Map();
manifestList.forEach(manifest => {
manifest.forEach(chunk => {
if (chunk.isChunk) {
Expand All @@ -155,9 +155,9 @@ class BundleEnsureWebpackPlugin extends Tapable {
}

renderStartupScript(compilation, manifest) {
var options = this.options;
var jsonpFunctionName = util.getJsonpFunctionName(compilation);
var publicPath = options.publicPath || util.getPublicPath(compilation);
const options = this.options;
const jsonpFunctionName = util.getJsonpFunctionName(compilation);
const publicPath = options.publicPath || util.getPublicPath(compilation);

//remove useless
manifest.forEach(item => {
Expand All @@ -174,21 +174,21 @@ class BundleEnsureWebpackPlugin extends Tapable {
}

emitStartupScript(compilation, script, entryName) {
var outputFilename = this.getScriptOutputFilename(entryName);
const outputFilename = this.getScriptOutputFilename(entryName);
compilation.assets[outputFilename] = {
source: () => script,
size: () => Buffer.byteLength(script)
};
}

getScriptOutputFilename(entryName) {
var filename = this.options.startupFilename;
let filename = this.options.startupFilename;
filename = filename.replace("[name]", entryName);
return filename;
}

apply(compiler) {
var options = this.options;
const options = this.options;

compiler.hooks.compilation.tap("BundleEnsureWebpackPlugin", compilation => {
if (compilation.compiler !== compiler) {
Expand Down Expand Up @@ -251,9 +251,9 @@ class BundleEnsureWebpackPlugin extends Tapable {

compilation.hooks.bundleEnsureWebpackPluginCreated.call(this);

var entryManifests = new Map();
const entryManifests = new Map();
compilation.entrypoints.forEach((entrypoint, entryName) => {
var manifest = this.hooks.countEntryManifest.call(
const manifest = this.hooks.countEntryManifest.call(
true,
entrypoint,
compilation
Expand All @@ -267,7 +267,7 @@ class BundleEnsureWebpackPlugin extends Tapable {
* should filter out entrypoints through the entry chunks which are included in page.
*/
if (options.associateWithHtmlPlugin) {
var startupScript;
let startupScript;
const {
htmlWebpackPluginAlterAssetTags,
htmlWebpackPluginAfterHtmlProcessing
Expand All @@ -276,16 +276,16 @@ class BundleEnsureWebpackPlugin extends Tapable {
htmlWebpackPluginAlterAssetTags.tapAsync(
"BundleEnsureWebpackPlugin",
(htmlPluginData, callback) => {
var entrypointsMap = this.findEntrypointsInHtml(
const entrypointsMap = this.findEntrypointsInHtml(
htmlPluginData.chunks,
compilation
);
var manifests = [];
const manifests = [];
entrypointsMap.forEach((point, entryName) => {
manifests.push(entryManifests.get(entryName));
});
var manifest = this.mergeManifest(manifests);
var entrypoints = Array.from(entrypointsMap.values());
const manifest = this.mergeManifest(manifests);
const entrypoints = Array.from(entrypointsMap.values());
startupScript = this.hooks.renderStartup.call(
true,
manifest,
Expand Down Expand Up @@ -329,8 +329,8 @@ class BundleEnsureWebpackPlugin extends Tapable {
"BundleEnsureWebpackPlugin",
(compilation, callback) => {
entryManifests.forEach((manifest, entryName) => {
var entrypoint = compilation.entrypoints.get(entryName);
var startupScript = this.hooks.renderStartup.call(
const entrypoint = compilation.entrypoints.get(entryName);
const startupScript = this.hooks.renderStartup.call(
true,
manifest,
[entrypoint],
Expand Down
2 changes: 1 addition & 1 deletion lib/modifyStartup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const asString = require("./util/template").asString;
const indent = require("./util/template").indent;

module.exports = (source, chunk, hash, checkerName) => {
var startupCode = asString([
const startupCode = asString([
`if(typeof ${checkerName} === "function"){`,
indent([`${checkerName}(__webpack_origin_startup__);`]),
"}else{",
Expand Down
8 changes: 4 additions & 4 deletions lib/templateFactory.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var fs = require("fs");
var path = require("path");
var templateDir = path.resolve(__dirname, "../template/retry/");
const fs = require("fs");
const path = require("path");
const templateDir = path.resolve(__dirname, "../template/retry/");

module.exports = function(setting) {
var template;
let template;
switch (setting) {
case "default":
template = fs.readFileSync(
Expand Down
9 changes: 4 additions & 5 deletions lib/util/compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require("path");

module.exports = {
getPublicPath: function(compilation) {
var publicPath =
let publicPath =
compilation.mainTemplate.getPublicPath({ hash: compilation.hash }) || "";
if (publicPath.length && !/\/$/.test(publicPath)) {
publicPath += "/";
Expand All @@ -15,14 +15,13 @@ module.exports = {
},

getUsedExternals(compilation) {
var usedExternals = new Map();
const usedExternals = new Map();
compilation.modules.forEach(module => {
if (
module.constructor.name == "ExternalModule" &&
module.constructor.name === "ExternalModule" &&
module.reasons.length
) {
var name = module.request;
usedExternals.set(name, module);
usedExternals.set(module.request, module);
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/util/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function indent(str) {
} else {
str = str.trimRight();
if (!str) return "";
var ind = str[0] === "\n" ? "" : " ";
const ind = str[0] === "\n" ? "" : " ";
return ind + str.replace(/\n([^\n])/g, "\n $1");
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/wrapChunkSource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var ConcatSource = require("webpack-sources").ConcatSource;
const ConcatSource = require("webpack-sources").ConcatSource;

module.exports = function(chunk, source, storeName, windowName) {
var newSource = new ConcatSource();
const newSource = new ConcatSource();
newSource.add(
`!((${storeName} = ${storeName} || {})[${JSON.stringify(chunk.id)}] = 0);`
);
Expand Down
Loading

0 comments on commit 0017bc9

Please sign in to comment.