1 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
---|
2 |
|
---|
3 | import generatePrefixMap from './generatePrefixMap';
|
---|
4 | import generatePluginList from './generatePluginList';
|
---|
5 |
|
---|
6 | function generateImportString(plugin, compatibility) {
|
---|
7 | if (compatibility) {
|
---|
8 | return 'var ' + plugin + ' = require(\'inline-style-prefixer/lib/plugins/' + plugin + '\')';
|
---|
9 | }
|
---|
10 | return 'import ' + plugin + ' from \'inline-style-prefixer/lib/plugins/' + plugin + '\'';
|
---|
11 | }
|
---|
12 |
|
---|
13 | export function generateFile(prefixMap, pluginList, compatibility) {
|
---|
14 | var pluginImports = pluginList.map(function (plugin) {
|
---|
15 | return generateImportString(plugin, compatibility);
|
---|
16 | }).join('\n');
|
---|
17 |
|
---|
18 | var moduleExporter = compatibility ? 'module.exports = ' : 'export default';
|
---|
19 | var pluginExport = '[' + pluginList.join(',') + ']';
|
---|
20 | var prefixMapExport = JSON.stringify(prefixMap);
|
---|
21 |
|
---|
22 | var prefixVariables = ['var w = ["Webkit"];', 'var m = ["Moz"];', 'var ms = ["ms"];', 'var wm = ["Webkit","Moz"];', 'var wms = ["Webkit","ms"];', 'var wmms = ["Webkit","Moz","ms"];'].join('\n');
|
---|
23 |
|
---|
24 | return pluginImports + '\n' + prefixVariables + '\n\n' + moduleExporter + ' {\n plugins: ' + pluginExport + ',\n prefixMap: ' + prefixMapExport.replace(/\["Webkit"\]/g, 'w').replace(/\["Moz"\]/g, 'm').replace(/\["ms"\]/g, 'ms').replace(/\["Webkit","Moz"\]/g, 'wm').replace(/\["Webkit","ms"\]/g, 'wms').replace(/\["Webkit","Moz","ms"\]/g, 'wmms') + '\n}';
|
---|
25 | }
|
---|
26 |
|
---|
27 | function saveFile(fileContent, path) {
|
---|
28 | /* eslint-disable global-require */
|
---|
29 | var fs = require('fs');
|
---|
30 | /* eslint-enable global-require */
|
---|
31 |
|
---|
32 | fs.writeFile(path, fileContent, function (err) {
|
---|
33 | if (err) {
|
---|
34 | throw err;
|
---|
35 | }
|
---|
36 |
|
---|
37 | console.log('Successfully saved data to "' + path + '".');
|
---|
38 | });
|
---|
39 | }
|
---|
40 |
|
---|
41 | var defaultOptions = {
|
---|
42 | prefixMap: true,
|
---|
43 | plugins: true,
|
---|
44 | compatibility: false
|
---|
45 | };
|
---|
46 |
|
---|
47 | export default function generateData(browserList) {
|
---|
48 | var userOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
---|
49 |
|
---|
50 | var options = _extends({}, defaultOptions, userOptions);
|
---|
51 |
|
---|
52 | var compatibility = options.compatibility,
|
---|
53 | plugins = options.plugins,
|
---|
54 | path = options.path,
|
---|
55 | prefixMap = options.prefixMap;
|
---|
56 |
|
---|
57 |
|
---|
58 | var requiredPrefixMap = prefixMap ? generatePrefixMap(browserList) : {};
|
---|
59 | var requiredPlugins = plugins ? generatePluginList(browserList) : [];
|
---|
60 |
|
---|
61 | if (path) {
|
---|
62 | saveFile(generateFile(requiredPrefixMap, requiredPlugins, compatibility), path);
|
---|
63 | }
|
---|
64 |
|
---|
65 | return {
|
---|
66 | prefixMap: requiredPrefixMap,
|
---|
67 | plugins: requiredPlugins
|
---|
68 | };
|
---|
69 | } |
---|