Skip to content

Commit

Permalink
Rename code splitting flags from "module" to "chunk".
Browse files Browse the repository at this point in the history
Use aliases to be backwards compatible.

Closes #2925

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196915847
  • Loading branch information
ChadKillingsworth authored and blickly committed May 17, 2018
1 parent e1ba9e0 commit f6cba36
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
72 changes: 38 additions & 34 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -127,8 +127,8 @@ public class CommandLineRunner extends
// UTF-8 BOM is 0xEF, 0xBB, 0xBF, of which character code is 65279.
public static final int UTF8_BOM_CODE = 65279;

// Allowable module name characters that aren't valid in a JS identifier
private static final Pattern extraModuleNameChars = Pattern.compile("[-.]+");
// Allowable chunk name characters that aren't valid in a JS identifier
private static final Pattern extraChunkNameChars = Pattern.compile("[-.]+");

// I don't really care about unchecked warnings in this class.
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -222,18 +222,19 @@ private static class Flags {
+ "written to stdout")
private String jsOutputFile = "";

@Option(name = "--module",
usage = "A JavaScript module specification. The format is "
+ "<name>:<num-js-files>[:[<dep>,...][:]]]. Module names must be "
+ "unique. Each dep is the name of a module that this module "
+ "depends on. Modules must be listed in dependency order, and JS "
@Option(name = "--chunk",
usage = "A JavaScript chunk specification. The format is "
+ "<name>:<num-js-files>[:[<dep>,...][:]]]. Chunk names must be "
+ "unique. Each dep is the name of a chunk that this chunk "
+ "depends on. Chunks must be listed in dependency order, and JS "
+ "source files must be listed in the corresponding order. Where "
+ "--module flags occur in relation to --js flags is unimportant. "
+ "<num-js-files> may be set to 'auto' for the first module if it "
+ "--chunk flags occur in relation to --js flags is unimportant. "
+ "<num-js-files> may be set to 'auto' for the first chunk if it "
+ "has no dependencies. "
+ "Provide the value 'auto' to trigger module creation from CommonJS"
+ "modules.")
private List<String> module = new ArrayList<>();
+ "Provide the value 'auto' to trigger chunk creation from CommonJS"
+ "modules.",
aliases = "--module")
private List<String> chunk = new ArrayList<>();

@Option(name = "--continue-saved-compilation",
usage = "Filename where the intermediate compilation state was previously saved.",
Expand Down Expand Up @@ -310,22 +311,24 @@ private static class Flags {
+ " like newline in the wrapper.")
private String outputWrapperFile = "";

@Option(name = "--module_wrapper",
usage = "An output wrapper for a JavaScript module (optional). "
+ "The format is <name>:<wrapper>. The module name must correspond "
+ "with a module specified using --module. The wrapper must "
@Option(name = "--chunk_wrapper",
usage = "An output wrapper for a JavaScript chunk (optional). "
+ "The format is <name>:<wrapper>. The chunk name must correspond "
+ "with a chunk specified using --chunk. The wrapper must "
+ "contain %s as the code placeholder. "
+ "Alternately, %output% can be used in place of %s. "
+ "%n% can be used to represent a newline. "
+ "The %basename% placeholder can "
+ "also be used to substitute the base name of the module output file.")
private List<String> moduleWrapper = new ArrayList<>();
+ "also be used to substitute the base name of the chunk output file.",
aliases = "--module_wrapper")
private List<String> chunkWrapper = new ArrayList<>();

@Option(name = "--module_output_path_prefix",
usage = "Prefix for filenames of compiled JS modules. "
+ "<module-name>.js will be appended to this prefix. Directories "
+ "will be created as needed. Use with --module")
private String moduleOutputPathPrefix = "./";
@Option(name = "--chunk_output_path_prefix",
usage = "Prefix for filenames of compiled JS chunks. "
+ "<chunk-name>.js will be appended to this prefix. Directories "
+ "will be created as needed. Use with --chunk",
aliases = "--module_output_path_prefix")
private String chunkOutputPathPrefix = "./";

@Option(name = "--create_source_map",
usage = "If specified, a source map file mapping the generated "
Expand Down Expand Up @@ -605,9 +608,10 @@ private static class Flags {
)
private String outputManifest = "";

@Option(name = "--output_module_dependencies",
usage = "Prints out a JSON file of dependencies between modules.")
private String outputModuleDependencies = "";
@Option(name = "--output_chunk_dependencies",
usage = "Prints out a JSON file of dependencies between chunks.",
aliases = "--output_module_dependencies")
private String outputChunkDependencies = "";

@Option(
name = "--language_in",
Expand Down Expand Up @@ -889,13 +893,13 @@ private void parse(List<String> args) throws CmdLineException {
"rewrite_polyfills"))
.putAll(
"Code Splitting",
ImmutableList.of("module", "module_output_path_prefix", "module_wrapper"))
ImmutableList.of("chunk", "chunk_output_path_prefix", "chunk_wrapper"))
.putAll(
"Reports",
ImmutableList.of(
"create_source_map",
"output_manifest",
"output_module_dependencies",
"output_chunk_dependencies",
"property_renaming_report",
"source_map_input",
"source_map_include_content",
Expand Down Expand Up @@ -1600,15 +1604,15 @@ private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err
.setJsOutputFile(flags.jsOutputFile)
.setSaveAfterChecksFileName(flags.saveAfterChecksFile)
.setContinueSavedCompilationFileName(flags.continueSavedCompilationFile)
.setModule(flags.module)
.setModule(flags.chunk)
.setVariableMapOutputFile(flags.variableMapOutputFile)
.setCreateNameMapFiles(flags.createNameMapFiles)
.setPropertyMapOutputFile(flags.propertyMapOutputFile)
.setCodingConvention(conv)
.setSummaryDetailLevel(flags.summaryDetailLevel)
.setOutputWrapper(flags.outputWrapper)
.setModuleWrapper(flags.moduleWrapper)
.setModuleOutputPathPrefix(flags.moduleOutputPathPrefix)
.setModuleWrapper(flags.chunkWrapper)
.setModuleOutputPathPrefix(flags.chunkOutputPathPrefix)
.setCreateSourceMap(flags.createSourceMap)
.setSourceMapFormat(flags.sourceMapFormat)
.setSourceMapLocationMappings(mappings)
Expand All @@ -1623,7 +1627,7 @@ private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err
.setOutputManifest(ImmutableList.of(flags.outputManifest))
.setOutputBundle(bundleFiles)
.setSkipNormalOutputs(skipNormalOutputs)
.setOutputModuleDependencies(flags.outputModuleDependencies)
.setOutputModuleDependencies(flags.outputChunkDependencies)
.setProcessCommonJSModules(flags.processCommonJsModules)
.setModuleRoots(moduleRoots)
.setTransformAMDToCJSModules(flags.transformAmdModules)
Expand All @@ -1646,8 +1650,8 @@ protected void addWhitelistWarningsGuard(
@Override
protected void checkModuleName(String name) {
if (!TokenStream.isJSIdentifier(
extraModuleNameChars.matcher(name).replaceAll("_"))) {
throw new FlagUsageException("Invalid module name: '" + name + "'");
extraChunkNameChars.matcher(name).replaceAll("_"))) {
throw new FlagUsageException("Invalid chunk name: '" + name + "'");
}
}

Expand Down
22 changes: 11 additions & 11 deletions test/com/google/javascript/jscomp/CommandLineRunnerTest.java
Expand Up @@ -985,7 +985,7 @@ public void testSourceMapExpansion1() {
public void testSourceMapExpansion2() {
useModules = ModulePattern.CHAIN;
args.add("--create_source_map=%outname%.map");
args.add("--module_output_path_prefix=foo");
args.add("--chunk_output_path_prefix=foo");
testSame(new String[] {"var x = 3;", "var y = 5;"});
assertThat(lastCommandLineRunner.expandSourceMapPath(lastCompiler.getOptions(), null))
.isEqualTo("foo.map");
Expand All @@ -994,7 +994,7 @@ public void testSourceMapExpansion2() {
public void testSourceMapExpansion3() {
useModules = ModulePattern.CHAIN;
args.add("--create_source_map=%outname%.map");
args.add("--module_output_path_prefix=foo_");
args.add("--chunk_output_path_prefix=foo_");
testSame(new String[] {"var x = 3;", "var y = 5;"});
assertThat(
lastCommandLineRunner.expandSourceMapPath(
Expand All @@ -1005,7 +1005,7 @@ public void testSourceMapExpansion3() {
public void testInvalidSourceMapPattern() {
useModules = ModulePattern.CHAIN;
args.add("--create_source_map=out.map");
args.add("--module_output_path_prefix=foo_");
args.add("--chunk_output_path_prefix=foo_");
test(
new String[] {"var x = 3;", "var y = 5;"},
AbstractCommandLineRunner.INVALID_MODULE_SOURCEMAP_PATTERN);
Expand Down Expand Up @@ -1284,7 +1284,7 @@ public void testSourceMapInputs() throws Exception {

public void testModuleWrapperBaseNameExpansion() throws Exception {
useModules = ModulePattern.CHAIN;
args.add("--module_wrapper=m0:%s // %basename%");
args.add("--chunk_wrapper=m0:%s // %basename%");
testSame(new String[] {
"var x = 3;",
"var y = 4;"
Expand All @@ -1299,7 +1299,7 @@ public void testModuleWrapperBaseNameExpansion() throws Exception {

public void testModuleWrapperExpansion() throws Exception {
useModules = ModulePattern.CHAIN;
args.add("--module_wrapper=m0:%output%%n%//# SourceMappingUrl=%basename%.map");
args.add("--chunk_wrapper=m0:%output%%n%//# SourceMappingUrl=%basename%.map");
testSame(new String[] {
"var x = 3;",
"var y = 4;"
Expand All @@ -1317,7 +1317,7 @@ public void testMultistageCompilation() throws Exception {

String inputString = "[{\"src\": \"alert('foo');\", \"path\":\"foo.js\"}]";
args.add("--json_streams=BOTH");
args.add("--module=foo--bar.baz:1");
args.add("--chunk=foo--bar.baz:1");

// Perform stage1
List<String> stage1Args = new ArrayList<>(args);
Expand Down Expand Up @@ -1627,7 +1627,7 @@ public void testProcessCJS() {
public void testProcessCJSWithModuleOutput() {
args.add("--process_common_js_modules");
args.add("--entry_point=foo/bar");
args.add("--module=auto");
args.add("--chunk=auto");
setFilename(0, "foo/bar.js");
test("exports.test = 1", "var module$foo$bar={default: {}}; module$foo$bar.default.test = 1;");
// With modules=auto no direct output is created.
Expand Down Expand Up @@ -1941,7 +1941,7 @@ public void testModuleJSON() {
args.add("--transform_amd_modules");
args.add("--process_common_js_modules");
args.add("--entry_point=foo/bar");
args.add("--output_module_dependencies=test.json");
args.add("--output_chunk_dependencies=test.json");
setFilename(0, "foo/bar.js");
test(
"define({foo: 1})",
Expand Down Expand Up @@ -2096,7 +2096,7 @@ public void testJsonStreamSourceMap() {
public void testOutputModuleNaming() {
String inputString = "[{\"src\": \"alert('foo');\", \"path\":\"foo.js\"}]";
args.add("--json_streams=BOTH");
args.add("--module=foo--bar.baz:1");
args.add("--chunk=foo--bar.baz:1");

CommandLineRunner runner =
new CommandLineRunner(
Expand Down Expand Up @@ -2272,10 +2272,10 @@ private CommandLineRunner createCommandLineRunner(String[] original) {
args.add("--js");
args.add("/path/to/input" + i + ".js");
if (useModules == ModulePattern.CHAIN) {
args.add("--module");
args.add("--chunk");
args.add("m" + i + ":1" + (i > 0 ? (":m" + (i - 1)) : ""));
} else if (useModules == ModulePattern.STAR) {
args.add("--module");
args.add("--chunk");
args.add("m" + i + ":1" + (i > 0 ? ":m0" : ""));
}
}
Expand Down

0 comments on commit f6cba36

Please sign in to comment.