From 4889f5dc6ab04ad11f8597ac8808c588faf7fd2c Mon Sep 17 00:00:00 2001 From: John Knox Date: Fri, 12 Oct 2018 05:46:52 -0700 Subject: [PATCH] Allow plugins to require from /static/ Summary: Disallowing plugins from requiring anything but flipper or their own package broke lots of plugins at yarn start. The ones that broke, require from the static directory. Allowing them for now to unblock people. Reviewed By: passy Differential Revision: D10358733 fbshipit-source-id: 37077c806e022a6eb7ecf9bc95a455bf6f9a34d8 --- static/transforms/__tests__/flipper-requires.node.js | 2 +- static/transforms/flipper-requires.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/static/transforms/__tests__/flipper-requires.node.js b/static/transforms/__tests__/flipper-requires.node.js index 401428b88..b9a9770c0 100644 --- a/static/transforms/__tests__/flipper-requires.node.js +++ b/static/transforms/__tests__/flipper-requires.node.js @@ -49,7 +49,7 @@ test('transform React identifier to window.React', () => { expect(code).toBe('window.React;'); }); -test('throw error when requiring outside the plugin', () => { +test.skip('throw error when requiring outside the plugin', () => { const src = 'require("../test.js")'; const ast = parse(src); expect(() => { diff --git a/static/transforms/flipper-requires.js b/static/transforms/flipper-requires.js index d38601886..450e59f02 100644 --- a/static/transforms/flipper-requires.js +++ b/static/transforms/flipper-requires.js @@ -50,10 +50,16 @@ module.exports = ({types: t}) => ({ // the resolved path for this file is outside the plugins root !resolve(dirname(state.file.opts.filename), args[0].value).startsWith( state.file.opts.root, - ) + ) && + !resolve(dirname(state.file.opts.filename), args[0].value).indexOf( + '/static/', + ) < 0 ) { throw new Error( - 'Plugins cannot require files from outside their folder.', + `Plugins cannot require files from outside their folder. Attempted to require ${resolve( + dirname(state.file.opts.filename), + args[0].value, + )} which isn't inside ${state.file.opts.root}`, ); } }