-
-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…3222) * feat: [cli] The configuration file follows the type property of package.json. * chore: [cli] Remove unnecessary tmp dependencies * fix: [cli] When building configuration files, do not bundle dependencies. * chore: [deps] Commit missing information. * refactor: [cli] Load the configuration file using `jiti` * pref(cli): jiti use sucrase.transform & remove __dirname in the esm file. * refactor(cli): support for `.cts`、`.mts` and follows `type: module` * chore(cli): correct method name * perf(cli): prevent some edge conditions from going wrong * chore: fix lock * chore: remove type-fest * chore: commit node_modules in monorepo * chore: support export default in ts config --------- Co-authored-by: yangjian.fe <[email protected]>
- Loading branch information
1 parent
79fd6dd
commit 955d01c
Showing
47 changed files
with
545 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const DEFAULT_CONFIG_NAME = "rspack.config" as const; | ||
|
||
export const DEFAULT_EXTENSIONS = [ | ||
".js", | ||
".ts", | ||
".mjs", | ||
".mts", | ||
".cjs", | ||
".cts" | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 dynamicImport(url); | ||
return config; | ||
} else { | ||
let result = require(path); | ||
// compatible with export default config in common ts config | ||
if (result && typeof result === "object" && "default" in result) { | ||
result = result.default || {}; | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
export default crossImport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import fs from "fs"; | ||
import { DEFAULT_EXTENSIONS } from "../constants"; | ||
|
||
/** | ||
* Takes a basePath like `webpack.config`, return `webpack.config.{ext}` if | ||
* exists. returns undefined if none of them exists | ||
*/ | ||
const findConfig = (basePath: string): string | undefined => { | ||
return DEFAULT_EXTENSIONS.map(ext => basePath + ext).find(fs.existsSync); | ||
}; | ||
|
||
export default findConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import path from "path"; | ||
import readPackageUp from "./readPackageUp"; | ||
|
||
const isEsmFile = (filePath: string, cwd = process.cwd()) => { | ||
const ext = path.extname(filePath); | ||
if (/\.(mjs|mts)$/.test(ext)) { | ||
return true; | ||
} else { | ||
const packageJson = readPackageUp(cwd); | ||
return packageJson?.type === "module"; | ||
} | ||
}; | ||
|
||
export default isEsmFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import path from "path"; | ||
|
||
const isTsFile = (configPath: string) => { | ||
const ext = path.extname(configPath); | ||
return /\.(c|m)?ts$/.test(ext); | ||
}; | ||
|
||
export default isTsFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const readPackageUp = (cwd = process.cwd()): { type?: "module" } | null => { | ||
let currentDir = path.resolve(cwd); | ||
let packageJsonPath = path.join(currentDir, "package.json"); | ||
|
||
while (!fs.existsSync(packageJsonPath)) { | ||
let parentDir = path.dirname(currentDir); | ||
if (parentDir === currentDir) { | ||
return null; | ||
} | ||
currentDir = parentDir; | ||
packageJsonPath = path.join(currentDir, "package.json"); | ||
} | ||
try { | ||
return JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
|
||
export default readPackageUp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Main cjs file"); |
10 changes: 10 additions & 0 deletions
10
packages/rspack-cli/tests/build/config/cjs/rspack.config.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
mode: "production", | ||
entry: path.resolve(__dirname, "main.ts"), | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "cjs.bundle.js" | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/rspack-cli/tests/build/config/cjs/rspack.config.cts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
mode: "production", | ||
entry: path.resolve(__dirname, "main.ts"), | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "cts.bundle.js" | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/rspack-cli/tests/build/config/cjs/rspack.config.export.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const path = require("path"); | ||
|
||
// @ts-ignore | ||
export = { | ||
mode: "production", | ||
entry: path.resolve(__dirname, "main.ts"), | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "ts.bundle.js" | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/rspack-cli/tests/build/config/cjs/rspack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
mode: "production", | ||
entry: path.resolve(__dirname, "main.ts"), | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "js.bundle.js" | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/rspack-cli/tests/build/config/cjs/rspack.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require("path"); | ||
|
||
export default { | ||
mode: "production", | ||
entry: path.resolve(__dirname, "main.ts"), | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "ts.bundle.js" | ||
} | ||
}; |
Oops, something went wrong.