Skip to content

Commit 126abbf

Browse files
committed
fix: Remove eval() from transaction context loading
- Replace eval('require') with lazy-loaded require() - Cleaner transaction context initialization - No more build warnings - Maintains same functionality for post-commit hooks
1 parent 124d6eb commit 126abbf

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

server/bootstrap/lifecycle.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
'use strict';
22

3-
const transactionCtx = (() => {
4-
try {
5-
const runtimeRequire = eval('require');
6-
return runtimeRequire('@strapi/database/dist/transaction-context').transactionCtx;
7-
} catch (error) {
8-
console.warn('[strapi-plugin-io] Unable to access transaction context:', error.message);
9-
return null;
3+
// Lazy-load transaction context to avoid bundling issues
4+
let transactionCtx = null;
5+
function getTransactionCtx() {
6+
if (!transactionCtx) {
7+
try {
8+
transactionCtx = require('@strapi/database/dist/transaction-context').transactionCtx;
9+
} catch (error) {
10+
console.warn('[strapi-plugin-io] Unable to access transaction context:', error.message);
11+
transactionCtx = { get: () => null, onCommit: () => {} }; // Fallback noop
12+
}
1013
}
11-
})();
14+
return transactionCtx;
15+
}
1216

1317
const { pluginId } = require('../utils/pluginId');
1418

@@ -18,8 +22,9 @@ const { pluginId } = require('../utils/pluginId');
1822
*/
1923
function scheduleAfterTransaction(callback, delay = 0) {
2024
const runner = () => setTimeout(callback, delay);
21-
if (transactionCtx?.get()) {
22-
transactionCtx.onCommit(runner);
25+
const ctx = getTransactionCtx();
26+
if (ctx.get()) {
27+
ctx.onCommit(runner);
2328
} else {
2429
runner();
2530
}

server/src/bootstrap/lifecycle.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
'use strict';
22

3-
const transactionCtx = (() => {
4-
try {
5-
const runtimeRequire = eval('require');
6-
return runtimeRequire('@strapi/database/dist/transaction-context').transactionCtx;
7-
} catch (error) {
8-
console.warn('[strapi-plugin-io] Unable to access transaction context:', error.message);
9-
return null;
3+
// Lazy-load transaction context to avoid bundling issues
4+
let transactionCtx = null;
5+
function getTransactionCtx() {
6+
if (!transactionCtx) {
7+
try {
8+
transactionCtx = require('@strapi/database/dist/transaction-context').transactionCtx;
9+
} catch (error) {
10+
console.warn('[strapi-plugin-io] Unable to access transaction context:', error.message);
11+
transactionCtx = { get: () => null, onCommit: () => {} }; // Fallback noop
12+
}
1013
}
11-
})();
14+
return transactionCtx;
15+
}
1216

1317
function scheduleAfterTransaction(callback, delay = 0) {
1418
const runner = () => setTimeout(callback, delay);
15-
if (transactionCtx?.get()) {
16-
transactionCtx.onCommit(runner);
19+
const ctx = getTransactionCtx();
20+
if (ctx.get()) {
21+
ctx.onCommit(runner);
1722
} else {
1823
runner();
1924
}

0 commit comments

Comments
 (0)