Skip to content

Commit 2ebfa23

Browse files
committed
Remove cjs loader
1 parent fa0b6b6 commit 2ebfa23

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

packages/rspack/src/util/require.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,15 @@ export function nonWebpackRequire(): RequireFn {
2424
}
2525

2626
const loaderCode = data?.toString() || "";
27-
28-
// 1. Assume it's a cjs
27+
// Only customn esm loader is supported.
28+
// Use `import(base64code)` to load ESM
29+
const dataUrl = `data:text/javascript;base64,${btoa(loaderCode)}`;
2930
try {
30-
// Use `new Function` to emulate CJS
31-
const module = { exports: {} };
32-
const exports = module.exports;
33-
const createRequire = () => {
34-
throw new Error(
35-
"@rspack/browser doesn't support `require` in loaders yet"
36-
);
37-
};
38-
39-
// rslint-disable no-implied-eval
40-
const wrapper = new Function(
41-
"module",
42-
"exports",
43-
"require",
44-
loaderCode
45-
);
46-
47-
wrapper(module, exports, createRequire);
48-
resolve(module.exports);
49-
} catch {
50-
// 2. Assume it's an esm
51-
// Use `import(base64code)` to load ESM
52-
const dataUrl = `data:text/javascript;base64,${btoa(loaderCode)}`;
53-
try {
54-
// biome-ignore lint/security/noGlobalEval: use `eval("import")` rather than `import` to suppress the warning in @rspack/browser
55-
const modulePromise = eval(`import("${dataUrl}")`);
56-
modulePromise.then(resolve);
57-
} catch (e) {
58-
reject(e);
59-
}
31+
// biome-ignore lint/security/noGlobalEval: use `eval("import")` rather than `import` to suppress the warning in @rspack/browser
32+
const modulePromise = eval(`import("${dataUrl}")`);
33+
modulePromise.then(resolve);
34+
} catch (e) {
35+
reject(e);
6036
}
6137
});
6238
})

0 commit comments

Comments
 (0)