-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: move to file-services/resolve as default resolver
- Loading branch information
Showing
42 changed files
with
207 additions
and
133 deletions.
There are no files selected for viewing
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
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
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,47 @@ | ||
// in browser build this gets remapped to an empty object via our package.json->"browser" | ||
import nodeModule from 'module'; | ||
// importing the factory directly, as we feed it our own fs, and don't want graceful-fs to be implicitly imported | ||
// this allows @stylable/core to be bundled for browser usage without special custom configuration | ||
import ResolverFactory from 'enhanced-resolve/lib/ResolverFactory.js'; | ||
import type { ModuleResolver } from './types'; | ||
import type { MinimalFS } from './cached-process-file'; | ||
|
||
function bundleSafeRequireExtensions(): string[] { | ||
let extensions: string[]; | ||
try { | ||
// we use nodeModule here to avoid bundling warnings about require.extensions we always has fallback for browsers | ||
extensions = Object.keys( | ||
(nodeModule as typeof nodeModule & { _extensions?: Record<string, unknown> }) | ||
._extensions ?? {} | ||
); | ||
} catch (e) { | ||
extensions = []; | ||
} | ||
return extensions.length ? extensions : ['.js', '.json']; | ||
} | ||
|
||
const resolverContext = {}; | ||
|
||
export function createLegacyResolver(fileSystem: MinimalFS, resolveOptions: any): ModuleResolver { | ||
const extensions = | ||
resolveOptions.extensions && resolveOptions.extensions.length | ||
? resolveOptions.extensions | ||
: bundleSafeRequireExtensions(); | ||
const eResolver = ResolverFactory.createResolver({ | ||
...resolveOptions, | ||
extensions, | ||
useSyncFileSystemCalls: true, | ||
cache: false, | ||
fileSystem, | ||
}); | ||
|
||
return (directoryPath, request): string => { | ||
const res = eResolver.resolveSync(resolverContext, directoryPath, request); | ||
if (res === false) { | ||
throw new Error( | ||
`Stylable does not support browser field 'false' values. ${request} resolved to 'false' from ${directoryPath}` | ||
); | ||
} | ||
return res; | ||
}; | ||
} |
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,47 +1,24 @@ | ||
// in browser build this gets remapped to an empty object via our package.json->"browser" | ||
import nodeModule from 'module'; | ||
// importing the factory directly, as we feed it our own fs, and don't want graceful-fs to be implicitly imported | ||
// this allows @stylable/core to be bundled for browser usage without special custom configuration | ||
import ResolverFactory from 'enhanced-resolve/lib/ResolverFactory.js'; | ||
import type { ModuleResolver } from './types'; | ||
import type { MinimalFS } from './cached-process-file'; | ||
|
||
function bundleSafeRequireExtensions(): string[] { | ||
let extensions: string[]; | ||
try { | ||
// we use nodeModule here to avoid bundling warnings about require.extensions we always has fallback for browsers | ||
extensions = Object.keys( | ||
(nodeModule as typeof nodeModule & { _extensions?: Record<string, unknown> }) | ||
._extensions ?? {} | ||
); | ||
} catch (e) { | ||
extensions = []; | ||
} | ||
return extensions.length ? extensions : ['.js', '.json']; | ||
} | ||
|
||
const resolverContext = {}; | ||
import { IRequestResolverOptions, createRequestResolver } from '@file-services/resolve'; | ||
import type { ModuleResolver } from './types'; | ||
|
||
export function createDefaultResolver(fileSystem: MinimalFS, resolveOptions: any): ModuleResolver { | ||
const extensions = | ||
resolveOptions.extensions && resolveOptions.extensions.length | ||
? resolveOptions.extensions | ||
: bundleSafeRequireExtensions(); | ||
const eResolver = ResolverFactory.createResolver({ | ||
...resolveOptions, | ||
extensions, | ||
useSyncFileSystemCalls: true, | ||
cache: false, | ||
fileSystem, | ||
export function createDefaultResolver(options: IRequestResolverOptions): ModuleResolver { | ||
const resolver = createRequestResolver({ | ||
extensions: ['.js', '.json', '.mjs', '.cjs', '.ts', '.mts', '.cts'], | ||
...options, | ||
}); | ||
|
||
return (directoryPath, request): string => { | ||
const res = eResolver.resolveSync(resolverContext, directoryPath, request); | ||
if (res === false) { | ||
const res = resolver(directoryPath, request); | ||
if (res.resolvedFile === false) { | ||
throw new Error( | ||
`Stylable does not support browser field 'false' values. ${request} resolved to 'false' from ${directoryPath}` | ||
); | ||
} | ||
return res; | ||
if (typeof res.resolvedFile !== 'string') { | ||
throw new Error(`Stylable could not resolve ${JSON.stringify(request)} from ${JSON.stringify(directoryPath)}`); | ||
} | ||
return res.resolvedFile; | ||
}; | ||
} |
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
Oops, something went wrong.