1818 */
1919package 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+
2127import 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 ;
2333import com .typesafe .config .Config ;
2434
2535/**
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 */
72138public 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}
0 commit comments