Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for TypeScript configs #1096

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ function discoverConfigFilePath(root: string, candidates: string[]) {
: path.join(root, candidate);

if (fs.existsSync(filename)) {
if (path.extname(filename) === '.mjs' && os.platform() === 'win32') {
if (os.platform() === 'win32' &&
(filename.endsWith('.mts') ||
filename.endsWith('.mjs'))) {
return url.pathToFileURL(filename).href;
}
return filename;
Expand Down
17 changes: 10 additions & 7 deletions packages/repack/src/commands/common/config/loadProjectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ export async function loadProjectConfig<C extends ConfigurationObject>(
let config: Configuration<C>;

try {
config = require(configFilePath);
} catch {
config = await import(configFilePath);
}

if ('default' in config) {
config = config.default as Configuration<C>;
// Always use dynamic import to support both TypeScript and JavaScript files
const imported = await import(configFilePath);
config = imported.default || imported;
} catch (error) {
// Fallback to require only for CommonJS files
if (configFilePath.endsWith('.js') || configFilePath.endsWith('.cjs') || configFilePath.endsWith('.cts')) {
config = require(configFilePath);
} else {
throw error;
}
}

return config;
Expand Down
6 changes: 6 additions & 0 deletions packages/repack/src/commands/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const DEFAULT_PORT = 8081;

/** Default webpack config locations. */
export const DEFAULT_WEBPACK_CONFIG_LOCATIONS = [
'webpack.config.mts',
'webpack.config.cts',
'webpack.config.ts',
'webpack.config.mjs',
'webpack.config.cjs',
'webpack.config.js',
Expand All @@ -17,6 +20,9 @@ export const DEFAULT_WEBPACK_CONFIG_LOCATIONS = [

/** Default rspack config locations. */
export const DEFAULT_RSPACK_CONFIG_LOCATIONS = [
'rspack.config.mts',
'rspack.config.cts',
'rspack.config.ts',
'rspack.config.mjs',
'rspack.config.cjs',
'rspack.config.js',
Expand Down
Loading