Skip to content

Commit

Permalink
perf(cli): prevent some edge conditions from going wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
molvqingtai committed Jun 18, 2023
1 parent a86e7a3 commit c5ca51f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/rspack-cli/src/utils/crossImport.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { pathToFileURL } from "url";
import isEsmFile from "./isEsmFile";

/**
* Dynamically import files. It will make sure it's not being compiled away by TS/Rollup.
*/
export const dynamicImport = new Function("path", "return import(path)");

const crossImport = async <T = any>(
path: string,
cwd = process.cwd()
): Promise<T> => {
if (isEsmFile(path, cwd)) {
const url = pathToFileURL(path).href;
const { default: config } = await import(url);
const { default: config } = await dynamicImport(url);
return config;
} else {
return require(path);
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack-cli/src/utils/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const registerLoader = (configPath: string) => {
if (failures) {
const messages = failures.map(failure => failure.error.message);
throw new Error(`${messages.join("\n")}`);
} else {
throw error;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-ignore
// @ts-ignore: Because dynamically create node_modules
import testDeps from "test-deps";

export default testDeps as unknown as { version: string };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-ignore
// @ts-ignore: Because dynamically create node_modules
import testDeps from "test-deps";

export default testDeps as unknown as { version: string };
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-ignore
// @ts-ignore: Because dynamically create {"type": "module"}
import packageA_deps from "./packageA/index.ts";
// @ts-ignore
// @ts-ignore: Because dynamically create {"type": "module"}
import packageB_deps from "./packageB/index.ts";

import path from "node:path";
Expand Down

0 comments on commit c5ca51f

Please sign in to comment.