@cocreate/mongodb 1.7.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -58,8 +58,7 @@ jobs:
58
58
  run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
59
59
  - name: Install dependencies
60
60
  run: yarn install
61
- - name: Build
62
- run: yarn build
61
+
63
62
  - name: Set Environment Variables
64
63
  run: |
65
64
  echo "organization_id=${{ secrets.COCREATE_ORGANIZATION_ID }}" >> $GITHUB_ENV
package/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ # [1.8.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.7.2...v1.8.0) (2023-09-07)
2
+
3
+
4
+ ### Features
5
+
6
+ * support $push, $delete, $pop, $pull methods on arrays. ([06bf984](https://github.com/CoCreate-app/CoCreate-mongodb/commit/06bf9844863885d2cf9c6cc9591500becb105f51))
7
+ * Use cursor when filtering through documents, increasing performance and enabeling ability to process very large data sets ([376c63f](https://github.com/CoCreate-app/CoCreate-mongodb/commit/376c63fa461221f71811032528d04c4b8235241e))
8
+
9
+ ## [1.7.2](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.7.1...v1.7.2) (2023-08-21)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * removed webpack.config and build step as no webpack.config exist ([2f15aff](https://github.com/CoCreate-app/CoCreate-mongodb/commit/2f15aff759bc500691359f708fcb24264d312470))
15
+
1
16
  ## [1.7.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.7.0...v1.7.1) (2023-08-21)
2
17
 
3
18
 
@@ -1,25 +1,23 @@
1
- module.exports = {
2
- "organization_id": "",
3
- "key": "",
4
- "host": "",
5
-
6
- "sources": [{
7
- "array": "files",
8
- "object": {
9
- "_id": "60145dc49f64ba1680b86693",
10
- "name": "index.html",
11
- "path": "/docs/mongodb/index.html",
12
- "src": "{{./docs/index.html}}",
13
- "host": [
14
- "cocreate.app",
15
- "general.cocreate.app"
16
- ],
17
- "directory": "/docs/mongodb",
18
- "content-type": "text/html",
19
- "public": "true",
20
- "website_id": "5ffbceb7f11d2d00103c4535"
21
- }
22
- }
23
- ]
24
- }
25
-
1
+ module.exports = {
2
+ "organization_id": "",
3
+ "key": "",
4
+ "host": "",
5
+ "sources": [
6
+ {
7
+ "array": "files",
8
+ "object": {
9
+ "_id": "60145dc49f64ba1680b86693",
10
+ "name": "index.html",
11
+ "path": "/docs/mongodb/index.html",
12
+ "src": "{{./docs/index.html}}",
13
+ "host": [
14
+ "*"
15
+ ],
16
+ "directory": "/docs/mongodb",
17
+ "content-type": "text/html",
18
+ "public": "true",
19
+ "website_id": "5ffbceb7f11d2d00103c4535"
20
+ }
21
+ }
22
+ ]
23
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.7.1",
3
+ "version": "1.8.0",
4
4
  "description": "A simple mongodb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "mongodb",
package/src/index.js CHANGED
@@ -131,7 +131,7 @@ function array(action, data) {
131
131
 
132
132
  if (action == 'readCollection') {
133
133
 
134
- let { query, sort } = getFilters(data);
134
+ let { query, sort } = await getFilters(data);
135
135
 
136
136
  db.listCollections().toArray(function (error, result) {
137
137
  if (error)
@@ -277,15 +277,9 @@ function object(action, data) {
277
277
  if (data.request)
278
278
  data[type] = data.request
279
279
 
280
-
281
280
  if (!data['timeStamp'])
282
281
  data['timeStamp'] = new Date().toISOString()
283
282
 
284
-
285
- let isFilter
286
- if (data.filter && data.filter.query)
287
- isFilter = true
288
-
289
283
  let databases = data.database;
290
284
  if (!Array.isArray(databases))
291
285
  databases = [databases]
@@ -301,60 +295,22 @@ function object(action, data) {
301
295
  const db = client.db(database);
302
296
  const arrayObj = db.collection(array);
303
297
 
304
- let { query, sort } = getFilters(data);
305
- if (data['organization_id']) {
306
- query['organization_id'] = { $eq: data['organization_id'] }
307
- }
308
-
309
- let _ids = []
310
- let update_ids = []
311
- let updateData = {}
298
+ if (data[type] && !Array.isArray(data[type]))
299
+ data[type] = [data[type]]
312
300
 
313
- if (data[type]) {
314
- if (!Array.isArray(data[type]))
315
- data[type] = [data[type]]
301
+ if (action == 'createObject') {
316
302
  for (let i = 0; i < data[type].length; i++) {
317
303
  data[type][i] = replaceArray(data[type][i])
304
+ data[type][i] = dotNotationToObject(data[type][i])
318
305
  data[type][i]['organization_id'] = data['organization_id'];
319
306
 
320
-
321
- if (action == 'createObject') {
322
- data[type][i] = dotNotationToObject(data[type][i])
323
-
324
- if (!data[type][i]._id)
325
- data[type][i]._id = ObjectId()
326
- else
327
- data[type][i]._id = ObjectId(data[type][i]._id)
328
- data[type][i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
329
- }
330
- if (action == 'readObject' && data[type][i]._id) {
331
- _ids.push(ObjectId(data[type][i]._id))
332
- }
333
- if (action == 'updateObject') {
334
- if (data[type][i]._id)
335
- update_ids.push({ _id: data[type][i]._id, updateDoc: data[type][i], updateType: '_id' })
336
-
337
- if (!data[type][i]._id)
338
- updateData = createUpdate({ object: [data[type][i]] }, type)
339
-
340
- data[type][i]['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
341
-
342
- }
343
- if (action == 'deleteObject') {
344
- if (data[type][i]._id) {
345
- _ids.push(ObjectId(data[type][i]._id))
346
- documents.push({ _id: data[type][i]._id, storage: data.storageName, database, array })
347
- }
348
- }
307
+ if (!data[type][i]._id)
308
+ data[type][i]._id = ObjectId()
309
+ else
310
+ data[type][i]._id = ObjectId(data[type][i]._id)
311
+ data[type][i]['created'] = { on: data.timeStamp, by: data.user_id || data.clientId }
349
312
  }
350
- if (_ids.length == 1)
351
- query['_id'] = ObjectId(_ids[0])
352
- else if (_ids.length > 0)
353
- query['_id'] = { $in: _ids }
354
- }
355
-
356
313
 
357
- if (action == 'createObject') {
358
314
  arrayObj.insertMany(data[type], function (error, result) {
359
315
  if (error)
360
316
  errorHandler(data, error, database, array)
@@ -371,58 +327,63 @@ function object(action, data) {
371
327
  resolve(data)
372
328
  }
373
329
  });
374
- }
330
+ } else {
331
+ let isFilter
332
+ if (data.filter && data.filter.query)
333
+ isFilter = true
334
+
335
+ let { query, sort, index, limit } = await getFilters(data, arrayObj);
336
+
337
+ if (action == 'readObject') {
338
+ if (data[type]) {
339
+ let _ids = []
340
+ for (let i = 0; i < data[type].length; i++) {
341
+ data[type][i] = replaceArray(data[type][i])
342
+ if (data[type][i]._id) {
343
+ _ids.push(ObjectId(data[type][i]._id))
344
+ }
345
+ }
375
346
 
376
- if (action == 'readObject') {
377
- let index = 0, limit = 0
378
- if (data.filter) {
379
- const count = await arrayObj.estimatedDocumentCount()
380
- data.filter.count = count
381
-
382
- if (data.filter.index)
383
- index = data.filter.index
384
- if (data.filter.limit)
385
- limit = data.filter.limit
386
- if (limit)
387
- limit = index + limit;
388
- }
347
+ if (_ids.length == 1)
348
+ query['_id'] = _ids[0]
349
+ else if (_ids.length > 0)
350
+ query['_id'] = { $in: _ids }
389
351
 
390
- arrayObj.find(query).skip(index).limit(limit).sort(sort).toArray(function (error, result) {
391
- if (error)
392
- errorHandler(data, error, database, array)
352
+ }
353
+
354
+ try {
355
+ const cursor = arrayObj.find(query).sort(sort).skip(index).limit(limit);
356
+ while (await cursor.hasNext()) {
357
+ const document = await cursor.next();
393
358
 
394
- if (result) {
395
- // TODO: forEach at cursor
396
- for (let doc of result) {
397
- let isMatch = true
398
- if (data.filter && data.filter['search'])
399
- isMatch = searchData(doc, data.filter['search'])
400
- if (isMatch) {
401
- doc.storage = 'mongodb'
402
- doc.database = database
403
- doc.array = array
404
- doc._id = doc._id.toString()
405
-
406
- if (data.returnObject == false) {
407
- let tempDoc = {};
408
- let docs = new Map(data[type].map((obj) => [obj._id, obj]));
409
- let doc1 = docs.get(doc._id)
410
- if (doc1) {
411
- tempDoc._id = tempDoc
412
- for (let key of Object.keys(doc1)) {
413
- tempDoc[key] = doc[key]
414
- }
415
- doc = tempDoc
359
+ if (data.filter && data.filter.search) {
360
+ let isMatch = searchData(document, data.filter.search)
361
+ if (!isMatch)
362
+ continue;
363
+ }
364
+
365
+ document.storage = 'mongodb'
366
+ document.database = database
367
+ document.array = array
368
+ document._id = document._id.toString()
369
+
370
+ if (data.returnObject == false) {
371
+ let tempDoc = {};
372
+ let docs = new Map(data[type].map((obj) => [obj._id, obj]));
373
+ let doc1 = docs.get(document._id)
374
+ if (doc1) {
375
+ tempDoc._id = tempDoc
376
+ for (let key of Object.keys(doc1)) {
377
+ tempDoc[key] = document[key]
416
378
  }
379
+ document = tempDoc
417
380
  }
418
-
419
- documents.push(doc)
420
381
  }
421
- }
422
382
 
423
- // if (index && limit) {
424
- // documents = documents.slice(index, limit)
425
- // }
383
+ documents.push(document)
384
+ }
385
+ } catch (error) {
386
+ errorHandler(data, error, database, array)
426
387
  }
427
388
 
428
389
  arraysLength -= 1
@@ -433,121 +394,111 @@ function object(action, data) {
433
394
  data = createData(data, documents, type)
434
395
  resolve(data)
435
396
  }
436
- });
437
- }
397
+ } else if (action == 'updateObject') {
438
398
 
439
- if (action == 'updateObject' || action == 'deleteObject') {
440
- const queryDocs = () => {
441
- return new Promise(async (resolve, reject) => {
399
+ if (!data[type].length) {
400
+ arraysLength -= 1
442
401
 
443
- arrayObj.find(query).sort(sort).toArray(function (error, result) {
444
- if (error)
445
- errorHandler(data, error, database, array)
402
+ if (!arraysLength)
403
+ databasesLength -= 1
446
404
 
447
- if (data.filter && data.filter.search) {
448
- let searchResult = []
405
+ if (!databasesLength && !arraysLength) {
406
+ data = createData(data, documents, type)
407
+ resolve(data)
408
+ }
409
+ }
449
410
 
450
- for (let doc of result) {
451
- let isMatch = searchData(doc, data.filter.search)
452
- if (isMatch)
453
- searchResult.push(doc)
411
+ let docsLength = data[type].length
412
+ for (let i = 0; i < data[type].length; i++) {
413
+ if (data[type][i]._id)
414
+ query['_id'] = ObjectId(data[type][i]._id)
415
+
416
+ data[type][i] = replaceArray(data[type][i])
417
+ let { update, projection } = createUpdate(data, type, i)
418
+
419
+ if (query['_id']) {
420
+ arrayObj.updateOne(query, update, {
421
+ upsert: data.upsert,
422
+ projection
423
+ }).then((result) => {
424
+ documents.push({ _id: query['_id'].toString(), storage: data.storageName, database, array, ...update['$set'] })
425
+ }).catch((error) => {
426
+ errorHandler(data, error, database, array)
427
+ console.log(action, 'error', error);
428
+ }).finally(() => {
429
+ docsLength -= 1
430
+ if (!docsLength)
431
+ arraysLength -= 1
432
+
433
+ if (!arraysLength)
434
+ databasesLength -= 1
435
+
436
+ if (!databasesLength && !arraysLength) {
437
+ data = createData(data, documents, type)
438
+ resolve(data)
439
+ }
440
+ })
441
+ } else if (update && isFilter) {
442
+ const cursor = arrayObj.find(query).sort(sort).skip(index).limit(limit);
443
+ while (await cursor.hasNext()) {
444
+ const document = await cursor.next();
445
+
446
+ if (data.filter && data.filter.search) {
447
+ let isMatch = searchData(document, data.filter.search)
448
+ if (!isMatch)
449
+ continue;
454
450
  }
455
- result = searchResult
456
- }
457
- resolve(result)
458
- })
459
- }, (err) => {
460
- console.log(err);
461
- });
462
- }
463
-
464
- let Result, $update, update, projection;
465
-
466
- if (isFilter && data.returnObject != false)
467
- if (action == 'deleteObject' || action == 'updateObject' && updateData.update)
468
- Result = await queryDocs()
469
-
470
- if (Result) {
471
- for (let doc of Result) {
472
- if (action == 'deleteObject')
473
- documents.push({ _id: doc._id, storage: data.storageName, database, array })
474
- else
475
- doc['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
476
-
477
- _ids.push(doc._id)
478
- }
479
- update_ids.push({ updateType: 'filter' })
480
- }
481
451
 
482
- if (action == 'updateObject') {
483
- let docsLength = update_ids.length
484
- for (let { updateDoc, updateType } of update_ids) {
485
-
486
- if (updateType == '_id') {
487
- let update_id = updateDoc._id
488
- query['_id'] = ObjectId(update_id)
489
- $update = createUpdate({ object: [updateDoc] }, type)
490
- update = $update.update
491
- projection = $update.projection
492
- documents.push({ _id: update_id, storage: data.storageName, database, array, ...update['$set'] })
452
+ arrayObj.updateOne({ _id: document._id }, update, {
453
+ upsert: data.upsert,
454
+ projection
455
+ }).then((result) => {
456
+ if (data.returnObject != false)
457
+ documents.push({ _id: document._id, storage: data.storageName, database, array, ...update['$set'] })
458
+ }).catch((error) => {
459
+ errorHandler(data, error, database, array)
460
+ console.log(action, 'error', error);
461
+ })
462
+ }
493
463
  }
464
+ }
465
+ } else if (action == 'deleteObject') {
466
+ if (isFilter && data.returnObject != false) {
467
+ const cursor = arrayObj.find(query).sort(sort).skip(index).limit(limit);
468
+ while (await cursor.hasNext()) {
469
+ const document = await cursor.next();
494
470
 
495
- if (updateType == 'filter') {
496
- query['_id'] = { $in: _ids }
497
- $update = updateData
498
- update = $update.update
499
- projection = $update.projection
500
- for (let _id of _ids)
501
- documents.push({ _id, storage: data.storageName, database, array, ...update['$set'] })
471
+ if (data.filter && data.filter.search) {
472
+ let isMatch = searchData(document, data.filter.search)
473
+ if (!isMatch)
474
+ continue;
475
+ }
502
476
 
477
+ arrayObj.deleteOne({
478
+ _id: document._id
479
+ }).then((result) => {
480
+ documents.push({ _id: document._id, storage: data.storageName, database, array })
481
+ }).catch((error) => {
482
+ errorHandler(data, error, database, array)
483
+ console.log(action, 'error', error);
484
+ });
503
485
  }
504
486
 
505
- update['$set']['organization_id'] = data.organization_id
506
-
507
- arrayObj.updateMany(query, update, {
508
- upsert: data.upsert,
509
- projection
510
- }).then((result) => {
511
-
512
- }).catch((error) => {
513
- errorHandler(data, error, database, array)
514
- console.log(action, 'error', error);
515
- }).finally((error) => {
516
- docsLength -= 1
517
- if (!docsLength)
518
- arraysLength -= 1
519
-
520
- if (!arraysLength)
521
- databasesLength -= 1
522
-
523
- if (!databasesLength && !arraysLength) {
524
- data = createData(data, documents, type)
525
- resolve(data)
526
- }
527
- })
528
487
  }
529
488
 
530
- if (!update_ids.length) {
531
- docsLength -= 1
532
- if (!docsLength)
533
- arraysLength -= 1
534
-
535
- if (!arraysLength)
536
- databasesLength -= 1
537
-
538
- if (!databasesLength && !arraysLength) {
539
- data = createData(data, documents, type)
540
- resolve(data)
489
+ let _ids = [];
490
+ for (let i = 0; i < data[type].length; i++) {
491
+ if (data[type][i]._id) {
492
+ _ids.push(ObjectId(data[type][i]._id))
493
+ documents.push({ _id: data[type][i]._id, storage: data.storageName, database, array })
541
494
  }
542
495
  }
543
496
 
544
- }
545
-
546
- if (action == 'deleteObject') {
547
497
  if (_ids.length == 1)
548
- query['_id'] = ObjectId(_ids[0])
549
- else if (_ids.length > 0)
498
+ query['_id'] = _ids[0]
499
+ else if (_ids.length)
550
500
  query['_id'] = { $in: _ids }
501
+
551
502
  arrayObj.deleteMany(query, function (error, result) {
552
503
  arraysLength -= 1
553
504
  if (!arraysLength)
@@ -560,7 +511,6 @@ function object(action, data) {
560
511
 
561
512
  })
562
513
  }
563
-
564
514
  }
565
515
 
566
516
  }
@@ -577,29 +527,81 @@ function object(action, data) {
577
527
 
578
528
  }
579
529
 
580
- function createUpdate(data, type) {
530
+ function createUpdate(data, type, index) {
581
531
  let update = {}, projection = {};
582
- if (data[type][0]) {
583
- update['$set'] = data[type][0]
584
- // update['$set']['organization_id'] = data['organization_id'];
532
+ if (data[type][index]) {
533
+ update['$set'] = data[type][index]
534
+ update['$set']['modified'] = { on: data.timeStamp, by: data.user_id || data.clientId }
535
+ update['$set']['organization_id'] = data.organization_id
536
+
585
537
  if (update['$set']['_id'])
586
538
  delete update['$set']['_id']
539
+
587
540
  Object.keys(update['$set']).forEach(key => {
541
+ if (key.includes('$rename') || key.includes('$update')) {
588
542
 
589
- if (key.includes('[u]')) {
590
- update['$addToSet'] = { [key.replace('[u]', '')]: update['$set'][key] }
543
+ } else if (key.includes('$delete')) {
544
+
545
+ } else if (key.includes('$inc')) {
546
+ update['$inc'] = update['$set'][key]
591
547
  delete update['$set'][key]
592
- } else if (key.includes('[]')) {
593
- if (!Array.isArray(update['$set'][key]))
594
- update['$set'][key] = [update['$set'][key]]
548
+ } else if (key.endsWith(']')) {
549
+ // Use a regular expression to extract the key and index (including a potentially undefined or keyword index)
550
+ const regex = /^(.*(?:\[\d+\].*?)?)\[(.*?)\](?:\[\])?$/;
551
+ const match = inputString.match(regex);
552
+ const index = parseInt(match[2], 10);
553
+
554
+ if (index && index !== -1 && index !== 1 && update['$set'][key] === '$delete') {
555
+ match[1] = replaceArray({ [update['$set'][key]]: update['$set'][key] });
556
+ match[1] = Object.keys(match[1])[0]
557
+
558
+ if (!update['$unset'])
559
+ update['$unset'] = {}
560
+
561
+ update.$unset[match[1]] = 1;
562
+
563
+ if (!update['$pull'])
564
+ update['$pull'] = {}
565
+
566
+ update.$pull[match[1]] = null;
567
+ } else {
568
+ match[1] = replaceArray({ [match[1]]: update['$set'][key] });
569
+ match[1] = Object.keys(match[1])[0]
570
+
571
+ if (update['$set'][key] === '$pop' || update['$set'][key] === '$delete') {
572
+ if (!update['$pop'])
573
+ update['$pop'] = {}
574
+
575
+ update.$pop[match[1]] = index || 1
576
+ } else if (match[2] === '$addToSet') {
577
+ if (!update['$addToSet'])
578
+ update['$addToSet'] = {}
579
+
580
+ update.$addToSet[match[1]] = update['$set'][key]
581
+ } else if (match[2] === '$pull') {
582
+ if (!update['$pull'])
583
+ update['$pull'] = {}
584
+
585
+ update.$pull[match[1]] = update['$set'][key]
586
+ } else {
587
+ if (!Array.isArray(update['$set'][key]))
588
+ update['$set'][key] = [update['$set'][key]]
589
+
590
+ let insert = { $each: update['$set'][key] }
591
+ if (index)
592
+ insert.$postion = match[2]
593
+
594
+ if (!update['$push'])
595
+ update['$push'] = {}
596
+
597
+ update.$push[match[1]] = insert
595
598
 
596
- update['$push'] = {
597
- [key.replace('[]', '')]: { $each: update['$set'][key] }
599
+ }
598
600
  }
601
+
599
602
  delete update['$set'][key]
603
+
600
604
  }
601
- // { $push: { "skills": { $each: ["Sports", "Acting"] } } })
602
- // { $addToSet: { "skills": "GST" } }) // adds "GST"to all arrays if not exist
603
605
  projection[key] = 1
604
606
  })
605
607
  }
@@ -645,42 +647,51 @@ function createData(data, array, type) {
645
647
  return data
646
648
  }
647
649
 
648
- function getFilters(data) {
649
- let query = {}, sort = {}
650
- let filter = {
651
- query: [],
652
- sort: [],
653
- search: {
654
- value: [],
655
- type: "or"
656
- },
657
- index: 0,
658
- ...data.filter
659
- };
660
-
661
- query = createQuery(filter.query);
662
-
663
-
664
- if (filter.sort) {
665
- for (let i = 0; i < filter.sort.length; i++) {
666
- let direction = filter.sort[i].direction
667
- if (direction == 'desc' || direction == -1)
668
- direction = -1;
669
- else
670
- direction = 1;
650
+ async function getFilters(data, arrayObj) {
651
+ let query =
652
+ {}, sort = {}, index = 0, limit = 0, count
653
+ if (data.filter) {
654
+
655
+ if (data.filter.query)
656
+ query = createQuery(data.filter.query);
657
+
658
+
659
+ if (data.filter.sort) {
660
+ for (let i = 0; i < data.filter.sort.length; i++) {
661
+ let direction = data.filter.sort[i].direction
662
+ if (direction == 'desc' || direction == -1)
663
+ direction = -1;
664
+ else
665
+ direction = 1;
671
666
 
672
- sort[filter.sort[i].key] = filter.sort[i].direction
667
+ sort[data.filter.sort[i].key] = direction
668
+ }
669
+ }
670
+
671
+ if (arrayObj) {
672
+ count = await arrayObj.estimatedDocumentCount()
673
+ data.filter.count = count
673
674
  }
675
+
676
+ if (data.filter.index)
677
+ index = data.filter.index
678
+ if (data.filter.limit)
679
+ limit = data.filter.limit
680
+ if (limit)
681
+ limit = index + limit;
674
682
  }
675
- return { query, sort }
683
+
684
+ if (data['organization_id'])
685
+ query['organization_id'] = { $eq: data['organization_id'] }
686
+
687
+ return { query, sort, index, limit, count }
676
688
  }
677
689
 
678
- // TODO: create impved mongodb query to cover many cases
679
- function createQuery(filters) {
690
+ // TODO: improve mongodb query to cover many cases
691
+ function createQuery(queries) {
680
692
  let query = new Object();
681
693
 
682
-
683
- for (let item of filters) {
694
+ for (let item of queries) {
684
695
 
685
696
  if (!item.key)
686
697
  continue
package/webpack.config.js DELETED
@@ -1,90 +0,0 @@
1
- const path = require("path")
2
- const TerserPlugin = require("terser-webpack-plugin")
3
- const MiniCssExtractPlugin = require("mini-css-extract-plugin")
4
- const { CleanWebpackPlugin } = require("clean-webpack-plugin")
5
-
6
- module.exports = (env, argv) => {
7
- let isProduction = false
8
- if (argv.mode === 'production')
9
- isProduction = true
10
-
11
- const config = {
12
- entry: {
13
- "CoCreate-mongodb": "./src/index.js",
14
- },
15
- output: {
16
- path: path.resolve(__dirname, "dist"),
17
- filename: isProduction ? "[name].min.js" : "[name].js",
18
- libraryTarget: "umd",
19
- libraryExport: "default",
20
- library: ["CoCreate", "mongodb"],
21
- globalObject: "this",
22
- },
23
-
24
- plugins: [
25
- new CleanWebpackPlugin(),
26
- new MiniCssExtractPlugin({
27
- filename: "[name].css",
28
- }),
29
- ],
30
- // Default mode for Webpack is production.
31
- // Depending on mode Webpack will apply different things
32
- // on final bundle. For now we don't need production's JavaScript
33
- // minifying and other thing so let's set mode to development
34
- mode: isProduction ? "production" : "development",
35
- module: {
36
- rules: [
37
- {
38
- test: /.js$/,
39
- exclude: /(node_modules)/,
40
- use: {
41
- loader: "babel-loader",
42
- options: {
43
- plugins: ["@babel/plugin-transform-modules-commonjs"],
44
- },
45
- },
46
- },
47
- {
48
- test: /.css$/i,
49
- use: [
50
- { loader: "style-loader", options: { injectType: "linkTag" } },
51
- "file-loader",
52
- ],
53
- },
54
- ],
55
- },
56
-
57
- // add source map
58
- ...(isProduction ? {} : { devtool: "eval-source-map" }),
59
-
60
- optimization: {
61
- minimize: true,
62
- minimizer: [
63
- new TerserPlugin({
64
- extractComments: true,
65
- // cache: true,
66
- parallel: true,
67
- // sourceMap: true, // Must be set to true if using source-maps in production
68
- terserOptions: {
69
- // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
70
- // extractComments: 'all',
71
- compress: {
72
- drop_console: true,
73
- },
74
- },
75
- }),
76
- ],
77
- splitChunks: {
78
- chunks: "all",
79
- minSize: 200,
80
- // maxSize: 99999,
81
- //minChunks: 1,
82
-
83
- cacheGroups: {
84
- defaultVendors: false,
85
- },
86
- },
87
- },
88
- }
89
- return config
90
- }