Unverified Commit b1a22ce5 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Fix dynamic package imports in Turbopack

parent 4a95af80
......@@ -3302,10 +3302,9 @@ async fn resolve_package_internal_with_imports_field(
conditions: &BTreeMap<RcStr, ConditionValue>,
unspecified_conditions: &ConditionValue,
) -> Result<Vc<ResolveResult>> {
let Pattern::Constant(specifier) = pattern else {
bail!("PackageInternal requests can only be Constant strings");
};
if specifier == "#" || specifier.ends_with('/') {
if let Pattern::Constant(specifier) = pattern
&& (specifier == "#" || specifier.ends_with('/'))
{
ResolvingIssue {
severity: resolve_error_severity(resolve_options).await?,
file_path: file_path.clone(),
......@@ -3331,7 +3330,7 @@ async fn resolve_package_internal_with_imports_field(
package_json_path.clone(),
resolve_options,
imports,
Pattern::Constant(specifier.clone()),
pattern.clone(),
conditions,
unspecified_conditions,
RcStr::default(),
......
......@@ -524,9 +524,12 @@ contextPrototype.r = commonJsRequire
function parseRequest(request: string): string {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#')
if (hashIndex !== -1) {
request = request.substring(0, hashIndex)
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#')
if (hashIndex !== -1) {
request = request.substring(0, hashIndex)
}
}
const queryIndex = request.indexOf('?')
......
function importTrailing(name) {
return import(`#/${name}.js`)
}
function importMiddle(locale) {
return import(`#/messages/${locale}/index.js`)
}
it('should resolve dynamic subpath imports with a trailing placeholder', async () => {
expect((await importTrailing('a')).default).toBe('a')
expect((await importTrailing('b')).default).toBe('b')
})
it('should resolve dynamic subpath imports with a middle placeholder', async () => {
expect((await importMiddle('en')).default).toBe('hello')
expect((await importMiddle('de')).default).toBe('hallo')
})
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="64c9678c-f7a9-2121-4902-35dfa89f5d34")}catch(e){}}();
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="dbd8ebb8-84f9-b7ae-d333-c37441575012")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
......@@ -388,9 +388,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......@@ -2484,5 +2487,5 @@ chunkListsToRegister.forEach(registerChunkList);
})();
//# debugId=64c9678c-f7a9-2121-4902-35dfa89f5d34
//# debugId=dbd8ebb8-84f9-b7ae-d333-c37441575012
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js.map
\ No newline at end of file
......@@ -369,9 +369,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......
......@@ -369,9 +369,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......
......@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......@@ -2483,4 +2486,4 @@ chunkListsToRegister.forEach(registerChunkList);
})();
//# sourceMappingURL=0_9x_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_17smy-b.js.map
//# sourceMappingURL=0_9x_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_17smy-b.js.map
\ No newline at end of file
......@@ -595,9 +595,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
var hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
var hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
var queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......
......@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......
......@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......
......@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......
......@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
// A leading `#` identifies a package import specifier rather than a fragment.
if (!request.startsWith('#')) {
const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
}
const queryIndex = request.indexOf('?');
if (queryIndex !== -1) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment