Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require main support #84

Merged
merged 1 commit into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions src/asset-relocator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function isReference(node, parent) {
return true;
}

const assetRegEx = /_\_dirname|_\_filename/;
const assetRegEx = /_\_dirname|_\_filename|require\.main/;
module.exports = function (code) {
const id = this.resourcePath;

Expand Down Expand Up @@ -93,7 +93,7 @@ module.exports = function (code) {
}
}
}
let didRelocate = false;
let transformed = false;

function computeStaticValue (expr, id) {
// function expression analysis disabled due to static-eval locals bug
Expand Down Expand Up @@ -149,8 +149,7 @@ module.exports = function (code) {
// __dirname and __filename only currently
// Can add require.resolve, import.meta.url, even path-like environment variables
if (node.type === 'Identifier' && isReference(node, parent)) {
if (!shadowDepths[node.name] &&
(node.name === '__dirname' || node.name === '__filename')) {
if ((node.name === '__dirname' || node.name === '__filename') && !shadowDepths[node.name]) {
curStaticValue = computeStaticValue(node, id);
// if it computes, then we start backtracking
if (curStaticValue) {
Expand All @@ -160,11 +159,22 @@ module.exports = function (code) {
}
}

else if (node.type === 'MemberExpression' &&
node.object.type === 'Identifier' &&
node.object.name === 'require' &&
!shadowDepths.require &&
node.property.type === 'Identifier' &&
node.property.name === 'main' &&
!node.computed) {
magicString.overwrite(node.object.start, node.object.end, '__non_webpack_require__');
transformed = true;
}

// for now we only support top-level variable declarations
// so "var { join } = require('path')" will only detect in the top scope.
// Intermediate scope handling for these requires is straightforward, but
// would need nested shadow depth handling of the pathIds.
if (parent === ast && node.type === 'VariableDeclaration') {
else if (parent === ast && node.type === 'VariableDeclaration') {
for (const decl of node.declarations) {
// var path = require('path')
if (decl.id.type === 'Identifier' &&
Expand Down Expand Up @@ -231,7 +241,7 @@ module.exports = function (code) {
if (isFile) {
const replacement = emitAsset(path.resolve(staticChildValue));
if (replacement) {
didRelocate = true;
transformed = true;
magicString.overwrite(staticChildNode.start, staticChildNode.end, replacement);
}
staticChildNode = staticChildValue = undefined;
Expand All @@ -240,7 +250,7 @@ module.exports = function (code) {
}
});

if (!didRelocate)
if (!transformed)
return this.callback(null, code);

code = magicString.toString();
Expand Down
1 change: 1 addition & 0 deletions test/unit/require-main/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(require.main.filename);
95 changes: 95 additions & 0 deletions test/unit/require-main/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // 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;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

console.log(require.main.filename);

/***/ })
/******/ ]);