1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const Module = require('module');
|
12 | const path = require('path');
|
13 | const logger = require('./util/logger');
|
14 |
|
15 | let relativeModules = {};
|
16 | let requiredModules = {};
|
17 |
|
18 | let loadedPlugins = [];
|
19 |
|
20 |
|
21 | class PluginHelper {
|
22 | constructor (plugins, op) {
|
23 | this.applyPlugin(0, op);
|
24 | return true;
|
25 | }
|
26 | applyPlugin (index, op) {
|
27 | let plg = loadedPlugins[index];
|
28 |
|
29 | if (!plg) {
|
30 | op.done && op.done(op);
|
31 | } else {
|
32 | op.next = () => {
|
33 | this.applyPlugin(index + 1, op);
|
34 | };
|
35 | op.catch = () => {
|
36 | op.error && op.error(op);
|
37 | };
|
38 | if (plg)
|
39 | plg.apply(op);
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 | exports = module.exports = {
|
45 | attach (resolve) {
|
46 | this.resolve = resolve;
|
47 | },
|
48 | loadCompiler (lang) {
|
49 | if (['wxml', 'xml', 'css', 'js'].indexOf(lang) > -1) {
|
50 | return (c) => {
|
51 | return Promise.resolve(c);
|
52 | };
|
53 | }
|
54 |
|
55 | let name = 'wepy-compiler-' + lang;
|
56 | let compiler = this.load(name);
|
57 |
|
58 | if (!compiler) {
|
59 | this.missingNPM = name;
|
60 | logger.warn('loader', `Missing compiler: ${name}.` );
|
61 | }
|
62 | return compiler;
|
63 | },
|
64 |
|
65 | getNodeModulePath(loc, relative) {
|
66 | relative = relative || process.cwd();
|
67 | if (typeof Module === 'object') return null;
|
68 |
|
69 | let relativeMod = relativeModules[relative];
|
70 | let paths = [];
|
71 |
|
72 | if (!relativeMod) {
|
73 | relativeMod = new Module;
|
74 |
|
75 | let filename = path.join(relative, './');
|
76 | relativeMod.id = filename;
|
77 | relativeMod.filename = filename;
|
78 | relativeMod.paths = [].concat(this.resolve.modulePaths);
|
79 |
|
80 | paths = Module._nodeModulePaths(relative);
|
81 | relativeModules[relative] = relativeMod;
|
82 | }
|
83 | paths.forEach((v) => {
|
84 | if (relativeMod.paths.indexOf(v) === -1) {
|
85 | relativeMod.paths.push(v);
|
86 | }
|
87 | });
|
88 | try {
|
89 | return Module._resolveFilename(loc, relativeMod);
|
90 | } catch (err) {
|
91 | return null;
|
92 | }
|
93 | },
|
94 | load(loc, relative) {
|
95 |
|
96 | if (requiredModules[loc])
|
97 | return requiredModules[loc];
|
98 |
|
99 | let modulePath = this.getNodeModulePath(loc, relative);
|
100 | let m = null;
|
101 | try {
|
102 | m = require(modulePath);
|
103 | } catch (e) {
|
104 | if (e.message !== 'missing path')
|
105 | console.log(e);
|
106 | }
|
107 | if (m) {
|
108 | m = m.default ? m.default : m;
|
109 | requiredModules[loc] = m;
|
110 | }
|
111 | return m;
|
112 | },
|
113 |
|
114 | loadPlugin(plugins, op) {
|
115 | let plg, plgkey, setting, config;
|
116 | for (plgkey in plugins) {
|
117 | let name = 'wepy-plugin-' + plgkey;
|
118 | setting = plugins[plgkey];
|
119 | plg = this.load(name);
|
120 |
|
121 | if (!plg) {
|
122 | this.missingNPM = name;
|
123 | logger.warn('loader', `Missing plugin: ${name}`);
|
124 | return false;
|
125 | }
|
126 | loadedPlugins.push(new plg(setting));
|
127 | }
|
128 | return true;
|
129 | },
|
130 | PluginHelper: PluginHelper
|
131 | }
|