Skip to content

Commit

Permalink
Fix #155: Add processCommonJSModules flag
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Nov 18, 2023
1 parent 89849de commit dc2d4f2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public class ResourcesOptimizerMojo extends AbstractMojo {
@Parameter
private boolean emitUseStrict;

/**
* Flag rewrites CommonJS modules so that modules can be concatenated together, by renaming all globals to avoid conflicting with other modules.
*/
@Parameter
private boolean processCommonJSModules;

/**
* Suffix for compressed / merged files.
*/
Expand Down Expand Up @@ -243,7 +249,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
processJsFiles(file, subDirJsFiles,
getSubDirAggregation(file, aggr, ResourcesScanner.JS_FILE_EXTENSION),
getCompilationLevel(compilationLevel), getWarningLevel(warningLevel),
resolveSourceMap(null), null, getLanguageIn(languageIn), getLanguageOut(languageOut), emitUseStrict);
resolveSourceMap(null), null, getLanguageIn(languageIn), getLanguageOut(languageOut), emitUseStrict, processCommonJSModules);
}
}
}
Expand All @@ -260,7 +266,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// handle JavaScript files
processJsFiles(dir, scanner.getJsFiles(), aggr, getCompilationLevel(compilationLevel),
getWarningLevel(warningLevel), resolveSourceMap(null), suffix,
getLanguageIn(languageIn), getLanguageOut(languageOut), emitUseStrict);
getLanguageIn(languageIn), getLanguageOut(languageOut), emitUseStrict, processCommonJSModules);
}
}
}
Expand Down Expand Up @@ -337,7 +343,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
processJsFiles(file, subDirJsFiles,
getSubDirAggregation(file, aggr, ResourcesScanner.JS_FILE_EXTENSION),
resolveCompilationLevel(rs), resolveWarningLevel(rs),
resolveSourceMap(rs), null, resolveLanguageIn(rs), resolveLanguageOut(rs), emitUseStrict);
resolveSourceMap(rs), null, resolveLanguageIn(rs), resolveLanguageOut(rs), emitUseStrict, processCommonJSModules);
}
}
}
Expand All @@ -354,7 +360,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// handle JavaScript files
processJsFiles(dir, scanner.getJsFiles(), aggr, resolveCompilationLevel(rs),
resolveWarningLevel(rs), resolveSourceMap(rs), suffix,
resolveLanguageIn(rs), resolveLanguageOut(rs), emitUseStrict);
resolveLanguageIn(rs), resolveLanguageOut(rs), emitUseStrict, processCommonJSModules);
}
}
}
Expand Down Expand Up @@ -391,11 +397,11 @@ private void processCssFiles(final File inputDir, final Set<File> cssFiles, fina

private void processJsFiles(final File inputDir, final Set<File> jsFiles, final Aggregation aggr, final CompilationLevel compilationLevel,
final WarningLevel warningLevel, final SourceMap sourceMap, final String suffix,
final LanguageMode languageIn, final LanguageMode languageOut, boolean emitUseStrict) throws MojoExecutionException {
final LanguageMode languageIn, final LanguageMode languageOut, boolean emitUseStrict, boolean processCommonJSModules) throws MojoExecutionException {
resFound = true;
final ResourcesSetAdapter rsa = new ResourcesSetJsAdapter(
inputDir, jsFiles, aggr, compilationLevel, warningLevel, sourceMap, encoding,
failOnWarning, suffix, languageIn, languageOut, emitUseStrict);
failOnWarning, suffix, languageIn, languageOut, emitUseStrict, processCommonJSModules);

final ClosureCompilerOptimizer closureOptimizer = new ClosureCompilerOptimizer(getLog());
closureOptimizer.optimize(rsa);
Expand Down Expand Up @@ -664,4 +670,4 @@ private double round(final double value, final int places) {

return roundedValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public void optimize(final ResourcesSetAdapter rsAdapter) throws MojoExecutionEx
options.setEmitUseStrict(rsa.isEmitUseStrict());
options.setEnvironment(CompilerOptions.Environment.BROWSER);

options.setProcessCommonJSModules(rsa.isProcessCommonJSModules());

try {
final Charset cset = Charset.forName(rsa.getEncoding());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ public class ResourcesSetJsAdapter extends ResourcesSetAdapter {
private final LanguageMode languageOut;

private final boolean emitUseStrict;
private boolean processCommonJSModules;

public ResourcesSetJsAdapter(File inputDir, Set<File> files, Aggregation aggregation, CompilationLevel compilationLevel,
WarningLevel warningLevel, SourceMap sourceMap, String encoding, boolean failOnWarning,
String suffix, LanguageMode languageIn, LanguageMode languageOut, boolean emitUseStrict) {
String suffix, LanguageMode languageIn, LanguageMode languageOut, boolean emitUseStrict, boolean processCommonJSModules) {
super(inputDir, files, aggregation, encoding, failOnWarning, suffix);
this.compilationLevel = compilationLevel;
this.warningLevel = warningLevel;
this.sourceMap = sourceMap;
this.languageIn = languageIn;
this.languageOut = languageOut;
this.emitUseStrict = emitUseStrict;
this.processCommonJSModules = processCommonJSModules;
}

public CompilationLevel getCompilationLevel() {
Expand All @@ -81,4 +83,8 @@ public LanguageMode getLanguageOut() {
public boolean isEmitUseStrict() {
return emitUseStrict;
}
}

public boolean isProcessCommonJSModules() {
return processCommonJSModules;
}
}

0 comments on commit dc2d4f2

Please sign in to comment.