-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
rust_to_wasm
support to transpile_web_ui
- Loading branch information
1 parent
742deb6
commit 123f48d
Showing
8 changed files
with
468 additions
and
267 deletions.
There are no files selected for viewing
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,46 @@ | ||
# Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
# You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
import("//brave/tools/crates/sources.gni") | ||
|
||
template("rust_to_wasm") { | ||
assert(defined(invoker.rust_packages), | ||
"Need rust_packages in $target_name listing the Config.toml files.") | ||
|
||
action_foreach(target_name) { | ||
deps = [ "//brave/tools/crates:build_wasm_pack" ] | ||
if (defined(invoker.deps)) { | ||
deps += invoker.deps | ||
} | ||
|
||
inputs = [ wasm_pack_exe ] | ||
sources = invoker.rust_packages | ||
inputs += sources | ||
|
||
script = "//build/gn_run_binary.py" | ||
|
||
args = [ | ||
rebase_path(wasm_pack_exe), | ||
"build", | ||
"{{source_dir}}", | ||
"--out-dir", | ||
rebase_path("$root_out_dir/{{source_gen_dir}}/pkg"), | ||
"--out-name", | ||
"index", | ||
"--", | ||
"--target-dir", | ||
rebase_path("$root_out_dir/{{source_gen_dir}}/target"), | ||
] | ||
|
||
outputs = [ | ||
"{{source_gen_dir}}/pkg/index_bg.js", | ||
"{{source_gen_dir}}/pkg/index_bg.wasm", | ||
"{{source_gen_dir}}/pkg/index_bg.wasm.d.ts", | ||
"{{source_gen_dir}}/pkg/index.d.ts", | ||
"{{source_gen_dir}}/pkg/index.js", | ||
"{{source_gen_dir}}/pkg/package.json", | ||
] | ||
} | ||
} |
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,75 @@ | ||
/* Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("webpack/lib/ModuleTypeConstants"); | ||
const AsyncWasmLoadingRuntimeModule = require("webpack/lib/wasm-async/AsyncWasmLoadingRuntimeModule"); | ||
|
||
const PLUGIN_NAME = "XHRCompileAsyncWasmPlugin"; | ||
|
||
class XHRCompileAsyncWasmPlugin { | ||
type = "xhr"; | ||
|
||
apply(compiler) { | ||
const { webpack } = compiler; | ||
const { RuntimeGlobals, Template, wasm } = webpack; | ||
wasm.EnableWasmLoadingPlugin.setEnabled(compiler, this.type); | ||
|
||
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => { | ||
const { outputOptions, runtimeTemplate } = compilation; | ||
const globalWasmLoading = outputOptions.wasmLoading; | ||
|
||
const isEnabledForChunk = chunk => { | ||
const options = chunk.getEntryOptions(); | ||
const wasmLoading = | ||
options && options.wasmLoading !== undefined | ||
? options.wasmLoading | ||
: globalWasmLoading; | ||
return wasmLoading === this.type; | ||
}; | ||
|
||
const generateLoadBinaryCode = path => | ||
Template.asString([ | ||
`new Promise(${runtimeTemplate.basicFunction( | ||
"resolve", | ||
[ | ||
"const request = new XMLHttpRequest();", | ||
`request.onload = ${runtimeTemplate.returningFunction( | ||
"resolve({ arrayBuffer() { return request.response }})", | ||
"" | ||
)};`, | ||
`request.open("GET", ${RuntimeGlobals.publicPath} + ${path});`, | ||
'request.responseType = "arraybuffer";', | ||
"request.send();", | ||
] | ||
)})` | ||
]); | ||
|
||
compilation.hooks.runtimeRequirementInTree | ||
.for(RuntimeGlobals.instantiateWasm) | ||
.tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => { | ||
if (!isEnabledForChunk(chunk)) return; | ||
if ( | ||
!chunkGraph.hasModuleInGraph( | ||
chunk, | ||
m => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC | ||
) | ||
) { | ||
return; | ||
} | ||
set.add(RuntimeGlobals.publicPath); | ||
compilation.addRuntimeModule( | ||
chunk, | ||
new class extends AsyncWasmLoadingRuntimeModule { | ||
constructor(generateLoadBinaryCode) { | ||
super({ generateLoadBinaryCode, supportsStreaming: false }); | ||
} | ||
}(generateLoadBinaryCode) | ||
); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = XHRCompileAsyncWasmPlugin; |
Oops, something went wrong.