From 6e5b8a1747929bc984c994b62faefb1fe96ff5f0 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 18 Sep 2024 19:07:14 -0700 Subject: [PATCH] build: add support for generating a base64-encoded Wasm module string --- .../base/daxpy-wasm/lib/binary.browser.js | 33 ++++++++++ .../@stdlib/blas/base/daxpy-wasm/package.json | 3 + .../blas/base/daxpy-wasm/scripts/build.js | 63 +++++++++++++++++++ .../blas/base/daxpy-wasm/scripts/template.txt | 33 ++++++++++ .../@stdlib/blas/base/daxpy-wasm/src/Makefile | 21 ++++++- 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 lib/node_modules/@stdlib/blas/base/daxpy-wasm/lib/binary.browser.js create mode 100644 lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/build.js create mode 100644 lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/template.txt diff --git a/lib/node_modules/@stdlib/blas/base/daxpy-wasm/lib/binary.browser.js b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/lib/binary.browser.js new file mode 100644 index 00000000000..4b15b608184 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/lib/binary.browser.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' ); + + +// MAIN // + +var wasm = base64ToUint8Array( 'AGFzbQEAAAAADwhkeWxpbmsuMAEEAAAAAAEYA2AAAGAGf3x/f39/AGAIf3x/f39/f38AAg8BA2VudgZtZW1vcnkCAAADBAMAAQIHTAQRX193YXNtX2NhbGxfY3RvcnMAABhfX3dhc21fYXBwbHlfZGF0YV9yZWxvY3MAAAdjX2RheHB5AAEPY19kYXhweV9uZGFycmF5AAIKmAIDAwABCz0BAn4gACABIAIgAyADrCIGQgEgAKwiB31+QgAgBkIAVxunIAQgBSAFrCIGQgEgB31+QgAgBkIAVxunEAIL0wEBBX8CQCAAQQBMDQAgAUQAAAAAAAAAAGENACAAQQFxIQkgAEEBRwRAIABB/v///wdxIQogBiAGaiELIAMgA2ohDEEAIQADQCAFIAdBA3RqIgggASACIARBA3RqKwMAoiAIKwMAoDkDACAFIAYgB2pBA3RqIgggASACIAMgBGpBA3RqKwMAoiAIKwMAoDkDACAHIAtqIQcgBCAMaiEEIABBAmoiACAKRw0ACwsgCUUNACAFIAdBA3RqIgAgASACIARBA3RqKwMAoiAAKwMAoDkDAAsL' ); + + +// EXPORTS // + +module.exports = wasm; diff --git a/lib/node_modules/@stdlib/blas/base/daxpy-wasm/package.json b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/package.json index 791b3e4cc35..da129445d19 100644 --- a/lib/node_modules/@stdlib/blas/base/daxpy-wasm/package.json +++ b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/package.json @@ -14,6 +14,9 @@ } ], "main": "./lib", + "browser": { + "./lib/binary.js": "./lib/binary.browser.js" + }, "directories": { "benchmark": "./benchmark", "doc": "./docs", diff --git a/lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/build.js b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/build.js new file mode 100644 index 00000000000..348354d7029 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/build.js @@ -0,0 +1,63 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFile = require( '@stdlib/fs/read-file' ).sync; +var writeFile = require( '@stdlib/fs/write-file' ).sync; +var replace = require( '@stdlib/string/replace' ); + + +// VARIABLES // + +var wpath = resolve( __dirname, '..', 'src', 'main.wasm' ); +var tpath = resolve( __dirname, 'template.txt' ); +var opath = resolve( __dirname, '..', 'lib', 'binary.browser.js' ); + +var opts = { + 'encoding': 'utf8' +}; + +var PLACEHOLDER = '{{WASM_BASE64}}'; + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var wasm; + var tmpl; + + wasm = readFile( wpath ); + tmpl = readFile( tpath, opts ); + + tmpl = replace( tmpl, PLACEHOLDER, wasm.toString( 'base64' ) ); + + writeFile( opath, tmpl, opts ); +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/template.txt b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/template.txt new file mode 100644 index 00000000000..12996dd89e3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/template.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' ); + + +// MAIN // + +var wasm = base64ToUint8Array( '{{WASM_BASE64}}' ); + + +// EXPORTS // + +module.exports = wasm; diff --git a/lib/node_modules/@stdlib/blas/base/daxpy-wasm/src/Makefile b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/src/Makefile index da6368a1087..1dd61310a79 100644 --- a/lib/node_modules/@stdlib/blas/base/daxpy-wasm/src/Makefile +++ b/lib/node_modules/@stdlib/blas/base/daxpy-wasm/src/Makefile @@ -73,6 +73,13 @@ else WASM_TO_JS := wasm2js endif +# Define the path to the Node.js executable: +ifdef NODE + NODEJS := $(NODE) +else + NODEJS := node +endif + # Define the command-line options when compiling C files: CFLAGS ?= \ -std=c99 \ @@ -115,6 +122,9 @@ wat_targets := main.wat # List of WebAssembly JavaScript targets: wasm_js_targets := main.wasm.js +# List of other JavaScript targets: +browser_js_targets := ./../lib/binary.browser.js + # RULES # @@ -155,7 +165,7 @@ all: wasm # @example # make wasm #/ -wasm: $(wasm_targets) $(wat_targets) +wasm: $(wasm_targets) $(wat_targets) $(browser_js_targets) .PHONY: wasm @@ -191,6 +201,15 @@ $(wat_targets): %.wat: %.wasm $(wasm_js_targets): %.wasm.js: %.wasm $(QUIET) $(WASM_TO_JS) -o $@ $(wasm_targets) +#/ +# Generates an inline WebAssembly build for use in bundlers. +# +# @private +# @param {string} NODE - Node.js executable +#/ +$(browser_js_targets): $(wasm_targets) + $(QUIET) $(NODEJS) ./../scripts/build.js + #/ # Removes generated WebAssembly files. #