-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally output eager es modules (#2781)
Summary: In this PR I added `eagerESModules` option to `relay-runtime` and `babel-plugin-relay`. I have two problem which can be solved with this change 1. Rollup handles only es modules. To handle commonjs there is a plugin which converts it into esm. Though it always skips module if import/export statements are found. As a result I get runtime error: "require is not defined". To workaround this I wrote custom plugin. Though it would be cool to have proper solution out of the box. ```js { name: 'relay-generated', transform(code) { // convert __generated__ requires into imports // remove after relay-compiler will be able to emit esm if (code.includes('__generated__')) { let i = -1; const paths = []; const processed = code.replace( /require\((.+__generated__.+)\)/g, (req, pathString) => { i += 1; paths.push(pathString); return `__generated__${i}`; }, ); const imports = paths.map( (p, i) => `import __generated__${i} from ${p};\n`, ); return { code: imports.join('') + processed, }; } }, }, ``` 2. Another problem is flow bug (facebook/flow#7444) which treats all not existent types as any. This leads to many type unsafe places. ```js import type {NotExistentType} from './__generated__/MyQuery.graphql'; type Props = {| my: NotExistentType |} ``` Pull Request resolved: #2781 Reviewed By: jstejada Differential Revision: D17385417 Pulled By: tyao1 fbshipit-source-id: dfa031412666b8afcac4cbae4678f64abbbe5ec9
- Loading branch information
1 parent
8c51286
commit db7a661
Showing
8 changed files
with
194 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/babel-plugin-relay/__tests__/BabelPluginRelay-esm-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+relay | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const transformerWithOptions = require('./transformerWithOptions'); | ||
|
||
describe('`development` option', () => { | ||
it('tests the hash when `development` is set', () => { | ||
expect( | ||
transformerWithOptions({eagerESModules: true}, 'development')( | ||
'graphql`fragment TestFrag on Node { id }`', | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('tests the hash when `isDevVariable` is set', () => { | ||
expect( | ||
transformerWithOptions({eagerESModules: true, isDevVariable: 'IS_DEV'})( | ||
'graphql`fragment TestFrag on Node { id }`', | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('uses a custom build command in message', () => { | ||
expect( | ||
transformerWithOptions( | ||
{ | ||
buildCommand: 'relay-build', | ||
eagerESModules: true, | ||
}, | ||
'development', | ||
)('graphql`fragment TestFrag on Node { id }`'), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('does not test the hash when `development` is not set', () => { | ||
expect( | ||
transformerWithOptions({eagerESModules: true}, 'production')( | ||
'graphql`fragment TestFrag on Node { id }`', | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelay-esm-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`\`development\` option does not test the hash when \`development\` is not set 1`] = ` | ||
"import _TestFrag from './__generated__/TestFrag.graphql'; | ||
_TestFrag; | ||
" | ||
`; | ||
|
||
exports[`\`development\` option tests the hash when \`development\` is set 1`] = ` | ||
"import _TestFrag from './__generated__/TestFrag.graphql'; | ||
_TestFrag.hash && | ||
_TestFrag.hash !== '0bb6b7b29bc3e910921551c4ff5b6757' && | ||
console.error( | ||
\\"The definition of 'TestFrag' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.\\", | ||
), | ||
_TestFrag; | ||
" | ||
`; | ||
|
||
exports[`\`development\` option tests the hash when \`isDevVariable\` is set 1`] = ` | ||
"import _TestFrag from './__generated__/TestFrag.graphql'; | ||
IS_DEV | ||
? (_TestFrag.hash && | ||
_TestFrag.hash !== '0bb6b7b29bc3e910921551c4ff5b6757' && | ||
console.error( | ||
\\"The definition of 'TestFrag' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.\\", | ||
), | ||
_TestFrag) | ||
: _TestFrag; | ||
" | ||
`; | ||
|
||
exports[`\`development\` option uses a custom build command in message 1`] = ` | ||
"import _TestFrag from './__generated__/TestFrag.graphql'; | ||
_TestFrag.hash && | ||
_TestFrag.hash !== '0bb6b7b29bc3e910921551c4ff5b6757' && | ||
console.error( | ||
\\"The definition of 'TestFrag' appears to have changed. Run \`relay-build\` to update the generated files to receive the expected data.\\", | ||
), | ||
_TestFrag; | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters