Skip to content

Commit

Permalink
chromeless fix (#192) (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored and rauchg committed Jan 15, 2019
1 parent 962d867 commit 8f8a821
Show file tree
Hide file tree
Showing 7 changed files with 618 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"bindings": "^1.3.1",
"bytes": "^3.0.0",
"canvas": "^2.2.0",
"chromeless": "^1.5.2",
"codecov": "^3.1.0",
"copy": "^0.3.2",
"core-js": "^2.5.7",
Expand Down
21 changes: 19 additions & 2 deletions src/loaders/relocate-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ module.exports = function (code) {
let scope = attachScopes(ast, 'scope');

let pathId, pathImportIds = {};
let fsId;
let pregypId, bindingsId, nbindId;

const shadowDepths = Object.create(null);
Expand Down Expand Up @@ -452,6 +453,12 @@ module.exports = function (code) {
if (shadowDepths[pathId] === 0)
vars[pathId] = path;
}
if (fsId) {
if (shadowDepths[fsId] === 0)
vars[fsId] = {
existsSync: fs.existsSync
};
}
if (pregypId) {
if (shadowDepths[pregypId] === 0)
vars[pregypId] = pregyp;
Expand Down Expand Up @@ -601,6 +608,12 @@ module.exports = function (code) {
shadowDepths[pathId] = 0;
return this.skip();
}
// var fs = require('fs')
else if (decl.init.arguments[0].value === 'fs') {
fsId = decl.id.name;
shadowDepths[fsId] = 0;
return this.skip();
}
// var binary = require('node-pre-gyp')
else if (isPregypId(decl.init.arguments[0].value)) {
pregypId = decl.id.name;
Expand All @@ -617,7 +630,6 @@ module.exports = function (code) {
else if (decl.init.arguments[0].value === 'nbind') {
nbindId = decl.id.name;
shadowDepths[nbindId] = 0;
return this.skip();
}
}
// var { join } = path | require('path');
Expand Down Expand Up @@ -673,7 +685,7 @@ module.exports = function (code) {
// -> compute and backtrack
if (staticChildNode) {
const curStaticValue = computeStaticValue(node, false);
if (curStaticValue) {
if (curStaticValue !== undefined) {
staticChildValue = curStaticValue;
staticChildNode = node;
staticChildValueBindingsInstance = staticBindingsInstance;
Expand All @@ -691,6 +703,11 @@ module.exports = function (code) {
}
catch (e) {}
}
// Boolean inlining
else if (typeof staticChildValue === 'boolean') {
transformed = true;
magicString.overwrite(staticChildNode.start, staticChildNode.end, String(staticChildValue));
}
if (stats && stats.isFile()) {
let replacement = emitAsset(path.resolve(staticChildValue));
// require('bindings')(...)
Expand Down
1 change: 1 addition & 0 deletions test/integration/chromeless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('chromeless');
5 changes: 5 additions & 0 deletions test/unit/fs-conditional/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('fs');
if (fs.existsSync(__dirname + '/input1.js'))
require('./input1.js');
else if (fs.existsSync(__dirname + '/input.js'))
require('./input.js');
59 changes: 59 additions & 0 deletions test/unit/fs-conditional/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports =
/******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
/******/ // 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;
/******/ }
/******/
/******/
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(103);
/******/ })
/************************************************************************/
/******/ ({

/***/ 103:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {

const fs = __webpack_require__(66);
if (false)
{}
else if (fs.existsSync(__dirname + '/input.js'))
__webpack_require__(103);


/***/ }),

/***/ 66:
/***/ (function(module) {

module.exports = require("fs");

/***/ })

/******/ });
59 changes: 59 additions & 0 deletions test/unit/fs-conditional/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports =
/******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
/******/ // 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;
/******/ }
/******/
/******/
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(103);
/******/ })
/************************************************************************/
/******/ ({

/***/ 103:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {

const fs = __webpack_require__(66);
if (false)
{}
else if (fs.existsSync(__dirname + '/input.js'))
__webpack_require__(103);


/***/ }),

/***/ 66:
/***/ (function(module) {

module.exports = require("fs");

/***/ })

/******/ });
Loading

0 comments on commit 8f8a821

Please sign in to comment.