feat/remove-native-modules-imports
Timothée Rebours 10 months ago
parent f906b0082e
commit 7c3e6ab188
  1. 1
      .gitignore
  2. 1
      __mocks__/@react-native-async-storage/async-storage.cjs
  3. 3
      __mocks__/@react-native-async-storage/async-storage.js
  4. 6
      index.d.ts
  5. 5
      jest.config.js
  6. 4
      karma.conf.local.cjs
  7. 6
      karma.conf.template.cjs
  8. 3605
      package-lock.json
  9. 26
      package.json
  10. 2
      src/browser/storage.react-native.js
  11. 6
      test/react-native/persistence.test.cjs
  12. 3
      test/react-native/resolver.cjs
  13. 3
      test/react-native/resolver.js
  14. 3
      test/utils.test.js
  15. 16
      webpack.config.js

1
.gitignore vendored

@ -27,3 +27,4 @@ browser-version/node_modules
test-results test-results
typings-tests.js typings-tests.js
cjs/* cjs/*
dist/*

@ -0,0 +1 @@
module.exports.default = require('@react-native-async-storage/async-storage/jest/async-storage-mock')

@ -1,3 +0,0 @@
import generated from '@react-native-async-storage/async-storage/jest/async-storage-mock'
export default generated

6
index.d.ts vendored

@ -10,16 +10,14 @@
/// <reference types="node" /> /// <reference types="node" />
import { EventEmitter } from "events";
export default Nedb; export default Nedb;
export type Document<Schema> = Schema & { export type Document<Schema> = Schema & {
_id: string; _id: string;
}; };
declare class Nedb<Schema = Record<string, any>> extends EventEmitter { declare class Nedb<Schema = Record<string, any>> {
constructor(pathOrOptions?: string | Nedb.DataStoreOptions); constructor(pathOrOptions?: Nedb.DataStoreOptions);
persistence: Nedb.Persistence; persistence: Nedb.Persistence;

@ -0,0 +1,5 @@
export default {
resolver: '<rootDir>/test/react-native/resolver.cjs',
testEnvironment: 'node',
testMatch: [ '**/*.test.cjs' ]
}

@ -1,8 +1,8 @@
'use strict' 'use strict'
import template from './karma.conf.template.js' const template = require('./karma.conf.template.cjs')
export default function (config) { module.exports = function (config) {
const localBrowser = { const localBrowser = {
ChromeHeadlessNoSandbox: { ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless', base: 'ChromeHeadless',

@ -1,6 +1,6 @@
'use strict' 'use strict'
export default (config) => ({ module.exports = (config) => ({
// Increase timeout in case connection in CI is slow // Increase timeout in case connection in CI is slow
captureTimeout: 120000, captureTimeout: 120000,
browserNoActivityTimeout: 300000, browserNoActivityTimeout: 300000,
@ -14,8 +14,8 @@ export default (config) => ({
// list of files / patterns to load in the browser // list of files / patterns to load in the browser
files: [ files: [
'node_modules/localforage/dist/localforage.min.js', 'node_modules/localforage/dist/localforage.min.js',
'testutils.min.js', 'dist/testutils.min.js',
'nedb.min.js', 'dist/nedb.min.js',
'test/browser/nedb-browser.spec.js', 'test/browser/nedb-browser.spec.js',
'test/browser/load.spec.js' 'test/browser/load.spec.js'
], ],

3605
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -2,8 +2,8 @@
"name": "@seald-io/nedb", "name": "@seald-io/nedb",
"version": "4.0.4", "version": "4.0.4",
"files": [ "files": [
"lib/**/*.js", "src/**/*.js",
"browser-version/**/*.js", "dist/**/*.js",
"index.js", "index.js",
"index.d.ts" "index.d.ts"
], ],
@ -54,7 +54,7 @@
"@types/jest": "^27.5.2", "@types/jest": "^27.5.2",
"browser-resolve": "^2.0.0", "browser-resolve": "^2.0.0",
"chai": "^4.3.7", "chai": "^4.3.7",
"jest": "^27.5.1", "jest": "^29.7.0",
"jsdoc-to-markdown": "^8.0.0", "jsdoc-to-markdown": "^8.0.0",
"karma": "^6.4.1", "karma": "^6.4.1",
"karma-chai": "^0.1.0", "karma-chai": "^0.1.0",
@ -74,7 +74,7 @@
"standard": "^17.0.0", "standard": "^17.0.0",
"terser-webpack-plugin": "^5.3.6", "terser-webpack-plugin": "^5.3.6",
"timers-browserify": "^2.0.12", "timers-browserify": "^2.0.12",
"ts-jest": "^27.1.5", "ts-jest": "^29.1.2",
"typescript": "^4.9.5", "typescript": "^4.9.5",
"util": "^0.12.5", "util": "^0.12.5",
"webpack": "^5.75.0", "webpack": "^5.75.0",
@ -85,13 +85,14 @@
"lint": "standard", "lint": "standard",
"pretest": "npm run build:cjs", "pretest": "npm run build:cjs",
"test": "mocha --reporter spec --timeout 10000", "test": "mocha --reporter spec --timeout 10000",
"build:cjs": "rollup --preserveModules src/* src/browser/* --dir cjs --entryFileNames \"[name].cjs\" --format cjs --preserveEntrySignatures strict", "build:cjs": "rollup --preserveModules src/* src/browser/* --dir cjs --entryFileNames \"[name].cjs\" --format cjs --preserveEntrySignatures strict --interop compat",
"build:browser": "webpack --config-name Nedb && webpack --config-name Nedb --env minimize && webpack --config-name testUtils --env minimize", "build:browser": "webpack --config-name Nedb && webpack --config-name Nedb --env minimize && webpack --config-name testUtils --env minimize",
"pretest:browser": "npm run build:browser", "pretest:browser": "npm run build:browser",
"test:browser": "xvfb-maybe karma start karma.conf.local.js", "test:browser": "xvfb-maybe karma start karma.conf.local.cjs",
"pretest:react-native": "npm run build:cjs",
"test:react-native": "jest test/react-native", "test:react-native": "jest test/react-native",
"test:typings": "tsc ./typings-tests.ts", "test:typings": "tsc ./typings-tests.ts",
"prepublishOnly": "npm run build:browser", "prepublishOnly": "npm run build:browser && npm run build:cjs",
"generateDocs:markdown": "jsdoc2md --no-cache -c jsdoc.conf.js --param-list-format list --files ./lib/*.js > API.md" "generateDocs:markdown": "jsdoc2md --no-cache -c jsdoc.conf.js --param-list-format list --files ./lib/*.js > API.md"
}, },
"main": "cjs/datastore.cjs", "main": "cjs/datastore.cjs",
@ -100,9 +101,9 @@
"./src/customUtils.js": "./src/browser/customUtils.js", "./src/customUtils.js": "./src/browser/customUtils.js",
"./src/storage.js": "./src/browser/storage.browser.js", "./src/storage.js": "./src/browser/storage.browser.js",
"./src/byline.js": "./src/browser/byline.js", "./src/byline.js": "./src/browser/byline.js",
"./cjs/customUtils.cjs": "./src/browser/customUtils.cjs", "./cjs/customUtils.cjs": "./cjs/browser/customUtils.cjs",
"./cjs/storage.cjs": "./src/browser/storage.browser.cjs", "./cjs/storage.cjs": "./cjs/browser/storage.browser.cjs",
"./cjs/byline.cjs": "./src/browser/byline.cjs" "./cjs/byline.cjs": "./cjs/browser/byline.cjs"
}, },
"react-native": { "react-native": {
"./src/customUtils.js": "./src/browser/customUtils.js", "./src/customUtils.js": "./src/browser/customUtils.js",
@ -121,10 +122,5 @@
"cjs", "cjs",
"**/*.ts" "**/*.ts"
] ]
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"resolver": "<rootDir>/test/react-native/resolver.js"
} }
} }

@ -122,7 +122,7 @@ const appendFileAsync = async (filename, toAppend, options) => {
const contents = (await AsyncStorage.getItem(filename)) || '' const contents = (await AsyncStorage.getItem(filename)) || ''
await AsyncStorage.setItem(filename, contents + toAppend) await AsyncStorage.setItem(filename, contents + toAppend)
} catch (error) { } catch (error) {
console.warn('An error happened appending to file writing, skip') console.warn('An error happened appending to file writing, skip', error)
} }
} }

@ -1,8 +1,8 @@
/* eslint-env jest */ /* eslint-env jest */
// Forked from https://github.com/antoniopresto/react-native-local-mongodb/blob/93acbc8a9aaca86aed1d632855cd8b984501147b/test/persistence.test.js // Forked from https://github.com/antoniopresto/react-native-local-mongodb/blob/93acbc8a9aaca86aed1d632855cd8b984501147b/test/persistence.test.js
import { promisify } from 'util' const { promisify } = require('util')
import AsyncStorage from '@react-native-async-storage/async-storage' const AsyncStorage = require('@react-native-async-storage/async-storage').default
import DataStore from '../../' const DataStore = require('../../')
const getDb = async () => { const getDb = async () => {
await AsyncStorage.clear() await AsyncStorage.clear()

@ -0,0 +1,3 @@
const browserResolve = require('browser-resolve')
module.exports = (id, opts) => browserResolve.sync(id, { ...opts, browser: 'react-native' })

@ -1,3 +0,0 @@
import browserResolve from 'browser-resolve'
export default (id, opts) => browserResolve.sync(id, { ...opts, browser: 'react-native' })

@ -39,5 +39,6 @@ export {
apply, apply,
waterfall, waterfall,
each, each,
wait wait,
_callbackify as callbackify
} }

@ -18,19 +18,13 @@ export default (env, argv) => {
minimize minimize
}, },
output: { output: {
path: __dirname, path: join(__dirname, 'dist'),
filename: pathData => `${pathData.chunk.name.toLowerCase()}${minimize ? '.min' : ''}.js`, filename: pathData => `${pathData.chunk.name.toLowerCase()}${minimize ? '.min' : ''}.js`,
libraryTarget: 'window', libraryTarget: 'window',
library: '[name]' library: '[name]'
} }
} }
const pluginsNedb = [
new webpack.NormalModuleReplacementPlugin(new RegExp(resolve(__dirname, 'src/storage.js')), resolve(__dirname, 'src/browser/storage.browser.js')),
new webpack.NormalModuleReplacementPlugin(new RegExp(resolve(__dirname, 'src/customUtils.js')), resolve(__dirname, 'src/browser/customUtils.js')),
new webpack.NormalModuleReplacementPlugin(new RegExp(resolve(__dirname, 'src/byline.js')), resolve(__dirname, 'src/browser/byline.js'))
]
const polyfillPlugins = [ const polyfillPlugins = [
new webpack.ProvidePlugin({ new webpack.ProvidePlugin({
process: 'process/browser', process: 'process/browser',
@ -43,8 +37,14 @@ export default (env, argv) => {
return [ return [
{ {
...baseConfig, ...baseConfig,
output: {
...baseConfig.output,
libraryExport: 'default'
},
name: 'Nedb', name: 'Nedb',
plugins: pluginsNedb, resolve: {
aliasFields: ['browser'], // to use files referenced in the package.json
},
entry: { entry: {
Nedb: join(__dirname, 'src', 'datastore.js') Nedb: join(__dirname, 'src', 'datastore.js')
} }

Loading…
Cancel
Save