Skip to content

Commit 1aea993

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 e5c777d commit 1aea993

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"chalk": "^2.0.1",
3030
"cookie": "^0.3.1",
3131
"debug": "^3.0.0",
32-
"exists-sync": "0.0.4",
3332
"najax": "^1.0.2",
33+
"resolve": "^1.8.1",
3434
"rsvp": "^4.7.0",
3535
"simple-dom": "^1.0.0",
3636
"source-map-support": "^0.5.0"

src/ember-app.js

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

77
const najax = require('najax');
88
const SimpleDOM = require('simple-dom');
9-
const existsSync = require('exists-sync');
9+
const resolve = require('resolve');
1010
const debug = require('debug')('fastboot:ember-app');
1111

1212
const FastBootInfo = require('./fastboot-info');
1313
const Result = require('./result');
1414
const FastBootSchemaVersions = require('./fastboot-schema-versions');
15+
const getPackageName = require('./utils/get-package-name');
1516

1617
/**
1718
* @private
@@ -117,21 +118,34 @@ class EmberApp {
117118
buildWhitelistedRequire(whitelist, distPath) {
118119
whitelist.forEach(function(whitelistedModule) {
119120
debug("module whitelisted; module=%s", whitelistedModule);
120-
});
121121

122-
return function(moduleName) {
123-
if (whitelist.indexOf(moduleName) > -1) {
124-
let nodeModulesPath = path.join(distPath, 'node_modules', moduleName);
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.");
125127

126-
if (existsSync(nodeModulesPath)) {
127-
return require(nodeModulesPath);
128-
} else {
129-
// If it's not on disk, assume it's a built-in node package
130-
return require(moduleName);
128+
if (whitelist.indexOf(packageName) === -1) {
129+
console.error("Package '" + packageName + "' is required to be in the whitelist.");
131130
}
132-
} else {
133-
throw new Error("Unable to require module '" + moduleName + "' because it was not in the whitelist.");
134131
}
132+
});
133+
134+
return function(modulePath) {
135+
let packageName = getPackageName(modulePath);
136+
let isWhitelisted = whitelist.indexOf(packageName) > -1;
137+
138+
if (isWhitelisted) {
139+
let resolvedModulePath = resolve.sync(modulePath, { basedir: distPath });
140+
return require(resolvedModulePath);
141+
}
142+
143+
// Remove after 2.0.0
144+
if (whitelist.indexOf(modulePath) > -1) {
145+
return require(modulePath);
146+
}
147+
148+
throw new Error("Unable to require module '" + modulePath + "' because its package '" + packageName + "' was not in the whitelist.");
135149
};
136150
}
137151

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/fixtures/custom-sandbox/custom-sandbox.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const vm = require('vm');
44
const fs = require('fs');
5-
const existsSync = require('exists-sync');
65
const Sandbox = require('./../../../src/sandbox');
76

87
/**

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)