mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
replace Browserify bundling with JSweet bundling
Browserify bundling is of course still possible but requires external configuration
This commit is contained in:
parent
8f36d8a65c
commit
2dcb567876
@ -222,19 +222,9 @@ public enum JSweetProblem {
|
|||||||
*/
|
*/
|
||||||
UNION_TYPE_MISMATCH(Severity.ERROR),
|
UNION_TYPE_MISMATCH(Severity.ERROR),
|
||||||
/**
|
/**
|
||||||
* Raised when an union type assignment is not compatible with one of the
|
* Raised when trying to create a bundle with a module kind selected.
|
||||||
* union-ed types.
|
|
||||||
*/
|
*/
|
||||||
BUNDLE_WITH_COMMONJS(Severity.WARNING),
|
BUNDLE_WITH_MODULE(Severity.ERROR),
|
||||||
/**
|
|
||||||
* 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),
|
|
||||||
/**
|
/**
|
||||||
* Raised when a package is named after an invalid name (typically a
|
* Raised when a package is named after an invalid name (typically a
|
||||||
* TypeScript keyword).
|
* TypeScript keyword).
|
||||||
@ -402,12 +392,8 @@ public enum JSweetProblem {
|
|||||||
return String.format("type mismatch, expecting '%s' (inferred from the indexed getter type)", params);
|
return String.format("type mismatch, expecting '%s' (inferred from the indexed getter type)", params);
|
||||||
case UNION_TYPE_MISMATCH:
|
case UNION_TYPE_MISMATCH:
|
||||||
return String.format("type mismatch in union type", params);
|
return String.format("type mismatch in union type", params);
|
||||||
case BUNDLE_WITH_COMMONJS:
|
case BUNDLE_WITH_MODULE:
|
||||||
return String.format("no bundle file generated: choose the 'commonjs' module kind when specifying a bundle file", params);
|
return String.format("bundle and module options are exclusive: choose one or the other", 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 PACKAGE_NAME_CONTAINS_KEYWORD:
|
case PACKAGE_NAME_CONTAINS_KEYWORD:
|
||||||
return String.format("a package name cannot contain top-level keyword(s): '%s'", params);
|
return String.format("a package name cannot contain top-level keyword(s): '%s'", params);
|
||||||
case WILDCARD_IMPORT:
|
case WILDCARD_IMPORT:
|
||||||
|
|||||||
@ -243,9 +243,6 @@ public class JSweetTranspiler implements JSweetOptions {
|
|||||||
if (!ProcessUtil.isInstalledWithNpm("tsc")) {
|
if (!ProcessUtil.isInstalledWithNpm("tsc")) {
|
||||||
ProcessUtil.installNodePackage("typescript", true);
|
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]));
|
ts2js(errorHandler, tsSources.toArray(new SourceFile[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
generateBundles(errorHandler, files);
|
|
||||||
logger.info("transpilation process finished in " + (System.currentTimeMillis() - transpilationStartTimestamp) + " ms");
|
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 {
|
private void createAuxiliaryModuleFiles(File rootDir) throws IOException {
|
||||||
if (context.useModules) {
|
if (context.useModules) {
|
||||||
// export-import submodules
|
// export-import submodules
|
||||||
@ -720,6 +660,11 @@ public class JSweetTranspiler implements JSweetOptions {
|
|||||||
context.useModules = isUsingModules();
|
context.useModules = isUsingModules();
|
||||||
context.sourceFiles = files;
|
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);
|
new GlobalBeforeTranslationScanner(transpilationHandler, context).process(compilationUnits);
|
||||||
|
|
||||||
if (context.useModules) {
|
if (context.useModules) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user