Permalink
Browse files

Merge pull request #11 from coursera/cliu_make_work_for_webpack_4

Make plugin work for webpack 4
  • Loading branch information...
cliu587 committed Aug 29, 2018
2 parents fa96013 + 112af4f commit 72135b54a863fc680ebfe5df40ee60afa7dcb40d
Showing with 60 additions and 25 deletions.
  1. +26 −9 lib/plugin.js
  2. +3 −3 package.json
  3. +31 −13 src/plugin.js
View
@@ -200,15 +200,32 @@ WebpackMultiOutput.prototype.apply = function (compiler) {
}, callback);
});
compilation.mainTemplate.plugin('jsonp-script', function (_) {
var source = _.split('\n');
var chunkIdModifier = 'var webpackMultiOutputGetChunkId = function(chunkId) {\n var map = {__WEBPACK_MULTI_OUTPUT_CHUNK_MAP__:2};\n return map[chunkId] ? \'__WEBPACK_MULTI_OUTPUT_VALUE__.\' + chunkId : chunkId;\n };\n ';
source[9] = source[9].replace('chunkId', 'webpackMultiOutputGetChunkId(chunkId)');
source.splice(0, 0, chunkIdModifier);
return source.join('\n');
compilation.mainTemplate.hooks.render.tap({
name: "JsonpMainTemplatePlugin chunkId replacement",
stage: Infinity
}, function (rawSource) {
var sourceString = rawSource.source();
if (!sourceString.includes('jsonpScriptSrc')) {
return rawSource;
} else {
// HACK: Find the line containing `jsonpScriptSrc`, which looks like
// /******/ function jsonpScriptSrc(chunkId) {
// /******/ return __webpack_require__.p + "" + ({$CHUNK_ID_TO_NAME_MAP}[chunkId]||chunkId) + ".js"
// /******/ }
// and replace `chunkId` with `webpackMultiOutputGetChunkId(chunkId)`, which attaches the locale
// [specified in `__WEBPACK_MULTI_OUTPUT_CHUNK_MAP__`] if necessary.
var sourceArray = sourceString.split('\n');
var chunkIdModifier = 'var webpackMultiOutputGetChunkId = function(chunkId) {\n var map = {__WEBPACK_MULTI_OUTPUT_CHUNK_MAP__:2};\n return map[chunkId] ? \'__WEBPACK_MULTI_OUTPUT_VALUE__.\' + chunkId : chunkId;\n };\n ';
var jsonpScriptSrcFunctionIndex = sourceArray.findIndex(function (a) {
return a.includes('jsonpScriptSrc');
});
sourceArray[jsonpScriptSrcFunctionIndex + 1] = sourceArray[jsonpScriptSrcFunctionIndex + 1].replace('chunkId', 'webpackMultiOutputGetChunkId(chunkId)');
sourceArray.splice(jsonpScriptSrcFunctionIndex + 1, 0, chunkIdModifier);
return sourceArray.join('\n');
}
});
});
View
@@ -1,6 +1,6 @@
{
"name": "@coursera/webpack-multi-output",
"version": "3.3.1",
"version": "4.0.0",
"description": "Webpack loader and plugin to produce multiple bundle from one import",
"main": "index.js",
"author": "Romain Berger <romain@romainberger.com>",
@@ -34,7 +34,7 @@
"moment": "^2.13.0",
"path-exists": "^3.0.0",
"style-loader": "^0.13.1",
"webpack": "^1.13.1",
"webpack": "^4.0.0",
"webpack-rtl-plugin": "^1.1.1"
},
"dependencies": {
@@ -45,6 +45,6 @@
"webpack-sources": "^0.1.1"
},
"peerDependencies": {
"webpack": "^1.13.1"
"webpack": "^4.0.0"
}
}
View
@@ -161,19 +161,37 @@ WebpackMultiOutput.prototype.apply = function(compiler: Object): void {
}, callback)
})
compilation.mainTemplate.plugin('jsonp-script', (_: string): string => {
const source = _.split('\n')
const chunkIdModifier = `var webpackMultiOutputGetChunkId = function(chunkId) {
var map = {__WEBPACK_MULTI_OUTPUT_CHUNK_MAP__:2};
return map[chunkId] ? '__WEBPACK_MULTI_OUTPUT_VALUE__.' + chunkId : chunkId;
};
`
source[9] = source[9].replace('chunkId', 'webpackMultiOutputGetChunkId(chunkId)')
source.splice(0, 0, chunkIdModifier)
return source.join('\n')
compilation.mainTemplate.hooks.render.tap(
// NOTE: the `stage: Infinity` setting is necessary to make this hook run after the code that generates
// the webpack runtime, as otherwise we don't have the right content to replace...
{
name: "JsonpMainTemplatePlugin chunkId replacement",
stage: Infinity
}, rawSource => {
const sourceString = rawSource.source()
if (!sourceString.includes('jsonpScriptSrc')) {
return rawSource;
} else {
// HACK: Find the line containing `jsonpScriptSrc`, which looks like
// /******/ function jsonpScriptSrc(chunkId) {
// /******/ return __webpack_require__.p + "" + ({$CHUNK_ID_TO_NAME_MAP}[chunkId]||chunkId) + ".js"
// /******/ }
// and replace `chunkId` with `webpackMultiOutputGetChunkId(chunkId)`, which attaches the locale
// [specified in `__WEBPACK_MULTI_OUTPUT_CHUNK_MAP__`] if necessary.
const sourceArray = sourceString.split('\n')
const chunkIdModifier = `var webpackMultiOutputGetChunkId = function(chunkId) {
var map = {__WEBPACK_MULTI_OUTPUT_CHUNK_MAP__:2};
return map[chunkId] ? '__WEBPACK_MULTI_OUTPUT_VALUE__.' + chunkId : chunkId;
};
`
const jsonpScriptSrcFunctionIndex = sourceArray.findIndex(a => a.includes('jsonpScriptSrc'))
sourceArray[jsonpScriptSrcFunctionIndex + 1] = sourceArray[jsonpScriptSrcFunctionIndex + 1].replace('chunkId', 'webpackMultiOutputGetChunkId(chunkId)')
sourceArray.splice(jsonpScriptSrcFunctionIndex + 1, 0, chunkIdModifier)
return sourceArray.join('\n');
}
})
})

0 comments on commit 72135b5

Please sign in to comment.