Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support Dynamic Import in Webpack Context #27

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 6
- 22
- 20
- 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
4 changes: 2 additions & 2 deletions heapdump.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const heapdump = require('heapdump'); // eslint-disable-line import/no-unresolve
const importFresh = require('.');

for (let i = 0; i < 100000; i++) {
require('./fixture.js')();
require('./fixture')();
}

heapdump.writeSnapshot(`require-${Date.now()}.heapsnapshot`);

for (let i = 0; i < 100000; i++) {
importFresh('./fixture.js')();
importFresh('./fixture')();
}

heapdump.writeSnapshot(`import-fresh-${Date.now()}.heapsnapshot`);
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const path = require('path');
const resolveFrom = require('resolve-from');
const parentModule = require('parent-module');

// Use plain require in webpack context for dynamic import
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require; // eslint-disable-line camelcase,no-undef

module.exports = moduleId => {
if (typeof moduleId !== 'string') {
throw new TypeError('Expected a string');
Expand All @@ -13,7 +16,7 @@ module.exports = moduleId => {
const cwd = parentPath ? path.dirname(parentPath) : __dirname;
const filePath = resolveFrom(cwd, moduleId);

const oldModule = require.cache[filePath];
const oldModule = requireFunc.cache[filePath];
// Delete itself from module parent
if (oldModule && oldModule.parent) {
let i = oldModule.parent.children.length;
Expand All @@ -25,9 +28,9 @@ module.exports = moduleId => {
}
}

delete require.cache[filePath]; // Delete module from cache
delete requireFunc.cache[filePath]; // Delete module from cache

const parent = require.cache[parentPath]; // If `filePath` and `parentPath` are the same, cache will already be deleted so we won't get a memory leak in next step
const parent = requireFunc.cache[parentPath]; // If `filePath` and `parentPath` are the same, cache will already be deleted so we won't get a memory leak in next step

return parent === undefined ? require(filePath) : parent.require(filePath); // In case cache doesn't have parent, fall back to normal require
return parent === undefined ? requireFunc(filePath) : parent.require(filePath); // In case cache doesn't have parent, fall back to normal require
};
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"sideEffects": false,
"engines": {
"node": ">=6"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd",
Expand All @@ -36,13 +36,13 @@
"bypass"
],
"dependencies": {
"parent-module": "^1.0.0",
"resolve-from": "^4.0.0"
"parent-module": "^2.0.0",
"resolve-from": "^5.0.0"
},
"devDependencies": {
"ava": "^1.0.1",
"heapdump": "^0.3.12",
"tsd": "^0.7.3",
"tsd": "^0.31.0",
"xo": "^0.23.0"
}
}