@@ -28,88 +28,92 @@ ESM5Info = provider(
28
28
)
29
29
30
30
def _map_closure_path (file ):
31
- result = file .short_path [:- len (".closure.js" )]
32
- # short_path is meant to be used when accessing runfiles in a binary, where
33
- # the CWD is inside the current repo. Therefore files in external repo have a
34
- # short_path of ../external/wkspc/path/to/package
35
- # We want to strip the first two segments from such paths.
36
- if (result .startswith ("../" )):
37
- result = "/" .join (result .split ("/" )[2 :])
38
- return result + ".js"
31
+ result = file .short_path [:- len (".closure.js" )]
32
+
33
+ # short_path is meant to be used when accessing runfiles in a binary, where
34
+ # the CWD is inside the current repo. Therefore files in external repo have a
35
+ # short_path of ../external/wkspc/path/to/package
36
+ # We want to strip the first two segments from such paths.
37
+ if (result .startswith ("../" )):
38
+ result = "/" .join (result .split ("/" )[2 :])
39
+ return result + ".js"
39
40
40
41
def _join (array ):
41
- return "/" .join ([p for p in array if p ])
42
+ return "/" .join ([p for p in array if p ])
42
43
43
44
def _esm5_outputs_aspect (target , ctx ):
44
- if not hasattr (target , "typescript" ):
45
- return []
46
-
47
- # We create a new tsconfig.json file that will have our compilation settings
48
- tsconfig = ctx .actions .declare_file ("%s_esm5.tsconfig.json" % target .label .name )
49
-
50
- workspace = target .label .workspace_root if target .label .workspace_root else ""
51
-
52
- # re-root the outputs under a ".esm5" directory so the path don't collide
53
- out_dir = ctx .label .name + ".esm5"
54
- if workspace :
55
- out_dir = out_dir + "/" + workspace
56
-
57
- outputs = [ctx .actions .declare_file (_join ([out_dir , _map_closure_path (f )]))
58
- for f in target .typescript .replay_params .outputs
59
- if not f .short_path .endswith (".externs.js" )]
60
-
61
- ctx .actions .run (
62
- executable = ctx .executable ._modify_tsconfig ,
63
- inputs = [target .typescript .replay_params .tsconfig ],
64
- outputs = [tsconfig ],
65
- arguments = [
66
- target .typescript .replay_params .tsconfig .path ,
67
- tsconfig .path ,
68
- _join ([workspace , target .label .package , ctx .label .name + ".esm5" ]),
69
- ctx .bin_dir .path
70
- ],
71
- )
72
-
73
- ctx .actions .run (
74
- progress_message = "Compiling TypeScript (ES5 with ES Modules) %s" % target .label ,
75
- inputs = target .typescript .replay_params .inputs + [tsconfig ],
76
- outputs = outputs ,
77
- arguments = [tsconfig .path ],
78
- executable = target .typescript .replay_params .compiler ,
79
- execution_requirements = {
80
- # TODO(alexeagle): enable worker mode for these compilations
81
- "supports-workers" : "0" ,
82
- },
83
- )
84
-
85
- root_dir = _join ([
86
- ctx .bin_dir .path ,
87
- workspace ,
88
- target .label .package ,
89
- ctx .label .name + ".esm5" ,
90
- ])
91
-
92
- transitive_output = {root_dir : depset (outputs )}
93
- for dep in ctx .rule .attr .deps :
94
- if ESM5Info in dep :
95
- transitive_output .update (dep [ESM5Info ].transitive_output )
96
-
97
- return [ESM5Info (
98
- transitive_output = transitive_output ,
99
- )]
45
+ if not hasattr (target , "typescript" ):
46
+ return []
47
+
48
+ # We create a new tsconfig.json file that will have our compilation settings
49
+ tsconfig = ctx .actions .declare_file ("%s_esm5.tsconfig.json" % target .label .name )
50
+
51
+ workspace = target .label .workspace_root if target .label .workspace_root else ""
52
+
53
+ # re-root the outputs under a ".esm5" directory so the path don't collide
54
+ out_dir = ctx .label .name + ".esm5"
55
+ if workspace :
56
+ out_dir = out_dir + "/" + workspace
57
+
58
+ outputs = [
59
+ ctx .actions .declare_file (_join ([out_dir , _map_closure_path (f )]))
60
+ for f in target .typescript .replay_params .outputs
61
+ if not f .short_path .endswith (".externs.js" )
62
+ ]
63
+
64
+ ctx .actions .run (
65
+ executable = ctx .executable ._modify_tsconfig ,
66
+ inputs = [target .typescript .replay_params .tsconfig ],
67
+ outputs = [tsconfig ],
68
+ arguments = [
69
+ target .typescript .replay_params .tsconfig .path ,
70
+ tsconfig .path ,
71
+ _join ([workspace , target .label .package , ctx .label .name + ".esm5" ]),
72
+ ctx .bin_dir .path ,
73
+ ],
74
+ )
75
+
76
+ ctx .actions .run (
77
+ progress_message = "Compiling TypeScript (ES5 with ES Modules) %s" % target .label ,
78
+ inputs = target .typescript .replay_params .inputs + [tsconfig ],
79
+ outputs = outputs ,
80
+ arguments = [tsconfig .path ],
81
+ executable = target .typescript .replay_params .compiler ,
82
+ execution_requirements = {
83
+ # TODO(alexeagle): enable worker mode for these compilations
84
+ "supports-workers" : "0" ,
85
+ },
86
+ )
87
+
88
+ root_dir = _join ([
89
+ ctx .bin_dir .path ,
90
+ workspace ,
91
+ target .label .package ,
92
+ ctx .label .name + ".esm5" ,
93
+ ])
94
+
95
+ transitive_output = {root_dir : depset (outputs )}
96
+ for dep in ctx .rule .attr .deps :
97
+ if ESM5Info in dep :
98
+ transitive_output .update (dep [ESM5Info ].transitive_output )
99
+
100
+ return [ESM5Info (
101
+ transitive_output = transitive_output ,
102
+ )]
100
103
101
104
# Downstream rules can use this aspect to access the ESM5 output flavor.
102
105
# Only terminal rules (those which expect never to be used in deps[]) should do
103
106
# this.
104
107
esm5_outputs_aspect = aspect (
105
108
implementation = _esm5_outputs_aspect ,
106
109
# Recurse to the deps of any target we visit
107
- attr_aspects = [' deps' ],
110
+ attr_aspects = [" deps" ],
108
111
attrs = {
109
112
"_modify_tsconfig" : attr .label (
110
113
default = Label ("//packages/bazel/src:modify_tsconfig" ),
111
114
executable = True ,
112
- cfg = "host" ),
115
+ cfg = "host" ,
116
+ ),
113
117
# We must list tsc_wrapped here to ensure it's built before the action runs
114
118
# For some reason, having the compiler output as an input to the action above
115
119
# is not sufficient.
@@ -128,43 +132,44 @@ esm5_outputs_aspect = aspect(
128
132
)
129
133
130
134
def esm5_root_dir (ctx ):
131
- return ctx .label .name + ".esm5"
135
+ return ctx .label .name + ".esm5"
132
136
133
137
def flatten_esm5 (ctx ):
134
- """Merge together the .esm5 folders from the dependencies.
135
-
136
- Two different dependencies A and B may have outputs like
137
- `bazel-bin/path/to/A.esm5/path/to/lib.js`
138
- `bazel-bin/path/to/B.esm5/path/to/main.js`
139
-
140
- In order to run rollup on this app, in case main.js contains `import from './lib'`
141
- they need to be together in the same root directory, so if we depend on both A and B
142
- we need the outputs to be
143
- `bazel-bin/path/to/my_rule.esm5/path/to/lib.js`
144
- `bazel-bin/path/to/my_rule.esm5/path/to/main.js`
145
-
146
- Args:
147
- ctx: the skylark rule execution context
148
-
149
- Returns:
150
- list of flattened files
151
- """
152
- esm5_sources = []
153
- result = []
154
- for dep in ctx .attr .deps :
155
- if ESM5Info in dep :
156
- transitive_output = dep [ESM5Info ].transitive_output
157
- esm5_sources .extend (transitive_output .values ())
158
- for f in depset (transitive = esm5_sources ).to_list ():
159
- path = f .short_path [f .short_path .find (".esm5" ) + len (".esm5" ):]
160
- if (path .startswith ("../" )):
161
- path = "external/" + path [3 :]
162
- rerooted_file = ctx .actions .declare_file ("/" .join ([esm5_root_dir (ctx ), path ]))
163
- result .append (rerooted_file )
164
- # print("copy", f.short_path, "to", rerooted_file.short_path)
165
- ctx .actions .expand_template (
166
- output = rerooted_file ,
167
- template = f ,
168
- substitutions = {},
169
- )
170
- return result
138
+ """Merge together the .esm5 folders from the dependencies.
139
+
140
+ Two different dependencies A and B may have outputs like
141
+ `bazel-bin/path/to/A.esm5/path/to/lib.js`
142
+ `bazel-bin/path/to/B.esm5/path/to/main.js`
143
+
144
+ In order to run rollup on this app, in case main.js contains `import from './lib'`
145
+ they need to be together in the same root directory, so if we depend on both A and B
146
+ we need the outputs to be
147
+ `bazel-bin/path/to/my_rule.esm5/path/to/lib.js`
148
+ `bazel-bin/path/to/my_rule.esm5/path/to/main.js`
149
+
150
+ Args:
151
+ ctx: the skylark rule execution context
152
+
153
+ Returns:
154
+ list of flattened files
155
+ """
156
+ esm5_sources = []
157
+ result = []
158
+ for dep in ctx .attr .deps :
159
+ if ESM5Info in dep :
160
+ transitive_output = dep [ESM5Info ].transitive_output
161
+ esm5_sources .extend (transitive_output .values ())
162
+ for f in depset (transitive = esm5_sources ).to_list ():
163
+ path = f .short_path [f .short_path .find (".esm5" ) + len (".esm5" ):]
164
+ if (path .startswith ("../" )):
165
+ path = "external/" + path [3 :]
166
+ rerooted_file = ctx .actions .declare_file ("/" .join ([esm5_root_dir (ctx ), path ]))
167
+ result .append (rerooted_file )
168
+
169
+ # print("copy", f.short_path, "to", rerooted_file.short_path)
170
+ ctx .actions .expand_template (
171
+ output = rerooted_file ,
172
+ template = f ,
173
+ substitutions = {},
174
+ )
175
+ return result
0 commit comments