Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: apply Vite resolving algorithm to node_modules libraries (#1673)
* fix: apply Vite resolving algorithm to node_modules libraries * chore: remove inlining solid-js * chore: skip builtin * refactor: cleanup * chore: suppress loader warnings * chore: add max limit to cpu when running rollup
- Loading branch information
1 parent
409aa45
commit b971ede
Showing
8 changed files
with
105 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { pathToFileURL } from 'url' | ||
import { isNodeBuiltin } from 'mlly' | ||
import { normalizeModuleId } from 'vite-node/utils' | ||
import { getWorkerState } from '../utils' | ||
import type { Loader, Resolver } from '../types/loader' | ||
|
||
// apply transformations only to libraries | ||
// inline code preccessed by vite-node | ||
export const resolve: Resolver = async (url, context, next) => { | ||
const { parentURL } = context | ||
if (!parentURL || !parentURL.includes('node_modules') || isNodeBuiltin(url)) | ||
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(), | ||
} | ||
} | ||
} | ||
return next(url, context, next) | ||
} | ||
|
||
export const load: Loader = (url, context, next) => { | ||
return next(url, context, next) | ||
} |
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,37 @@ | ||
import type { Awaitable } from './general' | ||
|
||
interface ModuleContext { | ||
conditions: string[] | ||
parentURL?: string | ||
} | ||
|
||
enum ModuleFormat { | ||
Builtin = 'builtin', | ||
Commonjs = 'commonjs', | ||
Json = 'json', | ||
Module = 'module', | ||
Wasm = 'wasm', | ||
} | ||
|
||
interface ResolveResult { | ||
url: string | ||
format?: ModuleFormat | ||
} | ||
|
||
export interface Resolver { | ||
(url: string, context: ModuleContext, next: Resolver): Awaitable<ResolveResult> | ||
} | ||
|
||
interface LoaderContext { | ||
format: ModuleFormat | ||
importAssertions: Record<string, string> | ||
} | ||
|
||
interface LoaderResult { | ||
format: ModuleFormat | ||
source: string | ArrayBuffer | SharedArrayBuffer | Uint8Array | ||
} | ||
|
||
export interface Loader { | ||
(url: string, context: LoaderContext, next: Loader): Awaitable<LoaderResult> | ||
} |
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,20 @@ | ||
// borrowed from tsx implementation: | ||
// https://github.com/esbuild-kit/tsx | ||
|
||
const ignoreWarnings = new Set([ | ||
'--experimental-loader is an experimental feature. This feature could change at any time', | ||
'Custom ESM Loaders is an experimental feature. This feature could change at any time', | ||
]) | ||
|
||
const { emit } = process | ||
|
||
process.emit = function (event, warning) { | ||
if ( | ||
event === 'warning' | ||
&& ignoreWarnings.has(warning.message) | ||
) | ||
return | ||
|
||
// eslint-disable-next-line prefer-rest-params | ||
return Reflect.apply(emit, this, arguments) | ||
} |