Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public V8Object hash() {
return register(new V8Object(v8));
}

public V8Function function(final JavaCallback callback) {
return register(new V8Function(v8, callback));
}

public V8Object hash(final Map<String, Object> hash) {
return register(V8ObjectUtils.toV8Object(v8, hash));
}
Expand Down
124 changes: 112 additions & 12 deletions jooby-assets-rollup/src/main/java/org/jooby/assets/Rollup.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@
*/
package org.jooby.assets;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.Optional;

import org.jooby.MediaType;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;
import com.eclipsesource.v8.utils.V8ObjectUtils;
import com.typesafe.config.Config;

/**
Expand All @@ -38,39 +48,98 @@
*
* pipeline {
* ...
* dev: [rollup]
* dist: [rollup]
* }
* }
* </pre>
*
* <h2>options</h2>
* <h3>generate</h3>
* <pre>
* assets {
* fileset {
* home: ...
* rollup {
* genereate {
* format: es
* }
* }
* </pre>
*
* pipeline {
* ...
* dist: [rollmap]
* <p>
* See
* <a href="https://github.com/rollup/rollup/wiki/JavaScript-API#bundlegenerate-options-">generate
* options</a>.
* </p>
*
* <h3>plugins</h3>
*
* <h4>babel</h4>
*
* <pre>
* rollup {
* plugins {
* babel {
* presets: [[es2015, {modules: false}]]
* }
* }
* }
* </pre>
*
* <p>
* See <a href="https://babeljs.io/">https://babeljs.io</a> for more options.
* </p>
*
* <h4>legacy</h4>
* <p>
* This plugins add a <code>export default</code> line to legacy modules:
* </p>
*
* <pre>
* rollup {
* plugins {
* legacy {
* "/js/lib/react.js": React
* }
* }
* }
* </pre>
*
* <h4>alias</h4>
* <p>
* Set an alias to a common (probably long) path.
* </p>
*
* <pre>
* rollup {
* output {
* format: amd
* plugins {
* alias {
* "/js/lib/react.js": "react"
* }
* }
* }
* }
* </pre>
*
* <p>
* Instead of:
* </p>
* <pre>
* import React from 'js/lib/react.js';
* </pre>
*
* <p>
* See: <a href="https://github.com/rollup/rollup/wiki/JavaScript-API">rollup.js options</a>.
* Now, you can import a module like:
* </p>
* <pre>
* import React from 'react';
* </pre>
*
* @author edgar
* @since 0.12.0
*/
public class Rollup extends AssetProcessor {

static final PathMatcher TRUE = p -> true;
static final PathMatcher FALSE = p -> false;

@Override
public boolean matches(final MediaType type) {
return MediaType.js.matches(type);
Expand All @@ -79,9 +148,40 @@ public boolean matches(final MediaType type) {
@Override
public String process(final String filename, final String source, final Config conf)
throws Exception {
return V8Context.run("window", v8 -> {
return v8.invoke("rollup.js", source, options(), filename);
return V8Context.run("window", ctx -> {
V8 v8 = ctx.v8;

V8Object j2v8 = ctx.hash();
j2v8.add("createFilter", ctx.function((receiver, args) -> {
PathMatcher includes = at(args, 0)
.map(it -> FileSystems.getDefault().getPathMatcher("glob:" + it))
.orElse(TRUE);
PathMatcher excludes = at(args, 1)
.map(it -> FileSystems.getDefault().getPathMatcher("glob:" + it))
.orElse(FALSE);

return ctx.function((self, arguments) -> {
Path path = Paths.get(arguments.get(0).toString());
if (includes.matches(path)) {
return !excludes.matches(path);
}
return false;
});
}));

v8.add("j2v8", j2v8);
return ctx.invoke("rollup.js", source, options(), filename);
});
}

private Optional<Object> at(final V8Array args, final int i) {
if (i < args.length()) {
Object value = V8ObjectUtils.getValue(args, i);
if (value == V8.getUndefined()) {
return Optional.empty();
}
return Optional.ofNullable(value);
}
return Optional.empty();
}
}
25 changes: 25 additions & 0 deletions jooby-assets-rollup/src/main/resources/lib/babel-6.24.0.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions jooby-assets-rollup/src/main/resources/lib/prim.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* prim 0.0.7 Copyright (c) 2012-2013, The Dojo Foundation All Rights Reserved.
* prim 1.0.0 Copyright (c) 2012-2016, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/requirejs/prim for details
*/
Expand Down Expand Up @@ -97,7 +97,10 @@ var prim;

try {
var then = v && v.then;
if (isFunObj(v) && typeof then === 'function') {
if (isFunObj(v) && typeof then === 'function' &&
// if error, keep on error pathway if a promise,
// 2.2.7.2 tests.
prop !== 'e') {
f2 = makeFulfill();
then.call(v, f2.resolve, f2.reject);
} else {
Expand Down
Loading