-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (123 loc) · 3.62 KB
/
index.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
'use strict';
// Defaults
var _options = {
svg: {
xmlns: 'http://www.w3.org/2000/svg',
style: 'position:absolute; width: 0; height: 0'
},
loop: 2,
svgoOptions: {},
name: 'sprite.[hash].svg',
prefix: 'icon-'
};
// Depends
var _ = require('lodash');
var path = require('path');
var url = require('url');
var glob = require('glob');
var slash = require('slash');
var utils = require('./helpers/utils');
var ConcatSource = require('webpack/lib/ConcatSource');
var ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers');
/**
* Constructor
* @param {string} input [description]
* @param {string} output [description]
* @param {object} options [description]
* @return {object}
*/
var WebpackSvgStore = function(input, output, options) {
// set attributes
this.input = input;
this.output = output;
this.options = _.merge({}, _options, options);
return this;
};
/**
* Build files map
* @param {string} input Destination path
* @return {array} Array of paths
*/
WebpackSvgStore.prototype.filesMap = function(input, cb) {
var files = [];
var data = input;
// in case if array was passed
if (data instanceof Array) {
data.forEach(function(source) {
this.filesMap(source, function(fileList) {
files = files.concat(fileList);
});
});
cb(files);
} else {
glob(data, {nodir: true}, function(error, fileList) {
if (error) {
throw error;
}
// slice off pattern
cb(fileList);
});
}
};
/**
* Runner method
* @param {[type]} compiler [description]
* @return {[type]} [description]
*/
WebpackSvgStore.prototype.apply = function(compiler) {
var chunkWrapper;
var publicPath;
var self = this;
var options = this.options;
var inputFolder = this.input;
var outputFolder = this.output;
var spriteName = this.options.name;
// subscribe to webpack emit state
compiler.plugin('compilation', function(compilation) {
// path into dist absolute path
publicPath = compilation.getStats().toJson().publicPath;
if (!publicPath) {
publicPath = path.isAbsolute(outputFolder)
? outputFolder
: '/'
;
}
// prepare input / output folders
utils.prepareFolder(inputFolder);
utils.prepareFolder(outputFolder);
self.filesMap(inputFolder, function(files) {
var fullPath;
var fileContent = utils.createSprite(utils.parseFiles(files, options));
var fileName = utils.hash(fileContent, spriteName);
var filePath = path.join(outputFolder, fileName);
// resolve with node url
fullPath = url.resolve(publicPath, filePath);
compilation.assets[slash(filePath)] = {
size: function() { return Buffer.byteLength(fileContent, 'utf8'); },
source: function() { return new Buffer(fileContent); }
};
// if chunk enable apply to chunk
if (options && options.chunk) {
chunkWrapper = options.chunk;
compilation.plugin('optimize-chunk-assets', function(chunks, callback) {
chunks.forEach(function(chunk) {
if (options.entryOnly && !chunk.initial) return;
if (chunk.name === chunkWrapper) {
chunk.files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)).forEach(function(file) {
if (/\.js?$/.test(file)) {
compilation.assets[file] = new ConcatSource(utils.svgXHR(fullPath, options.baseUrl), '\n', compilation.assets[file]);
}
});
}
});
callback();
});
}
});
});
};
/**
* Return function
* @type {[type]}
*/
module.exports = WebpackSvgStore;