forked from ractivejs/ractive
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gobblefile.js
196 lines (170 loc) · 4.78 KB
/
gobblefile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var gobble = require( 'gobble' ),
sander = require( 'sander' ),
junk = require( 'junk' ),
Promise = sander.Promise,
path = require( 'path' ),
esperanto = require( 'esperanto' ),
sandbox = gobble( 'sandbox' ).moveTo( 'sandbox' ),
version = require( './package.json' ).version,
es5, lib, test;
var babelTransformWhitelist = [
'es3.memberExpressionLiterals',
'es3.propertyLiterals',
'es6.arrowFunctions',
'es6.blockScoping',
'es6.constants',
'es6.destructuring',
'es6.parameters.default',
'es6.parameters.rest',
'es6.properties.shorthand',
'es6.templateLiterals'
];
es5 = gobble( 'src' ).transform( 'babel', {
whitelist: babelTransformWhitelist
});
lib = (function () {
var banner = sander.readFileSync( __dirname, 'src/banner.js' ).toString()
.replace( '${version}', version )
.replace( '${time}', new Date() )
.replace( '${commitHash}', process.env.COMMIT_HASH || 'unknown' );
var lib = [
es5.transform( 'esperanto-bundle', {
type: 'umd',
transform: function ( src, path ) {
if ( /(Ractive\.js|utils\/log\.js)$/.test( path ) ) {
return src.replace( /<@version@>/g, version );
}
return src;
},
banner: banner,
entry: 'Ractive.js',
name: 'Ractive',
dest: 'ractive-legacy.js'
})
];
if ( gobble.env() === 'production' ) {
// Add non-legacy and runtime-only builds
lib.push(
es5.transform( 'esperanto-bundle', {
type: 'umd',
transform: function ( src, path ) {
if ( /(Ractive\.js|utils\/log\.js)$/.test( path ) ) {
return src.replace( /<@version@>/g, version );
}
if ( /legacy\.js/.test( path ) ) {
return 'export default null;';
}
return src;
},
banner: banner,
entry: 'Ractive.js',
name: 'Ractive',
dest: 'ractive.js'
}),
es5.transform( 'esperanto-bundle', {
type: 'umd',
transform: function ( src, path ) {
if ( /(Ractive\.js|utils\/log\.js)$/.test( path ) ) {
return src.replace( /<@version@>/g, version );
}
if ( /legacy\.js|_parse\.js/.test( path ) ) {
return 'export default null;';
}
return src;
},
banner: banner,
entry: 'Ractive.js',
name: 'Ractive',
dest: 'ractive.runtime.js'
}),
es5.transform( 'esperanto-bundle', {
type: 'umd',
transform: function ( src, path ) {
if ( /(Ractive\.js|utils\/log\.js)$/.test( path ) ) {
return src.replace( /<@version@>/g, version );
}
if ( /_parse\.js/.test( path ) ) {
return 'export default null;';
}
return src;
},
banner: banner,
entry: 'Ractive.js',
name: 'Ractive',
dest: 'ractive-legacy.runtime.js'
})
);
}
else {
lib.push( sandbox );
}
// Combine sourcemaps from babel and esperanto
lib = lib.map( function ( node ) {
return node.transform( 'sorcery' );
});
return lib;
})();
test = (function () {
function compileTemplate ( str ) {
return function ( data ) {
return str.replace( /\${([^}]+)}/g, function ( match, $1 ) {
return $1 in data ? data[ $1 ] : match;
});
};
}
var templates = {
testpage: compileTemplate( sander.readFileSync( 'test/templates/testpage.html' ).toString() ),
index: compileTemplate( sander.readFileSync( 'test/templates/index.html' ).toString() )
};
var testModules = gobble([
gobble([
gobble( 'test/__tests' ).moveTo( '__tests' ),
gobble( 'test/testdeps' )
]).transform( 'babel', {
whitelist: babelTransformWhitelist,
sourceMap: false
}),
es5
]).transform( function bundleTests ( inputdir, outputdir, options ) {
return sander.lsr( inputdir, '__tests' ).then( function ( testModules ) {
var promises = testModules.filter( junk.not ).sort().map( function ( mod ) {
return esperanto.bundle({
base: inputdir,
entry: '__tests/' + mod
}).then( function ( bundle ) {
return sander.writeFile( outputdir, mod, bundle.concat({}).code );
});
});
return Promise.all( promises ).then( function () {
// index page
var index = templates.index({
scriptBlock: testModules.map( function ( src ) {
return '<script src="' + src + '"></script>';
}).join( '\n\t' )
});
return sander.writeFile( outputdir, 'index.html', index );
});
});
});
var testPages = testModules.transform( function () {
return templates.testpage({
testModule: path.basename( this.filename ),
name: this.filename.replace( /\.js$/, '' )
});
}, { accept: '.js', ext: '.html' });
return gobble([
testModules,
testPages,
gobble( 'test/root' ),
gobble( 'test/__nodetests' ).moveTo( '__nodetests' ),
gobble( 'test/testdeps/samples' )
.include( '*.js' )
.transform( 'babel', { whitelist: babelTransformWhitelist, sourceMap: false })
.transform( 'esperanto', { type: 'cjs', sourceMap: false })
.moveTo( '__nodetests/samples' )
]).moveTo( 'test' );
})();
module.exports = gobble([
lib,
test
]);