Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
57 lines (44 sloc)
1.97 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| Object.defineProperty(exports, "__esModule", { | |
| value: true | |
| }); | |
| var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | |
| var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | |
| var _createClass2 = require('babel-runtime/helpers/createClass'); | |
| var _createClass3 = _interopRequireDefault(_createClass2); | |
| var _path = require('path'); | |
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| // This plugin modifies the require-ensure code generated by Webpack | |
| // to work with Next.js SSR | |
| var NextJsSsrImportPlugin = function () { | |
| function NextJsSsrImportPlugin(_ref) { | |
| var dir = _ref.dir, | |
| dist = _ref.dist; | |
| (0, _classCallCheck3.default)(this, NextJsSsrImportPlugin); | |
| this.dir = dir; | |
| this.dist = dist; | |
| } | |
| (0, _createClass3.default)(NextJsSsrImportPlugin, [{ | |
| key: 'apply', | |
| value: function apply(compiler) { | |
| var _this = this; | |
| compiler.plugin('compilation', function (compilation) { | |
| compilation.mainTemplate.plugin('require-ensure', function (code) { | |
| // Update to load chunks from our custom chunks directory | |
| var chunksDirPath = (0, _path.join)(_this.dir, _this.dist, 'dist'); | |
| // Make sure even in windows, the path looks like in unix | |
| // Node.js require system will convert it accordingly | |
| var chunksDirPathNormalized = chunksDirPath.replace(/\\/g, '/'); | |
| var updatedCode = code.replace('require("./"', 'require("' + chunksDirPathNormalized + '/"'); | |
| // Replace a promise equivalent which runs in the same loop | |
| // If we didn't do this webpack's module loading process block us from | |
| // doing SSR for chunks | |
| updatedCode = updatedCode.replace('return Promise.resolve();', 'return require(\'next/dynamic\').SameLoopPromise.resolve();'); | |
| return updatedCode; | |
| }); | |
| }); | |
| } | |
| }]); | |
| return NextJsSsrImportPlugin; | |
| }(); | |
| exports.default = NextJsSsrImportPlugin; |