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

chore: added type checking using comments #2107

Merged
49 changes: 46 additions & 3 deletions lib/utils/addEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
const webpack = require('webpack');
const createDomain = require('./createDomain');

/**
* A Entry, it can be of type string or string[] or Object<string | string[],string>
* @typedef {(string[] | string | Object<string | string[],string>)} Entry
*/

/**
* Add entries Method
* @param {?Object} config - Webpack config
* @param {?Object} options - Dev-Server options
* @param {?Object} server
* @returns {void}
*/
function addEntries(config, options, server) {
if (options.inline !== false) {
// we're stubbing the app in this method as it's static and doesn't require
Expand All @@ -15,21 +27,33 @@ function addEntries(config, options, server) {
},
};

/** @type {string} */
const domain = createDomain(options, app);
/** @type {string} */
const sockHost = options.sockHost ? `&sockHost=${options.sockHost}` : '';
/** @type {string} */
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
/** @type {string} */
const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
/** @type {string} */
const clientEntry = `${require.resolve(
'../../client/'
)}?${domain}${sockHost}${sockPath}${sockPort}`;

/** @type {(string[] | string)} */
let hotEntry;

if (options.hotOnly) {
hotEntry = require.resolve('webpack/hot/only-dev-server');
} else if (options.hot) {
hotEntry = require.resolve('webpack/hot/dev-server');
}

/**
* prependEntry Method
* @param {Entry} originalEntry
* @param {Entry} additionalEntries
* @returns {Entry}
*/
const prependEntry = (originalEntry, additionalEntries) => {
if (typeof originalEntry === 'function') {
return () =>
Expand All @@ -39,6 +63,7 @@ function addEntries(config, options, server) {
}

if (typeof originalEntry === 'object' && !Array.isArray(originalEntry)) {
/** @type {Object<string,string>} */
const clone = {};

Object.keys(originalEntry).forEach((key) => {
Expand All @@ -51,6 +76,7 @@ function addEntries(config, options, server) {

// in this case, entry is a string or an array.
// make sure that we do not add duplicates.
/** @type {Entry} */
const entriesClone = additionalEntries.slice(0);
[].concat(originalEntry).forEach((newEntry) => {
if (!entriesClone.includes(newEntry)) {
Expand All @@ -60,21 +86,38 @@ function addEntries(config, options, server) {
return entriesClone;
};

/**
*
* Description of the option for checkInject method
* @typedef {Function} checkInjectOptionsParam
* @param {Object} _config - compilerConfig
* @return {Boolean}
*/

/**
*
* @param {Boolean | checkInjectOptionsParam} option - inject(Hot|Client) it is Boolean | fn => Boolean
* @param {Object} _config
* @param {Boolean} defaultValue
* @return {Boolean}
*/
// eslint-disable-next-line no-shadow
const checkInject = (option, config, defaultValue) => {
const checkInject = (option, _config, defaultValue) => {
if (typeof option === 'boolean') return option;
if (typeof option === 'function') return option(config);
if (typeof option === 'function') return option(_config);
return defaultValue;
};

// eslint-disable-next-line no-shadow
[].concat(config).forEach((config) => {
/** @type {Boolean} */
const webTarget =
config.target === 'web' ||
config.target === 'webworker' ||
config.target === 'electron-renderer' ||
config.target === 'node-webkit' ||
config.target == null;
/** @type {Entry} */
const additionalEntries = checkInject(
options.injectClient,
config,
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css}\" --list-different",
"lint:js": "eslint . --cache",
"lint": "npm-run-all -l -p \"lint:**\"",
"lint:type": "tsc --noEmit",
"commitlint": "commitlint --from=master",
"security": "npm audit",
"test:only": "jest",
Expand Down Expand Up @@ -106,6 +107,7 @@
"style-loader": "^0.23.1",
"supertest": "^4.0.2",
"tcp-port-used": "^1.0.1",
"typescript": "^3.4.5",
"url-loader": "^1.1.2",
"webpack": "^4.35.2",
"webpack-cli": "^3.3.5"
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"lib": ["es2017", "dom"],
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": false,
"noImplicitThis": true,
"alwaysStrict": true,
"types": ["node"],
"esModuleInterop": true
},
"include": ["lib/utils/addEntries.js"]
}