From 1ccd8bf5cfe32dc0ca24261b2d2ee6ab9f99df7b Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Fri, 20 May 2022 13:17:25 -0300 Subject: [PATCH] feat: removed cjs wrapper (#281) --- declarations/cjs.d.ts | 3 - package.json | 10 +-- src/StylelintError.js | 2 +- src/cjs.js | 3 - src/getStylelint.js | 14 ++-- src/index.js | 18 +++-- src/linter.js | 13 ++-- src/options.js | 13 ++-- src/utils.js | 26 ++++--- test/cjs.test.js | 8 --- {declarations => types}/StylelintError.d.ts | 2 +- {declarations => types}/getStylelint.d.ts | 38 +++++++---- {declarations => types}/index.d.ts | 23 ++++--- {declarations => types}/linter.d.ts | 45 ++++++++---- {declarations => types}/options.d.ts | 76 ++++++++++----------- {declarations => types}/utils.d.ts | 5 ++ {declarations => types}/worker.d.ts | 0 17 files changed, 166 insertions(+), 133 deletions(-) delete mode 100644 declarations/cjs.d.ts delete mode 100644 src/cjs.js delete mode 100644 test/cjs.test.js rename {declarations => types}/StylelintError.d.ts (83%) rename {declarations => types}/getStylelint.d.ts (79%) rename {declarations => types}/index.d.ts (82%) rename {declarations => types}/linter.d.ts (69%) rename {declarations => types}/options.d.ts (100%) rename {declarations => types}/utils.d.ts (87%) rename {declarations => types}/worker.d.ts (100%) diff --git a/declarations/cjs.d.ts b/declarations/cjs.d.ts deleted file mode 100644 index 0b54926..0000000 --- a/declarations/cjs.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare const _exports: typeof plugin.default; -export = _exports; -import plugin = require('./index'); diff --git a/package.json b/package.json index 54c4e4e..80b0ab3 100644 --- a/package.json +++ b/package.json @@ -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": ">= 10.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", @@ -39,7 +39,7 @@ }, "files": [ "dist", - "declarations" + "types" ], "peerDependencies": { "stylelint": "^13.0.0 || ^14.0.0", diff --git a/src/StylelintError.js b/src/StylelintError.js index 54f226e..b6a92c0 100644 --- a/src/StylelintError.js +++ b/src/StylelintError.js @@ -9,4 +9,4 @@ class StylelintError extends Error { } } -export default StylelintError; +module.exports = StylelintError; diff --git a/src/cjs.js b/src/cjs.js deleted file mode 100644 index baa7dac..0000000 --- a/src/cjs.js +++ /dev/null @@ -1,3 +0,0 @@ -const plugin = require('./index'); - -module.exports = plugin.default; diff --git a/src/getStylelint.js b/src/getStylelint.js index 8b85bf2..f8f1b66 100644 --- a/src/getStylelint.js +++ b/src/getStylelint.js @@ -1,11 +1,11 @@ -import { cpus } from 'os'; +const { cpus } = require('os'); -import { Worker as JestWorker } from 'jest-worker'; +const { Worker: JestWorker } = require('jest-worker'); // @ts-ignore -import { setup, lintFiles } from './worker'; -import { jsonStringifyReplacerSortKeys } from './utils'; -import { getStylelintOptions } from './options'; +const { setup, lintFiles } = require('./worker'); +const { jsonStringifyReplacerSortKeys } = require('./utils'); +const { getStylelintOptions } = require('./options'); /** @type {{[key: string]: any}} */ const cache = {}; @@ -78,7 +78,7 @@ function loadStylelintThreaded(key, poolSize, options) { * @param {Options} options * @returns {Linter} */ -export default function getStylelint(key, { threads, ...options }) { +function getStylelint(key, { threads, ...options }) { const max = typeof threads !== 'number' ? (threads ? cpus().length - 1 : 1) : threads; @@ -100,3 +100,5 @@ export default function getStylelint(key, { threads, ...options }) { function getCacheKey(key, options) { return JSON.stringify({ key, options }, jsonStringifyReplacerSortKeys); } + +module.exports = getStylelint; diff --git a/src/index.js b/src/index.js index b0fb571..68b3137 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,12 @@ -import { isAbsolute, join } from 'path'; +const { isAbsolute, join } = require('path'); -// @ts-ignore -import arrify from 'arrify'; -// @ts-ignore -import globby from 'globby'; -import { isMatch } from 'micromatch'; +const arrify = require('arrify'); +const globby = require('globby'); +const { isMatch } = require('micromatch'); -import { getOptions } from './options'; -import linter from './linter'; -import { parseFiles, parseFoldersToGlobs } from './utils'; +const { getOptions } = require('./options'); +const linter = require('./linter'); +const { parseFiles, parseFoldersToGlobs } = require('./utils'); /** @typedef {import('webpack').Compiler} Compiler */ /** @typedef {import('webpack').Module} Module */ @@ -244,4 +242,4 @@ class StylelintWebpackPlugin { } } -export default StylelintWebpackPlugin; +module.exports = StylelintWebpackPlugin; diff --git a/src/linter.js b/src/linter.js index 3fe6e91..4a611f6 100644 --- a/src/linter.js +++ b/src/linter.js @@ -1,10 +1,9 @@ -import { dirname, isAbsolute, join } from 'path'; +const { dirname, isAbsolute, join } = require('path'); -// @ts-ignore -import arrify from 'arrify'; +const arrify = require('arrify'); -import StylelintError from './StylelintError'; -import getStylelint from './getStylelint'; +const StylelintError = require('./StylelintError'); +const getStylelint = require('./getStylelint'); /** @typedef {import('stylelint')} Stylelint */ /** @typedef {import('stylelint').LintResult} LintResult */ @@ -28,7 +27,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 {Stylelint} */ let stylelint; @@ -270,3 +269,5 @@ function getResultStorage({ compiler }) { } return storage; } + +module.exports = linter; diff --git a/src/options.js b/src/options.js index 886a4ad..f6b3ceb 100644 --- a/src/options.js +++ b/src/options.js @@ -1,7 +1,7 @@ -import { validate } from 'schema-utils'; +const { validate } = require('schema-utils'); // @ts-ignore -import schema from './options.json'; +const schema = require('./options.json'); /** @typedef {import("stylelint")} stylelint */ /** @typedef {import("stylelint").LinterOptions} StylelintOptions */ @@ -37,7 +37,7 @@ import schema from './options.json'; * @param {Options} pluginOptions * @returns {Partial} */ -export function getOptions(pluginOptions) { +function getOptions(pluginOptions) { const options = { extensions: ['css', 'scss', 'sass'], emitError: true, @@ -60,7 +60,7 @@ export function getOptions(pluginOptions) { * @param {Options} pluginOptions * @returns {Partial} */ -export function getStylelintOptions(pluginOptions) { +function getStylelintOptions(pluginOptions) { const stylelintOptions = { ...pluginOptions }; // Keep the files and formatter option because it is common to both the plugin and Stylelint. @@ -75,3 +75,8 @@ export function getStylelintOptions(pluginOptions) { return stylelintOptions; } + +module.exports = { + getOptions, + getStylelintOptions, +}; diff --git a/src/utils.js b/src/utils.js index 5d8b6f2..7c0ce62 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,20 +1,18 @@ -import { resolve } from 'path'; -import { statSync } from 'fs'; +const { resolve } = require('path'); +const { statSync } = require('fs'); -// @ts-ignore -import normalizePath from 'normalize-path'; -// @ts-ignore -import arrify from 'arrify'; +const normalizePath = require('normalize-path'); +const arrify = require('arrify'); /** * @param {string|(string|undefined)[]} files * @param {string} context * @returns {string[]} */ -export function parseFiles(files, context) { +function parseFiles(files, context) { return arrify(files) - .filter((/** @type {string} */ file) => typeof file === 'string') - .map((/** @type {string} */ file) => normalizePath(resolve(context, file))); + .filter((file) => typeof file === 'string') + .map((file) => normalizePath(resolve(context, file || ''))); } /** @@ -22,7 +20,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 @@ -54,7 +52,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 @@ -69,3 +67,9 @@ export const jsonStringifyReplacerSortKeys = (_, value) => { ? Object.keys(value).sort().reduce(insert, {}) : value; }; + +module.exports = { + parseFiles, + parseFoldersToGlobs, + jsonStringifyReplacerSortKeys, +}; diff --git a/test/cjs.test.js b/test/cjs.test.js deleted file mode 100644 index c0fed75..0000000 --- a/test/cjs.test.js +++ /dev/null @@ -1,8 +0,0 @@ -import StylelintWebpackPlugin from '../src'; -import CJSStylelintWebpackPlugin from '../src/cjs'; - -describe('cjs', () => { - it('should exported plugin', () => { - expect(CJSStylelintWebpackPlugin).toEqual(StylelintWebpackPlugin); - }); -}); diff --git a/declarations/StylelintError.d.ts b/types/StylelintError.d.ts similarity index 83% rename from declarations/StylelintError.d.ts rename to types/StylelintError.d.ts index f5cd484..5e0339f 100644 --- a/declarations/StylelintError.d.ts +++ b/types/StylelintError.d.ts @@ -1,4 +1,4 @@ -export default StylelintError; +export = StylelintError; declare class StylelintError extends Error { /** * @param {string=} messages diff --git a/declarations/getStylelint.d.ts b/types/getStylelint.d.ts similarity index 79% rename from declarations/getStylelint.d.ts rename to types/getStylelint.d.ts index 13e0597..201e61b 100644 --- a/declarations/getStylelint.d.ts +++ b/types/getStylelint.d.ts @@ -1,14 +1,33 @@ /// +export = getStylelint; /** * @param {string|undefined} key * @param {Options} options * @returns {Linter} */ -export default function getStylelint( +declare function getStylelint( key: string | undefined, { threads, ...options }: Options ): Linter; -export type Stylelint = import('postcss').PluginCreator< +declare namespace getStylelint { + export { + Stylelint, + LintResult, + Options, + AsyncTask, + LintTask, + Linter, + Worker, + }; +} +type Options = import('./options').Options; +type Linter = { + stylelint: Stylelint; + lintFiles: LintTask; + cleanup: AsyncTask; + threads: number; +}; +type Stylelint = import('postcss').PluginCreator< import('stylelint').PostcssPluginOptions > & { lint: ( @@ -63,17 +82,10 @@ export type Stylelint = import('postcss').PluginCreator< ) => void; }; }; -export type LintResult = import('stylelint').LintResult; -export type Options = import('./options').Options; -export type AsyncTask = () => Promise; -export type LintTask = (files: string | string[]) => Promise; -export type Linter = { - stylelint: Stylelint; - lintFiles: LintTask; - cleanup: AsyncTask; - threads: number; -}; -export type Worker = JestWorker & { +type LintResult = import('stylelint').LintResult; +type AsyncTask = () => Promise; +type LintTask = (files: string | string[]) => Promise; +type Worker = JestWorker & { lintFiles: LintTask; }; import { Worker as JestWorker } from 'jest-worker'; diff --git a/declarations/index.d.ts b/types/index.d.ts similarity index 82% rename from declarations/index.d.ts rename to types/index.d.ts index 98c305f..3d14b0c 100644 --- a/declarations/index.d.ts +++ b/types/index.d.ts @@ -1,13 +1,4 @@ -export default StylelintWebpackPlugin; -export type Compiler = import('webpack').Compiler; -export type Module = import('webpack').Module; -export type Options = import('./options').Options; -export type FileSystemInfoEntry = Partial< - | { - timestamp: number; - } - | number ->; +export = StylelintWebpackPlugin; declare class StylelintWebpackPlugin { /** * @param {Options} options @@ -62,3 +53,15 @@ declare class StylelintWebpackPlugin { > ): string[]; } +declare namespace StylelintWebpackPlugin { + export { Compiler, Module, Options, FileSystemInfoEntry }; +} +type Compiler = import('webpack').Compiler; +type Options = import('./options').Options; +type FileSystemInfoEntry = Partial< + | { + timestamp: number; + } + | number +>; +type Module = import('webpack').Module; diff --git a/declarations/linter.d.ts b/types/linter.d.ts similarity index 69% rename from declarations/linter.d.ts rename to types/linter.d.ts index 894f450..411a9c0 100644 --- a/declarations/linter.d.ts +++ b/types/linter.d.ts @@ -1,11 +1,12 @@ /// +export = linter; /** * @param {string|undefined} key * @param {Options} options * @param {Compilation} compilation * @returns {{lint: Linter, report: Reporter, threads: number}} */ -export default function linter( +declare function linter( key: string | undefined, options: Options, compilation: Compilation @@ -14,7 +15,27 @@ export default function linter( report: Reporter; threads: number; }; -export type Stylelint = import('postcss').PluginCreator< +declare namespace linter { + export { + Stylelint, + LintResult, + Compiler, + Compilation, + Options, + FormatterType, + FormatterFunction, + GenerateReport, + Report, + Reporter, + Linter, + LintResultMap, + }; +} +type Options = import('./options').Options; +type Compilation = import('webpack').Compilation; +type Linter = (files: string | string[]) => void; +type Reporter = () => Promise; +type Stylelint = import('postcss').PluginCreator< import('stylelint').PostcssPluginOptions > & { lint: ( @@ -69,21 +90,17 @@ export type Stylelint = import('postcss').PluginCreator< ) => void; }; }; -export type LintResult = import('stylelint').LintResult; -export type Compiler = import('webpack').Compiler; -export type Compilation = import('webpack').Compilation; -export type Options = import('./options').Options; -export type FormatterType = import('./options').FormatterType; -export type FormatterFunction = (results: LintResult[]) => string; -export type GenerateReport = (compilation: Compilation) => Promise; -export type Report = { +type LintResult = import('stylelint').LintResult; +type Compiler = import('webpack').Compiler; +type FormatterType = import('./options').FormatterType; +type FormatterFunction = (results: LintResult[]) => string; +type GenerateReport = (compilation: Compilation) => Promise; +type Report = { errors?: StylelintError; warnings?: StylelintError; generateReportAsset?: GenerateReport; }; -export type Reporter = () => Promise; -export type Linter = (files: string | string[]) => void; -export type LintResultMap = { +type LintResultMap = { [files: string]: import('stylelint').LintResult; }; -import StylelintError from './StylelintError'; +import StylelintError = require('./StylelintError'); diff --git a/declarations/options.d.ts b/types/options.d.ts similarity index 100% rename from declarations/options.d.ts rename to types/options.d.ts index a248a24..b580f8b 100644 --- a/declarations/options.d.ts +++ b/types/options.d.ts @@ -1,42 +1,4 @@ /// -/** @typedef {import("stylelint")} stylelint */ -/** @typedef {import("stylelint").LinterOptions} StylelintOptions */ -/** @typedef {import("stylelint").FormatterType} FormatterType */ -/** - * @typedef {Object} OutputReport - * @property {string=} filePath - * @property {FormatterType=} formatter - */ -/** - * @typedef {Object} PluginOptions - * @property {string} context - * @property {boolean} emitError - * @property {boolean} emitWarning - * @property {string|string[]=} exclude - * @property {string|string[]} extensions - * @property {boolean} failOnError - * @property {boolean} failOnWarning - * @property {string|string[]} files - * @property {FormatterType} formatter - * @property {boolean} lintDirtyModulesOnly - * @property {boolean} quiet - * @property {string} stylelintPath - * @property {OutputReport} outputReport - * @property {number|boolean=} threads - */ -/** @typedef {Partial} Options */ -/** - * @param {Options} pluginOptions - * @returns {Partial} - */ -export function getOptions(pluginOptions: Options): Partial; -/** - * @param {Options} pluginOptions - * @returns {Partial} - */ -export function getStylelintOptions( - pluginOptions: Options -): Partial; export type stylelint = import('postcss').PluginCreator< import('stylelint').PostcssPluginOptions > & { @@ -115,3 +77,41 @@ export type PluginOptions = { threads?: (number | boolean) | undefined; }; export type Options = Partial; +/** @typedef {import("stylelint")} stylelint */ +/** @typedef {import("stylelint").LinterOptions} StylelintOptions */ +/** @typedef {import("stylelint").FormatterType} FormatterType */ +/** + * @typedef {Object} OutputReport + * @property {string=} filePath + * @property {FormatterType=} formatter + */ +/** + * @typedef {Object} PluginOptions + * @property {string} context + * @property {boolean} emitError + * @property {boolean} emitWarning + * @property {string|string[]=} exclude + * @property {string|string[]} extensions + * @property {boolean} failOnError + * @property {boolean} failOnWarning + * @property {string|string[]} files + * @property {FormatterType} formatter + * @property {boolean} lintDirtyModulesOnly + * @property {boolean} quiet + * @property {string} stylelintPath + * @property {OutputReport} outputReport + * @property {number|boolean=} threads + */ +/** @typedef {Partial} Options */ +/** + * @param {Options} pluginOptions + * @returns {Partial} + */ +export function getOptions(pluginOptions: Options): Partial; +/** + * @param {Options} pluginOptions + * @returns {Partial} + */ +export function getStylelintOptions( + pluginOptions: Options +): Partial; diff --git a/declarations/utils.d.ts b/types/utils.d.ts similarity index 87% rename from declarations/utils.d.ts rename to types/utils.d.ts index 25ef5af..5868460 100644 --- a/declarations/utils.d.ts +++ b/types/utils.d.ts @@ -16,4 +16,9 @@ export function parseFoldersToGlobs( patterns: string | string[], extensions?: string | string[] ): string[]; +/** + * + * @param {string} _ key, but unused + * @param {any} value + */ export function jsonStringifyReplacerSortKeys(_: string, value: any): any; diff --git a/declarations/worker.d.ts b/types/worker.d.ts similarity index 100% rename from declarations/worker.d.ts rename to types/worker.d.ts