You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- **esm compat**
- **Support esm modules**
<!-- ELLIPSIS_HIDDEN -->
----
> [!IMPORTANT]
> Adds ESM module support, modifies TypeScript import paths for ESM, and
includes tests and integration setup for ESM compatibility.
>
> - **ESM Support**:
> - Adds ESM module format support in `parse_generator()` in `v2.rs`.
> - Updates `generators.baml` to include `lang_typescript_esm` with
`module_format esm`.
> - **TypeScript Imports**:
> - Adds `replace_ts_imports_with_js()` in `typescript/mod.rs` to modify
import paths to `.js` for ESM.
> - Uses `modify_files()` in `dir_writer.rs` to apply import path
changes.
> - **Testing**:
> - Adds tests for `replace_ts_imports_with_js()` in
`typescript/mod.rs`.
> - Adds `typescript-esm` integration test setup with `package.json`,
`main.ts`, and `README.md`.
>
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for c3dd2b4. You can
[customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this
summary. It will automatically update as commits are pushed.</sup>
<!-- ELLIPSIS_HIDDEN -->
// Regex to find import/export statements with module specifiers.
307
+
// It captures the import/export part, quotes, and the path itself.
308
+
// Escaped curly braces in the character set just in case.
309
+
let re = Regex::new(r#"(import(?:["'\s]*(?:[\w\*\{\}\n\r\t, ]+)from\s*)?|export(?:["'\s]*(?:[\w\*\{\}\n\r\t, ]+)from\s*)?)(["'])([^"']+)(["'])"#).unwrap();
310
+
311
+
re.replace_all(content, |caps:®ex::Captures| {
312
+
let import_export_part = &caps[1];
313
+
let quote = &caps[2];
314
+
let path = &caps[3];
315
+
let closing_quote = &caps[4];
316
+
317
+
// Check if it's a relative path (starts with ./ or ../)
318
+
if path.starts_with("./") || path.starts_with("../"){
319
+
// Check if it already has a common JS/TS/CSS extension
320
+
if !path.ends_with(".js") &&
321
+
!path.ends_with(".mjs") &&
322
+
!path.ends_with(".cjs") &&
323
+
!path.ends_with(".jsx") && // Consider react specific extensions too
324
+
!path.ends_with(".tsx") &&
325
+
!path.ends_with(".css") && // Ignore CSS files
326
+
!path.ends_with(".json")
327
+
{
328
+
// Remove existing .ts if present before adding .js
"const path = '/path/to/file.ts';"// This is not an import/export statement
824
+
);
825
+
826
+
// Empty string
827
+
assert_eq!(replace_ts_imports_with_js(""),"");
828
+
// String with no imports
829
+
assert_eq!(
830
+
replace_ts_imports_with_js("const x = 10; function y() {}"),
831
+
"const x = 10; function y() {}"
832
+
);
833
+
// Mixed content
834
+
assert_eq!(
835
+
replace_ts_imports_with_js(
836
+
"console.log('hello');\nimport { a } from './a.ts';\nimport { b } from './b';\nimport { c } from './c.js';\nimport { d } from 'd-lib';\nexport { e } from '../e.ts';\nconsole.log('world');"
837
+
),
838
+
"console.log('hello');\nimport { a } from './a.js';\nimport { b } from './b.js';\nimport { c } from './c.js';\nimport { d } from 'd-lib';\nexport { e } from '../e.js';\nconsole.log('world');"
0 commit comments