Skip to content

Commit

Permalink
feat: removed cjs wrapper (#265)
Browse files Browse the repository at this point in the history
* feat: removed cjs wrapper

* fix: exports

* docs: formatting

* ci: update
  • Loading branch information
ricardogobbosouza authored Mar 21, 2022
1 parent fd8111a commit dc9a2e3
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 135 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,7 @@ type outputReport =
formatter?:
| (
| string
| ((
results: Array<import('stylelint').LintResult>
) => string)
| ((results: Array<import('stylelint').LintResult>) => string)
)
| undefined;
};
Expand Down
3 changes: 0 additions & 3 deletions declarations/cjs.d.ts

This file was deleted.

10 changes: 5 additions & 5 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,7 +39,7 @@
},
"files": [
"dist",
"declarations"
"types"
],
"peerDependencies": {
"stylelint": "^13.0.0 || ^14.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/StylelintError.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ class StylelintError extends Error {
}
}

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

This file was deleted.

14 changes: 8 additions & 6 deletions src/getStylelint.js
Original file line number Diff line number Diff line change
@@ -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 = {};
Expand Down Expand Up @@ -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;

Expand All @@ -102,3 +102,5 @@ export default function getStylelint(key, { threads, ...options }) {
function getCacheKey(key, options) {
return JSON.stringify({ key, options }, jsonStringifyReplacerSortKeys);
}

module.exports = getStylelint;
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -110,6 +108,8 @@ class StylelintWebpackPlugin {
}

compilation.hooks.finishModules.tapPromise(this.key, async () => {
/** @type {string[]} */
// @ts-ignore
const files = (
await Promise.all(
(compiler.modifiedFiles
Expand All @@ -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) {
Expand Down Expand Up @@ -185,4 +185,4 @@ class StylelintWebpackPlugin {
}
}

export default StylelintWebpackPlugin;
module.exports = StylelintWebpackPlugin;
12 changes: 7 additions & 5 deletions src/linter.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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;

Expand Down Expand Up @@ -275,3 +275,5 @@ function getResultStorage({ compiler }) {
}
return storage;
}

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("stylelint")} stylelint */
/** @typedef {import("stylelint").LinterOptions} StylelintOptions */
Expand Down Expand Up @@ -37,7 +36,7 @@ import schema from './options.json';
* @param {Options} pluginOptions
* @returns {Partial<PluginOptions>}
*/
export function getOptions(pluginOptions) {
function getOptions(pluginOptions) {
const options = {
extensions: ['css', 'scss', 'sass'],
emitError: true,
Expand All @@ -60,7 +59,7 @@ export function getOptions(pluginOptions) {
* @param {Options} pluginOptions
* @returns {Partial<StylelintOptions>}
*/
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.
Expand All @@ -75,3 +74,8 @@ export function getStylelintOptions(pluginOptions) {

return stylelintOptions;
}

module.exports = {
getOptions,
getStylelintOptions,
};
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 @@ -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
Expand All @@ -110,3 +109,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.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default StylelintError;
export = StylelintError;
declare class StylelintError extends Error {
/**
* @param {string=} messages
Expand Down
39 changes: 26 additions & 13 deletions declarations/getStylelint.d.ts → types/getStylelint.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
/// <reference types="stylelint" />
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: (
Expand Down Expand Up @@ -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<void>;
export type LintTask = (files: string | string[]) => Promise<LintResult[]>;
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<void>;
type LintTask = (files: string | string[]) => Promise<LintResult[]>;
type Worker = import('jest-worker').Worker & {
lintFiles: LintTask;
};
23 changes: 13 additions & 10 deletions declarations/index.d.ts → types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
>;
Loading

0 comments on commit dc9a2e3

Please sign in to comment.