replace Browserify bundling with JSweet bundling

Browserify bundling is of course still possible but requires external
configuration
This commit is contained in:
Renaud Pawlak 2016-05-23 12:28:47 +02:00
parent 8f36d8a65c
commit 2dcb567876
2 changed files with 9 additions and 78 deletions

View File

@ -222,19 +222,9 @@ public enum JSweetProblem {
*/
UNION_TYPE_MISMATCH(Severity.ERROR),
/**
* Raised when an union type assignment is not compatible with one of the
* union-ed types.
* Raised when trying to create a bundle with a module kind selected.
*/
BUNDLE_WITH_COMMONJS(Severity.WARNING),
/**
* Raised when a bundle cannot be done because of a cycle in the
* module/packages.
*/
BUNDLE_HAS_CYCLE(Severity.ERROR),
/**
* Raised when a bundle cannot be done because it has no entries.
*/
BUNDLE_HAS_NO_ENTRIES(Severity.WARNING),
BUNDLE_WITH_MODULE(Severity.ERROR),
/**
* Raised when a package is named after an invalid name (typically a
* TypeScript keyword).
@ -402,12 +392,8 @@ public enum JSweetProblem {
return String.format("type mismatch, expecting '%s' (inferred from the indexed getter type)", params);
case UNION_TYPE_MISMATCH:
return String.format("type mismatch in union type", params);
case BUNDLE_WITH_COMMONJS:
return String.format("no bundle file generated: choose the 'commonjs' module kind when specifying a bundle file", params);
case BUNDLE_HAS_CYCLE:
return String.format("no bundle file generated: cycle detected in package graph %s", params);
case BUNDLE_HAS_NO_ENTRIES:
return String.format("no bundle file generated: no entries found, you must define at least one main method", params);
case BUNDLE_WITH_MODULE:
return String.format("bundle and module options are exclusive: choose one or the other", params);
case PACKAGE_NAME_CONTAINS_KEYWORD:
return String.format("a package name cannot contain top-level keyword(s): '%s'", params);
case WILDCARD_IMPORT:

View File

@ -243,9 +243,6 @@ public class JSweetTranspiler implements JSweetOptions {
if (!ProcessUtil.isInstalledWithNpm("tsc")) {
ProcessUtil.installNodePackage("typescript", true);
}
if (!ProcessUtil.isInstalledWithNpm("browserify")) {
ProcessUtil.installNodePackage("browserify", true);
}
}
/**
@ -608,66 +605,9 @@ public class JSweetTranspiler implements JSweetOptions {
ts2js(errorHandler, tsSources.toArray(new SourceFile[0]));
}
generateBundles(errorHandler, files);
logger.info("transpilation process finished in " + (System.currentTimeMillis() - transpilationStartTimestamp) + " ms");
}
private void generateBundles(ErrorCountTranspilationHandler errorHandler, SourceFile... files) {
if (bundle && context.useModules && errorHandler.getErrorCount() == 0) {
if (moduleKind != ModuleKind.commonjs) {
errorHandler.report(JSweetProblem.BUNDLE_WITH_COMMONJS, null, JSweetProblem.BUNDLE_WITH_COMMONJS.getMessage());
}
context.packageDependencies.topologicalSort(node -> {
if (errorHandler.getErrorCount() == 0) {
errorHandler.report(JSweetProblem.BUNDLE_HAS_CYCLE, null,
JSweetProblem.BUNDLE_HAS_CYCLE.getMessage(context.packageDependencies.toString()));
}
});
if (errorHandler.getErrorCount() > 0) {
return;
}
logger.info("checking for used modules: " + context.getUsedModules());
for (String module : context.getUsedModules()) {
if (module.endsWith(JSweetConfig.MODULE_FILE_NAME)) {
continue;
}
logger.debug("cheking for module " + module);
if (!ProcessUtil.isNodePackageInstalled(module)) {
logger.debug("installing " + module + "...");
// TODO: error reporting
ProcessUtil.installNodePackage(module, false);
}
}
Set<String> entries = new HashSet<>();
for (SourceFile f : files) {
if (context.entryFiles.contains(f.getJavaFile())) {
entries.add(f.jsFile.getAbsolutePath());
}
}
if (entries.isEmpty()) {
errorHandler.report(JSweetProblem.BUNDLE_HAS_NO_ENTRIES, null, JSweetProblem.BUNDLE_HAS_NO_ENTRIES.getMessage());
}
for (String entry : entries) {
String[] args = { entry };
File entryDir = new File(entry).getParentFile();
File bundleDirectory = bundlesDirectory != null ? bundlesDirectory : entryDir;
if (!bundleDirectory.exists()) {
bundleDirectory.mkdirs();
}
String bundleName = "bundle-" + entryDir.getName() + ".js";
args = ArrayUtils.addAll(args, "-o", new File(bundleDirectory, bundleName).getAbsolutePath());
logger.info("creating bundle file with browserify, args: " + StringUtils.join(args, ' '));
// TODO: keep original ts files sourcemaps:
// http://stackoverflow.com/questions/23453160/keep-original-typescript-source-maps-after-using-browserify
ProcessUtil.runCommand("browserify", ProcessUtil.USER_HOME_DIR, false, null, null, null, args);
}
}
}
private void createAuxiliaryModuleFiles(File rootDir) throws IOException {
if (context.useModules) {
// export-import submodules
@ -720,6 +660,11 @@ public class JSweetTranspiler implements JSweetOptions {
context.useModules = isUsingModules();
context.sourceFiles = files;
if (context.useModules && bundle) {
transpilationHandler.report(JSweetProblem.BUNDLE_WITH_MODULE, null, JSweetProblem.BUNDLE_WITH_MODULE.getMessage());
return;
}
new GlobalBeforeTranslationScanner(transpilationHandler, context).process(compilationUnits);
if (context.useModules) {