Search in sources :

Example 1 with TranspilerHook

use of org.nustaq.kontraktor.webapp.transpiler.TranspilerHook in project kontraktor by RuedigerMoeller.

the class DynamicResourceManager method getResource.

@Override
public Resource getResource(String initialPath) {
    String normalizedPath;
    byte[] debugLib = debugInstalls.get(initialPath);
    if (debugLib != null) {
        return new MyResource(initialPath, initialPath.substring(1), debugLib, "" + "text/javascript", null);
    }
    if (initialPath.startsWith("/")) {
        normalizedPath = initialPath.substring(1);
    } else {
        normalizedPath = initialPath;
    }
    if (!isDevMode()) {
        // lookup cache if not devmode
        if (normalizedPath.startsWith("f5_")) {
            lookupCache.clear();
            return super.getResource(initialPath);
        }
        Resource res = lookupCache.get(normalizedPath);
        if (res != null) {
            return res;
        }
        if (cachedIndexDir != null && cachedIndexDir.exists() && initialPath.endsWith("index.html")) {
            try {
                byte[] bytes = FileUtil.readFully(new File(cachedIndexDir, normalizedPath));
                Log.Info(this, "reading " + normalizedPath + " from static file " + new File(cachedIndexDir, normalizedPath).getAbsolutePath());
                return mightCache(normalizedPath, new MyResource(initialPath, normalizedPath, bytes, "text/html", !isDevMode() ? lastStartup : null));
            } catch (IOException e) {
                Log.Warn(this, e);
            }
        }
    }
    // import shim is applied to *index.html file only
    if (initialPath.endsWith("index.html") && importShim != null) {
        try {
            Element element = importShim.shimImports(normalizedPath);
            if (element == null) {
                return super.getResource(initialPath);
            }
            byte[] bytes = element.toString().getBytes("UTF-8");
            if (!devMode && cachedIndexDir != null) {
                // save to file for static serving
                cachedIndexDir.mkdirs();
                Files.write(new File(cachedIndexDir, normalizedPath).toPath(), bytes);
            }
            return mightCache(normalizedPath, new MyResource(initialPath, normalizedPath, bytes, "text/html", !isDevMode() ? lastStartup : null));
        } catch (IOException e) {
            Log.Warn(this, e);
        }
    } else {
        File file = dependencyResolver.locateResource(normalizedPath);
        if (file != null) {
            // FIMXE: could be done via TranspilerHook now ..
            final String fname = file.getName();
            if (fname.endsWith(".js") && minify) {
                try {
                    byte[] bytes = FileUtil.readFully(file);
                    bytes = runJSPostProcessors(jsPostProcessors, bytes);
                    return mightCache(normalizedPath, new MyResource(initialPath, normalizedPath, bytes, "text/javascript", !isDevMode() ? lastStartup : null));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (transpilerMap != null && transpilerMap.size() > 0) {
                try {
                    int idx = fname.lastIndexOf('.');
                    if (idx > 0) {
                        String ext = fname.substring(idx + 1).toLowerCase();
                        TranspilerHook transpilerHook = transpilerMap.get(ext);
                        if (transpilerHook != null) {
                            byte[] transpiled = transpilerHook.transpile(file, this, new HashMap());
                            if (minify) {
                                transpiled = runJSPostProcessors(jsPostProcessors, transpiled);
                            }
                            if (transpiled != null) {
                                return mightCache(normalizedPath, new MyResource(initialPath, normalizedPath, transpiled, "text/javascript", !isDevMode() ? lastStartup : null));
                            }
                        }
                    }
                } catch (Exception ex) {
                    Log.Error(this, ex);
                }
            }
            try {
                return mightCache(normalizedPath, getFileResource(file, initialPath));
            } catch (IOException e) {
                FSTUtil.rethrow(e);
            }
        }
    }
    return super.getResource(initialPath);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Element(org.jsoup.nodes.Element) FileResource(io.undertow.server.handlers.resource.FileResource) Resource(io.undertow.server.handlers.resource.Resource) IOException(java.io.IOException) TranspilerHook(org.nustaq.kontraktor.webapp.transpiler.TranspilerHook) File(java.io.File) IOException(java.io.IOException)

Example 2 with TranspilerHook

use of org.nustaq.kontraktor.webapp.transpiler.TranspilerHook in project kontraktor by RuedigerMoeller.

the class DynamicResourceManager method resolve.

@Override
public byte[] resolve(File baseDir, String name, Map<String, Object> alreadyProcessed) {
    try {
        // check relative to current dir
        File file = resolveFile(baseDir, name);
        byte[] bytes = null;
        // fixme: doubles logic of getResource
        if (file != null) {
            String normalizedPath = file.getCanonicalPath();
            if (alreadyProcessed.containsKey(normalizedPath)) {
                // System.out.println("REJECT:"+normalizedPath);
                return new byte[0];
            }
            // System.out.println("LOAD:"+normalizedPath);
            alreadyProcessed.put(normalizedPath, 1);
            final String fname = file.getName();
            if (transpilerMap != null && transpilerMap.size() > 0) {
                try {
                    int idx = fname.lastIndexOf('.');
                    if (idx > 0) {
                        String ext = fname.substring(idx + 1).toLowerCase();
                        // if a js file is resolved from index.jsx, treat it like jsx
                        // to support es6 imports.
                        TranspilerHook transpilerHook = transpilerMap.get(ext);
                        if (transpilerHook == null && alreadyProcessed.containsKey("JSXIndex") && "js".equals(ext)) {
                            transpilerHook = transpilerMap.get("jsx");
                        }
                        if (transpilerHook != null) {
                            bytes = transpilerHook.transpile(file, this, alreadyProcessed);
                        }
                    }
                } catch (Exception ex) {
                    Log.Error(this, ex);
                }
            }
            if (bytes == null)
                bytes = FileUtil.readFully(file);
        }
        return bytes;
    } catch (Exception e) {
        FSTUtil.rethrow(e);
    }
    return null;
}
Also used : TranspilerHook(org.nustaq.kontraktor.webapp.transpiler.TranspilerHook) File(java.io.File) IOException(java.io.IOException)

Example 3 with TranspilerHook

use of org.nustaq.kontraktor.webapp.transpiler.TranspilerHook in project kontraktor by RuedigerMoeller.

the class DynamicResourceManager method retrieveBytes.

/**
 * invoke transpilers during prodmode inlining
 * @param impFi
 * @return
 */
@Override
public byte[] retrieveBytes(File impFi) {
    String fname = impFi.getName();
    int idx = fname.indexOf('.');
    if (idx >= 0) {
        String ext = fname.substring(idx + 1).toLowerCase();
        TranspilerHook transpilerHook = transpilerMap.get(ext);
        if (transpilerHook != null) {
            return transpilerHook.transpile(impFi, this, new HashMap());
        }
    }
    return null;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TranspilerHook(org.nustaq.kontraktor.webapp.transpiler.TranspilerHook)

Aggregations

TranspilerHook (org.nustaq.kontraktor.webapp.transpiler.TranspilerHook)3 File (java.io.File)2 IOException (java.io.IOException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 FileResource (io.undertow.server.handlers.resource.FileResource)1 Resource (io.undertow.server.handlers.resource.Resource)1 Element (org.jsoup.nodes.Element)1