From 3555002f48f46279628d387ef7d9da3b84e1c909 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Sun, 7 Feb 2021 07:57:54 +0100 Subject: [PATCH] Integrate objectbox-js & objectbox-dart - make webpack export as a global object (namespace) --- objectbox-js/README.md | 10 +++++++++- objectbox-js/package.json | 1 + objectbox-js/webpack.config.js | 2 ++ objectbox/lib/objectbox.js | 2 +- objectbox/lib/objectbox.js.map | 2 +- objectbox/lib/src/web/number.dart | 2 +- objectbox/pubspec.yaml | 1 + 7 files changed, 16 insertions(+), 4 deletions(-) diff --git a/objectbox-js/README.md b/objectbox-js/README.md index 0680c9ff..1ea14a7d 100644 --- a/objectbox-js/README.md +++ b/objectbox-js/README.md @@ -3,21 +3,29 @@ This is a very basic ObjectBox implementation for JavaScript/TypeScript, based on IndexedDB instead of the native objectbox-c API. ### Build and copy to objectbox-dart + The following code builds TypeScript files into `build` folder, uses `webpack` to create a single JS, copies the JS (and sourcemap) to `../objectbox/lib/`, and generates dart binding code in `../objectbox/lib/src/web/`. -``` +```shell npm run build:main npm run install npm run generate:dart ``` +Or run a single action to do it all: +```shell +npm run all:dart +``` + ### Notes and useful links about the compilation/integration: + * modules are not supported, we need to compile to a single JS file (using `webpack`) * example: https://github.com/google/chartjs.dart/ * example: https://github.com/matanlurey/dart_js_interop * modules issue: https://github.com/dart-lang/sdk/issues/25059 ### IndexedDB resources + * https://developers.google.com/web/ilt/pwa/working-with-indexeddb * https://www.tutorialspoint.com/html5/html5_indexeddb.htm * https://javascript.info/indexeddb diff --git a/objectbox-js/package.json b/objectbox-js/package.json index d3d6907c..6e91fa21 100644 --- a/objectbox-js/package.json +++ b/objectbox-js/package.json @@ -14,6 +14,7 @@ "build:module": "tsc -p tsconfig.module.json", "install": "cp build/objectbox.js* ../objectbox/lib/", "generate:dart": "cd build/main/lib && dart_js_facade_gen --destination=../../../../objectbox/lib/src/web/ number.d.ts", + "all:dart": "run-p build:main && run-p install && run-p generate:dart", "fix": "run-s fix:*", "fix:prettier": "prettier \"src/**/*.ts\" --write", "fix:lint": "eslint src --ext .ts --fix", diff --git a/objectbox-js/webpack.config.js b/objectbox-js/webpack.config.js index cec49fcf..74035fbd 100644 --- a/objectbox-js/webpack.config.js +++ b/objectbox-js/webpack.config.js @@ -19,5 +19,7 @@ module.exports = { output: { filename: 'objectbox.js', path: path.resolve(__dirname, 'build'), + library: 'objectbox', + libraryTarget: 'window' }, }; diff --git a/objectbox/lib/objectbox.js b/objectbox/lib/objectbox.js index 31920f64..f4b140fb 100644 --- a/objectbox/lib/objectbox.js +++ b/objectbox/lib/objectbox.js @@ -1,2 +1,2 @@ -(()=>{"use strict";var e={607:function(e,t,r){var o=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,o){void 0===o&&(o=r),e[o]=t[r]}),n=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||o(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),n(r(764),t)},764:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.power=t.double=void 0,t.double=function(e){return 2*e},t.power=function(e,t){return Math.pow(e,t)}}},t={};!function r(o){if(t[o])return t[o].exports;var n=t[o]={exports:{}};return e[o].call(n.exports,n,n.exports,r),n.exports}(607)})(); +window.objectbox=(()=>{"use strict";var e={607:function(e,t,r){var o=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,o){void 0===o&&(o=r),e[o]=t[r]}),n=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||o(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),n(r(764),t)},764:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.power=t.double=void 0,t.double=function(e){return 2*e},t.power=function(e,t){return Math.pow(e,t)}}},t={};return function r(o){if(t[o])return t[o].exports;var n=t[o]={exports:{}};return e[o].call(n.exports,n,n.exports,r),n.exports}(607)})(); //# sourceMappingURL=objectbox.js.map \ No newline at end of file diff --git a/objectbox/lib/objectbox.js.map b/objectbox/lib/objectbox.js.map index d1ead503..d7406eae 100644 --- a/objectbox/lib/objectbox.js.map +++ b/objectbox/lib/objectbox.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack://objectbox-js/./src/index.ts","webpack://objectbox-js/./src/lib/number.ts","webpack://objectbox-js/webpack/bootstrap","webpack://objectbox-js/webpack/startup"],"names":["double","value","power","base","exponent","__webpack_module_cache__","__webpack_require__","moduleId","exports","module","__webpack_modules__","call"],"mappings":"waAAA,a,qFCqBa,EAAAA,OAAS,SAACC,GACrB,OAAe,EAARA,GAuBI,EAAAC,MAAQ,SAACC,EAAcC,GAIlC,OAAO,SAAAD,EAAQC,MChDbC,EAA2B,IAG/B,SAASC,EAAoBC,GAE5B,GAAGF,EAAyBE,GAC3B,OAAOF,EAAyBE,GAAUC,QAG3C,IAAIC,EAASJ,EAAyBE,GAAY,CAGjDC,QAAS,IAOV,OAHAE,EAAoBH,GAAUI,KAAKF,EAAOD,QAASC,EAAQA,EAAOD,QAASF,GAGpEG,EAAOD,QCjBfF,CAAoB,M","file":"objectbox.js","sourcesContent":["export * from './lib/number';\n","/**\n * Multiplies a value by 2. (Also a full example of TypeDoc's functionality.)\n *\n * ### Example (es module)\n * ```js\n * import { double } from 'typescript-starter'\n * console.log(double(4))\n * // => 8\n * ```\n *\n * ### Example (commonjs)\n * ```js\n * var double = require('typescript-starter').double;\n * console.log(double(4))\n * // => 8\n * ```\n *\n * @param value - Comment describing the `value` parameter.\n * @returns Comment describing the return type.\n * @anotherNote Some other value.\n */\nexport const double = (value: number) => {\n return value * 2;\n};\n\n/**\n * Raise the value of the first parameter to the power of the second using the\n * es7 exponentiation operator (`**`).\n *\n * ### Example (es module)\n * ```js\n * import { power } from 'typescript-starter'\n * console.log(power(2,3))\n * // => 8\n * ```\n *\n * ### Example (commonjs)\n * ```js\n * var power = require('typescript-starter').power;\n * console.log(power(2,3))\n * // => 8\n * ```\n * @param base - the base to exponentiate\n * @param exponent - the power to which to raise the base\n */\nexport const power = (base: number, exponent: number) => {\n /**\n * This es7 exponentiation operator is transpiled by TypeScript\n */\n return base ** exponent;\n};\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tif(__webpack_module_cache__[moduleId]) {\n\t\treturn __webpack_module_cache__[moduleId].exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// startup\n// Load entry module\n// This entry module is referenced by other modules so it can't be inlined\n__webpack_require__(607);\n"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack://objectbox/./src/index.ts","webpack://objectbox/./src/lib/number.ts","webpack://objectbox/webpack/bootstrap","webpack://objectbox/webpack/startup"],"names":["double","value","power","base","exponent","__webpack_module_cache__","__webpack_require__","moduleId","exports","module","__webpack_modules__","call"],"mappings":"ybAAA,a,qFCqBa,EAAAA,OAAS,SAACC,GACrB,OAAe,EAARA,GAuBI,EAAAC,MAAQ,SAACC,EAAcC,GAIlC,OAAO,SAAAD,EAAQC,MChDbC,EAA2B,GCE/B,ODCA,SAASC,EAAoBC,GAE5B,GAAGF,EAAyBE,GAC3B,OAAOF,EAAyBE,GAAUC,QAG3C,IAAIC,EAASJ,EAAyBE,GAAY,CAGjDC,QAAS,IAOV,OAHAE,EAAoBH,GAAUI,KAAKF,EAAOD,QAASC,EAAQA,EAAOD,QAASF,GAGpEG,EAAOD,QCjBRF,CAAoB,M","file":"objectbox.js","sourcesContent":["export * from './lib/number';\n","/**\n * Multiplies a value by 2. (Also a full example of TypeDoc's functionality.)\n *\n * ### Example (es module)\n * ```js\n * import { double } from 'typescript-starter'\n * console.log(double(4))\n * // => 8\n * ```\n *\n * ### Example (commonjs)\n * ```js\n * var double = require('typescript-starter').double;\n * console.log(double(4))\n * // => 8\n * ```\n *\n * @param value - Comment describing the `value` parameter.\n * @returns Comment describing the return type.\n * @anotherNote Some other value.\n */\nexport const double = (value: number) => {\n return value * 2;\n};\n\n/**\n * Raise the value of the first parameter to the power of the second using the\n * es7 exponentiation operator (`**`).\n *\n * ### Example (es module)\n * ```js\n * import { power } from 'typescript-starter'\n * console.log(power(2,3))\n * // => 8\n * ```\n *\n * ### Example (commonjs)\n * ```js\n * var power = require('typescript-starter').power;\n * console.log(power(2,3))\n * // => 8\n * ```\n * @param base - the base to exponentiate\n * @param exponent - the power to which to raise the base\n */\nexport const power = (base: number, exponent: number) => {\n /**\n * This es7 exponentiation operator is transpiled by TypeScript\n */\n return base ** exponent;\n};\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tif(__webpack_module_cache__[moduleId]) {\n\t\treturn __webpack_module_cache__[moduleId].exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// module exports must be returned from runtime so entry inlining is disabled\n// startup\n// Load entry module and return exports\nreturn __webpack_require__(607);\n"],"sourceRoot":""} \ No newline at end of file diff --git a/objectbox/lib/src/web/number.dart b/objectbox/lib/src/web/number.dart index 5c5a842b..489c0bf4 100644 --- a/objectbox/lib/src/web/number.dart +++ b/objectbox/lib/src/web/number.dart @@ -1,4 +1,4 @@ -@JS() +@JS('objectbox') library number; import "package:js/js.dart"; diff --git a/objectbox/pubspec.yaml b/objectbox/pubspec.yaml index 5f60e46f..4b6acba5 100644 --- a/objectbox/pubspec.yaml +++ b/objectbox/pubspec.yaml @@ -16,6 +16,7 @@ dependencies: ffi: ^1.0.0 meta: ^1.3.0 path: ^1.8.0 + js: ^0.6.3 dev_dependencies: build_runner: ^2.0.0