Skip to content

Commit

Permalink
feat: support using ts as configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Mar 13, 2023
1 parent ee958e4 commit 374c42e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/rspack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"source-map-support": "^0.5.19",
"ts-node": "10.9.1"
},
"peerDependencies": {
"ts-node": "10.9.1"
},
"dependencies": {
"@discoveryjs/json-ext": "^0.5.7",
"@rspack/core": "workspace:*",
Expand All @@ -37,4 +40,4 @@
"webpack-bundle-analyzer": "4.6.1",
"yargs": "17.6.2"
}
}
}
41 changes: 34 additions & 7 deletions packages/rspack-cli/src/utils/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import fs from "fs";
import { RspackCLIOptions } from "../types";
import { RspackOptions, MultiRspackOptions } from "@rspack/core";

const defaultConfig = "rspack.config.js";
const defaultEntry = "src/index.js";
const supportedExtensions = [".js", ".ts"];
const defaultConfig = "rspack.config";
const defaultEntry = "src/index";

export type LoadedRspackConfig =
| undefined
Expand All @@ -25,20 +26,26 @@ export function loadRspackConfig(
if (!fs.existsSync(resolvedConfigPath)) {
throw new Error(`config file "${resolvedConfigPath}" not exists`);
}
loadedConfig = require(resolvedConfigPath);
loadedConfig = requireWithAdditionalExtension(resolvedConfigPath);
} else {
let defaultConfigPath = path.resolve(process.cwd(), defaultConfig);
if (fs.existsSync(defaultConfigPath)) {
loadedConfig = require(defaultConfigPath);
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: path.resolve(process.cwd(), defaultEntry)
main: defaultEntryPath
};
}
loadedConfig = {
Expand All @@ -48,3 +55,23 @@ export function loadRspackConfig(
}
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;
require("ts-node").register({ transpileOnly: true });
}
return require(resolvedPath);
}
2 changes: 1 addition & 1 deletion packages/rspack-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"path": "../rspack-dev-server"
}
]
}
}

0 comments on commit 374c42e

Please sign in to comment.