Skip to content

Commit

Permalink
feat: removed cjs wrapper (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored May 20, 2022
1 parent 9e14831 commit 1ccd8bf
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 133 deletions.
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": ">= 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",
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 @@ -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;

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

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

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

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

module.exports = linter;
13 changes: 9 additions & 4 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -37,7 +37,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 +60,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 +75,8 @@ export function getStylelintOptions(pluginOptions) {

return stylelintOptions;
}

module.exports = {
getOptions,
getStylelintOptions,
};
26 changes: 15 additions & 11 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
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 || '')));
}

/**
* @param {string|string[]} patterns
* @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 @@ -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
Expand All @@ -69,3 +67,9 @@ export const jsonStringifyReplacerSortKeys = (_, value) => {
? Object.keys(value).sort().reduce(insert, {})
: value;
};

module.exports = {
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
38 changes: 25 additions & 13 deletions declarations/getStylelint.d.ts → types/getStylelint.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
/// <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 = {
stylelint: Stylelint;
lintFiles: LintTask;
cleanup: AsyncTask;
threads: number;
};
type Stylelint = import('postcss').PluginCreator<
import('stylelint').PostcssPluginOptions
> & {
lint: (
Expand Down Expand Up @@ -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<void>;
export type LintTask = (files: string | string[]) => Promise<LintResult[]>;
export type Linter = {
stylelint: Stylelint;
lintFiles: LintTask;
cleanup: AsyncTask;
threads: number;
};
export type Worker = JestWorker & {
type LintResult = import('stylelint').LintResult;
type AsyncTask = () => Promise<void>;
type LintTask = (files: string | string[]) => Promise<LintResult[]>;
type Worker = JestWorker & {
lintFiles: LintTask;
};
import { Worker as JestWorker } from 'jest-worker';
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 Down Expand Up @@ -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;
Loading

0 comments on commit 1ccd8bf

Please sign in to comment.