保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const relateContext = require('./util/relate-context');
const pluginCompat = require('./util/plugin-compat');
 
const ParserSchemas3 = [['Parser', 'options']];
const ParserSchemas4 = [
  ['JsonParser', 'options'],
  ['Parser', 'options', 'sourceType'],
  ['WebAssemblyParser', 'options'],
];
 
try {
  ParserSchemas3[0].Parser = require('webpack/lib/Parser');
} catch (_) {}
 
try {
  ParserSchemas4[0].Parser = require('webpack/lib/JsonParser');
  ParserSchemas4[1].Parser = require('webpack/lib/Parser');
  try {
    ParserSchemas4[2].Parser = require('webpack/lib/WebAssemblyParser');
  } catch (_) {
    ParserSchemas4[2].Parser = require('webpack/lib/wasm/WebAssemblyParser');
  }
} catch (_) {}
 
const freezeArgument = {};
 
const thawArgument = {};
 
function freezeParser(parser, extra, methods) {
  const schemas = extra.schemas;
  for (let i = 0; i < schemas.length; i++) {
    if (parser.constructor === schemas[i].Parser) {
      const frozen = {
        type: schemas[i][0],
      };
      for (let j = 1; j < schemas[i].length; j++) {
        let arg = parser[schemas[i][j]];
        if (freezeArgument[schemas[i][j]]) {
          arg = freezeArgument[schemas[i][j]](arg, parser, extra, methods);
        }
        frozen[schemas[i][j]] = arg;
      }
      return frozen;
    }
  }
}
 
function thawParser(frozen, extra, methods) {
  const schemas = extra.schemas;
  for (let i = 0; i < schemas.length; i++) {
    if (frozen.type === schemas[i][0]) {
      const Parser = schemas[i].Parser;
      const args = [];
      for (let j = 1; j < schemas[i].length; j++) {
        let arg = frozen[schemas[i][j]];
        if (thawArgument[schemas[i][j]]) {
          arg = thawArgument[schemas[i][j]](arg, frozen, extra, methods);
        }
        args.push(arg);
      }
      try {
        return new Parser(...args);
      } catch (_) {
        return new (Function.prototype.bind.apply(
          Parser,
          [null].concat(args),
        ))();
      }
    }
  }
}
 
class TransformParserPlugin {
  constructor(options) {
    this.options = options || {};
  }
 
  apply(compiler) {
    const schema = this.options.schema;
    let schemas = ParserSchemas4;
    if (schema < 4) {
      schemas = ParserSchemas3;
    }
 
    pluginCompat.register(
      compiler,
      '_hardSourceBeforeFreezeParser',
      'syncWaterfall',
      ['frozen', 'item', 'extra'],
    );
    pluginCompat.register(
      compiler,
      '_hardSourceFreezeParser',
      'syncWaterfall',
      ['frozen', 'item', 'extra'],
    );
    pluginCompat.register(
      compiler,
      '_hardSourceAfterFreezeParser',
      'syncWaterfall',
      ['frozen', 'item', 'extra'],
    );
 
    pluginCompat.register(
      compiler,
      '_hardSourceBeforeThawParser',
      'syncWaterfall',
      ['item', 'frozen', 'extra'],
    );
    pluginCompat.register(compiler, '_hardSourceThawParser', 'syncWaterfall', [
      'item',
      'frozen',
      'extra',
    ]);
    pluginCompat.register(
      compiler,
      '_hardSourceAfterThawParser',
      'syncWaterfall',
      ['item', 'frozen', 'extra'],
    );
 
    let methods;
 
    pluginCompat.tap(
      compiler,
      '_hardSourceMethods',
      'TransformParserPlugin',
      _methods => {
        methods = _methods;
      },
    );
 
    pluginCompat.tap(
      compiler,
      '_hardSourceFreezeParser',
      'TransformParserPlugin freeze',
      (frozen, parser, extra) => {
        extra.schemas = schemas;
        frozen = freezeParser(parser, extra, methods);
        if (schema === 4) {
          frozen.moduleType = extra.module.type;
        }
        return frozen;
      },
    );
 
    pluginCompat.tap(
      compiler,
      '_hardSourceThawParser',
      'TransformParserPlugin thaw',
      (parser, { options, moduleType }, { normalModuleFactory }) => {
        if (schema < 4) {
          return normalModuleFactory.getParser(options);
        } else {
          return normalModuleFactory.getParser(moduleType, options);
        }
      },
    );
  }
}
 
module.exports = TransformParserPlugin;