This project demonstrates the usage of the @pintjuk/golang-regex package, which provides regex matching with full compatibility with Go's regex syntax. The demo includes a form where users can test regex patterns against input text.
Ensure you have the following installed:
- Node.js (version 14 or later)
- npm (comes with Node.js)
-
Clone the repository:
git clone https://github.com/yourusername/golang-regex-demo.git cd golang-regex-demo -
Install dependencies:
npm install
The project uses TailwindCSS for styling. To compile the TailwindCSS styles:
npm run build:cssThis command compiles the src/styles.css file into the dist/styles.css file.
If you want to watch for changes and rebuild automatically:
npm run build:css -- --watchThe project uses Webpack to bundle the JavaScript files. To build the JavaScript bundle:
npm run buildThe bundled JavaScript file will be output to the dist/ directory as bundle.js.
To serve the demo, you can use a lightweight static server like http-server. Start the server with:
npm run startBy default, the server will run on http://127.0.0.1:8080.
Once the server is running, open your browser and navigate to:
http://127.0.0.1:8080
You should see the demo interface. Enter a regex pattern and some text to test the matching functionality.
The @pintjuk/golang-regex package uses WebAssembly (WASM) to execute regex operations, leveraging Go's regex engine. WebAssembly modules are binary files that the browser must download and compile before they can be used.
- Non-blocking Execution: Downloading and compiling the WASM module can take time. Asynchronous loading prevents the page from freezing while the browser performs these operations.
- Streaming Compilation: The
WebAssembly.instantiateStreamingmethod allows browsers to compile the WASM module while it is still being downloaded, improving performance. - Network Considerations: Loading the WASM asynchronously ensures that the rest of the page resources can load and render without waiting for the WASM module.
If you want to integrate the @pintjuk/golang-regex package into your own project, configure your Webpack bundler as follows:
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', // Ensures WebAssembly files are handled correctly
},
],
},
experiments: {
asyncWebAssembly: true, // Enable asynchronous WebAssembly loading
},
};-
type: 'asset/resource'for.wasmFiles:- This ensures that Webpack emits
.wasmfiles as separate assets and includes them in the output directory.
- This ensures that Webpack emits
-
Enable
asyncWebAssembly:- This allows Webpack to handle asynchronous WebAssembly imports, ensuring the module is loaded efficiently.
golang-regex-demo/
├── dist/ # Compiled assets (CSS and JS)
│ ├── bundle.js # Bundled JavaScript
│ └── styles.css # Compiled TailwindCSS
├── src/ # Source files
│ ├── styles.css # TailwindCSS source file
│ └── main.js # JavaScript entry point
├── public/ # Static assets
│ ├── wasm_exec.js # WebAssembly runtime
│ └── goregex.wasm # Compiled WebAssembly module
├── index.html # HTML file for the demo
├── tailwind.config.js # TailwindCSS configuration
├── webpack.config.js # Webpack configuration
├── package.json # Project dependencies and scripts
| Command | Description |
|---|---|
npm run build:css |
Compile TailwindCSS into dist/styles.css. |
npm run build |
Bundle JavaScript into dist/bundle.js. |
npm run start |
Start the HTTP server to serve the demo locally. |
This project is licensed under the MIT License.