1 | 'use strict';
|
2 |
|
3 | var handlebars = require('handlebars');
|
4 | var path = require('path');
|
5 | var fs = require('fs');
|
6 | var xtend = require('xtend');
|
7 |
|
8 | var templatePath = path.join(__dirname, 'remote.hbs');
|
9 | var template = fs.readFileSync(templatePath, 'utf8');
|
10 | var remote = handlebars.compile(template);
|
11 | var genPath = path.join(__dirname, '..', 'loaders');
|
12 |
|
13 | function mapDeps(config) {
|
14 | if (!config.deps) return config;
|
15 | return xtend(config, {
|
16 | deps: Object.keys(config.deps).map(function (k) {
|
17 | return { key: k, global: config.deps[k] };
|
18 | }),
|
19 | });
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var genRemote = module.exports = function (config, cb) {
|
39 | var name = config.key;
|
40 | var opts = mapDeps(config);
|
41 | opts.exportRemotePath = require.resolve('./export-remote');
|
42 |
|
43 | var s = remote(opts);
|
44 | var filePath = path.join(genPath, name + '.js');
|
45 | fs.writeFile(filePath, s, 'utf8', function (err) {
|
46 | if (err) return cb(err);
|
47 | cb(null, filePath);
|
48 | });
|
49 | };
|
50 |
|