Search in sources :

Example 1 with LocationMapping

use of com.google.javascript.jscomp.SourceMap.LocationMapping in project closure-compiler by google.

the class CommandLineRunner method initConfigFromFlags.

private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err) {
    errorStream = err;
    List<String> processedArgs = processArgs(args);
    Flags.guardLevels.clear();
    Flags.mixedJsSources.clear();
    List<String> jsFiles = null;
    List<FlagEntry<JsSourceType>> mixedSources = null;
    List<LocationMapping> mappings = null;
    ImmutableMap<String, String> sourceMapInputs = null;
    boolean parseInlineSourceMaps = false;
    boolean applyInputSourceMaps = false;
    try {
        flags.parse(processedArgs);
        processFlagFiles();
        jsFiles = flags.getJsFiles();
        mixedSources = flags.getMixedJsSources();
        mappings = flags.getSourceMapLocationMappings();
        sourceMapInputs = flags.getSourceMapInputs();
        parseInlineSourceMaps = flags.parseInlineSourceMaps;
        applyInputSourceMaps = flags.applyInputSourceMaps;
    } catch (CmdLineException e) {
        reportError(e.getMessage());
    } catch (IOException ioErr) {
        reportError("ERROR - ioException: " + ioErr);
    }
    List<ModuleIdentifier> entryPoints = new ArrayList<>();
    if (flags.processCommonJsModules) {
        flags.processClosurePrimitives = true;
        if (flags.commonJsEntryModule != null) {
            if (flags.entryPoints.isEmpty()) {
                entryPoints.add(ModuleIdentifier.forFile(flags.commonJsEntryModule));
            } else {
                reportError("--common_js_entry_module cannot be used with --entry_point.");
            }
        }
    }
    if (flags.outputWrapper == null) {
        flags.outputWrapper = "";
    }
    if (flags.outputWrapperFile != null && !flags.outputWrapperFile.isEmpty()) {
        try {
            flags.outputWrapper = Files.asCharSource(new File(flags.outputWrapperFile), UTF_8).read();
        } catch (Exception e) {
            reportError("ERROR - invalid output_wrapper_file specified.");
        }
    }
    if (!flags.outputWrapper.isEmpty() && !flags.outputWrapper.contains(CommandLineRunner.OUTPUT_MARKER)) {
        reportError("ERROR - invalid output_wrapper specified. Missing '" + CommandLineRunner.OUTPUT_MARKER + "'.");
    }
    if (!flags.outputWrapper.isEmpty() && flags.isolationMode != IsolationMode.NONE) {
        reportError("--output_wrapper and --isolation_mode may not be used together.");
    }
    if (flags.isolationMode == IsolationMode.IIFE) {
        flags.outputWrapper = "(function(){%output%}).call(this);";
    }
    // Handle --compilation_level=BUNDLE
    List<String> bundleFiles = ImmutableList.of();
    boolean skipNormalOutputs = false;
    if (flags.compilationLevelParsed == CompilationLevel.BUNDLE) {
        if (flags.jsOutputFile.isEmpty()) {
            reportError("--compilation_level=BUNDLE cannot be used without a --js_output_file.");
        } else {
            bundleFiles = ImmutableList.of(flags.jsOutputFile);
            flags.jsOutputFile = "";
            skipNormalOutputs = true;
        }
    }
    if (errors) {
        Flags.printShortUsageAfterErrors(errorStream);
    } else if (flags.displayHelp) {
        flags.printUsage(out);
    } else if (flags.version) {
        out.println("Closure Compiler (http://github.com/google/closure-compiler)\n" + "Version: " + Compiler.getReleaseVersion() + "\n" + "Built on: " + Compiler.getReleaseDate());
        out.flush();
    } else {
        runCompiler = true;
        CodingConvention conv;
        if (flags.thirdParty) {
            conv = CodingConventions.getDefault();
        } else if (flags.chromePass) {
            conv = new ChromeCodingConvention();
        } else {
            conv = new ClosureCodingConvention();
        }
        // For backwards compatibility, allow both commonJsPathPrefix and jsModuleRoot.
        List<String> moduleRoots = new ArrayList<>();
        if (!flags.moduleRoot.isEmpty()) {
            moduleRoots.addAll(flags.moduleRoot);
            if (!flags.commonJsPathPrefix.isEmpty()) {
                reportError("--commonJsPathPrefix cannot be used with --js_module_root.");
            }
        } else if (flags.commonJsPathPrefix != null) {
            moduleRoots.addAll(flags.commonJsPathPrefix);
        } else {
            moduleRoots.add(ModuleLoader.DEFAULT_FILENAME_PREFIX);
        }
        for (String entryPoint : flags.entryPoints) {
            if (entryPoint.startsWith("goog:")) {
                entryPoints.add(ModuleIdentifier.forClosure(entryPoint));
            } else {
                entryPoints.add(ModuleIdentifier.forFile(entryPoint));
            }
        }
        if (flags.dependencyMode == CompilerOptions.DependencyMode.STRICT && entryPoints.isEmpty()) {
            reportError("When --dependency_mode=STRICT, you must specify at least " + "one --entry_point.");
        }
        CompilerOptions.DependencyMode depMode = flags.dependencyMode;
        if (flags.onlyClosureDependencies || flags.manageClosureDependencies) {
            if (flags.dependencyMode != CompilerOptions.DependencyMode.NONE) {
                reportError((flags.onlyClosureDependencies ? "--only_closure_dependencies" : "--manage_closure_dependencies") + " cannot be used with --dependency_mode.");
            } else {
                if (flags.manageClosureDependencies) {
                    depMode = CompilerOptions.DependencyMode.LOOSE;
                } else if (flags.onlyClosureDependencies) {
                    depMode = CompilerOptions.DependencyMode.STRICT;
                }
                if (!flags.closureEntryPoint.isEmpty() && !flags.entryPoints.isEmpty()) {
                    reportError("--closure_entry_point cannot be used with --entry_point.");
                } else {
                    for (String entryPoint : flags.closureEntryPoint) {
                        entryPoints.add(ModuleIdentifier.forClosure(entryPoint));
                    }
                }
            }
        }
        if (!flags.renaming && flags.compilationLevelParsed == CompilationLevel.ADVANCED_OPTIMIZATIONS) {
            reportError("ERROR - renaming cannot be disabled when ADVANCED_OPTIMIZATIONS is used.");
            runCompiler = false;
        }
        getCommandLineConfig().setPrintTree(flags.printTree).setPrintAst(flags.printAst).setPrintPassGraph(flags.printPassGraph).setJscompDevMode(flags.jscompDevMode).setLoggingLevel(flags.loggingLevel).setExterns(flags.externs).setJs(jsFiles).setJsZip(flags.jszip).setMixedJsSources(mixedSources).setJsOutputFile(flags.jsOutputFile).setSaveAfterChecksFileName(flags.saveAfterChecksFile).setContinueSavedCompilationFileName(flags.continueSavedCompilationFile).setModule(flags.module).setVariableMapOutputFile(flags.variableMapOutputFile).setCreateNameMapFiles(flags.createNameMapFiles).setPropertyMapOutputFile(flags.propertyMapOutputFile).setCodingConvention(conv).setSummaryDetailLevel(flags.summaryDetailLevel).setOutputWrapper(flags.outputWrapper).setModuleWrapper(flags.moduleWrapper).setModuleOutputPathPrefix(flags.moduleOutputPathPrefix).setCreateSourceMap(flags.createSourceMap).setSourceMapFormat(flags.sourceMapFormat).setSourceMapLocationMappings(mappings).setSourceMapInputFiles(sourceMapInputs).setParseInlineSourceMaps(parseInlineSourceMaps).setApplyInputSourceMaps(applyInputSourceMaps).setWarningGuards(Flags.guardLevels).setDefine(flags.define).setCharset(flags.charset).setDependencyMode(depMode).setEntryPoints(entryPoints).setOutputManifest(ImmutableList.of(flags.outputManifest)).setOutputBundle(bundleFiles).setSkipNormalOutputs(skipNormalOutputs).setOutputModuleDependencies(flags.outputModuleDependencies).setProcessCommonJSModules(flags.processCommonJsModules).setModuleRoots(moduleRoots).setTransformAMDToCJSModules(flags.transformAmdModules).setWarningsWhitelistFile(flags.warningsWhitelistFile).setHideWarningsFor(flags.hideWarningsFor).setAngularPass(flags.angularPass).setInstrumentationTemplateFile(flags.instrumentationFile).setNewTypeInference(flags.useNewTypeInference).setJsonStreamMode(flags.jsonStreamMode).setErrorFormat(flags.errorFormat);
    }
    errorStream = null;
}
Also used : LocationMapping(com.google.javascript.jscomp.SourceMap.LocationMapping) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) IOException(java.io.IOException) File(java.io.File) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 2 with LocationMapping

use of com.google.javascript.jscomp.SourceMap.LocationMapping in project closure-compiler by google.

the class CompileTask method createCompilerOptions.

private CompilerOptions createCompilerOptions() {
    CompilerOptions options = new CompilerOptions();
    this.compilationLevel.setOptionsForCompilationLevel(options);
    if (this.debugOptions) {
        this.compilationLevel.setDebugOptionsForCompilationLevel(options);
    }
    options.setEnvironment(this.environment);
    options.setPrettyPrint(this.prettyPrint);
    options.setPrintInputDelimiter(this.printInputDelimiter);
    options.setPreferSingleQuotes(this.preferSingleQuotes);
    options.setGenerateExports(this.generateExports);
    options.setLanguageIn(this.languageIn);
    options.setLanguageOut(this.languageOut);
    options.setOutputCharset(this.outputEncoding);
    this.warningLevel.setOptionsForWarningLevel(options);
    options.setManageClosureDependencies(manageDependencies);
    convertEntryPointParameters(options);
    options.setTrustedStrings(true);
    options.setAngularPass(angularPass);
    if (replaceProperties) {
        convertPropertiesMap(options);
    }
    convertDefineParameters(options);
    for (Warning warning : warnings) {
        CheckLevel level = warning.getLevel();
        String groupName = warning.getGroup();
        DiagnosticGroup group = new DiagnosticGroups().forName(groupName);
        if (group == null) {
            throw new BuildException("Unrecognized 'warning' option value (" + groupName + ")");
        }
        options.setWarningLevel(group, level);
    }
    if (!Strings.isNullOrEmpty(sourceMapFormat)) {
        options.setSourceMapFormat(Format.valueOf(sourceMapFormat));
    }
    if (!Strings.isNullOrEmpty(sourceMapLocationMapping)) {
        String[] tokens = sourceMapLocationMapping.split("\\|", -1);
        LocationMapping lm = new LocationMapping(tokens[0], tokens[1]);
        options.setSourceMapLocationMappings(Arrays.asList(lm));
    }
    options.setApplyInputSourceMaps(applyInputSourceMaps);
    if (sourceMapOutputFile != null) {
        File parentFile = sourceMapOutputFile.getParentFile();
        if (parentFile.mkdirs()) {
            log("Created missing parent directory " + parentFile, Project.MSG_DEBUG);
        }
        options.setSourceMapOutputPath(parentFile.getAbsolutePath());
    }
    return options;
}
Also used : LocationMapping(com.google.javascript.jscomp.SourceMap.LocationMapping) CheckLevel(com.google.javascript.jscomp.CheckLevel) DiagnosticGroup(com.google.javascript.jscomp.DiagnosticGroup) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) BuildException(org.apache.tools.ant.BuildException) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File) DiagnosticGroups(com.google.javascript.jscomp.DiagnosticGroups)

Example 3 with LocationMapping

use of com.google.javascript.jscomp.SourceMap.LocationMapping in project closure-compiler by google.

the class CommandLineRunnerTest method testSourceMapLocationsTranslations2.

public void testSourceMapLocationsTranslations2() {
    args.add("--js_output_file");
    args.add("/path/to/out.js");
    args.add("--create_source_map=%outname%.map");
    args.add("--source_map_location_mapping=foo/|http://bar");
    args.add("--source_map_location_mapping=xxx/|http://yyy");
    testSame("var x = 3;");
    List<LocationMapping> mappings = lastCompiler.getOptions().sourceMapLocationMappings;
    assertThat(ImmutableSet.copyOf(mappings)).containsExactly(new LocationMapping("foo/", "http://bar"), new LocationMapping("xxx/", "http://yyy"));
}
Also used : LocationMapping(com.google.javascript.jscomp.SourceMap.LocationMapping)

Example 4 with LocationMapping

use of com.google.javascript.jscomp.SourceMap.LocationMapping in project closure-compiler by google.

the class CommandLineRunnerTest method testSourceMapLocationsTranslations1.

public void testSourceMapLocationsTranslations1() {
    args.add("--js_output_file");
    args.add("/path/to/out.js");
    args.add("--create_source_map=%outname%.map");
    args.add("--source_map_location_mapping=foo/|http://bar");
    testSame("var x = 3;");
    List<LocationMapping> mappings = lastCompiler.getOptions().sourceMapLocationMappings;
    assertThat(ImmutableSet.copyOf(mappings)).containsExactly(new LocationMapping("foo/", "http://bar"));
}
Also used : LocationMapping(com.google.javascript.jscomp.SourceMap.LocationMapping)

Aggregations

LocationMapping (com.google.javascript.jscomp.SourceMap.LocationMapping)4 File (java.io.File)2 CheckLevel (com.google.javascript.jscomp.CheckLevel)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 DiagnosticGroup (com.google.javascript.jscomp.DiagnosticGroup)1 DiagnosticGroups (com.google.javascript.jscomp.DiagnosticGroups)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 BuildException (org.apache.tools.ant.BuildException)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1