forked from nyaruka/temba-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateIncludesPlugin.js
104 lines (92 loc) · 3.19 KB
/
CreateIncludesPlugin.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
const fs = require('fs');
const path = require('path');
var mkdirp = require('mkdirp');
let templates = '';
function CreateIncludesPlugin(options) {
// where we will put our django templates
templates = options.templates;
}
function getScript(filename, nomodule) {
return (
'<script src="{{STATIC_URL}}@nyaruka/temba-components/build/' +
filename +
'"' +
(nomodule ? ' nomodule="">' : '>') +
'</script>\n'
);
}
CreateIncludesPlugin.prototype.apply = function (compiler) {
let loaderFile = '';
const bodyScripts = [];
compiler.plugin(
'emit',
function (compilation, callback) {
Object.keys(compilation.assets).forEach(function (filename) {
// The loader file is responsible for bringing in polyfills as
// necessary. It references files that need to be located
// inside our static directory
if (filename.startsWith('loader.')) {
loaderFile = 'prefixed.' + filename;
let loaderSource = compilation.assets[filename].source();
loaderSource = loaderSource.replace(
/\"\.\/temba-components/g,
'static_url + "@nyaruka/temba-components/build/temba-components'
);
loaderSource = loaderSource.replace(
/\"polyfills\//g,
'static_url + "@nyaruka/temba-components/build/polyfills/'
);
mkdirp(path.resolve(compiler.options.output.path)).then(() => {
fs.writeFileSync(
path.resolve(compiler.options.output.path, loaderFile),
loaderSource
);
});
}
// our main components file, it'll be included in the head of our template
if (
filename.startsWith('temba-components') &&
filename.endsWith('.js')
) {
mkdirp(templates).then((err) => {
fs.writeFileSync(
path.resolve(templates, 'components-head.html'),
'<link rel="preload" href="{{STATIC_URL}}@nyaruka/temba-components/build/' +
filename +
'" as="script"></link>'
);
});
}
// we have some polyfills that are always present in our body, keep track of those here
if (
filename.indexOf('.js.map') === -1 &&
(filename.startsWith('polyfills/core-js') ||
filename.startsWith('polyfills/regenerator'))
) {
bodyScripts.push(filename);
}
});
// our main body template has a couple universal polyfills and our dynamic loader for any remaining
// ones the current browser might need
mkdirp(templates).then((err) => {
fs.writeFileSync(
path.resolve(templates, 'components-body.html'),
getScript(bodyScripts[0], true) +
getScript(bodyScripts[1], true) +
getScript(loaderFile, false)
);
callback();
});
}.bind(this)
);
compiler.plugin('done', function () {
console.log('\x1b[36m%s\x1b[0m', 'Generated templates in ' + templates);
console.log(
'\x1b[36m%s\x1b[0m',
'Generated static includes in ' +
compiler.options.output.path +
'/components'
);
});
};
module.exports = CreateIncludesPlugin;