1 | 'use strict';
|
2 |
|
3 | var _loaderUtils = require('loader-utils');
|
4 |
|
5 | var _loaderUtils2 = _interopRequireDefault(_loaderUtils);
|
6 |
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = function pluginsLoader(source) {
|
16 |
|
17 | this.cacheable();
|
18 | var query = _loaderUtils2.default.parseQuery(this.query);
|
19 |
|
20 | var pluginPromises = [];
|
21 | |
22 |
|
23 |
|
24 |
|
25 | function renderToClient(pluginOptions) {
|
26 |
|
27 | if (!pluginOptions || !pluginOptions.name) {
|
28 | throw new Error('Plugin name is mandatory');
|
29 | }
|
30 |
|
31 |
|
32 | pluginPromises.push(new Promise(function (resolve, reject) {
|
33 | Promise.resolve(pluginOptions.frontendData).then(function (frontendData) {
|
34 | return resolve({
|
35 | name: pluginOptions.name,
|
36 | frontendData: frontendData,
|
37 | frontendPlugin: pluginOptions.frontendPlugin
|
38 | });
|
39 | }).catch(function (err) {
|
40 | return reject(err);
|
41 | });
|
42 | }));
|
43 | }
|
44 |
|
45 |
|
46 | var pluginData = { source: source, dest: query.dest, commonsChunkFilename: query.commonsChunkFilename };
|
47 |
|
48 |
|
49 | this._compilation.applyPlugins(
|
50 | 'carte-blanche-plugin-before-processing', pluginData, this);
|
51 |
|
52 | this._compilation.applyPlugins(
|
53 | 'carte-blanche-plugin-processing', renderToClient, pluginData, this);
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | var callback = this.async();
|
59 |
|
60 | Promise.all(pluginPromises).then(function (plugins) {
|
61 |
|
62 |
|
63 | return plugins.map(function (plugin) {
|
64 |
|
65 | var frontendCode = 'function() {\n return (require(' + JSON.stringify(plugin.frontendPlugin) + '))\n .default.apply(\n this,\n Array.prototype.concat.apply([this.frontendData, pluginData], arguments)\n )\n }';
|
66 |
|
67 | return '{\n name: ' + JSON.stringify(plugin.name) + ',\n frontendData: ' + JSON.stringify(plugin.frontendData) + ',\n frontendPlugin: ' + (plugin.frontendPlugin ? frontendCode : '""') + '\n }';
|
68 | });
|
69 | })
|
70 |
|
71 | .then(function (pluginsData) {
|
72 | callback(null, '\n var pluginData = ' + JSON.stringify(pluginData) + ';\n module.exports = [' + pluginsData.join(',') + '];\n ');
|
73 | }).catch(function (err) {
|
74 | return callback(err);
|
75 | });
|
76 | }; |
\ | No newline at end of file |