-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
202 lines (163 loc) · 7.22 KB
/
Copy pathindex.js
File metadata and controls
202 lines (163 loc) · 7.22 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
const { ConcatSource } = require('webpack-sources');
function isNormalModule(module) {
return Boolean(module && typeof module.request === 'string' && module.rawRequest != null);
}
function RequireJsExportPlugin() {
}
function gatherRequireJsImports(modules) {
let needsImport = [];
for (let module of modules) {
if (isNormalModule(module) && String(module.request).indexOf('requirejs-loader') !== -1) {
needsImport.push('mixins!' + module.rawRequest);
}
}
return needsImport;
}
function shouldExport(module) {
if (!isNormalModule(module)) return false;
if (String(module.request).indexOf('requirejs-loader') !== -1) return false;
return true;
}
function gatherRequireJsExports(modules) {
let needsExport = [];
for (let module of modules) {
if (shouldExport(module)) {
let name = module.rawRequest;
if (name.indexOf('script-loader!') === 0) {
name = name.substr('script-loader!'.length);
}
needsExport.push({ id: module.id, name: name });
}
}
return needsExport;
}
function generateProlog(chunkId, imports) {
const jsonImports = JSON.stringify(imports);
const jsonDefineStub = JSON.stringify('__webpack_export_' + chunkId);
let prolog = `
(function (){
window.__requirejs_exports__ = window.__requirejs_exports__ || {};
var __requirejs_exports__ = window.__requirejs_exports__;`;
if (imports.length !== 0) {
prolog += `
window.define(${jsonDefineStub}, ${jsonImports}, function () {`;
}
return prolog;
}
function generateEpilog(chunkId, imports, exports) {
let epilog = '';
if (imports.length !== 0) {
epilog += `
});`;
}
const jsonDefineStubs = JSON.stringify(imports.length === 0 ? [] : ['__webpack_export_' + chunkId]);
for (let module of exports) {
const jsonName = JSON.stringify(module.name);
const jsonId = JSON.stringify(module.id);
epilog += `
window.define(${jsonName}, ${jsonDefineStubs}, function () {
var exp = __requirejs_exports__[${jsonId}];
return (exp && exp.__esModule && exp.default) ? exp.default : exp;
});`;
}
if (imports.length !== 0) {
epilog += `
window.require(['__webpack_export_${chunkId}'], function () {});`;
}
epilog += `
}());`;
return epilog;
}
RequireJsExportPlugin.prototype.apply = function (compiler) {
const isWebpack5 = Boolean(compiler.webpack);
if (isWebpack5) {
compiler.options.output = compiler.options.output || {};
if (Object.prototype.hasOwnProperty.call(compiler.options.output, 'iife')
&& compiler.options.output.iife === true) {
throw new Error('RequireJsExportPlugin requires output.iife = false when using webpack 5.');
}
compiler.options.output.iife = false;
compiler.hooks.compilation.tap('RequireJsExportPlugin', (compilation) => {
const Compilation = compiler.webpack.Compilation;
const JavascriptModulesPlugin =
compiler.webpack.javascript.JavascriptModulesPlugin;
const jsHooks =
JavascriptModulesPlugin.getCompilationHooks(compilation);
jsHooks.renderModuleContent.tap(
'RequireJsExportPlugin',
(source, module) => {
if (!shouldExport(module)) {
return source;
}
return new ConcatSource(
source,
`
window.__requirejs_exports__ =
window.__requirejs_exports__ || {};
if (typeof __webpack_exports__ !== 'undefined') {
window.__requirejs_exports__[${JSON.stringify(module.id)}] =
(__webpack_exports__ &&
__webpack_exports__.__esModule &&
Object.prototype.hasOwnProperty.call(
__webpack_exports__,
'default'
))
? __webpack_exports__.default
: __webpack_exports__;
}
`
);
}
);
compilation.hooks.processAssets.tap(
{
name: 'RequireJsExportPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
},
() => {
for (const chunk of compilation.chunks) {
const chunkModules = Array.from(
compilation.chunkGraph.getChunkModulesIterable(chunk)
);
const needsImport = gatherRequireJsImports(chunkModules);
const needsExport = gatherRequireJsExports(chunkModules);
if (needsImport.length === 0 && needsExport.length === 0) continue;
const prolog = generateProlog(chunk.id, needsImport);
const epilog = generateEpilog(chunk.id, needsImport, needsExport);
for (const filename of chunk.files) {
if (!filename.endsWith('.js')) continue;
compilation.updateAsset(
filename,
(old) => new ConcatSource(prolog, '\n', old, '\n', epilog)
);
}
}
}
);
});
} else {
compiler.hooks.compilation.tap('RequireJsExportPlugin', (compilation) => {
compilation.hooks.afterOptimizeModuleIds.tap('RequireJsExportPlugin', (modules) => {
for (let module of modules) {
if (shouldExport(module) && module._source) {
const definition = '__webpack_exports__[' + JSON.stringify(module.id) + '] = module.exports;';
module._source = new ConcatSource(module._source, '\n', definition);
}
}
});
compilation.hooks.chunkAsset.tap('RequireJsExportPlugin', (chunk, filename) => {
if (!filename.endsWith('.js')) return;
const modules = chunk.modulesIterable ? Array.from(chunk.modulesIterable) : [];
const needsImport = gatherRequireJsImports(modules);
const needsExport = gatherRequireJsExports(modules);
if (needsImport.length === 0 && needsExport.length === 0) return;
const prolog = generateProlog(chunk.id, needsImport);
const epilog = generateEpilog(chunk.id, needsImport, needsExport);
compilation.assets[filename] = new ConcatSource(prolog, "\n", compilation.assets[filename], "\n", epilog);
chunk['--requirejs-export:done'] = true;
});
});
}
};
module.exports = RequireJsExportPlugin;