From 693974555acdf6ef4b43be23733d7543920e3aa7 Mon Sep 17 00:00:00 2001 From: etai Date: Mon, 28 Dec 2020 11:26:06 +0200 Subject: [PATCH] support web workers --- README.md | 14 +++++++++++ package.json | 3 ++- worker.webpack.config.js | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 worker.webpack.config.js diff --git a/README.md b/README.md index ad06b7f..d217e41 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,20 @@ const { ComplexZmanimCalendar, getZmanimJson } = require("kosher-zmanim"); For UMD, a global `KosherZmanim` object is exposed. +**Web Worker** + +In your worker, assuming the following project structure +``` +- node_modules/ +-- kosher-zmanim/ +- worker.js +``` +Worker code: +```javascript +importScripts('node_modules/kosher-zmanim/lib-worker/kosher-zmanim.min.js') +const { ComplexZmanimCalendar, getZmanimJson } = KosherZmanim +``` + #### Library Usage: The KosherJava library has been ported to JS, following the original API as close as possible. The classes are exposed as named exports. You can instantiate or extend those classes as necessary, the same way you would in Java. diff --git a/package.json b/package.json index 2853274..908d160 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "scripts": { "clean": "rimraf ./dist/*", "build": "npm run clean && npm run build:all && npm run webpack", - "build:all": "npm run build:cjs && npm run build:es6 && npm run build:types", + "build:all": "npm run build:cjs && npm run build:es6 && npm run build:types && npm run build:worker", "build:cjs": "tsc -p ./src/", "build:es6": "tsc -p ./src/tsconfig-es6.json", "build:types": "tsc -p ./src/tsconfig-types.json", + "build:worker": "webpack --config worker.webpack.config.js", "lint": "eslint --ext .ts src tests", "test": "jest", "prepublishOnly": "npm run build", diff --git a/worker.webpack.config.js b/worker.webpack.config.js new file mode 100644 index 0000000..47a3ba1 --- /dev/null +++ b/worker.webpack.config.js @@ -0,0 +1,54 @@ +const path = require("path"); +const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); + +module.exports = { + mode: "production", + devtool: 'source-map', + context: __dirname, + target: "webworker", + entry: { + "kosher-zmanim": "./src/kosher-zmanim.ts", + "kosher-zmanim.min": "./src/kosher-zmanim.ts", + }, + output: { + filename: "[name].js", + path: path.resolve(__dirname, "./dist/lib-worker"), + library: "KosherZmanim", + }, + module: { + rules: [ + { + test: /\.ts$/, + use: [ + { + loader: "ts-loader", + options: { + transpileOnly: true, + logInfoToStdOut: true, + compilerOptions: { + target: "es5", + module: "es6", + }, + configFile: "src/tsconfig.json", + }, + }, + ], + }, + ], + }, + resolve: { + extensions: [".ts", ".js"], + }, + optimization: { + minimizer: [new UglifyJsPlugin({ + sourceMap: true, + include: /\.min\.js$/, + uglifyOptions: { + mangle: { + keep_fnames: true, + }, + }, + })], + }, +}; +