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( ...@@ -3302,10 +3302,9 @@ async fn resolve_package_internal_with_imports_field(
conditions: &BTreeMap<RcStr, ConditionValue>, conditions: &BTreeMap<RcStr, ConditionValue>,
unspecified_conditions: &ConditionValue, unspecified_conditions: &ConditionValue,
) -> Result<Vc<ResolveResult>> { ) -> Result<Vc<ResolveResult>> {
let Pattern::Constant(specifier) = pattern else { if let Pattern::Constant(specifier) = pattern
bail!("PackageInternal requests can only be Constant strings"); && (specifier == "#" || specifier.ends_with('/'))
}; {
if specifier == "#" || specifier.ends_with('/') {
ResolvingIssue { ResolvingIssue {
severity: resolve_error_severity(resolve_options).await?, severity: resolve_error_severity(resolve_options).await?,
file_path: file_path.clone(), file_path: file_path.clone(),
...@@ -3331,7 +3330,7 @@ async fn resolve_package_internal_with_imports_field( ...@@ -3331,7 +3330,7 @@ async fn resolve_package_internal_with_imports_field(
package_json_path.clone(), package_json_path.clone(),
resolve_options, resolve_options,
imports, imports,
Pattern::Constant(specifier.clone()), pattern.clone(),
conditions, conditions,
unspecified_conditions, unspecified_conditions,
RcStr::default(), RcStr::default(),
......
...@@ -524,9 +524,12 @@ contextPrototype.r = commonJsRequire ...@@ -524,9 +524,12 @@ contextPrototype.r = commonJsRequire
function parseRequest(request: string): string { function parseRequest(request: string): string {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#') // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex) const hashIndex = request.indexOf('#')
if (hashIndex !== -1) {
request = request.substring(0, hashIndex)
}
} }
const queryIndex = request.indexOf('?') 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([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js", "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)"]} {"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; ...@@ -388,9 +388,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
...@@ -2484,5 +2487,5 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -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 //# 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; ...@@ -369,9 +369,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
......
...@@ -369,9 +369,12 @@ contextPrototype.r = commonJsRequire; ...@@ -369,9 +369,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
......
...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire; ...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
...@@ -2483,4 +2486,4 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -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; ...@@ -595,9 +595,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
var hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); var hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
var queryIndex = request.indexOf('?'); var queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
......
...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire; ...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
......
...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire; ...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
......
...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire; ...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { if (queryIndex !== -1) {
......
...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire; ...@@ -387,9 +387,12 @@ contextPrototype.r = commonJsRequire;
*/ function parseRequest(request) { */ function parseRequest(request) {
// Per the URI spec fragments can contain `?` characters, so we should trim it off first // 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 // https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
const hashIndex = request.indexOf('#'); // A leading `#` identifies a package import specifier rather than a fragment.
if (hashIndex !== -1) { if (!request.startsWith('#')) {
request = request.substring(0, hashIndex); const hashIndex = request.indexOf('#');
if (hashIndex !== -1) {
request = request.substring(0, hashIndex);
}
} }
const queryIndex = request.indexOf('?'); const queryIndex = request.indexOf('?');
if (queryIndex !== -1) { 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