Skip to content

Commit

Permalink
feat!: remove chokidar, introduce another way to handle imported styl…
Browse files Browse the repository at this point in the history
…e files
  • Loading branch information
ModyQyW committed Oct 14, 2024
1 parent 032e984 commit 17f3d5d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 77 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const STYLELINT_SEVERITY = {
WARNING: "warning",
} as const;

export const CWD = process.cwd();

export const PLUGIN_NAME = "vite:stylelint";

export const COLOR_MAPPING: Record<
Expand Down
63 changes: 21 additions & 42 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { dirname, extname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { Worker } from "node:worker_threads";
import type { FSWatcher } from "chokidar";
import chokidar from "chokidar";
import debugWrap from "debug";
import type * as Vite from "vite";
import { CWD, PLUGIN_NAME } from "./constants";
import { PLUGIN_NAME } from "./constants";
import type {
StylelintFormatter,
StylelintInstance,
Expand All @@ -31,7 +29,6 @@ export default function StylelintPlugin(
const options = getOptions(userOptions);

let worker: Worker;
let watcher: FSWatcher;

const filter = getFilter(options);
let stylelintInstance: StylelintInstance;
Expand Down Expand Up @@ -80,50 +77,33 @@ export default function StylelintPlugin(
},
async transform(_, id) {
debug("==== transform hook ====");
// initialize watcher
if (options.chokidar) {
if (watcher) return;
debug("Initialize watcher");
watcher = chokidar
.watch(options.include, { ignored: options.exclude })
.on("change", async (path) => {
debug("==== change event ====");
const fullPath = resolve(CWD, path);
// worker + watcher
if (worker) return worker.postMessage(fullPath);
// watcher only
const shouldIgnore = await shouldIgnoreModule(
fullPath,
filter,
true,
);
debug(`should ignore: ${shouldIgnore}`);
if (shouldIgnore) return;
return await lintFiles(
{
files: options.lintDirtyOnly ? fullPath : options.include,
stylelintInstance,
formatter,
options,
},
// this, // TODO: use transform hook context will breaks build
);
});
return;
}
// no watcher
debug("id: ", id);
// current file
const filePath = getFilePath(id);
debug("filePath", filePath);
debug(`id: ${id}`);
debug(`filePath: ${filePath}`);
// related files
const watchFiles = this.getWatchFiles();
const watchFilePaths = watchFiles.map((f) => getFilePath(f));
debug(`watchFilePaths: ${watchFilePaths}`);
// all files
const paths = [filePath, ...watchFilePaths];
// worker
if (worker) return worker.postMessage(filePath);
if (worker) {
for (const p of paths) {
worker.postMessage(p);
}
return;
}
// no worker
const shouldIgnore = await shouldIgnoreModule(id, filter);
// filtered
const filtered = paths.filter((p) => !shouldIgnoreModule(p, filter));
debug(`filtered: ${filtered}`);
const shouldIgnore = filtered.length === 0;
debug(`should ignore: ${shouldIgnore}`);
if (shouldIgnore) return;
return await lintFiles(
{
files: options.lintDirtyOnly ? filePath : options.include,
files: options.lintDirtyOnly ? filtered : options.include,
stylelintInstance,
formatter,
options,
Expand All @@ -133,7 +113,6 @@ export default function StylelintPlugin(
},
async buildEnd() {
debug("==== buildEnd hook ====");
if (watcher?.close) await watcher.close();
if (worker) await worker.terminate();
},
};
Expand Down
15 changes: 0 additions & 15 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,6 @@ export interface StylelintPluginOptions extends StylelintLinterOptions {
* @default true
*/
lintDirtyOnly: boolean;
/**
* Run Stylelint in Chokidar `change` event instead of `transform` hook. This is disabled by
* default.
*
* This plugin can lint style files imported by `@import` when enable this option.
*
* Recommend to enable `lintOnStart` if you enable this one.
*
* 在 Chokidar `change` 事件中而不是在 `transform` 生命周期中运行 Stylelint。默认禁用。
*
* 启用后,可以校验通过 `@import` 导入的样式文件。
*
* 如果你启用这个选项,建议也启用 `lintOnStart`。
*/
chokidar: boolean;
/**
* Emit found errors. This is enabled by default.
*
Expand Down
16 changes: 3 additions & 13 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const getOptions = ({
lintInWorker,
lintOnStart,
lintDirtyOnly,
chokidar,
emitError,
emitErrorAsWarning,
emitWarning,
Expand All @@ -44,7 +43,6 @@ export const getOptions = ({
lintInWorker: lintInWorker ?? false,
lintOnStart: lintOnStart ?? false,
lintDirtyOnly: lintDirtyOnly ?? true,
chokidar: chokidar ?? false,
emitError: emitError ?? true,
emitErrorAsWarning: emitErrorAsWarning ?? false,
emitWarning: emitWarning ?? true,
Expand Down Expand Up @@ -91,22 +89,14 @@ export const isVirtualModule = (id: string) =>

export const getFilePath = (id: string) => normalizePath(id).split("?")[0];

export const shouldIgnoreModule = async (
id: string,
filter: Filter,
chokidar = false,
) => {
export const shouldIgnoreModule = (id: string, filter: Filter) => {
// virtual module
if (isVirtualModule(id)) return true;
// not included
if (!filter(id)) return true;
// not chokidar: should only include xxx.vue?type=style or yyy.svelte?type=style style modules
// chokidar: should include xxx.vue or yyy.svelte
// // xxx.vue?type=style or yyy.svelte?type=style style modules
const filePath = getFilePath(id);
if (
!chokidar &&
[".vue", ".svelte"].some((extname) => filePath.endsWith(extname))
) {
if ([".vue", ".svelte"].some((extname) => filePath.endsWith(extname))) {
return !(id.includes("?") && id.includes("type=style"));
}
return false;
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ parentPort?.on("message", async (files) => {
if (!stylelintInstance) await initPromise;
debug("==== message event ====");
debug(`message: ${files}`);
const shouldIgnore = await shouldIgnoreModule(
files,
filter,
options.chokidar,
);
const shouldIgnore = shouldIgnoreModule(files, filter);
debug(`should ignore: ${shouldIgnore}`);
if (shouldIgnore) return;
lintFiles({
Expand Down

0 comments on commit 17f3d5d

Please sign in to comment.