1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 |
|
---|
7 | 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; };
|
---|
8 |
|
---|
9 | exports.generateFile = generateFile;
|
---|
10 | exports.default = generateData;
|
---|
11 |
|
---|
12 | var _generatePrefixMap = require('./generatePrefixMap');
|
---|
13 |
|
---|
14 | var _generatePrefixMap2 = _interopRequireDefault(_generatePrefixMap);
|
---|
15 |
|
---|
16 | var _generatePluginList = require('./generatePluginList');
|
---|
17 |
|
---|
18 | var _generatePluginList2 = _interopRequireDefault(_generatePluginList);
|
---|
19 |
|
---|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
21 |
|
---|
22 | function generateImportString(plugin, compatibility) {
|
---|
23 | if (compatibility) {
|
---|
24 | return 'var ' + plugin + ' = require(\'inline-style-prefixer/lib/plugins/' + plugin + '\')';
|
---|
25 | }
|
---|
26 | return 'import ' + plugin + ' from \'inline-style-prefixer/lib/plugins/' + plugin + '\'';
|
---|
27 | }
|
---|
28 |
|
---|
29 | function generateFile(prefixMap, pluginList, compatibility) {
|
---|
30 | var pluginImports = pluginList.map(function (plugin) {
|
---|
31 | return generateImportString(plugin, compatibility);
|
---|
32 | }).join('\n');
|
---|
33 |
|
---|
34 | var moduleExporter = compatibility ? 'module.exports = ' : 'export default';
|
---|
35 | var pluginExport = '[' + pluginList.join(',') + ']';
|
---|
36 | var prefixMapExport = JSON.stringify(prefixMap);
|
---|
37 |
|
---|
38 | 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');
|
---|
39 |
|
---|
40 | 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}';
|
---|
41 | }
|
---|
42 |
|
---|
43 | function saveFile(fileContent, path) {
|
---|
44 | /* eslint-disable global-require */
|
---|
45 | var fs = require('fs');
|
---|
46 | /* eslint-enable global-require */
|
---|
47 |
|
---|
48 | fs.writeFile(path, fileContent, function (err) {
|
---|
49 | if (err) {
|
---|
50 | throw err;
|
---|
51 | }
|
---|
52 |
|
---|
53 | console.log('Successfully saved data to "' + path + '".');
|
---|
54 | });
|
---|
55 | }
|
---|
56 |
|
---|
57 | var defaultOptions = {
|
---|
58 | prefixMap: true,
|
---|
59 | plugins: true,
|
---|
60 | compatibility: false
|
---|
61 | };
|
---|
62 |
|
---|
63 | function generateData(browserList) {
|
---|
64 | var userOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
---|
65 |
|
---|
66 | var options = _extends({}, defaultOptions, userOptions);
|
---|
67 |
|
---|
68 | var compatibility = options.compatibility,
|
---|
69 | plugins = options.plugins,
|
---|
70 | path = options.path,
|
---|
71 | prefixMap = options.prefixMap;
|
---|
72 |
|
---|
73 |
|
---|
74 | var requiredPrefixMap = prefixMap ? (0, _generatePrefixMap2.default)(browserList) : {};
|
---|
75 | var requiredPlugins = plugins ? (0, _generatePluginList2.default)(browserList) : [];
|
---|
76 |
|
---|
77 | if (path) {
|
---|
78 | saveFile(generateFile(requiredPrefixMap, requiredPlugins, compatibility), path);
|
---|
79 | }
|
---|
80 |
|
---|
81 | return {
|
---|
82 | prefixMap: requiredPrefixMap,
|
---|
83 | plugins: requiredPlugins
|
---|
84 | };
|
---|
85 | } |
---|