Skip to content

Commit

Permalink
Fix relative imports in wasm loader
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Sep 15, 2024
1 parent 81c309b commit 051fdbd
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 4 deletions.
3 changes: 3 additions & 0 deletions _test/build.both.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions _test/build.dart2js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions _test/build.dart2wasm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions _test/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions _test/test/sub-dir/subdir_source.dart
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions _test/test/subdir_source_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}
10 changes: 10 additions & 0 deletions _test/test/subdir_source_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>

<head>
<link rel="x-dart-test" href="subdir_source_test.dart">
<script src="sub-dir/subdir_source.dart.js"></script>
<script src="packages/test/dart.js"></script>
</head>
<body>
</body>
</html>
4 changes: 4 additions & 0 deletions build_web_compilers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 11 additions & 3 deletions build_web_compilers/lib/src/web_entrypoint_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, []);
''');
Expand All @@ -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);
}
''');
Expand Down
2 changes: 1 addition & 1 deletion build_web_compilers/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 051fdbd

Please sign in to comment.