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

feat: removed cjs wrapper #152

Merged
merged 2 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions declarations/ESLintError.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions declarations/cjs.d.ts

This file was deleted.

34 changes: 0 additions & 34 deletions declarations/linter.d.ts

This file was deleted.

7,259 changes: 3,086 additions & 4,173 deletions package-lock.json

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"type": "opencollective",
"url": "https://opencollective.com/webpack"
},
"main": "dist/cjs.js",
"types": "declarations/index.d.ts",
"main": "dist/index.js",
"types": "types/index.d.ts",
"engines": {
"node": ">= 12.13.0"
},
"scripts": {
"start": "npm run build -- -w",
"clean": "del-cli dist declarations",
"clean": "del-cli dist types",
"prebuild": "npm run clean",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir declarations && prettier \"declarations/**/*.ts\" --write",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
"build": "npm-run-all -p \"build:**\"",
"commitlint": "commitlint --from=master",
Expand All @@ -39,48 +39,48 @@
},
"files": [
"dist",
"declarations"
"types"
],
"peerDependencies": {
"eslint": "^7.0.0 || ^8.0.0",
"webpack": "^5.0.0"
},
"dependencies": {
"@types/eslint": "^7.28.2 || ^8.2.0",
"jest-worker": "^27.3.1",
"micromatch": "^4.0.4",
"@types/eslint": "^7.29.0 || ^8.4.1",
"jest-worker": "^28.0.2",
"micromatch": "^4.0.5",
"normalize-path": "^3.0.0",
"schema-utils": "^4.0.0"
},
"devDependencies": {
"@babel/cli": "^7.16.0",
"@babel/core": "^7.16.0",
"@babel/preset-env": "^7.16.4",
"@commitlint/cli": "^15.0.0",
"@commitlint/config-conventional": "^15.0.0",
"@babel/cli": "^7.17.10",
"@babel/core": "^7.17.10",
"@babel/preset-env": "^7.17.10",
"@commitlint/cli": "^16.2.4",
"@commitlint/config-conventional": "^16.2.4",
"@types/fs-extra": "^9.0.13",
"@types/micromatch": "^4.0.2",
"@types/normalize-path": "^3.0.0",
"@types/webpack": "^5.28.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^27.3.1",
"chokidar": "^3.5.2",
"babel-jest": "^28.0.3",
"chokidar": "^3.5.3",
"cross-env": "^7.0.3",
"del": "^6.0.0",
"del-cli": "^4.0.1",
"eslint": "^8.3.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"fs-extra": "^10.0.0",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"fs-extra": "^10.1.0",
"husky": "^7.0.4",
"jest": "^27.3.1",
"lint-staged": "^12.1.2",
"jest": "^28.0.3",
"lint-staged": "^12.4.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.4.1",
"prettier": "^2.6.2",
"standard-version": "^9.3.2",
"typescript": "^4.5.2",
"webpack": "^5.64.3"
"typescript": "^4.6.4",
"webpack": "^5.72.0"
},
"keywords": [
"eslint",
Expand Down
2 changes: 1 addition & 1 deletion src/ESLintError.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ class ESLintError extends Error {
}
}

export default ESLintError;
module.exports = ESLintError;
3 changes: 0 additions & 3 deletions src/cjs.js

This file was deleted.

20 changes: 13 additions & 7 deletions src/getESLint.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { cpus } from 'os';
const { cpus } = require('os');

import { Worker as JestWorker } from 'jest-worker';
const { Worker: JestWorker } = require('jest-worker');

import { getESLintOptions } from './options';
import { jsonStringifyReplacerSortKeys } from './utils';
const { getESLintOptions } = require('./options');
const { jsonStringifyReplacerSortKeys } = require('./utils');

/** @type {{[key: string]: any}} */
const cache = {};
Expand All @@ -20,7 +20,7 @@ const cache = {};
* @param {Options} options
* @returns {Linter}
*/
export function loadESLint(options) {
function loadESLint(options) {
const { eslintPath } = options;

const { ESLint } = require(eslintPath || 'eslint');
Expand Down Expand Up @@ -51,7 +51,7 @@ export function loadESLint(options) {
* @param {Options} options
* @returns {Linter}
*/
export function loadESLintThreaded(key, poolSize, options) {
function loadESLintThreaded(key, poolSize, options) {
const cacheKey = getCacheKey(key, options);
const { eslintPath = 'eslint' } = options;
const source = require.resolve('./worker');
Expand Down Expand Up @@ -90,7 +90,7 @@ export function loadESLintThreaded(key, poolSize, options) {
* @param {Options} options
* @returns {Linter}
*/
export default function getESLint(key, { threads, ...options }) {
function getESLint(key, { threads, ...options }) {
const max =
typeof threads !== 'number'
? threads
Expand All @@ -115,3 +115,9 @@ export default function getESLint(key, { threads, ...options }) {
function getCacheKey(key, options) {
return JSON.stringify({ key, options }, jsonStringifyReplacerSortKeys);
}

module.exports = {
loadESLint,
loadESLintThreaded,
getESLint,
};
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isAbsolute, join } from 'path';
const { isAbsolute, join } = require('path');

import { isMatch } from 'micromatch';
const { isMatch } = require('micromatch');

import { getOptions } from './options';
import linter from './linter';
import { arrify, parseFiles, parseFoldersToGlobs } from './utils';
const { getOptions } = require('./options');
const linter = require('./linter');
const { arrify, parseFiles, parseFoldersToGlobs } = require('./utils');

/** @typedef {import('webpack').Compiler} Compiler */
/** @typedef {import('./options').Options} Options */
Expand Down Expand Up @@ -175,4 +175,4 @@ class ESLintWebpackPlugin {
}
}

export default ESLintWebpackPlugin;
module.exports = ESLintWebpackPlugin;
24 changes: 13 additions & 11 deletions src/linter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dirname, isAbsolute, join } from 'path';
const { dirname, isAbsolute, join } = require('path');

import ESLintError from './ESLintError';
import getESLint from './getESLint';
const ESLintError = require('./ESLintError');
const { getESLint } = require('./getESLint');

/** @typedef {import('eslint').ESLint} ESLint */
/** @typedef {import('eslint').ESLint.Formatter} Formatter */
Expand All @@ -25,7 +25,7 @@ const resultStorage = new WeakMap();
* @param {Compilation} compilation
* @returns {{lint: Linter, report: Reporter, threads: number}}
*/
export default function linter(key, options, compilation) {
function linter(key, options, compilation) {
/** @type {ESLint} */
let eslint;

Expand Down Expand Up @@ -92,7 +92,7 @@ export default function linter(key, options, compilation) {
}

const formatter = await loadFormatter(eslint, options.formatter);
const { errors, warnings } = formatResults(
const { errors, warnings } = await formatResults(
formatter,
parseResults(options, results)
);
Expand Down Expand Up @@ -136,9 +136,9 @@ export default function linter(key, options, compilation) {
return;
}

const content = outputReport.formatter
const content = await (outputReport.formatter
? (await loadFormatter(eslint, outputReport.formatter)).format(results)
: formatter.format(results);
: formatter.format(results));

let { filePath } = outputReport;
if (!isAbsolute(filePath)) {
Expand All @@ -153,17 +153,17 @@ export default function linter(key, options, compilation) {
/**
* @param {Formatter} formatter
* @param {{ errors: LintResult[]; warnings: LintResult[]; }} results
* @returns {{errors?: ESLintError, warnings?: ESLintError}}
* @returns {Promise<{errors?: ESLintError, warnings?: ESLintError}>}
*/
function formatResults(formatter, results) {
async function formatResults(formatter, results) {
let errors;
let warnings;
if (results.warnings.length > 0) {
warnings = new ESLintError(formatter.format(results.warnings));
warnings = new ESLintError(await formatter.format(results.warnings));
}

if (results.errors.length > 0) {
errors = new ESLintError(formatter.format(results.errors));
errors = new ESLintError(await formatter.format(results.errors));
}

return {
Expand Down Expand Up @@ -314,3 +314,5 @@ function asList(x) {
/* istanbul ignore next */
return Array.isArray(x) ? x : [x];
}

module.exports = linter;
14 changes: 9 additions & 5 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { validate } from 'schema-utils';
const { validate } = require('schema-utils');

// @ts-ignore
import schema from './options.json';
const schema = require('./options.json');

/** @typedef {import("eslint").ESLint.Options} ESLintOptions */
/** @typedef {import('eslint').ESLint.LintResult} LintResult */
Expand Down Expand Up @@ -45,7 +44,7 @@ import schema from './options.json';
* @param {Options} pluginOptions
* @returns {PluginOptions}
*/
export function getOptions(pluginOptions) {
function getOptions(pluginOptions) {
const options = {
extensions: 'js',
emitError: true,
Expand All @@ -68,7 +67,7 @@ export function getOptions(pluginOptions) {
* @param {Options} loaderOptions
* @returns {ESLintOptions}
*/
export function getESLintOptions(loaderOptions) {
function getESLintOptions(loaderOptions) {
const eslintOptions = { ...loaderOptions };

// Keep the fix option because it is common to both the loader and ESLint.
Expand All @@ -83,3 +82,8 @@ export function getESLintOptions(loaderOptions) {

return eslintOptions;
}

module.exports = {
getOptions,
getESLintOptions,
};
22 changes: 14 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { resolve } from 'path';
import { statSync } from 'fs';
const { resolve } = require('path');
const { statSync } = require('fs');

// @ts-ignore
import normalizePath from 'normalize-path';
const normalizePath = require('normalize-path');

/**
* @template T
Expand All @@ -20,7 +19,7 @@ import normalizePath from 'normalize-path';
}
*/
/* istanbul ignore next */
export function arrify(value) {
function arrify(value) {
// eslint-disable-next-line no-undefined
if (value === null || value === undefined) {
// @ts-ignore
Expand Down Expand Up @@ -52,7 +51,7 @@ export function arrify(value) {
* @param {string} context
* @returns {string[]}
*/
export function parseFiles(files, context) {
function parseFiles(files, context) {
return arrify(files).map((/** @type {string} */ file) =>
normalizePath(resolve(context, file))
);
Expand All @@ -63,7 +62,7 @@ export function parseFiles(files, context) {
* @param {string|string[]} extensions
* @returns {string[]}
*/
export function parseFoldersToGlobs(patterns, extensions = []) {
function parseFoldersToGlobs(patterns, extensions = []) {
const extensionsList = arrify(extensions);
const [prefix, postfix] = extensionsList.length > 1 ? ['{', '}'] : ['', ''];
const extensionsGlob = extensionsList
Expand Down Expand Up @@ -94,7 +93,7 @@ export function parseFoldersToGlobs(patterns, extensions = []) {
* @param {string} _ key, but unused
* @param {any} value
*/
export const jsonStringifyReplacerSortKeys = (_, value) => {
const jsonStringifyReplacerSortKeys = (_, value) => {
/**
* @param {{ [x: string]: any; }} sorted
* @param {string | number} key
Expand All @@ -109,3 +108,10 @@ export const jsonStringifyReplacerSortKeys = (_, value) => {
? Object.keys(value).sort().reduce(insert, {})
: value;
};

module.exports = {
arrify,
parseFiles,
parseFoldersToGlobs,
jsonStringifyReplacerSortKeys,
};
8 changes: 0 additions & 8 deletions test/cjs.test.js

This file was deleted.

8 changes: 8 additions & 0 deletions types/ESLintError.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = ESLintError;
declare class ESLintError extends Error {
/**
* @param {string=} messages
*/
constructor(messages?: string | undefined);
stack: string;
}
Loading