Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: treat pseudo ESM as ESM with custom loader, disable custom Node…
… Loader by default (#1778) * feat: treat pseudo ESM as ESM * chore: remove empty main fields * chore: cleanup * chore: disable node loader by default * chore: cleanup * chore: register loader for esm test * chore: don't disable loader for now * chore: revert * chore: cleanup * chore disable node loader by default * chore: disable Node Loader tests
- Loading branch information
1 parent
135b6d8
commit 155943c
Showing
12 changed files
with
138 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,94 @@ | ||
import { pathToFileURL } from 'url' | ||
import { isNodeBuiltin } from 'mlly' | ||
import { readFile } from 'fs/promises' | ||
import { hasCJSSyntax, isNodeBuiltin } from 'mlly' | ||
import { normalizeModuleId } from 'vite-node/utils' | ||
import { getWorkerState } from '../utils' | ||
import type { Loader, Resolver } from '../types/loader' | ||
import type { Loader, ResolveResult, Resolver } from '../types/loader' | ||
import { ModuleFormat } from '../types/loader' | ||
|
||
// TODO fix in mlly (add "}" as a possible first character: "}export default") | ||
const ESM_RE = /([\s;}]|^)(import[\w,{}\s*]*from|import\s*['"*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m | ||
function hasESMSyntax(code: string) { | ||
return ESM_RE.test(code) | ||
} | ||
|
||
interface ContextCache { | ||
isPseudoESM: boolean | ||
source: string | ||
} | ||
|
||
const cache = new Map<string, ContextCache>() | ||
|
||
const getPotentialSource = async (filepath: string, result: ResolveResult) => { | ||
if (!result.url.startsWith('file://') || result.format === 'module') | ||
return null | ||
let source = cache.get(result.url)?.source | ||
if (source == null) | ||
source = await readFile(filepath, 'utf8') | ||
return source | ||
} | ||
|
||
const detectESM = (url: string, source: string | null) => { | ||
const cached = cache.get(url) | ||
if (cached) | ||
return cached.isPseudoESM | ||
if (!source) | ||
return false | ||
return (hasESMSyntax(source) && !hasCJSSyntax(source)) | ||
} | ||
|
||
// apply transformations only to libraries | ||
// inline code preccessed by vite-node | ||
// make Node pseudo ESM | ||
export const resolve: Resolver = async (url, context, next) => { | ||
const { parentURL } = context | ||
if (!parentURL || !parentURL.includes('node_modules') || isNodeBuiltin(url)) | ||
const state = getWorkerState() | ||
const resolver = state?.rpc.resolveId | ||
|
||
if (!parentURL || isNodeBuiltin(url) || !resolver) | ||
return next(url, context, next) | ||
|
||
const id = normalizeModuleId(url) | ||
const importer = normalizeModuleId(parentURL) | ||
const state = getWorkerState() | ||
const resolver = state?.rpc.resolveId | ||
if (resolver) { | ||
const resolved = await resolver(id, importer) | ||
if (resolved) { | ||
return { | ||
url: pathToFileURL(resolved.id).toString(), | ||
shortCircuit: true, | ||
} | ||
const resolved = await resolver(id, importer) | ||
|
||
let result: ResolveResult | ||
let filepath: string | ||
if (resolved) { | ||
const resolvedUrl = pathToFileURL(resolved.id).toString() | ||
filepath = resolved.id | ||
result = { | ||
url: resolvedUrl, | ||
shortCircuit: true, | ||
} | ||
} | ||
return next(url, context, next) | ||
else { | ||
const { url: resolvedUrl, format } = await next(url, context, next) | ||
filepath = new URL(resolvedUrl).pathname | ||
result = { | ||
url: resolvedUrl, | ||
format, | ||
shortCircuit: true, | ||
} | ||
} | ||
|
||
const source = await getPotentialSource(filepath, result) | ||
const isPseudoESM = detectESM(result.url, source) | ||
if (typeof source === 'string') | ||
cache.set(result.url, { isPseudoESM, source }) | ||
if (isPseudoESM) | ||
result.format = ModuleFormat.Module | ||
return result | ||
} | ||
|
||
export const load: Loader = (url, context, next) => { | ||
return next(url, context, next) | ||
export const load: Loader = async (url, context, next) => { | ||
const result = await next(url, context, next) | ||
const cached = cache.get(url) | ||
if (cached?.isPseudoESM && result.format !== 'module') { | ||
return { | ||
source: cached.source, | ||
format: ModuleFormat.Module, | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@vitest/test-esm", | ||
"private": true, | ||
"scripts": { | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"dependencies": { | ||
"css-what": "6.1.0", | ||
"tslib": "2.4.0" | ||
}, | ||
"devDependencies": { | ||
"vitest": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { __assign } from 'tslib' | ||
import { parse } from 'css-what' | ||
import { expect, test } from 'vitest' | ||
|
||
// TODO check on Linux Node 14 | ||
test.skip('imported libs have incorrect ESM, but still work', () => { | ||
expect(__assign({}, { a: 1 })).toEqual({ a: 1 }) | ||
expect(parse('a')).toBeDefined() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
deps: { | ||
external: [/tslib/, /css-what/], | ||
registerNodeLoader: true, | ||
}, | ||
}, | ||
}) |