forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for emitting node modules from webpack
This has two fold impact: 1. Allows for arbitrary loaders to run in both node and browser environments (Fixes vercel#1825) 2. Optimizes the build time for modules. Risks: - Transitions all harmony code to utilize harmony end to end. Previously this had not happened, which may break users in unexpected ways. (See vercel#2508) - Require logic changed across the board to utilize node and webpack behaviors where possible, simplifying code, but potentially breaking things in the wild.
- Loading branch information
Showing
18 changed files
with
455 additions
and
178 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Webpack ApiPlugin aliases for node execution environment. | ||
// https://github.com/webpack/webpack/blob/master/lib/APIPlugin.js | ||
// | ||
// Only used within the next server process and will be properly populated | ||
// via the next runtime. | ||
|
||
// __webpack_require__.p = the bundle public path | ||
exports.publicPath = '/_next/webpack' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default class HarmonyCompatibilityDependencyTemplate { | ||
apply (dep, source) { | ||
source.insert(-10, ` | ||
const __webpack_exports__ = module.exports = exports = {}; | ||
Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); | ||
const __webpack_require__ = require; | ||
__webpack_require__.o = function(object, property) { | ||
return Object.prototype.hasOwnProperty.call(object, property); | ||
}; | ||
// Via MainTemplate | ||
// define getter function for harmony exports | ||
__webpack_require__.d = function(exports, name, getter) { | ||
if(!__webpack_require__.o(exports, name)) { | ||
Object.defineProperty(exports, name, { | ||
configurable: false, | ||
enumerable: true, | ||
get: getter | ||
}); | ||
} | ||
}; | ||
// getDefaultExport function for compatibility with non-harmony modules"); | ||
__webpack_require__.n = function(module) { | ||
var getter = module && module.__esModule ? | ||
function getDefault() { return module['default']; } : | ||
function getModuleExports() { return module; }; | ||
__webpack_require__.d(getter, 'a', getter); | ||
return getter; | ||
}; | ||
`) | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export default class HarmonyExportDependencyTemplate { | ||
apply (dep, source) { | ||
const used = dep.originModule.isUsed('default') | ||
const content = this.getContent(dep.originModule, used) | ||
|
||
if (dep.range) { | ||
source.replace(dep.rangeStatement[0], dep.range[0] - 1, `${content}(`) | ||
source.replace(dep.range[1], dep.rangeStatement[1] - 1, ');') | ||
return | ||
} | ||
|
||
source.replace(dep.rangeStatement[0], dep.rangeStatement[1] - 1, content) | ||
} | ||
|
||
getContent (module, used) { | ||
const exportsName = module.exportsArgument || 'exports' | ||
if (used) { | ||
return `/* harmony default export */ ${exportsName}[${JSON.stringify( | ||
used | ||
)}] = ` | ||
} | ||
return '/* unused harmony default export */ var _unused_webpack_default_export = ' | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export default class HarmonyImportSpecifierDependencyTemplate { | ||
apply (dep, source) { | ||
const content = this.getContent(dep) | ||
source.replace(dep.range[0], dep.range[1] - 1, content) | ||
} | ||
|
||
getContent (dep) { | ||
const importedModule = dep.importDependency.module | ||
const defaultImport = | ||
dep.directImport && | ||
dep.id === 'default' && | ||
!( | ||
importedModule && | ||
(!importedModule.meta || importedModule.meta.harmonyModule) && | ||
!/node_modules/.test(importedModule.resource) | ||
) | ||
const shortHandPrefix = this.getShortHandPrefix(dep) | ||
const { importedVar } = dep | ||
const importedVarSuffix = this.getImportVarSuffix( | ||
dep, | ||
defaultImport, | ||
importedModule | ||
) | ||
|
||
if (dep.call && defaultImport) { | ||
return `${shortHandPrefix}${importedVar}_default()` | ||
} | ||
|
||
if (dep.call && dep.id) { | ||
return `${shortHandPrefix}Object(${importedVar}${importedVarSuffix})` | ||
} | ||
|
||
return `${shortHandPrefix}${importedVar}${importedVarSuffix}` | ||
} | ||
|
||
getImportVarSuffix (dep, defaultImport, importedModule) { | ||
if (defaultImport) { | ||
return '_default.a' | ||
} | ||
|
||
if (dep.id) { | ||
const used = | ||
importedModule && !/node_modules/.test(importedModule.resource) | ||
? importedModule.isUsed(dep.id) | ||
: dep.id | ||
const optionalComment = dep.id !== used ? ` /* ${dep.id} */` : '' | ||
return `[${JSON.stringify(used)}${optionalComment}]` | ||
} | ||
|
||
return '' | ||
} | ||
|
||
getShortHandPrefix (dep) { | ||
if (!dep.shorthand) { | ||
return '' | ||
} | ||
|
||
return `${dep.name}: ` | ||
} | ||
} |
Oops, something went wrong.