This repository was archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrailsRegistry.js
213 lines (186 loc) · 6.02 KB
/
railsRegistry.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import BaseRegistry from './baseRegistry'
import Preset from '../preset'
import Clean from '../clean'
import CleanDigest from '../cleanDigest'
import CssNano from '../cssNano'
import Images from '../images'
import Sass from '../sass'
import RollupAmd from '../rollupAmd'
import RollupIife from '../rollupIife'
import RollupCjs from '../rollupCjs'
import RollupCjsBundled from '../rollupCjsBundled'
import RollupUmd from '../rollupUmd'
import ScssLint from '../scssLint'
import EsLint from '../eslint'
import Rev from '../rev'
import RevReplace from '../revReplace'
import Uglify from '../uglify'
import Aggregate from '../aggregate'
import parallel from '../util/parallel'
import series from '../util/series'
import tmpDir from '../util/tmpDir'
import clean from '../util/clean'
// per class name defaults that can be overridden
export const Default = {
// Class-based configuration overrides:
// - these may be a single config hash or array of config hashes (last hash overrides earlier hashes)
// - in some cases, passing false for the class name may be implemented as omitting the registration of the recipe (see implementation of #init for details)
RollupIife: true, // absent any overrides, build iife
RollupCjs: false,
RollupCjsBundled: false,
RollupAmd: false,
RollupUmd: false
}
/**
* gulp.registry(new RailsRegistry(...configs))
*/
const RailsRegistry = class extends BaseRegistry {
/**
* @param config - customized overrides of the Default, last one wins
*/
constructor(...configs) {
super(Default, {preset: Preset.rails()}, ...configs)
}
init(gulp) {
let preset = this.config.preset
const js = new Aggregate(gulp, 'js',
series(gulp,
this.esLinters(gulp),
this.rollups(gulp)
),
...this.keyConfig('js')
)
const css = new Aggregate(gulp, 'css',
series(gulp,
this.scssLinters(gulp),
new Sass(gulp, preset, ...this.classConfig(Sass))
),
...this.keyConfig('css')
)
const defaultRecipes = new Aggregate(gulp, 'default',
series(gulp,
new Clean(gulp, preset),
parallel(gulp,
new Images(gulp, preset, ...this.classConfig(Images)),
js,
css
)
),
...this.keyConfig('default')
)
// Create the production assets
const tmpDirObj = tmpDir()
const minifiedAssetsDir = tmpDirObj.name
this.debug(`tmpDir for minified assets: ${minifiedAssetsDir}`)
// digests need to be one task, tmpDir makes things interdependent
const digests = {task: false, watch: false}
const digest = new Aggregate(gulp, 'digest',
series(gulp,
new CleanDigest(gulp, preset, digests),
// minify application.(css|js) to a tmp directory
parallel(gulp,
new Uglify(gulp, preset, digests, {dest: minifiedAssetsDir, concat: {dest: 'application.js'}}, ...this.classConfig(Uglify)),
new CssNano(gulp, preset, digests, {dest: minifiedAssetsDir, minExtension: false}, ...this.classConfig(CssNano))
),
// rev minified css|js from tmp
new Rev(gulp, preset, digests, {
source: {
options: {
cwd: minifiedAssetsDir
}
}
}),
// rev all the rest from the debug dir (except the minified application(css|js)) and merge with the previous rev
new Rev(gulp, preset, digests, {
source: {
options: {
ignore: ['**/application.js', '**/*.js.map', '**/application.css']
}
}
}),
// rewrite all revised urls in the assets i.e. css, js
new RevReplace(gulp, preset, digests),
// cleanup the temp files and folders
clean(gulp, `${minifiedAssetsDir}/**`)
),
...this.keyConfig('digest')
)
// default then digest
new Aggregate(gulp, 'all',
series(gulp,
defaultRecipes,
digest
),
...this.keyConfig('all')
)
}
esLinters(gulp) {
return new EsLint(gulp, this.config.preset, ...this.classConfig(EsLint))
}
scssLinters(gulp){
return new ScssLint(gulp, this.config.preset, ...this.classConfig(ScssLint))
}
rollups(gulp) {
let preset = this.config.preset
// javascripts may have two different needs, one standard iife, and one cjs for rails engines
let rollups = []
// All rails apps need the iife which is ultimately the application.js.
// Some rails engines may want it only for the purpose of ensuring that libraries can be included properly otherwise the build breaks (a good thing)
if (this.config.RollupIife) {
rollups.push(
new RollupIife(gulp, preset, {
options: {
dest: 'application.js',
moduleName: 'App'
}
}, ...this.classConfig(RollupIife))
)
}
// Rails apps probably don't need commonjs, so by default it is off.
// Rails engines DO need commonjs, it is consumed by the rails app like any other node library.
if (this.config.RollupCjs) {
rollups.push(
new RollupCjs(gulp, preset, {
options: {
dest: 'application.cjs.js',
moduleName: 'App'
}
}, ...this.classConfig(RollupCjs))
)
}
if (this.config.RollupCjsBundled) {
rollups.push(
new RollupCjsBundled(gulp, preset, {
options: {
dest: 'application.cjs-bundled.js',
moduleName: 'App'
}
}, ...this.classConfig(RollupCjsBundled))
)
}
if (this.config.RollupUmd) {
rollups.push(
new RollupUmd(gulp, preset, {
options: {
dest: 'application.umd.js',
moduleName: 'App'
}
}, ...this.classConfig(RollupUmd))
)
}
if (this.config.RollupAmd) {
rollups.push(
new RollupAmd(gulp, preset, {
options: {
dest: 'application.amd.js',
moduleName: 'App'
}
}, ...this.classConfig(RollupAmd))
)
}
return parallel(gulp,
...rollups
)
}
}
export default RailsRegistry