Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,43 @@ RequireJsExportPlugin.prototype.apply = function (compiler) {

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(
{
Expand All @@ -118,12 +155,6 @@ RequireJsExportPlugin.prototype.apply = function (compiler) {

if (needsImport.length === 0 && needsExport.length === 0) continue;

const captureCode = needsExport
.map(({ id }) =>
` try { __requirejs_exports__[${JSON.stringify(id)}] = __webpack_require__(${JSON.stringify(id)}); } catch(e) { /* Intentionally ignore modules that cannot be required in this chunk context. */ }`
)
.join('\n');

const prolog = generateProlog(chunk.id, needsImport, needsExport);
const epilog = generateEpilog(chunk.id, needsImport, needsExport);

Expand All @@ -132,7 +163,7 @@ RequireJsExportPlugin.prototype.apply = function (compiler) {

compilation.updateAsset(
filename,
(old) => new ConcatSource(prolog, '\n', old, '\n', captureCode, '\n', epilog)
(old) => new ConcatSource(prolog, '\n', old, '\n', epilog)
);
}
}
Expand Down Expand Up @@ -170,4 +201,4 @@ RequireJsExportPlugin.prototype.apply = function (compiler) {
}
};

module.exports = RequireJsExportPlugin;
module.exports = RequireJsExportPlugin;