1
1
import esbuild from "esbuild" ;
2
+ import { dirname } from "node:path" ;
3
+ import { mkdirSync , writeFileSync } from "node:fs" ;
2
4
import { copyFile , readFile , writeFile , rm } from "node:fs/promises" ;
3
5
import { glob } from "glob" ;
4
6
7
+ /**
8
+ * Internal function to rewrite relative import extensions.
9
+ *
10
+ * based on:
11
+ * @onyx /esbuild-plugin-rewrite-relative-import-extensions
12
+ *
13
+ * MIT License
14
+ *
15
+ * Copyright (c) 2025 Santiago Aguilar Hernández
16
+ *
17
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
18
+ * of this software and associated documentation files (the "Software"), to deal
19
+ * in the Software without restriction, including without limitation the rights
20
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
+ * copies of the Software, and to permit persons to whom the Software is
22
+ * furnished to do so, subject to the following conditions:
23
+ *
24
+ * The above copyright notice and this permission notice shall be included in all
25
+ * copies or substantial portions of the Software.
26
+ *
27
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33
+ * SOFTWARE.
34
+ */
35
+ export function rewriteRelativeImportExtensionsPlugin ( ) {
36
+ return {
37
+ name : "rewrite-relative-import-extensions" ,
38
+ setup ( build ) {
39
+ const write = build . initialOptions . write ;
40
+ build . initialOptions . write = false ;
41
+
42
+ build . onEnd ( ( result ) => {
43
+ const files = result . outputFiles ?? [ ] ;
44
+
45
+ for ( const file of files ) {
46
+ let output = file . text ;
47
+
48
+ const matches = output . matchAll (
49
+ / (?< = (?: i m p o r t | e x p o r t \s * [ * { ] ) [ ^ " ' ] + [ " ' ] ) ( [ ^ " ' ] + ) (? = [ " ' ] ) / g,
50
+ ) ;
51
+
52
+ for ( const match of matches ) {
53
+ output = output . replaceAll ( match [ 0 ] , ( filePath , index ) => {
54
+ if ( match . index !== index ) {
55
+ return filePath ;
56
+ }
57
+
58
+ if ( ! / ^ \. \. ? \/ / . test ( filePath ) ) {
59
+ return filePath ;
60
+ }
61
+
62
+ return filePath . replace (
63
+ / \. ( [ j t ] s x ) $ | ( (?: \. d ) ? ) ( (?: \. [ ^ . / ] + ?) ? ) \. ( [ c m ] ? ) t s $ / i,
64
+ function ( m , tsx , d , ext , cm ) {
65
+ return tsx
66
+ ? ".js"
67
+ : d && ( ! ext || ! cm )
68
+ ? m
69
+ : d + ext + "." + cm . toLowerCase ( ) + "js" ;
70
+ } ,
71
+ ) ;
72
+ } ) ;
73
+ }
74
+
75
+ if ( write === undefined || write ) {
76
+ mkdirSync ( dirname ( file . path ) , { recursive : true } ) ;
77
+ writeFileSync ( file . path , output ) ;
78
+ }
79
+ }
80
+ } ) ;
81
+ } ,
82
+ } ;
83
+ }
84
+
5
85
/** @type {esbuild.BuildOptions } */
6
86
const sharedOptions = {
7
87
sourcemap : "external" ,
@@ -24,6 +104,7 @@ async function main() {
24
104
bundle : false ,
25
105
...sharedOptions ,
26
106
sourcemap : false ,
107
+ plugins : [ rewriteRelativeImportExtensionsPlugin ( ) ] ,
27
108
} ) ;
28
109
29
110
// Remove the types file from the dist-src folder
0 commit comments