Skip to content

Commit 4e9754f

Browse files
committed
Don't copy sources before bundling, plus some cleanup
1 parent a3a8436 commit 4e9754f

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/BundleJarTask.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,6 @@ public void finish(TaskContext taskContext) throws IOException {
110110
Files.copy(bundle.getAbsolutePath(), targetFile, StandardCopyOption.REPLACE_EXISTING);
111111
}
112112

113-
// File destSourcesDir = outputDir.toPath().resolve(Closure.SOURCES_DIRECTORY_NAME).toFile();
114-
// destSourcesDir.mkdirs();
115-
// for (Path dir : jsSources.stream().map(Input::getParentPaths).flatMap(Collection::stream).map(p -> p.resolve(Closure
116-
// .SOURCES_DIRECTORY_NAME)).collect(Collectors.toSet())) {
117-
// FileUtils.copyDirectory(dir.toFile(), destSourcesDir);
118-
// }
119-
120113
try {
121114
Gson gson = new GsonBuilder().setPrettyPrinting().create();
122115
String scriptsArray = gson.toJson(sourceOrder.stream()

j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/ClosureBundleTask.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import com.vertispan.j2cl.build.task.OutputTypes;
2727
import com.vertispan.j2cl.build.task.Project;
2828
import com.vertispan.j2cl.build.task.TaskFactory;
29-
import com.vertispan.j2cl.tools.Closure;
3029
import io.methvin.watcher.hashing.Murmur3F;
31-
import org.apache.commons.io.FileUtils;
3230

3331
import java.io.BufferedInputStream;
3432
import java.io.BufferedReader;
@@ -115,17 +113,9 @@ public Task resolve(Project project, Config config) {
115113
return;// nothing to do
116114
}
117115

118-
// copy the sources locally so that we can create usable sourcemaps
119-
//TODO consider a soft link
120-
File sources = new File(closureOutputDir, Closure.SOURCES_DIRECTORY_NAME);
121-
for (Path path : js.stream().map(Input::getParentPaths).flatMap(Collection::stream).collect(Collectors.toUnmodifiableList())) {
122-
FileUtils.copyDirectory(path.toFile(), sources);
123-
}
124-
125116
List<DependencyInfoAndSource> dependencyInfos = new ArrayList<>();
126117
Compiler jsCompiler = new Compiler(System.err);//TODO before merge, write this to the log
127118

128-
Path sourcesPath = context.outputPath().resolve(Closure.SOURCES_DIRECTORY_NAME);
129119
if (incrementalEnabled && context.lastSuccessfulOutput().isPresent()) {
130120
// collect any dep info from disk for existing files
131121
final Map<String, DependencyInfoAndSource> depInfoMap;
@@ -135,9 +125,15 @@ public Task resolve(Project project, Config config) {
135125
}.getType();
136126
List<DependencyInfoFormat> deps = gson.fromJson(new BufferedReader(new InputStreamReader(inputStream)), listType);
137127
depInfoMap = deps.stream()
138-
.map(info -> new DependencyInfoAndSource(
139-
info,
140-
() -> Files.readString(lastOutput.resolve(Closure.SOURCES_DIRECTORY_NAME).resolve(info.getName())))
128+
.map(info -> {
129+
Path p = js.stream().flatMap(jsInput -> jsInput.getParentPaths().stream())
130+
.map(parent -> parent.resolve(info.getName()))
131+
.filter(Files::exists)
132+
.findFirst().get();
133+
return new DependencyInfoAndSource(
134+
p, info,
135+
() -> Files.readString(p));
136+
}
141137
)
142138
.collect(Collectors.toMap(DependencyInfo::getName, Function.identity()));
143139
}
@@ -149,14 +145,18 @@ public Task resolve(Project project, Config config) {
149145
depInfoMap.remove(change.getSourcePath().toString());
150146
} else {
151147
// ADD or MODIFY
148+
Path p = jsInput.getParentPaths().stream()
149+
.map(parent -> parent.resolve(change.getSourcePath()))
150+
.filter(Files::exists)
151+
.findFirst().get();
152152
CompilerInput input = new CompilerInput(SourceFile.builder()
153-
.withPath(sourcesPath.resolve(change.getSourcePath()))
153+
.withPath(p)
154154
.withOriginalPath(change.getSourcePath().toString())
155155
.build());
156156
input.setCompiler(jsCompiler);
157157
depInfoMap.put(
158158
change.getSourcePath().toString(),
159-
new DependencyInfoAndSource(input, input::getCode)
159+
new DependencyInfoAndSource(p, input, input::getCode)
160160
);
161161
}
162162
}
@@ -170,13 +170,17 @@ public Task resolve(Project project, Config config) {
170170
//non-incremental, read everything
171171
for (Input jsInput : js) {
172172
for (CachedPath path : jsInput.getFilesAndHashes()) {
173+
Path p = jsInput.getParentPaths().stream()
174+
.map(parent -> parent.resolve(path.getSourcePath()))
175+
.filter(Files::exists)
176+
.findFirst().get();
173177
CompilerInput input = new CompilerInput(SourceFile.builder()
174-
.withPath(sourcesPath.resolve(path.getSourcePath()))
178+
.withPath(p)
175179
.withOriginalPath(path.getSourcePath().toString())
176180
.build());
177181
input.setCompiler(jsCompiler);
178182

179-
dependencyInfos.add(new DependencyInfoAndSource(input, input::getCode));
183+
dependencyInfos.add(new DependencyInfoAndSource(p, input, input::getCode));
180184
}
181185
}
182186
}
@@ -212,33 +216,35 @@ public Task resolve(Project project, Config config) {
212216
for (DependencyInfoAndSource info : sorter.getSortedList()) {
213217
String code = info.getSource();
214218
String name = info.getName();
215-
String sourcemapContents = info.loadSourcemap(sourcesPath);
219+
String sourcemapContents = info.loadSourcemap();
216220

217221
//TODO do we actually need this?
218222
if (Compiler.isFillFileName(name) && code.isEmpty()) {
219223
continue;
220224
}
221225

226+
// Append a note indicating the name of the JS file that will follow
222227
writer.append("//").append(name).append("\n");
223228

229+
// Immediately before appending the JS file's contents, check which line we're starting at, and
230+
// merge sourcemap contents
224231
if (sourcemapContents != null) {
225232
sourceMapGenerator.setStartingPosition(writer.getLine(), 0);
226233
SourceMapConsumerV3 section = new SourceMapConsumerV3();
227234
section.parse(sourcemapContents);
228235
section.visitMappings((sourceName, symbolName, sourceStartPosition, startPosition, endPosition) -> sourceMapGenerator.addMapping(Paths.get(name).resolveSibling(sourceName).toString(), symbolName, sourceStartPosition, startPosition, endPosition));
229236
for (String source : section.getOriginalSources()) {
230-
String content = Files.readString(sourcesPath.resolve(name).resolveSibling(source));
237+
String content = Files.readString(info.getAbsolutePath().resolveSibling(source));
231238
sourceMapGenerator.addSourcesContent(Paths.get(name).resolveSibling(source).toString(), content);
232239
}
233240
}
234241

235-
// append this file and a comment where it came from
242+
// Append the current file
236243
bundler.withPath(name).appendTo(writer, info, code);
237244
writer.append("\n");
238245
}
239246

240247
// write a reference to our new sourcemaps
241-
// writer.append("// " + writer.getLine()).append("\n");
242248
writer.append("//# sourceMappingURL=").append(sourcemapOutFileName).append('\n');
243249
}
244250

@@ -324,14 +330,20 @@ public interface SourceSupplier {
324330
String get() throws IOException;
325331
}
326332
public static class DependencyInfoAndSource implements DependencyInfo {
333+
private final Path absolutePath;
327334
private final DependencyInfo delegate;
328335
private final SourceSupplier sourceSupplier;
329336

330-
public DependencyInfoAndSource(DependencyInfo delegate, SourceSupplier sourceSupplier) {
337+
public DependencyInfoAndSource(Path absolutePath, DependencyInfo delegate, SourceSupplier sourceSupplier) {
338+
this.absolutePath = absolutePath;
331339
this.delegate = delegate;
332340
this.sourceSupplier = sourceSupplier;
333341
}
334342

343+
public Path getAbsolutePath() {
344+
return absolutePath;
345+
}
346+
335347
public String getSource() throws IOException {
336348
return sourceSupplier.get();
337349
}
@@ -392,15 +404,15 @@ public boolean getHasNoCompileAnnotation() {
392404
return delegate.getHasNoCompileAnnotation();
393405
}
394406

395-
public String loadSourcemap(Path outPath) throws IOException {
407+
public String loadSourcemap() throws IOException {
396408
String sourceMappingUrlMarker = "//# sourceMappingURL=";
397409
int offset = getSource().lastIndexOf(sourceMappingUrlMarker);
398410
if (offset == -1) {
399411
return null;
400412
}
401413
int urlPos = offset + sourceMappingUrlMarker.length();
402414
String sourcemapName = getSource().substring(urlPos).split("\\s")[0];
403-
return Files.readString(outPath.resolve(getName()).resolveSibling(sourcemapName));
415+
return Files.readString(absolutePath.resolveSibling(sourcemapName));
404416
}
405417
}
406418

0 commit comments

Comments
 (0)