Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability in watch mode to copy source maps to output folder for … #250

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ public boolean getSourcemapsEnabled() {
return Boolean.parseBoolean(getString("enableSourcemaps"));
}

@Override
public boolean getEnableIncrementalSourcemaps() {
return Boolean.parseBoolean(getString("enableIncrementalSourcemaps"));
}

@Override
public String getInitialScriptFilename() {
return getString("initialScriptFilename");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface Config {

boolean getSourcemapsEnabled();

boolean getEnableIncrementalSourcemaps();

String getInitialScriptFilename();

Map<String, String> getDefines();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public class BuildMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "false")
protected boolean enableSourcemaps;

/**
* True to copy only changed source files to the output directory. This is a performance optimization.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* True to copy only changed source files to the output directory. This is a performance optimization.
* True to copy only changed source files to the output directory. This is a performance optimization, but could result in incorrect output if generated files are altered outside of this plugin.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or if output is cached from an earlier run.

Basically you are sacrificing correctness for speed, especially if your "speed" also comes in the form of "i made it faster by using a cache outside of target".

*/
@Parameter(defaultValue = "false")
protected boolean enableIncrementalSourcemaps;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// pre-create the directory so it is easier to find up front, even if it starts off empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ public class TestMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "false")
protected boolean enableSourcemaps;

/**
* True to copy only changed source files to the output directory. This is a performance optimization.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* True to copy only changed source files to the output directory. This is a performance optimization.
* True to copy only changed source files to the output directory. This is a performance optimization, but could result in incorrect output if generated files are altered outside of this plugin.

*/
@Parameter(defaultValue = "false")
protected boolean enableIncrementalSourcemaps;

/**
* Closure flag: "Source of translated messages. Currently only supports XTB."
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public class WatchMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "true")
protected boolean enableSourcemaps;

/**
* True to copy only changed source files to the output directory. This is a performance optimization.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* True to copy only changed source files to the output directory. This is a performance optimization.
* True to copy only changed source files to the output directory. This is a performance optimization, but could result in incorrect output if generated files are altered outside of this plugin.

*/
@Parameter(defaultValue = "false")
protected boolean enableIncrementalSourcemaps;

/**
* @deprecated Will be removed in 0.21
*/
Expand Down
2 changes: 1 addition & 1 deletion j2cl-tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public Task resolve(Project project, Config config) {
}

File initialScriptFile = config.getWebappDirectory().resolve(config.getInitialScriptFilename()).toFile();
boolean sourcemapsEnabled = config.getSourcemapsEnabled() && !config.getEnableIncrementalSourcemaps();

Map<String, Object> defines = new LinkedHashMap<>(config.getDefines());

List<Input> outputToCopy = Stream.concat(
Expand Down Expand Up @@ -113,11 +115,14 @@ public void finish(TaskContext taskContext) throws IOException {
Files.copy(bundle.getAbsolutePath(), targetFile, StandardCopyOption.REPLACE_EXISTING);
}

File destSourcesDir = outputDir.toPath().resolve(Closure.SOURCES_DIRECTORY_NAME).toFile();
destSourcesDir.mkdirs();
for (Path dir : jsSources.stream().map(Input::getParentPaths).flatMap(Collection::stream).map(p -> p.resolve(Closure
.SOURCES_DIRECTORY_NAME)).collect(Collectors.toSet())) {
FileUtils.copyDirectory(dir.toFile(), destSourcesDir);
// Copy sources to the output directory, if sourcemaps are enabled and incremental source maps is not.
if (sourcemapsEnabled) {
File destSourcesDir = outputDir.toPath().resolve(Closure.SOURCES_DIRECTORY_NAME).toFile();
destSourcesDir.mkdirs();
for (Path dir : jsSources.stream().map(Input::getParentPaths).flatMap(Collection::stream).map(p -> p.resolve(Closure
.SOURCES_DIRECTORY_NAME)).collect(Collectors.toSet())) {
FileUtils.copyDirectory(dir.toFile(), destSourcesDir);
}
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public Task resolve(Project project, Config config) {

// Consider treating this always as true, since the build doesnt get more costly to be incremental
boolean incrementalEnabled = config.isIncrementalEnabled();
boolean sourcemapsEnabled = config.getSourcemapsEnabled() && config.getEnableIncrementalSourcemaps();
Path sourceMapsFolder = sourcemapsEnabled ? prepareSourcesFolder(config) : null;

Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();

Expand Down Expand Up @@ -197,6 +199,10 @@ public Task resolve(Project project, Config config) {
""
)).useEval(true);

if (sourcemapsEnabled) {
FileUtils.copyDirectory(context.outputPath().resolve("sources").toFile(), sourceMapsFolder.toFile());
}

try (OutputStream outputStream = Files.newOutputStream(Paths.get(outputFile));
BufferedWriter bundleOut = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
for (DependencyInfoAndSource info : sorter.getSortedList()) {
Expand Down Expand Up @@ -238,6 +244,16 @@ public Task resolve(Project project, Config config) {
};
}

private Path prepareSourcesFolder(Config config) {
try {
Path initialScriptFile = config.getWebappDirectory().resolve(config.getInitialScriptFilename());
Path destSourcesDir = Files.createDirectories(initialScriptFile.getParent()).resolve(Closure.SOURCES_DIRECTORY_NAME);
return Files.createDirectories(destSourcesDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public interface SourceSupplier {
String get() throws IOException;
}
Expand Down Expand Up @@ -313,9 +329,9 @@ public boolean getHasNoCompileAnnotation() {

public static class DependencyInfoFormat implements DependencyInfo {
private String name;
// private String pathRelativeToClosureBase = name;
// private String pathRelativeToClosureBase = name;
private List<String> provides;
// private List<RequireFormat> requires; //skipping requires as it isnt used by the dep sorter
// private List<RequireFormat> requires; //skipping requires as it isnt used by the dep sorter
private List<String> requiredSymbols;
private List<String> typeRequires;
private Map<String, String> loadFlags;
Expand Down