Skip to content

Commit

Permalink
fix: node ES module + worker issue on Windows
Browse files Browse the repository at this point in the history
close #389
  • Loading branch information
JounQin committed May 8, 2022
1 parent 0fc5001 commit 124b3de
Show file tree
Hide file tree
Showing 11 changed files with 709 additions and 693 deletions.
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"node": "14",
"node": "16",
"packages": [
"packages/*"
],
Expand Down
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ on:

jobs:
ci:
name: Lint and Test with Node.js ${{ matrix.node }}
name: Lint and Test with Node.js ${{ matrix.node }} on ${{ matrix.os }}
strategy:
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest
node:
- 14
- 16
- 18
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repo
uses: actions/checkout@v3
Expand All @@ -39,6 +43,7 @@ jobs:
PARSER_NO_WATCH: true

- name: Codecov
if: matrix.os != 'windows-latest'
run: |
yarn global add codecov
codecov
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"build": "run-p build:*",
"build:r": "r -f es2015",
"build:ts": "tsc -b",
"clean": "rimraf packages/*/{lib,*.tsbuildinfo} node_modules/@1stg/eslint-config/node_modules",
"clean": "rimraf packages/*/{lib,*.tsbuildinfo}",
"lint": "run-p lint:*",
"lint:es": "eslint . --cache -f friendly",
"lint:tsc": "tsc --noEmit",
"postinstall": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prelint": "yarn build",
"prepare": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prerelease": "yarn build",
"release": "lerna publish --conventional-commits --create-release github --yes",
"release-next": "lerna publish --conventional-prerelease --preid next --pre-dist-tag next --yes",
Expand All @@ -27,16 +27,16 @@
},
"devDependencies": {
"@1stg/lib-config": "^6.0.0",
"@types/eslint": "^8.4.1",
"@types/eslint": "^8.4.2",
"@types/eslint-plugin-markdown": "^2.0.0",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.29",
"@types/react": "^18.0.8",
"@types/jest": "^27.5.0",
"@types/node": "^17.0.31",
"@types/react": "^18.0.9",
"@types/unist": "^2.0.6",
"lerna": "^4.0.0",
"patch-package": "^6.4.7",
"react": "^18.1.0",
"ts-jest": "^28.0.0-next.3",
"ts-jest": "^28.0.2",
"ts-node": "^10.7.0",
"type-coverage": "^2.21.1",
"typescript": "^4.6.4"
Expand Down Expand Up @@ -66,9 +66,6 @@
"^eslint-mdx$": "<rootDir>/packages/eslint-mdx/src",
"^eslint-plugin-mdx$": "<rootDir>/packages/eslint-plugin-mdx/src"
},
"transform": {
"^.+\\.tsx?$": "ts-jest/legacy"
},
"collectCoverage": true,
"coverageThreshold": {
"global": {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"remark-mdx": "^2.1.1",
"remark-parse": "^10.0.1",
"remark-stringify": "^10.0.2",
"synckit": "^0.7.0",
"synckit": "^0.7.1",
"tslib": "^2.4.0",
"unified": "^10.1.2"
}
Expand Down
33 changes: 31 additions & 2 deletions packages/eslint-mdx/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable unicorn/no-await-expression-member */
import fs from 'fs'
import path from 'path'
import { pathToFileURL } from 'url'

import type { SourceLocation } from 'estree'
import { createSyncFn } from 'synckit'
Expand Down Expand Up @@ -64,6 +66,30 @@ export const arrayify = <T, R = T extends Array<infer S> ? S : T>(
return arr
}, [])

/**
* Given a filepath, get the nearest path that is a regular file.
* The filepath provided by eslint may be a virtual filepath rather than a file
* on disk. This attempts to transform a virtual path into an on-disk path
*/
export const getPhysicalFilename = (
filename: string,
child?: string,
): string => {
try {
if (fs.statSync(filename).isDirectory()) {
return child || filename
}
} catch (err) {
const { code } = err as { code: string }
// https://github.com/eslint/eslint/issues/11989
// Additionally, it seems there is no `ENOTDIR` code on Windows...
if (code === 'ENOTDIR' || code === 'ENOENT') {
return getPhysicalFilename(path.dirname(filename), filename)
}
}
return filename
}

/**
* ! copied from https://github.com/just-jeb/angular-builders/blob/master/packages/custom-webpack/src/utils.ts#L53-L67
*
Expand Down Expand Up @@ -91,10 +117,13 @@ export const loadEsmModule = <T>(modulePath: URL | string): Promise<T> =>
* @returns
*/
export const loadModule = async <T>(modulePath: string): Promise<T> => {
const esModulePath = path.isAbsolute(modulePath)
? pathToFileURL(modulePath)
: modulePath
switch (path.extname(modulePath)) {
/* istanbul ignore next */
case '.mjs': {
return (await loadEsmModule<{ default: T }>(modulePath)).default
return (await loadEsmModule<{ default: T }>(esModulePath)).default
}
/* istanbul ignore next */
case '.cjs': {
Expand All @@ -113,7 +142,7 @@ export const loadModule = async <T>(modulePath: string): Promise<T> => {
// Load the ESM configuration file using the TypeScript dynamic import workaround.
// Once TypeScript provides support for keeping the dynamic import this workaround can be
// changed to a direct dynamic import.
return (await loadEsmModule<{ default: T }>(modulePath)).default
return (await loadEsmModule<{ default: T }>(esModulePath)).default
}

throw err
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './helpers'
export * from './parser'
export * from './processor'
export * from './traverse'
export * from './types'
2 changes: 1 addition & 1 deletion packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
isMdxNode,
normalizePosition,
performSyncWork,
getPhysicalFilename,
} from './helpers'
import { getPhysicalFilename } from './processor'
import { traverse } from './traverse'
import type { ParserOptions, WorkerParseResult } from './types'

Expand Down
22 changes: 0 additions & 22 deletions packages/eslint-mdx/src/processor.ts

This file was deleted.

9 changes: 6 additions & 3 deletions packages/eslint-mdx/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable unicorn/no-await-expression-member */
import path from 'path'
import { pathToFileURL } from 'url'

import type { Token } from 'acorn'
import { cosmiconfig } from 'cosmiconfig'
Expand Down Expand Up @@ -207,9 +208,11 @@ runAsWorker(
if (!TokenTranslator) {
TokenTranslator = (
await loadEsmModule<typeof import('espree/lib/token-translator')>(
path.resolve(
require.resolve('espree/package.json'),
'../lib/token-translator.js',
pathToFileURL(
path.resolve(
require.resolve('espree/package.json'),
'../lib/token-translator.js',
),
),
)
).default
Expand Down
Loading

0 comments on commit 124b3de

Please sign in to comment.