@pintjuk/golang-regex
is a JavaScript package that enables regex matching using Go's regex engine via WebAssembly. It supports the full Go regex syntax, providing powerful and efficient matching capabilities directly in JavaScript environments.
- Full compatibility with Go's regex syntax.
- Efficient WebAssembly-based implementation.
- Simple integration with modern JavaScript/TypeScript projects.
- Supports lazy loading for optimized performance.
For a complete demonstration of how to use this package, check out the live demo repository:
Install the package via npm:
npm install @pintjuk/golang-regex
To use @pintjuk/golang-regex
in a Webpack-based project, configure Webpack to handle .wasm
files and enable WebAssembly support.
const path = require('path');
module.exports = {
entry: './src/main.js', // Your JavaScript entry point
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
mode: 'production',
module: {
rules: [
{
test: /\.wasm$/, // Handles `.wasm` files
type: 'asset/resource', // Emits `.wasm` files as separate assets
},
],
},
experiments: {
asyncWebAssembly: true, // Enable asynchronous WebAssembly loading
},
};
Key Configuration Points:
type: 'asset/resource'
for.wasm
Files:- Ensures
.wasm
files are correctly emitted to the output directory.
- Ensures
- Enable
asyncWebAssembly
:- Allows WebAssembly files to be loaded asynchronously.
Here’s how to use the package in a typical JavaScript application:
import GolangRegex from '@pintjuk/golang-regex';
import wasmPath from '@pintjuk/golang-regex/goregex.wasm';
(async () => {
await GolangRegex.initialize(wasmPath);
const pattern = '^hello';
const text = 'hello world';
const isMatch = GolangRegex.match(pattern, text);
console.log(isMatch ? 'Match found!' : 'No match found.');
})();
Since @pintjuk/golang-regex
uses WebAssembly, it is recommended to lazy load the package to avoid blocking page load. Lazy loading ensures the WebAssembly module is initialized only when required.
document.getElementById('regexButton').addEventListener('click', async () => {
const GolangRegex = await import('@pintjuk/golang-regex');
const wasmPath = await import('@pintjuk/golang-regex/goregex.wasm');
await GolangRegex.initialize(wasmPath);
const pattern = '^hello';
const text = 'hello world';
const isMatch = GolangRegex.match(pattern, text);
console.log(isMatch ? 'Match found!' : 'No match found.');
});
- Improved Performance: Loads the WebAssembly module only when needed, reducing initial load time.
- Non-blocking: Keeps the main thread free during the initial page load.
- Scalability: Ideal for applications where regex functionality is used conditionally or infrequently.
WebAssembly modules, such as goregex.wasm
, must be downloaded and compiled by the browser. Loading these modules asynchronously provides several benefits:
- Non-blocking Execution: Prevents UI freezing during WebAssembly loading and compilation.
- Streaming Compilation: The
WebAssembly.instantiateStreaming
method allows browsers to compile the WebAssembly module while it's still downloading, improving performance. - Optimized Resource Loading: Ensures that the WebAssembly module is only downloaded when needed.
This project is licensed under the MIT License.