-
Notifications
You must be signed in to change notification settings - Fork 0
/
pantofile.js
96 lines (85 loc) · 2.69 KB
/
pantofile.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
/**
* Copyright (C) 2016 pantojs.xyz
* pantofile.js
*
* changelog
* 2016-07-20[20:00:54]:revised
*
* @author yanni4night@gmail.com
* @version 0.1.0
* @since 0.1.0
*/
'use strict';
const path = require('path');
module.exports = panto => {
panto.setOptions({
src: 'src',
output: 'output',
cwd: __dirname
});
require('load-panto-transformers')(panto);
require('time-panto')(panto);
let scriptIntegrity, styleIntegrity;
const RES_MAP = new Map();
// Clear first
panto.on('start', () => {
scriptIntegrity = styleIntegrity = undefined;
RES_MAP.clear();
});
const WRITE_ORIGIN = {
destname: file => path.join(path.dirname(file.filename), file.stamp)
};
const CACHE = {
isCacheable: true
};
// Image
panto.$('**/*.{jpg,png,gif}').tag('image').read().stamp(CACHE).aspect({
aspect: file => RES_MAP.set(file.filename, path.join(path.dirname(file.filename), file.stamp))
}).write(WRITE_ORIGIN);
// LESS
panto.$('styles/main.less').tag('less').read().less({
lessOptions: {
compress: true
}
}).resource({
getResourceAlias: (resname, filename) => path.relative(path.dirname(filename), RES_MAP.get(resname))
}).stamp(CACHE).integrity(CACHE).aspect({
aspect: file => {
styleIntegrity = file.integrity;
RES_MAP.set(file.filename, path.join(path.dirname(file.filename), file.stamp));
}
}).write(WRITE_ORIGIN);
// JSX
panto.$('**/*.jsx').tag('js').read().babel({
extend: __dirname + '/.babelrc'
}).browserify({
bundle: 'scripts/bundle.js',
entry: 'scripts/main.jsx',
process: {
env: {
NODE_ENV: 'production'
}
}
}).uglify({
isSlient: true
}).stamp(CACHE).integrity(CACHE).aspect({
aspect: file => {
scriptIntegrity = file.integrity;
RES_MAP.set(file.filename, path.join(path.dirname(file.filename), file.stamp));
}
}).write(WRITE_ORIGIN);
// HTML
panto.$('index.html').tag('index.html').read().resource({
getResourceAlias: name => RES_MAP.get(name)
}).replace({
replacements: [
['<!-- scripts -->', function () {
return `<script src="./${RES_MAP.get('scripts/bundle.js')}" integrity="${scriptIntegrity}"></script>`;
}],
['<!-- styles -->', function () {
return `<link rel="stylesheet" href="./${RES_MAP.get('styles/main.less')}" integrity="${styleIntegrity}"/>`;
}]
]
}).write();
panto.reportDependencies('index.html', '**/*.{less,css}', '**/*.{jsx,js}');
};