Skip to content

Commit 41b058b

Browse files
committed
Allow to require module path from whitelisted dependency
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.
1 parent b79df75 commit 41b058b

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"cookie": "^0.3.1",
3131
"debug": "^3.0.0",
3232
"najax": "^1.0.2",
33+
"resolve": "^1.8.1",
3334
"rsvp": "^4.7.0",
3435
"simple-dom": "^1.0.0",
3536
"source-map-support": "^0.5.0"

src/ember-app.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ const chalk = require('chalk');
66

77
const najax = require('najax');
88
const SimpleDOM = require('simple-dom');
9+
const resolve = require('resolve');
910
const debug = require('debug')('fastboot:ember-app');
1011

1112
const FastBootInfo = require('./fastboot-info');
1213
const Result = require('./result');
1314
const FastBootSchemaVersions = require('./fastboot-schema-versions');
15+
const getPackageName = require('./utils/get-package-name');
1416

1517
/**
1618
* @private
@@ -116,21 +118,34 @@ class EmberApp {
116118
buildWhitelistedRequire(whitelist, distPath) {
117119
whitelist.forEach(function(whitelistedModule) {
118120
debug("module whitelisted; module=%s", whitelistedModule);
121+
122+
// Remove after 2.0.0
123+
let packageName = getPackageName(whitelistedModule);
124+
125+
if (packageName !== whitelistedModule) {
126+
console.warn("[DEPRECATION] Whitelisting module path '" + whitelistedModule + "' will not be supported after fastboot#2.0.0 and it should be removed from the whitelist.");
127+
128+
if (whitelist.indexOf(packageName) === -1) {
129+
console.error("Package '" + packageName + "' is required to be in the whitelist.");
130+
}
131+
}
119132
});
120133

121134
return function(moduleName) {
122-
if (whitelist.indexOf(moduleName) > -1) {
123-
let nodeModulesPath = path.join(distPath, 'node_modules', moduleName);
135+
let packageName = getPackageName(moduleName);
136+
let isWhitelisted = whitelist.indexOf(packageName) > -1;
124137

125-
if (fs.existsSync(nodeModulesPath)) {
126-
return require(nodeModulesPath);
127-
} else {
128-
// If it's not on disk, assume it's a built-in node package
129-
return require(moduleName);
130-
}
131-
} else {
132-
throw new Error("Unable to require module '" + moduleName + "' because it was not in the whitelist.");
138+
if (isWhitelisted) {
139+
let resolvedModulePath = resolve.sync(moduleName, { basedir: distPath });
140+
return require(resolvedModulePath);
141+
}
142+
143+
// Remove after 2.0.0
144+
if (whitelist.indexOf(moduleName) > -1) {
145+
return require(moduleName);
133146
}
147+
148+
throw new Error("Unable to require module '" + moduleName + "' because its package '" + packageName + "' was not in the whitelist.");
134149
};
135150
}
136151

src/utils/get-package-name.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
function getPackageName(modulePath) {
4+
let parts = modulePath.split('/');
5+
6+
if (modulePath[0] === '@') {
7+
return parts[0] + '/' + parts[1];
8+
}
9+
return parts[0];
10+
}
11+
12+
module.exports = getPackageName;

test/get-package-name-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const expect = require('chai').expect;
3+
const getPackageName = require('../src/utils/get-package-name');
4+
5+
describe('utils/get-package-name', function() {
6+
it('gets package name from module path', function() {
7+
expect(getPackageName('foo')).to.equal('foo');
8+
expect(getPackageName('foo/bar')).to.equal('foo');
9+
expect(getPackageName('@foo/baz')).to.equal('@foo/baz');
10+
expect(getPackageName('@foo/baz/bar')).to.equal('@foo/baz');
11+
});
12+
});

0 commit comments

Comments
 (0)