Skip to content

Commit

Permalink
optimize(core): loader analysis compatible with next.js module logic (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
easy1090 authored Feb 21, 2024
1 parent 6901930 commit 9677ac3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-walls-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rsdoctor/core': patch
---

optimize(core): loader analysis compatible with next.js module logic
51 changes: 30 additions & 21 deletions packages/core/src/inner-plugins/plugins/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,37 @@ export class InternalLoaderPlugin<
});

const newLoaders = cloneDeep(originLoaders);
const proxyModule = new Proxy(module, {
get(target, p, receiver) {
if (p === 'loaders') return newLoaders;
return Reflect.get(target, p, receiver);
},
set(target, p, newValue, receiver) {
const _newValue = cloneDeep(newValue);
if (p === 'loaders') {
if (Array.isArray(_newValue)) {
newLoaders.length = 0;
_newValue.forEach((e) => {
newLoaders.push(e);
});
if (
typeof compiler.options.cache === 'object' &&
'version' in compiler.options.cache &&
typeof compiler.options.cache.version === 'string' &&
compiler.options.cache.version.indexOf('next/dist/build') > -1
) {
callback(loaderContext, module);
} else {
const proxyModule = new Proxy(module, {
get(target, p, receiver) {
if (p === 'loaders') return newLoaders;
return Reflect.get(target, p, receiver);
},
set(target, p, newValue, receiver) {
const _newValue = cloneDeep(newValue);
if (p === 'loaders') {
if (Array.isArray(_newValue)) {
newLoaders.length = 0;
_newValue.forEach((e) => {
newLoaders.push(e);
});
}
}
}
return Reflect.set(target, p, _newValue, receiver);
},
deleteProperty(target, p) {
return Reflect.deleteProperty(target, p);
},
});
callback(loaderContext, proxyModule);
return Reflect.set(target, p, _newValue, receiver);
},
deleteProperty(target, p) {
return Reflect.deleteProperty(target, p);
},
});
callback(loaderContext, proxyModule);
}

// loaders are overwrite when originLoader is not same with newLoaders
if (!isEqual(originLoaders, newLoaders)) {
Expand Down

0 comments on commit 9677ac3

Please sign in to comment.