1
1
import Twig from "twig"
2
+ import { resolve , dirname } from "node:path"
3
+ import { existsSync } from "node:fs"
4
+
2
5
const { twig } = Twig
3
- import { resolve } from "node:path"
4
6
5
7
const FRAMEWORK_REACT = "react"
6
8
const FRAMEWORK_HTML = "html"
@@ -21,7 +23,14 @@ const includeTokenTypes = [
21
23
"Twig.logic.type.import" ,
22
24
]
23
25
24
- const pluckIncludes = ( tokens ) => {
26
+ const resolveFile = ( directory , file ) => {
27
+ if ( existsSync ( resolve ( file ) ) ) {
28
+ return resolve ( file )
29
+ }
30
+ return resolve ( directory , file )
31
+ }
32
+
33
+ const pluckIncludes = ( tokens , relative ) => {
25
34
return [
26
35
...tokens
27
36
. filter ( ( token ) => includeTokenTypes . includes ( token . token ?. type ) )
@@ -33,7 +42,10 @@ const pluckIncludes = (tokens) => {
33
42
[ ]
34
43
) ,
35
44
...tokens . reduce (
36
- ( carry , token ) => [ ...carry , ...pluckIncludes ( token . token ?. output || [ ] ) ] ,
45
+ ( carry , token ) => [
46
+ ...carry ,
47
+ ...pluckIncludes ( token . token ?. output || [ ] , relative ) ,
48
+ ] ,
37
49
[ ]
38
50
) ,
39
51
] . filter ( ( value , index , array ) => {
@@ -55,7 +67,11 @@ const compileTemplate = (id, file, { namespaces }) => {
55
67
return
56
68
}
57
69
resolve ( {
58
- includes : pluckIncludes ( template . tokens ) ,
70
+ // We don't use dirname here because this ends up in the browser.
71
+ includes : pluckIncludes (
72
+ template . tokens ,
73
+ template . id . split ( "/" ) . slice ( 0 , - 1 ) . join ( "/" )
74
+ ) ,
59
75
code : template . compile ( options ) ,
60
76
} )
61
77
} ,
@@ -70,7 +86,9 @@ const errorHandler =
70
86
( e ) => {
71
87
if ( isDefault ) {
72
88
return {
73
- code : `export default () => 'An error occurred whilst rendering ${ id } : ${ e . toString ( ) } ';` ,
89
+ code : `export default () => 'An error occurred whilst rendering ${ id } : ${ e . toString ( ) } ${
90
+ e . stack
91
+ } ';`,
74
92
map : null ,
75
93
}
76
94
}
@@ -117,8 +135,11 @@ const plugin = (options = {}) => {
117
135
includes = result . includes
118
136
const includePromises = [ ]
119
137
const processIncludes = ( template ) => {
120
- const file = Twig . path . expandNamespace ( options . namespaces , template )
121
- if ( ! seen . includes ( file ) ) {
138
+ const file = resolveFile (
139
+ dirname ( id ) ,
140
+ Twig . path . expandNamespace ( options . namespaces , template )
141
+ )
142
+ if ( ! seen . includes ( template ) ) {
122
143
includePromises . push (
123
144
new Promise ( async ( resolve , reject ) => {
124
145
const { includes, code } = await compileTemplate (
@@ -132,15 +153,16 @@ const plugin = (options = {}) => {
132
153
resolve ( code )
133
154
} )
134
155
)
135
- seen . push ( file )
156
+ seen . push ( template )
136
157
}
137
158
}
138
159
includes . forEach ( processIncludes )
139
160
embed = includes
140
161
. filter ( ( template ) => template !== "_self" )
141
162
. map (
142
163
( template ) =>
143
- `import '${ resolve (
164
+ `import '${ resolveFile (
165
+ dirname ( id ) ,
144
166
Twig . path . expandNamespace ( options . namespaces , template )
145
167
) } ';`
146
168
)
0 commit comments