Skip to content

Commit 6e5b8a1

Browse files
committed
build: add support for generating a base64-encoded Wasm module string
1 parent 990ffea commit 6e5b8a1

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );
24+
25+
26+
// MAIN //
27+
28+
var wasm = base64ToUint8Array( 'AGFzbQEAAAAADwhkeWxpbmsuMAEEAAAAAAEYA2AAAGAGf3x/f39/AGAIf3x/f39/f38AAg8BA2VudgZtZW1vcnkCAAADBAMAAQIHTAQRX193YXNtX2NhbGxfY3RvcnMAABhfX3dhc21fYXBwbHlfZGF0YV9yZWxvY3MAAAdjX2RheHB5AAEPY19kYXhweV9uZGFycmF5AAIKmAIDAwABCz0BAn4gACABIAIgAyADrCIGQgEgAKwiB31+QgAgBkIAVxunIAQgBSAFrCIGQgEgB31+QgAgBkIAVxunEAIL0wEBBX8CQCAAQQBMDQAgAUQAAAAAAAAAAGENACAAQQFxIQkgAEEBRwRAIABB/v///wdxIQogBiAGaiELIAMgA2ohDEEAIQADQCAFIAdBA3RqIgggASACIARBA3RqKwMAoiAIKwMAoDkDACAFIAYgB2pBA3RqIgggASACIAMgBGpBA3RqKwMAoiAIKwMAoDkDACAHIAtqIQcgBCAMaiEEIABBAmoiACAKRw0ACwsgCUUNACAFIAdBA3RqIgAgASACIARBA3RqKwMAoiAAKwMAoDkDAAsL' );
29+
30+
31+
// EXPORTS //
32+
33+
module.exports = wasm;

lib/node_modules/@stdlib/blas/base/daxpy-wasm/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
}
1515
],
1616
"main": "./lib",
17+
"browser": {
18+
"./lib/binary.js": "./lib/binary.browser.js"
19+
},
1720
"directories": {
1821
"benchmark": "./benchmark",
1922
"doc": "./docs",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2024 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFile = require( '@stdlib/fs/read-file' ).sync;
27+
var writeFile = require( '@stdlib/fs/write-file' ).sync;
28+
var replace = require( '@stdlib/string/replace' );
29+
30+
31+
// VARIABLES //
32+
33+
var wpath = resolve( __dirname, '..', 'src', 'main.wasm' );
34+
var tpath = resolve( __dirname, 'template.txt' );
35+
var opath = resolve( __dirname, '..', 'lib', 'binary.browser.js' );
36+
37+
var opts = {
38+
'encoding': 'utf8'
39+
};
40+
41+
var PLACEHOLDER = '{{WASM_BASE64}}';
42+
43+
44+
// MAIN //
45+
46+
/**
47+
* Main execution sequence.
48+
*
49+
* @private
50+
*/
51+
function main() {
52+
var wasm;
53+
var tmpl;
54+
55+
wasm = readFile( wpath );
56+
tmpl = readFile( tpath, opts );
57+
58+
tmpl = replace( tmpl, PLACEHOLDER, wasm.toString( 'base64' ) );
59+
60+
writeFile( opath, tmpl, opts );
61+
}
62+
63+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );
24+
25+
26+
// MAIN //
27+
28+
var wasm = base64ToUint8Array( '{{WASM_BASE64}}' );
29+
30+
31+
// EXPORTS //
32+
33+
module.exports = wasm;

lib/node_modules/@stdlib/blas/base/daxpy-wasm/src/Makefile

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ else
7373
WASM_TO_JS := wasm2js
7474
endif
7575

76+
# Define the path to the Node.js executable:
77+
ifdef NODE
78+
NODEJS := $(NODE)
79+
else
80+
NODEJS := node
81+
endif
82+
7683
# Define the command-line options when compiling C files:
7784
CFLAGS ?= \
7885
-std=c99 \
@@ -115,6 +122,9 @@ wat_targets := main.wat
115122
# List of WebAssembly JavaScript targets:
116123
wasm_js_targets := main.wasm.js
117124

125+
# List of other JavaScript targets:
126+
browser_js_targets := ./../lib/binary.browser.js
127+
118128

119129
# RULES #
120130

@@ -155,7 +165,7 @@ all: wasm
155165
# @example
156166
# make wasm
157167
#/
158-
wasm: $(wasm_targets) $(wat_targets)
168+
wasm: $(wasm_targets) $(wat_targets) $(browser_js_targets)
159169

160170
.PHONY: wasm
161171

@@ -191,6 +201,15 @@ $(wat_targets): %.wat: %.wasm
191201
$(wasm_js_targets): %.wasm.js: %.wasm
192202
$(QUIET) $(WASM_TO_JS) -o $@ $(wasm_targets)
193203

204+
#/
205+
# Generates an inline WebAssembly build for use in bundlers.
206+
#
207+
# @private
208+
# @param {string} NODE - Node.js executable
209+
#/
210+
$(browser_js_targets): $(wasm_targets)
211+
$(QUIET) $(NODEJS) ./../scripts/build.js
212+
194213
#/
195214
# Removes generated WebAssembly files.
196215
#

0 commit comments

Comments
 (0)