Skip to content

Commit 73ff0ac

Browse files
committed
Rollup.js enhancements
* upgrade to latest 0.41.6 * babel+alias+legacy plugins fix #721 fix #722
1 parent 35879d2 commit 73ff0ac

13 files changed

Lines changed: 10511 additions & 6545 deletions

File tree

jooby-assets-j2v8/src/main/java/org/jooby/assets/V8Context.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public V8Object hash() {
8080
return register(new V8Object(v8));
8181
}
8282

83+
public V8Function function(final JavaCallback callback) {
84+
return register(new V8Function(v8, callback));
85+
}
86+
8387
public V8Object hash(final Map<String, Object> hash) {
8488
return register(V8ObjectUtils.toV8Object(v8, hash));
8589
}

jooby-assets-rollup/src/main/java/org/jooby/assets/Rollup.java

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@
1818
*/
1919
package org.jooby.assets;
2020

21+
import java.nio.file.FileSystems;
22+
import java.nio.file.Path;
23+
import java.nio.file.PathMatcher;
24+
import java.nio.file.Paths;
25+
import java.util.Optional;
26+
2127
import org.jooby.MediaType;
2228

29+
import com.eclipsesource.v8.V8;
30+
import com.eclipsesource.v8.V8Array;
31+
import com.eclipsesource.v8.V8Object;
32+
import com.eclipsesource.v8.utils.V8ObjectUtils;
2333
import com.typesafe.config.Config;
2434

2535
/**
@@ -38,39 +48,98 @@
3848
*
3949
* pipeline {
4050
* ...
51+
* dev: [rollup]
4152
* dist: [rollup]
4253
* }
4354
* }
4455
* </pre>
4556
*
4657
* <h2>options</h2>
58+
* <h3>generate</h3>
4759
* <pre>
48-
* assets {
49-
* fileset {
50-
* home: ...
60+
* rollup {
61+
* genereate {
62+
* format: es
63+
* }
5164
* }
65+
* </pre>
5266
*
53-
* pipeline {
54-
* ...
55-
* dist: [rollmap]
67+
* <p>
68+
* See
69+
* <a href="https://github.com/rollup/rollup/wiki/JavaScript-API#bundlegenerate-options-">generate
70+
* options</a>.
71+
* </p>
72+
*
73+
* <h3>plugins</h3>
74+
*
75+
* <h4>babel</h4>
76+
*
77+
* <pre>
78+
* rollup {
79+
* plugins {
80+
* babel {
81+
* presets: [[es2015, {modules: false}]]
82+
* }
83+
* }
84+
* }
85+
* </pre>
86+
*
87+
* <p>
88+
* See <a href="https://babeljs.io/">https://babeljs.io</a> for more options.
89+
* </p>
90+
*
91+
* <h4>legacy</h4>
92+
* <p>
93+
* This plugins add a <code>export default</code> line to legacy modules:
94+
* </p>
95+
*
96+
* <pre>
97+
* rollup {
98+
* plugins {
99+
* legacy {
100+
* "/js/lib/react.js": React
101+
* }
102+
* }
56103
* }
104+
* </pre>
57105
*
106+
* <h4>alias</h4>
107+
* <p>
108+
* Set an alias to a common (probably long) path.
109+
* </p>
110+
*
111+
* <pre>
58112
* rollup {
59-
* output {
60-
* format: amd
113+
* plugins {
114+
* alias {
115+
* "/js/lib/react.js": "react"
116+
* }
61117
* }
62118
* }
63-
* }
64119
* </pre>
120+
*
121+
* <p>
122+
* Instead of:
123+
* </p>
124+
* <pre>
125+
* import React from 'js/lib/react.js';
126+
* </pre>
127+
*
65128
* <p>
66-
* See: <a href="https://github.com/rollup/rollup/wiki/JavaScript-API">rollup.js options</a>.
129+
* Now, you can import a module like:
67130
* </p>
131+
* <pre>
132+
* import React from 'react';
133+
* </pre>
68134
*
69135
* @author edgar
70136
* @since 0.12.0
71137
*/
72138
public class Rollup extends AssetProcessor {
73139

140+
static final PathMatcher TRUE = p -> true;
141+
static final PathMatcher FALSE = p -> false;
142+
74143
@Override
75144
public boolean matches(final MediaType type) {
76145
return MediaType.js.matches(type);
@@ -79,9 +148,40 @@ public boolean matches(final MediaType type) {
79148
@Override
80149
public String process(final String filename, final String source, final Config conf)
81150
throws Exception {
82-
return V8Context.run("window", v8 -> {
83-
return v8.invoke("rollup.js", source, options(), filename);
151+
return V8Context.run("window", ctx -> {
152+
V8 v8 = ctx.v8;
153+
154+
V8Object j2v8 = ctx.hash();
155+
j2v8.add("createFilter", ctx.function((receiver, args) -> {
156+
PathMatcher includes = at(args, 0)
157+
.map(it -> FileSystems.getDefault().getPathMatcher("glob:" + it))
158+
.orElse(TRUE);
159+
PathMatcher excludes = at(args, 1)
160+
.map(it -> FileSystems.getDefault().getPathMatcher("glob:" + it))
161+
.orElse(FALSE);
162+
163+
return ctx.function((self, arguments) -> {
164+
Path path = Paths.get(arguments.get(0).toString());
165+
if (includes.matches(path)) {
166+
return !excludes.matches(path);
167+
}
168+
return false;
169+
});
170+
}));
171+
172+
v8.add("j2v8", j2v8);
173+
return ctx.invoke("rollup.js", source, options(), filename);
84174
});
85175
}
86176

177+
private Optional<Object> at(final V8Array args, final int i) {
178+
if (i < args.length()) {
179+
Object value = V8ObjectUtils.getValue(args, i);
180+
if (value == V8.getUndefined()) {
181+
return Optional.empty();
182+
}
183+
return Optional.ofNullable(value);
184+
}
185+
return Optional.empty();
186+
}
87187
}

jooby-assets-rollup/src/main/resources/lib/babel-6.24.0.min.js

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jooby-assets-rollup/src/main/resources/lib/prim.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* prim 0.0.7 Copyright (c) 2012-2013, The Dojo Foundation All Rights Reserved.
2+
* prim 1.0.0 Copyright (c) 2012-2016, The Dojo Foundation All Rights Reserved.
33
* Available via the MIT or new BSD license.
44
* see: http://github.com/requirejs/prim for details
55
*/
@@ -97,7 +97,10 @@ var prim;
9797

9898
try {
9999
var then = v && v.then;
100-
if (isFunObj(v) && typeof then === 'function') {
100+
if (isFunObj(v) && typeof then === 'function' &&
101+
// if error, keep on error pathway if a promise,
102+
// 2.2.7.2 tests.
103+
prop !== 'e') {
101104
f2 = makeFulfill();
102105
then.call(v, f2.resolve, f2.reject);
103106
} else {

0 commit comments

Comments
 (0)