-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(babel-preset-gatsby-package): add conditional compilation plugin #32687
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f8925a
feat(babel-preset-gatsby-package): add conditional compilation plugin
wardpeet 80038b1
fix bootstrap
wardpeet 33d89aa
update COMPILER_OPTIONS parsing
wardpeet 549f49f
fix availableflags
wardpeet b465ec5
fix typings
wardpeet fe4c5a5
fix types
wardpeet 6b284e2
review changes
wardpeet c174c39
update snapshots
pieh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"plugins": [ | ||
["@babel/plugin-transform-modules-commonjs"] | ||
], | ||
"overrides": [ | ||
{ | ||
"test": ["**/*.ts", "**/*.tsx"], | ||
"plugins": [["@babel/plugin-transform-typescript", { "isTSX": true }]], | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
yarn.lock | ||
dist/ |
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
3 changes: 3 additions & 0 deletions
3
packages/babel-preset-gatsby-package/__tests__/babel-transform-compiler-flags.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,3 @@ | ||
const runner = require(`@babel/helper-plugin-test-runner`).default | ||
|
||
runner(__dirname) |
17 changes: 17 additions & 0 deletions
17
packages/babel-preset-gatsby-package/__tests__/fixtures/compiler-flags/default/input.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,17 @@ | ||
|
||
if (_CFLAGS_.MAJOR === '4') { | ||
console.log('We only load this for Gatsby 4') | ||
} else { | ||
console.log('Old code path') | ||
} | ||
|
||
function isGatsby4() { | ||
return _CFLAGS_.MAJOR === '4' | ||
} | ||
|
||
|
||
if (isGatsby4()) { | ||
console.log('We only load this for Gatsby 4') | ||
} else { | ||
console.log('Old code path') | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/babel-preset-gatsby-package/__tests__/fixtures/compiler-flags/default/output.mjs
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,15 @@ | ||
if ("4" === '4') { | ||
console.log('We only load this for Gatsby 4'); | ||
} else { | ||
console.log('Old code path'); | ||
} | ||
|
||
function isGatsby4() { | ||
return "4" === '4'; | ||
} | ||
|
||
if (isGatsby4()) { | ||
console.log('We only load this for Gatsby 4'); | ||
} else { | ||
console.log('Old code path'); | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/babel-preset-gatsby-package/__tests__/fixtures/compiler-flags/unused/input.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,6 @@ | ||
|
||
if (_CFLAGS_.UNKNOWN === 'true') { | ||
console.log('We only load this when true') | ||
} else { | ||
console.log('Old code path') | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-preset-gatsby-package/__tests__/fixtures/compiler-flags/unused/options.json
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,3 @@ | ||
{ | ||
"throws": "UNKNOWN is not part of the available compiler flags." | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/babel-preset-gatsby-package/__tests__/fixtures/options.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,17 @@ | ||
const path = require("path") | ||
|
||
module.exports = { | ||
sourceType: "module", | ||
plugins: [ | ||
[path.resolve(__dirname, "../../src/babel-transform-compiler-flags.ts"), | ||
{ | ||
availableFlags: ['MAJOR'], | ||
flags: { | ||
MAJOR: '4', | ||
} | ||
} | ||
], | ||
], | ||
babelrc: false, | ||
configFile: false, | ||
} |
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
52 changes: 52 additions & 0 deletions
52
packages/babel-preset-gatsby-package/src/babel-transform-compiler-flags.ts
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,52 @@ | ||
import type { NodePath, PluginObj, types, PluginPass } from "@babel/core" | ||
|
||
interface IPluginOptions { | ||
flags: Record<string, string> | ||
availableFlags: Array<string> | ||
} | ||
|
||
export default function compilerFlags( | ||
{ | ||
types: t, | ||
}: { | ||
types: typeof types | ||
}, | ||
opts: Partial<IPluginOptions> | ||
): PluginObj { | ||
if (!opts.flags) { | ||
throw new Error(`flags option needs to be set`) | ||
} | ||
if (!opts.availableFlags) { | ||
throw new Error(`availableFlags option needs to be set`) | ||
} | ||
|
||
return { | ||
name: `babel-transform-compiler-flags`, | ||
visitor: { | ||
Identifier( | ||
nodePath: NodePath<types.Identifier>, | ||
state: PluginPass | ||
): void { | ||
const flags = (state.opts as IPluginOptions).flags | ||
const availableFlags = (state.opts as IPluginOptions).availableFlags | ||
|
||
if ( | ||
nodePath.node.name === `_CFLAGS_` && | ||
t.isMemberExpression(nodePath.parent) | ||
) { | ||
const cFlag = (nodePath.parent.property as types.Identifier).name | ||
|
||
if (!availableFlags.includes(cFlag)) { | ||
throw new Error( | ||
`${cFlag} is not part of the available compiler flags.` | ||
) | ||
} | ||
|
||
nodePath.parentPath.replaceWith( | ||
t.stringLiteral(flags[cFlag] ? flags[cFlag] : ``) | ||
) | ||
} | ||
}, | ||
}, | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...s/babel-preset-gatsby-package/resolver.js → ...bel-preset-gatsby-package/src/resolver.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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
const r = m => require.resolve(m) | ||
|
||
module.exports = r; | ||
module.exports = r |
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,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"strict": true | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"resolveJsonModule": true, | ||
"esModuleInterop": true, | ||
"jsx": "preserve", | ||
"typeRoots": ["./types", "**/node_modules/@types", "node_modules/@types"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is necessary to find the global CFLAGS types in the monorepo |
||
"paths": { | ||
"@reach/router/*": ["./node_modules/@gatsbyjs/reach-router/*"] | ||
} | ||
|
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,3 @@ | ||
declare const _CFLAGS_: { | ||
GATSBY_MAJOR: string | ||
} |
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,5 @@ | ||
{ | ||
"name": "gatsby-monorepo", | ||
"types": "./global.d.ts", | ||
"private": true | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are not necessary anymore, else I had to change a few things in babelrc