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

fix: webpack 5 wrapper handling #186

Merged
merged 4 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ const globalBindings: any = {
__importStar: normalizeWildcardRequire,
MONGOOSE_DRIVER_PATH: undefined,
URL: URL,
Object: {
assign: Object.assign
}
};
globalBindings.global = globalBindings.GLOBAL = globalBindings.globalThis = globalBindings;

Expand Down Expand Up @@ -345,12 +348,12 @@ export default async function analyze(id: string, code: string, job: Job): Promi

function computePureStaticValue (expr: Node, computeBranches = true) {
const vars = Object.create(null);
Object.keys(knownBindings).forEach(name => {
vars[name] = getKnownBinding(name);
});
Object.keys(globalBindings).forEach(name => {
vars[name] = { value: globalBindings[name] };
});
Object.keys(knownBindings).forEach(name => {
vars[name] = getKnownBinding(name);
});
vars['import.meta'] = { url: importMetaUrl };
// evaluate returns undefined for non-statically-analyzable
const result = evaluate(expr, vars, computeBranches);
Expand Down
13 changes: 12 additions & 1 deletion src/utils/static-eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ const visitors: Record<string, (this: State, node: Node, walk: Walk) => Evaluate
}
return { value: arr };
},
'ArrowFunctionExpression': function (this: State, node: Node, walk: Walk) {
// () => val support only
if (node.params.length === 0 && !node.generator && !node.async && node.expression) {
const innerValue = walk(node.body);
if (!innerValue || !('value' in innerValue))
return;
return { value: () => innerValue.value };
}
return undefined;
},
'BinaryExpression': function BinaryExpression(this: State, node: Node, walk: Walk) {
const op = node.operator;

Expand Down Expand Up @@ -160,7 +170,8 @@ const visitors: Record<string, (this: State, node: Node, walk: Walk) => Evaluate
},
'CallExpression': function CallExpression(this: State, node: Node, walk: Walk) {
const callee = walk(node.callee);
if (!callee || 'test' in callee) return;
if (!callee || 'test' in callee)
return;
let fn: any = callee.value;
if (typeof fn === 'object' && fn !== null) fn = fn[FUNCTION];
if (typeof fn !== 'function') return;
Expand Down
62 changes: 42 additions & 20 deletions src/utils/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export function handleWrappers(ast: Node) {
// },
// function(e, t, r) {
// const n = require("fs");
// const ns = { a: require("fs") };
// const ns = Object.assign(a => require("fs"), { a: require("fs") });
// }
// ]);
//
Expand Down Expand Up @@ -432,29 +432,52 @@ export function handleWrappers(ast: Node) {
node.arguments[0].type === 'Identifier' &&
assignedVars.get(node.arguments[0].name)) {
if (maybeParent && maybeParent.init === node) {
const req = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require'
},
arguments: [{
type: 'Literal',
value: assignedVars.get(node.arguments[0].name)
}]
};
maybeParent.init = {
type: 'ObjectExpression',
properties: [{
type: 'ObjectProperty',
method: false,
computed: false,
shorthand: false,
key: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'Object'
},
property: {
type: 'Identifier',
name: 'a'
name: 'assign'
}
},
arguments: [
{
type: 'ArrowFunctionExpression',
expression: true,
params: [],
body: req
},
value: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require'
},
arguments: [{
type: 'Literal',
value: assignedVars.get(node.arguments[0].name)
{
type: 'ObjectExpression',
properties: [{
type: 'ObjectProperty',
method: false,
computed: false,
shorthand: false,
key: {
type: 'Identifier',
name: 'a'
},
value: req
}]
}
}]
]
};
}
}
Expand All @@ -465,4 +488,3 @@ export function handleWrappers(ast: Node) {
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "word": "word" }
140 changes: 140 additions & 0 deletions test/unit/webpack-5-wrapper-namespace/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = require('path');
/******/
/******/ // 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
/******/ var threw = true;
/******/ try {
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ threw = false;
/******/ } finally {
/******/ if(threw) delete installedModules[moduleId];
/******/ }
/******/
/******/ // 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, __webpack_require__) {

module.exports = __webpack_require__("PicC");


/***/ }),

/***/ "PicC":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return handler; });
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("oyvS");
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("mw/K");
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(fs__WEBPACK_IMPORTED_MODULE_1__);

f(__webpack_require__("oyvS"));


function handler(req, res) {
const dictionaryPath = path__WEBPACK_IMPORTED_MODULE_0___default().join(process.cwd(), "assets", "dictionary.json");
const content = fs__WEBPACK_IMPORTED_MODULE_1___default().readFileSync(dictionaryPath, "utf-8");
res.json(content);
}

/***/ }),

/***/ "mw/K":
/***/ (function(module, exports) {

module.exports = require("fs");

/***/ }),

/***/ "oyvS":
/***/ (function(module, exports) {

module.exports = require("path");

/***/ })

/******/ });
5 changes: 5 additions & 0 deletions test/unit/webpack-5-wrapper-namespace/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"package.json",
"test/unit/webpack-5-wrapper-namespace/assets/dictionary.json",
"test/unit/webpack-5-wrapper-namespace/input.js"
]