Skip to content

Commit

Permalink
fix(NODE-6730): allow webpack bundling (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: Neal Beeken <neal.beeken@mongodb.com>
  • Loading branch information
durran and nbbeeken authored Feb 7, 2025
1 parent af9a3e7 commit 3787edd
Show file tree
Hide file tree
Showing 15 changed files with 9,000 additions and 6 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/webpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Webpack

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Use Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 'lts/*'

- name: "Build libmongocrypt"
shell: bash
run: |
npm run install:libmongocrypt
- name: "Install dependencies"
shell: bash
run: |
npm install
- name: "Install dependencies in the Webpack bundle test package"
shell: bash
working-directory: ./test/bundling/webpack
run: |
npm install
- name: "Install local mongodb-client-encryption into Webpack bundle test package"
shell: bash
working-directory: ./test/bundling/webpack
run: |
npm run install:ce
- name: "Run webpack build"
shell: bash
working-directory: ./test/bundling/webpack
run: |
npm run build
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"clang-format": "clang-format --style=file:.clang-format --Werror -i addon/*",
"check:eslint": "ESLINT_USE_FLAT_CONFIG=false eslint src test",
"check:clang-format": "clang-format --style=file:.clang-format --dry-run --Werror addon/*",
"test": "mocha --v8-expose-gc test",
"test": "mocha --v8-expose-gc test/unit",
"prepare": "tsc",
"rebuild": "node-gyp rebuild",
"prebuild": "prebuild --runtime napi --strip --verbose --all"
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ function load() {
try {
return require('../build/Release/mongocrypt.node');
} catch {
return require('../build/Debug/mongocrypt.node');
// Webpack will fail when just returning the require, so we need to wrap
// in a try/catch and rethrow.
/* eslint no-useless-catch: 0 */
try {
return require('../build/Debug/mongocrypt.node');
} catch (error) {
throw error;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions test/bundling/webpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/*
24 changes: 24 additions & 0 deletions test/bundling/webpack/install_ce.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const { execSync } = require('node:child_process');
const { readFileSync } = require('node:fs');
const { resolve } = require('node:path');

const xtrace = (...args) => {
console.log(`running: ${args[0]}`);
return execSync(...args);
};

const ceRoot = resolve(__dirname, '../../..');
console.log(`mongodb-client-encryption package root: ${ceRoot}`);

const ceVersion = JSON.parse(
readFileSync(resolve(ceRoot, 'package.json'), { encoding: 'utf8' })
).version;
console.log(`mongodb-client-encryption Version: ${ceVersion}`);

xtrace('npm pack --pack-destination test/bundling/webpack', { cwd: ceRoot });

xtrace(`npm install --no-save mongodb-client-encryption-${ceVersion}.tgz`);

console.log('mongodb-client-encryption installed!');
Loading

0 comments on commit 3787edd

Please sign in to comment.