From dc9a2e3522c26310cf38126571edd3cdf4966792 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Mon, 21 Mar 2022 10:10:59 -0300 Subject: [PATCH] feat: removed cjs wrapper (#265) * feat: removed cjs wrapper * fix: exports * docs: formatting * ci: update --- .github/workflows/nodejs.yml | 2 +- README.md | 4 +- declarations/cjs.d.ts | 3 - package.json | 10 +-- src/StylelintError.js | 2 +- src/cjs.js | 3 - src/getStylelint.js | 14 ++-- src/index.js | 20 +++--- src/linter.js | 12 ++-- src/options.js | 14 ++-- src/utils.js | 22 +++--- test/cjs.test.js | 8 --- {declarations => types}/StylelintError.d.ts | 2 +- {declarations => types}/getStylelint.d.ts | 39 +++++++---- {declarations => types}/index.d.ts | 23 ++++--- {declarations => types}/linter.d.ts | 50 ++++++++++---- {declarations => types}/options.d.ts | 76 ++++++++++----------- {declarations => types}/utils.d.ts | 5 ++ {declarations => types}/worker.d.ts | 0 19 files changed, 174 insertions(+), 135 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 (76%) rename {declarations => types}/index.d.ts (66%) rename {declarations => types}/linter.d.ts (65%) rename {declarations => types}/options.d.ts (100%) rename {declarations => types}/utils.d.ts (93%) rename {declarations => types}/worker.d.ts (100%) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index c994bda..fe4a5da 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -52,7 +52,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] - node-version: [12.x, 14.x, 16.x, 17.x] + node-version: [12.x, 14.x, 16.x] stylelint-version: [13.x, 14.x] webpack-version: [latest] diff --git a/README.md b/README.md index 5adeee3..222430d 100644 --- a/README.md +++ b/README.md @@ -274,9 +274,7 @@ type outputReport = formatter?: | ( | string - | (( - results: Array - ) => string) + | ((results: Array) => string) ) | undefined; }; 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 4ab17ae..e81e9d0 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": ">= 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", @@ -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 2d7c3cc..eedb901 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 4d7888d..ae06120 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 = {}; @@ -80,7 +80,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; @@ -102,3 +102,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 a3ab2c8..12047ea 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,11 @@ -import { isAbsolute, join } from 'path'; +const { isAbsolute, join } = require('path'); -// @ts-ignore -import globby from 'globby'; +const globby = require('globby'); +const { isMatch } = require('micromatch'); -import { isMatch } from '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('webpack').Module} Module */ @@ -110,6 +108,8 @@ class StylelintWebpackPlugin { } compilation.hooks.finishModules.tapPromise(this.key, async () => { + /** @type {string[]} */ + // @ts-ignore const files = ( await Promise.all( (compiler.modifiedFiles @@ -119,7 +119,7 @@ class StylelintWebpackPlugin { !isMatch(file, exclude, { dot: true }) ) : globby.sync(wanted, { dot: true, ignore: exclude }) - ).map(async (/** @type {string | undefined} */ file) => { + ).map(async (file) => { try { return (await api.isPathIgnored(file)) ? false : file; } catch (e) { @@ -185,4 +185,4 @@ class StylelintWebpackPlugin { } } -export default StylelintWebpackPlugin; +module.exports = StylelintWebpackPlugin; diff --git a/src/linter.js b/src/linter.js index c076775..d37e13b 100644 --- a/src/linter.js +++ b/src/linter.js @@ -1,8 +1,8 @@ -import { dirname, isAbsolute, join } from 'path'; +const { dirname, isAbsolute, join } = require('path'); -import StylelintError from './StylelintError'; -import getStylelint from './getStylelint'; -import { arrify } from './utils'; +const StylelintError = require('./StylelintError'); +const getStylelint = require('./getStylelint'); +const { arrify } = require('./utils'); /** @typedef {import('stylelint')} Stylelint */ /** @typedef {import('stylelint').LintResult} LintResult */ @@ -27,7 +27,7 @@ const resultStorage = new WeakMap(); * @param {Compilation} compilation * @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}} */ -export default function linter(key, options, compilation) { +function linter(key, options, compilation) { /** @type {Stylelint} */ let stylelint; @@ -275,3 +275,5 @@ function getResultStorage({ compiler }) { } return storage; } + +module.exports = linter; diff --git a/src/options.js b/src/options.js index 886a4ad..ede5267 100644 --- a/src/options.js +++ b/src/options.js @@ -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("stylelint")} stylelint */ /** @typedef {import("stylelint").LinterOptions} StylelintOptions */ @@ -37,7 +36,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 +59,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 +74,8 @@ export function getStylelintOptions(pluginOptions) { return stylelintOptions; } + +module.exports = { + getOptions, + getStylelintOptions, +}; diff --git a/src/utils.js b/src/utils.js index c8de890..a8e489d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 @@ -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 @@ -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)) ); @@ -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 @@ -95,7 +94,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 @@ -110,3 +109,10 @@ export const jsonStringifyReplacerSortKeys = (_, value) => { ? Object.keys(value).sort().reduce(insert, {}) : value; }; + +module.exports = { + arrify, + 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 76% rename from declarations/getStylelint.d.ts rename to types/getStylelint.d.ts index 72717f9..0f18e13 100644 --- a/declarations/getStylelint.d.ts +++ b/types/getStylelint.d.ts @@ -1,14 +1,34 @@ /// +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 = { + api: import('stylelint').InternalApi; + stylelint: Stylelint; + lintFiles: LintTask; + cleanup: AsyncTask; + threads: number; +}; +type Stylelint = import('postcss').PluginCreator< import('stylelint').PostcssPluginOptions > & { lint: ( @@ -63,16 +83,9 @@ 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 = import('jest-worker').Worker & { +type LintResult = import('stylelint').LintResult; +type AsyncTask = () => Promise; +type LintTask = (files: string | string[]) => Promise; +type Worker = import('jest-worker').Worker & { lintFiles: LintTask; }; diff --git a/declarations/index.d.ts b/types/index.d.ts similarity index 66% rename from declarations/index.d.ts rename to types/index.d.ts index 284b2c2..55ed90b 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 @@ -33,3 +24,15 @@ declare class StylelintWebpackPlugin { */ getContext(compiler: Compiler): string; } +declare namespace StylelintWebpackPlugin { + export { Compiler, Module, Options, FileSystemInfoEntry }; +} +type Compiler = import('webpack').Compiler; +type Options = import('./options').Options; +type Module = import('webpack').Module; +type FileSystemInfoEntry = Partial< + | { + timestamp: number; + } + | number +>; diff --git a/declarations/linter.d.ts b/types/linter.d.ts similarity index 65% rename from declarations/linter.d.ts rename to types/linter.d.ts index 894f450..7e1d272 100644 --- a/declarations/linter.d.ts +++ b/types/linter.d.ts @@ -1,20 +1,44 @@ /// +export = linter; /** * @param {string|undefined} key * @param {Options} options * @param {Compilation} compilation - * @returns {{lint: Linter, report: Reporter, threads: number}} + * @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}} */ -export default function linter( +declare function linter( key: string | undefined, options: Options, compilation: Compilation ): { + api: InternalApi; lint: Linter; report: Reporter; threads: number; }; -export type Stylelint = import('postcss').PluginCreator< +declare namespace linter { + export { + Stylelint, + LintResult, + InternalApi, + Compiler, + Compilation, + Options, + FormatterType, + FormatterFunction, + GenerateReport, + Report, + Reporter, + Linter, + LintResultMap, + }; +} +type Options = import('./options').Options; +type Compilation = import('webpack').Compilation; +type InternalApi = import('stylelint').InternalApi; +type Linter = (files: string | string[]) => void; +type Reporter = () => Promise; +type Stylelint = import('postcss').PluginCreator< import('stylelint').PostcssPluginOptions > & { lint: ( @@ -69,21 +93,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 93% rename from declarations/utils.d.ts rename to types/utils.d.ts index bf224e2..d3931a2 100644 --- a/declarations/utils.d.ts +++ b/types/utils.d.ts @@ -39,4 +39,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