Skip to content

Commit

Permalink
Allow to require module path from whitelisted dependency
Browse files Browse the repository at this point in the history
Required module path can be one of

- `fs` (built-in Node.js module),
- `abortcontroller-polyfill` (external main module),
- `abortcontroller-polyfill/dist/cjs-ponyfill` (external submodule).

FastBoot's build system requires only dependency whitelisting,
thus looking only for the first part of module path.
  • Loading branch information
bobisjan committed Nov 21, 2018
1 parent b79df75 commit 41b058b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"cookie": "^0.3.1",
"debug": "^3.0.0",
"najax": "^1.0.2",
"resolve": "^1.8.1",
"rsvp": "^4.7.0",
"simple-dom": "^1.0.0",
"source-map-support": "^0.5.0"
Expand Down
35 changes: 25 additions & 10 deletions src/ember-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const chalk = require('chalk');

const najax = require('najax');
const SimpleDOM = require('simple-dom');
const resolve = require('resolve');
const debug = require('debug')('fastboot:ember-app');

const FastBootInfo = require('./fastboot-info');
const Result = require('./result');
const FastBootSchemaVersions = require('./fastboot-schema-versions');
const getPackageName = require('./utils/get-package-name');

/**
* @private
Expand Down Expand Up @@ -116,21 +118,34 @@ class EmberApp {
buildWhitelistedRequire(whitelist, distPath) {
whitelist.forEach(function(whitelistedModule) {
debug("module whitelisted; module=%s", whitelistedModule);

// Remove after 2.0.0
let packageName = getPackageName(whitelistedModule);

if (packageName !== whitelistedModule) {
console.warn("[DEPRECATION] Whitelisting module path '" + whitelistedModule + "' will not be supported after fastboot#2.0.0 and it should be removed from the whitelist.");

if (whitelist.indexOf(packageName) === -1) {
console.error("Package '" + packageName + "' is required to be in the whitelist.");
}
}
});

return function(moduleName) {
if (whitelist.indexOf(moduleName) > -1) {
let nodeModulesPath = path.join(distPath, 'node_modules', moduleName);
let packageName = getPackageName(moduleName);
let isWhitelisted = whitelist.indexOf(packageName) > -1;

if (fs.existsSync(nodeModulesPath)) {
return require(nodeModulesPath);
} else {
// If it's not on disk, assume it's a built-in node package
return require(moduleName);
}
} else {
throw new Error("Unable to require module '" + moduleName + "' because it was not in the whitelist.");
if (isWhitelisted) {
let resolvedModulePath = resolve.sync(moduleName, { basedir: distPath });
return require(resolvedModulePath);
}

// Remove after 2.0.0
if (whitelist.indexOf(moduleName) > -1) {
return require(moduleName);
}

throw new Error("Unable to require module '" + moduleName + "' because its package '" + packageName + "' was not in the whitelist.");
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/utils/get-package-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

function getPackageName(modulePath) {
let parts = modulePath.split('/');

if (modulePath[0] === '@') {
return parts[0] + '/' + parts[1];
}
return parts[0];
}

module.exports = getPackageName;
12 changes: 12 additions & 0 deletions test/get-package-name-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const expect = require('chai').expect;
const getPackageName = require('../src/utils/get-package-name');

describe('utils/get-package-name', function() {
it('gets package name from module path', function() {
expect(getPackageName('foo')).to.equal('foo');
expect(getPackageName('foo/bar')).to.equal('foo');
expect(getPackageName('@foo/baz')).to.equal('@foo/baz');
expect(getPackageName('@foo/baz/bar')).to.equal('@foo/baz');
});
});

0 comments on commit 41b058b

Please sign in to comment.