Skip to content

Commit

Permalink
Merge pull request #172 from kwonoj/fix-treeshake
Browse files Browse the repository at this point in the history
fix(createmoduleloader): fix non treeshakable import
  • Loading branch information
kwonoj authored Jan 26, 2019
2 parents 119ab73 + c3d2d1e commit 751ac55
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 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.5"></a>
# [2.0.0-beta.5](https://github.com/kwonoj/hunspell-asm/compare/v2.0.0-beta.4...v2.0.0-beta.5) (2019-01-26)


### Bug Fixes

* **createmoduleloader:** fix non treeshakable import ([a77a149](https://github.com/kwonoj/hunspell-asm/commit/a77a149))



<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)

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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hunspell-asm",
"version": "2.0.0-beta.4",
"version": "2.0.0-beta.5",
"description": "WebAssembly based Javascript bindings for hunspell spellchecker",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
18 changes: 2 additions & 16 deletions src/createModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,10 @@ const createModuleLoader = async (

log(`module loader configured`, { env, timeout });

//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 =
env === ENVIRONMENT.NODE && !locateBinary
? baseModule
: {
...baseModule,
locateFile: (filePath: string) => (filePath.endsWith('.wasm') ? lookupBinary(filePath) : filePath)
};
//apply overridden environment values to custom patched hunspell preamble.
const overriddenModule = { locateFile: locateBinary, ENVIRONMENT: env };

const moduleLoader = await getModuleLoader<HunspellFactory, HunspellAsmModule>(
(runtime: HunspellAsmModule) => hunspellLoader(runtime, env),
Expand Down
2 changes: 0 additions & 2 deletions src/loadAsmModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { log } from './util/logger';
*
* @param [InitOptions] Options to initialize cld3 asm binary.
* @param {number} [InitOptions.timeout] - timeout to wait wasm binary compilation & load.
* @param {string | object} [InitOptions.locateBinary] - custom resolution logic for wasm binary.
* @param {ENVIRONMENT} [InitOptions.environment] For overriding running environment
* It could be either remote endpoint url, or loader-returned object for bundler. Check examples/browser_* for references.
*
Expand All @@ -18,7 +17,6 @@ import { log } from './util/logger';
const loadAsmModule = async (
initOptions: Partial<{
timeout: number;
locateBinary: (filePath: string) => string | object;
environment?: ENVIRONMENT;
}> = {}
) => {
Expand Down
23 changes: 21 additions & 2 deletions src/loadModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ENVIRONMENT } from 'emscripten-wasm-loader';
import { ENVIRONMENT, isNode } from 'emscripten-wasm-loader';
import { createModuleLoader } from './createModuleLoader';
import { log } from './util/logger';

Expand Down Expand Up @@ -26,7 +26,26 @@ const loadModule = async (
//tslint:disable-next-line:no-require-imports no-var-requires
const runtime = require(`./lib/hunspell`);

return createModuleLoader(initOptions, runtime);
const { locateBinary, environment } = initOptions;
const env = environment ? environment : isNode() ? ENVIRONMENT.NODE : ENVIRONMENT.WEB;

//Override default wasm binary resolution in preamble if needed.
//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 lookupBinary =
env === ENVIRONMENT.NODE && !locateBinary
? undefined
: locateBinary ||
//tslint:disable-next-line:no-require-imports no-var-requires
((filePath: string) => (filePath.endsWith('.wasm') ? require('./lib/hunspell.wasm') : filePath));

return createModuleLoader(
{
locateBinary: lookupBinary,
...initOptions
},
runtime
);
};

export { loadModule };

0 comments on commit 751ac55

Please sign in to comment.