-
-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support configuration file written in TypeScript (#2227)
* chore: add editorconfig to the project This unifies all editors configuration * refactor: move loadConfig out of rspack-cli * feat: support using ts as configuration file * test: add test for typescript config file fix #2188 * feat: report ts-node require error
- Loading branch information
1 parent
665aae9
commit c4c20d6
Showing
10 changed files
with
158 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@rspack/cli": patch | ||
--- | ||
|
||
Support TypeScript as configuration file. |
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,19 @@ | ||
root = true | ||
|
||
[*.{js,ts}] | ||
indent_style = tab | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
max_line_length = 80 | ||
|
||
[*.{yml,yaml,json}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[test/cases/parsing/bom/bomfile.{css,js}] | ||
charset = utf-8-bom | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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,83 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { RspackCLIOptions } from "../types"; | ||
import { RspackOptions, MultiRspackOptions } from "@rspack/core"; | ||
|
||
const supportedExtensions = [".js", ".ts"]; | ||
const defaultConfig = "rspack.config"; | ||
const defaultEntry = "src/index"; | ||
|
||
export type LoadedRspackConfig = | ||
| undefined | ||
| RspackOptions | ||
| MultiRspackOptions | ||
| (( | ||
env: Record<string, any>, | ||
argv: Record<string, any> | ||
) => RspackOptions | MultiRspackOptions); | ||
|
||
export function loadRspackConfig( | ||
options: RspackCLIOptions | ||
): LoadedRspackConfig { | ||
let loadedConfig: LoadedRspackConfig; | ||
// if we pass config paras | ||
if (options.config) { | ||
const resolvedConfigPath = path.resolve(process.cwd(), options.config); | ||
if (!fs.existsSync(resolvedConfigPath)) { | ||
throw new Error(`config file "${resolvedConfigPath}" not exists`); | ||
} | ||
loadedConfig = requireWithAdditionalExtension(resolvedConfigPath); | ||
} else { | ||
let defaultConfigPath = findFileWithSupportedExtensions( | ||
path.resolve(process.cwd(), defaultConfig) | ||
); | ||
if (defaultConfigPath != null) { | ||
loadedConfig = requireWithAdditionalExtension(defaultConfigPath); | ||
} else { | ||
let entry: Record<string, string> = {}; | ||
if (options.entry) { | ||
entry = { | ||
main: options.entry.map(x => path.resolve(process.cwd(), x))[0] // Fix me when entry supports array | ||
}; | ||
} else { | ||
const defaultEntryBase = path.resolve(process.cwd(), defaultEntry); | ||
const defaultEntryPath = | ||
findFileWithSupportedExtensions(defaultEntryBase) || | ||
defaultEntryBase + ".js"; // default entry is js | ||
entry = { | ||
main: defaultEntryPath | ||
}; | ||
} | ||
loadedConfig = { | ||
entry | ||
}; | ||
} | ||
} | ||
return loadedConfig; | ||
} | ||
|
||
// takes a basePath like `webpack.config`, return `webpack.config.{js,ts}` if | ||
// exists. returns null if none of them exists | ||
function findFileWithSupportedExtensions(basePath: string): string | null { | ||
for (const extension of supportedExtensions) { | ||
if (fs.existsSync(basePath + extension)) { | ||
return basePath + extension; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
let hasRegisteredTS = false; | ||
function requireWithAdditionalExtension(resolvedPath: string) { | ||
if (resolvedPath.endsWith("ts") && !hasRegisteredTS) { | ||
hasRegisteredTS = true; | ||
let tsNode: any; | ||
try { | ||
tsNode = require("ts-node"); | ||
} catch (e) { | ||
throw new Error("`ts-node` is required to use TypeScript configuration."); | ||
} | ||
tsNode.register({ transpileOnly: true }); | ||
} | ||
return require(resolvedPath); | ||
} |
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 typescript file"); |
12 changes: 12 additions & 0 deletions
12
packages/rspack-cli/tests/build/typescript/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,12 @@ | ||
import * as path from "path"; | ||
|
||
const config = { | ||
mode: "production", | ||
entry: "./main.ts", | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "foo.bundle.js", | ||
}, | ||
}; | ||
|
||
export = config; |
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,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs" | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/rspack-cli/tests/build/typescript/typescript.test.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,26 @@ | ||
import { run } from "../../utils/test-utils"; | ||
import { existsSync } from "fs"; | ||
import { resolve } from "path"; | ||
|
||
describe("webpack cli", () => { | ||
it("should support default config in typescript", async () => { | ||
const { exitCode, stderr, stdout } = await run(__dirname, []); | ||
|
||
expect(stderr).toBeFalsy(); | ||
expect(stdout).toBeTruthy(); | ||
expect(exitCode).toBe(0); | ||
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy(); | ||
}); | ||
|
||
it("should support specifying config in typescript", async () => { | ||
const { exitCode, stderr, stdout } = await run(__dirname, [ | ||
"-c", | ||
"./rspack.config.ts" | ||
]); | ||
|
||
expect(stderr).toBeFalsy(); | ||
expect(stdout).toBeTruthy(); | ||
expect(exitCode).toBe(0); | ||
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy(); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -21,4 +21,4 @@ | |
"path": "../rspack-dev-server" | ||
} | ||
] | ||
} | ||
} |