diff --git a/_test/build.both.yaml b/_test/build.both.yaml index 2a1800020..7b3aacb27 100644 --- a/_test/build.both.yaml +++ b/_test/build.both.yaml @@ -18,6 +18,9 @@ targets: - test/hello_world_deferred_test.dart.browser_test.dart - test/hello_world_custom_html_test.dart - test/hello_world_custom_html_test.dart.browser_test.dart + - test/subdir_source_test.dart + - test/subdir_source_test.dart.browser_test.dart - test/other_test.dart.browser_test.dart + - test/sub-dir/subdir_source.dart - test/sub-dir/subdir_test.dart - test/sub-dir/subdir_test.dart.browser_test.dart diff --git a/_test/build.dart2js.yaml b/_test/build.dart2js.yaml index fb2f7083c..965ce35d1 100644 --- a/_test/build.dart2js.yaml +++ b/_test/build.dart2js.yaml @@ -17,6 +17,9 @@ targets: - test/hello_world_deferred_test.dart.browser_test.dart - test/hello_world_custom_html_test.dart - test/hello_world_custom_html_test.dart.browser_test.dart + - test/subdir_source_test.dart + - test/subdir_source_test.dart.browser_test.dart - test/other_test.dart.browser_test.dart + - test/sub-dir/subdir_source.dart - test/sub-dir/subdir_test.dart - test/sub-dir/subdir_test.dart.browser_test.dart diff --git a/_test/build.dart2wasm.yaml b/_test/build.dart2wasm.yaml index d88c5fe20..afd1d7970 100644 --- a/_test/build.dart2wasm.yaml +++ b/_test/build.dart2wasm.yaml @@ -23,6 +23,9 @@ targets: - test/hello_world_deferred_test.dart.browser_test.dart - test/hello_world_custom_html_test.dart - test/hello_world_custom_html_test.dart.browser_test.dart + - test/subdir_source_test.dart + - test/subdir_source_test.dart.browser_test.dart - test/other_test.dart.browser_test.dart + - test/sub-dir/subdir_source.dart - test/sub-dir/subdir_test.dart - test/sub-dir/subdir_test.dart.browser_test.dart diff --git a/_test/build.yaml b/_test/build.yaml index ce556f218..9b76a096c 100644 --- a/_test/build.yaml +++ b/_test/build.yaml @@ -10,4 +10,6 @@ targets: - test/hello_world_deferred_test.dart.browser_test.dart - test/hello_world_custom_html_test.dart.browser_test.dart - test/other_test.dart.browser_test.dart + - test/subdir_source_test.dart.browser_test.dart + - test/sub-dir/subdir_source.dart - test/sub-dir/subdir_test.dart.browser_test.dart diff --git a/_test/test/sub-dir/subdir_source.dart b/_test/test/sub-dir/subdir_source.dart new file mode 100644 index 000000000..c10383522 --- /dev/null +++ b/_test/test/sub-dir/subdir_source.dart @@ -0,0 +1,10 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; + +void main() { + globalContext['otherScriptLoaded'] = true.toJS; +} diff --git a/_test/test/subdir_source_test.dart b/_test/test/subdir_source_test.dart new file mode 100644 index 000000000..575bc6fbe --- /dev/null +++ b/_test/test/subdir_source_test.dart @@ -0,0 +1,15 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; + +import 'package:test/test.dart'; + +void main() { + test('did load script from a subdirectory', () { + // The custom HTML file includes `sub-dir/subdir_source.dart.js`, which + // should have set the `otherScriptLoader` propery. + expect(globalContext.has('otherScriptLoaded'), isTrue); + }); +} diff --git a/_test/test/subdir_source_test.html b/_test/test/subdir_source_test.html new file mode 100644 index 000000000..da6dbbd60 --- /dev/null +++ b/_test/test/subdir_source_test.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/build_web_compilers/CHANGELOG.md b/build_web_compilers/CHANGELOG.md index 2f15fe517..58f059fef 100644 --- a/build_web_compilers/CHANGELOG.md +++ b/build_web_compilers/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.1.0-dev + +- Fix loading compiled modules from subdirectories. + ## 4.1.0-beta.0 - Support compiling to WebAssembly by using `dart2wasm` as a value for the diff --git a/build_web_compilers/lib/src/web_entrypoint_builder.dart b/build_web_compilers/lib/src/web_entrypoint_builder.dart index b0fa98bd1..f03bcbe88 100644 --- a/build_web_compilers/lib/src/web_entrypoint_builder.dart +++ b/build_web_compilers/lib/src/web_entrypoint_builder.dart @@ -334,7 +334,15 @@ class WebEntrypointBuilder implements Builder { var jsCompiler = options.optionsFor(WebCompiler.Dart2Js) ?? options.optionsFor(WebCompiler.DartDevc); - var loaderResult = StringBuffer('''(async () => { + var loaderResult = StringBuffer(''' +(async () => { +const thisScript = document.currentScript; + +function relativeURL(ref) { + const base = thisScript?.src ?? document.baseURI; + return new URL(ref, base).toString(); +} + '''); // If we're compiling to JS, start a feature detection to prefer wasm but @@ -357,7 +365,7 @@ if (supportsWasmGC()) { loaderResult.writeln(''' let { instantiate, invoke } = await import("./$basename${wasmCompiler.extension}"); -let modulePromise = WebAssembly.compileStreaming(fetch("$basename.wasm")); +let modulePromise = WebAssembly.compileStreaming(fetch(relativeURL("$basename.wasm"))); let instantiated = await instantiate(modulePromise, {}); invoke(instantiated, []); '''); @@ -367,7 +375,7 @@ invoke(instantiated, []); } else { const scriptTag = document.createElement("script"); scriptTag.type = "application/javascript"; -scriptTag.src = new URL("./$basename${jsCompiler.extension}", document.baseURI).toString(); +scriptTag.src = relativeURL("./$basename${jsCompiler.extension}") document.head.append(scriptTag); } '''); diff --git a/build_web_compilers/pubspec.yaml b/build_web_compilers/pubspec.yaml index d2c429de3..4d18c5442 100644 --- a/build_web_compilers/pubspec.yaml +++ b/build_web_compilers/pubspec.yaml @@ -1,5 +1,5 @@ name: build_web_compilers -version: 4.1.0-beta.0 +version: 4.1.0-dev description: Builder implementations wrapping the dart2js and DDC compilers. repository: https://github.com/dart-lang/build/tree/master/build_web_compilers # This package can't be part of the workspace because it requires a very recent