Skip to content

Commit

Permalink
Merge pull request #171 from kwonoj/fix-env
Browse files Browse the repository at this point in the history
fix(createmoduleloader): apply overridden env
  • Loading branch information
kwonoj authored Jan 26, 2019
2 parents 21569c1 + a7cfb76 commit 119ab73
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="2.0.0-beta.4"></a>
# [2.0.0-beta.4](https://github.com/kwonoj/hunspell-asm/compare/v2.0.0-beta.3...v2.0.0-beta.4) (2019-01-26)


### Bug Fixes

* **createmoduleloader:** apply overridden env ([22645b6](https://github.com/kwonoj/hunspell-asm/commit/22645b6))



<a name="2.0.0-beta.3"></a>
# [2.0.0-beta.3](https://github.com/kwonoj/hunspell-asm/compare/v2.0.0-beta.2...v2.0.0-beta.3) (2019-01-26)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hunspell-asm",
"version": "2.0.0-beta.3",
"version": "2.0.0-beta.4",
"description": "WebAssembly based Javascript bindings for hunspell spellchecker",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand All @@ -9,7 +9,7 @@
"commitizen": {
"path": "cz-conventional-changelog"
},
"hunspell-version": "dab1b23-190125"
"hunspell-version": "19b43c8-190125"
},
"lint-staged": {
"*.ts": [
Expand Down
4 changes: 2 additions & 2 deletions spec/hunspell-asm/loadModule-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getModuleLoader as getModuleLoaderMock, isNode } from 'emscripten-wasm-loader';
import { ENVIRONMENT, getModuleLoader as getModuleLoaderMock, isNode } from 'emscripten-wasm-loader';
import { loadModule } from '../../src/loadModule';

jest.mock('../../src/lib/hunspell.wasm', () => jest.fn(), { virtual: true });
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('loadModule', () => {
});
await loadModule();

expect((getModuleLoaderMock as jest.Mock).mock.calls[0][2]).toBeUndefined();
expect((getModuleLoaderMock as jest.Mock).mock.calls[0][2]).toEqual({ ENVIRONMENT: ENVIRONMENT.NODE });
});

it('should override path for wasm binary on browser', async () => {
Expand Down
9 changes: 7 additions & 2 deletions src/createModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ const createModuleLoader = async (
//tslint:disable-next-line:no-require-imports no-var-requires
const lookupBinary = locateBinary || ((_filePath: string) => require('./lib/hunspell.wasm'));

//https://github.com/kwonoj/docker-hunspell-wasm/issues/63
//apply overridden environment values to custom patched hunspell preamble.
const baseModule = { ENVIRONMENT: env };

//Build module object to construct wasm binary module via emscripten preamble.
//This allows to override default wasm binary resolution in preamble.
//By default, hunspell-asm overrides to direct require to binary on *browser* environment to allow bundler like webpack resolves it.
//On node, it relies on default resolution logic.
const overriddenModule =
isNode() && !locateBinary
? undefined
env === ENVIRONMENT.NODE && !locateBinary
? baseModule
: {
...baseModule,
locateFile: (filePath: string) => (filePath.endsWith('.wasm') ? lookupBinary(filePath) : filePath)
};

Expand Down

0 comments on commit 119ab73

Please sign in to comment.