forked from ractivejs/ractive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobblefile.js
165 lines (144 loc) · 4.58 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
/*global require, module, __dirname, process */
/*eslint no-var:0, object-shorthand:0 */
var gobble = require( 'gobble' );
var sander = require( 'sander' );
var junk = require( 'junk' );
var Promise = sander.Promise;
var rollup = require( 'rollup' );
var rollupBuble = require( 'rollup-plugin-buble' );
var sandbox = gobble( 'sandbox' ).moveTo( 'sandbox' );
var version = require( './package.json' ).version;
var hash = process.env.COMMIT_HASH || 'unknown';
var versionExt = ~version.indexOf( '-edge' ) ? '-' + hash : '';
var bubleLegacyOptions = { target: { ie: 8 } };
var src = gobble( 'src' );
var es5 = src.transform( 'buble', { target: { ie: 8 } });
var lib;
var test;
function noop () {}
function adjustAndSkip ( pattern ) {
return { transform: function ( src, path ) {
if ( /(Ractive\.js|utils[\/\\]log\.js)$/.test( path ) ) {
return src.replace( /<@version@>/g, version + versionExt );
}
if ( pattern && pattern.test( path ) ) {
return 'export default null;';
}
return src;
} };
}
function noConflict ( src ) {
src = src.replace( 'global.Ractive = factory()', '(function() { var current = global.Ractive; var next = factory(); next.noConflict = function() { global.Ractive = current; return next; }; return global.Ractive = next; })()' );
return src;
}
function buildLib ( dest, pattern ) {
return es5.transform( 'rollup', {
plugins: [ adjustAndSkip( pattern ) ],
format: 'umd',
entry: 'Ractive.js',
moduleName: 'Ractive',
dest: dest,
banner: banner
}).transform( noConflict );
}
var banner = sander.readFileSync( __dirname, 'src/banner.js' ).toString()
.replace( '${version}', version )
.replace( '${time}', new Date() )
.replace( '${commitHash}', hash );
if ( gobble.env() === 'production' ) {
lib = gobble([
buildLib( 'ractive-legacy.js' ),
buildLib( 'ractive.js', /legacy\.js/ ),
buildLib( 'ractive.runtime.js', /legacy\.js|_parse\.js/ ),
buildLib( 'ractive-legacy.runtime.js', /_parse\.js/ )
]).transform( noConflict );
} else {
lib = gobble([
buildLib( 'ractive-legacy.js' ),
sandbox
]);
}
test = (function () {
var testFiles = sander.lsrSync( 'test/browser-tests' ).filter( junk.not );
var globals = {
qunit: 'QUnit',
simulant: 'simulant'
};
var browserTests = gobble( 'test/browser-tests' ).moveTo( 'browser-tests' ).transform( function bundleTests ( inputdir, outputdir, options ) {
testFiles.sort();
var modules = [];
var files = Promise.all( testFiles.map( function( f ) {
f = f.replace(/\\/g, '/');
return sander.readFile( inputdir, 'browser-tests', f ).then( function( data ) {
data = data.toString( 'utf8' );
if ( f.indexOf( 'polyfills.js' ) !== -1 || data.indexOf( 'initModule(' ) !== -1 ) {
modules.push( f );
return sander.link( inputdir, 'browser-tests', f ).to( outputdir, 'browser-tests', f );
} else {
console.log( 'WARNING: not including test ' + f );
}
});
}));
return files.then( function() {
var imprts = [], stmts = [];
modules.map( function( f ) {
var mod = f.replace( /[^a-z]/gi, '_' );
imprts.push( 'import ' + mod + ' from \'./' + f + '\';' );
stmts.push( mod + '();' );
});
return sander.writeFile( outputdir, 'browser-tests/all.js', imprts.join( '\n' ) + stmts.join( '\n' ) );
});
});
var testModules = gobble([
es5,
gobble([ browserTests, gobble( 'test/__support/js' )
.moveTo( 'browser-tests' ) ])
.transform( 'buble', bubleLegacyOptions )
]).transform( 'rollup', {
plugins: [ adjustAndSkip() ],
format: 'umd',
entry: 'browser-tests/all.js',
moduleName: 'tests',
dest: 'all.js',
globals: {
qunit: 'QUnit',
simulant: 'simulant'
}
});
return gobble([
gobble( 'test/__support/index.html' )
.transform( 'replace', {
scriptBlock: '<script src="all.js"></script>'
}),
testModules,
gobble( 'test/__support/files' ),
gobble( 'test/node-tests' ).moveTo( 'node-tests' ),
gobble( 'test/__support/js/samples' )
.include( '*.js' )
.transform( function bundleSamples ( inputDir, outputDir, options ) {
return sander.lsr(inputDir).then( function ( files ) {
var promises = files.map( function ( file ) {
return rollup.rollup({
entry: inputDir + '/' + file,
plugins: [ rollupBuble( bubleLegacyOptions ) ],
globals: globals,
onwarn: noop
}).then( function ( bundle ) {
return bundle.write({
dest: outputDir + '/' + file,
format: 'cjs',
globals: globals,
moduleName: 'tests'
});
});
});
return Promise.all(promises);
});
})
.moveTo( 'node-tests/samples' )
]).moveTo( 'test' );
})();
module.exports = gobble([
lib,
test
]);