Skip to content
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

fix(babel-preset-gatsby): no compile #32713

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/babel-preset-gatsby-package/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path")
module.exports = {
sourceType: "module",
plugins: [
[path.resolve(__dirname, "../../src/babel-transform-compiler-flags.ts"),
[path.resolve(__dirname, "../../babel-transform-compiler-flags.js"),
{
availableFlags: ['MAJOR'],
flags: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const preset = require(`../src/index.js`)
const preset = require(`../index.js`)

jest.mock(`../src/resolver`, () => jest.fn(moduleName => moduleName))
jest.mock(`../resolver`, () => jest.fn(moduleName => moduleName))


describe(`babel-preset-gatsby-package`, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

/**
* @typedef {import('@babel/core').NodePath} NodePath
* @typedef {import('@babel/core').PluginObj} PluginObj
* @typedef {import('@babel/core').PluginPass} PluginPass
* @typedef {import('@babel/core').types} BabelTypes
* @typedef {import('@babel/core').types.Identifier} Identifier
* @typedef {import('@babel/core').types.MemberExpression} MemberExpression
*/

/**
* @typedef {Object} IPluginOptions
* @property {Record<string, string>} flags
* @property {Array<string>} availableFlags
*/

/**
*
* @param {{ types: BabelTypes }} args
* @param {Partial<IPluginOptions>} opts
* @returns {PluginObj}
*/
module.exports = function compilerFlags(
{
types: t,
},
opts
) {
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: {
/**
* @param {NodePath} nodePath
* @param {PluginPass} state
*/
Identifier(
nodePath,
state
) {
const identifier = /** @type {Identifier} */ (nodePath.node)
const flags = /** @type {IPluginOptions} */ (state.opts).flags
const availableFlags = /** @type {IPluginOptions} */ (state.opts).availableFlags

if (
identifier.name === `_CFLAGS_` &&
t.isMemberExpression(nodePath.parent)
) {
const parentNode = /** @type {MemberExpression} */ (nodePath.parent)
const cFlag = /** @type {Identifier} */ (parentNode.property).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] : ``)
)
}
},
},
}
}
7 changes: 5 additions & 2 deletions packages/babel-preset-gatsby-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
"@babel/core": "^7.11.6"
},
"license": "MIT",
"main": "dist/index.js",
"main": "lib/index.js",
"engines": {
"node": ">=12.13.0"
},
"files": [
"lib/*.js"
],
"scripts": {
"build": "babel src --out-dir dist/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" --extensions \".ts,.js\"",
"build": "",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir dist/ --ignore \"**/__tests__\" --extensions \".ts,.js\""
}
Expand Down

This file was deleted.

6 changes: 5 additions & 1 deletion packages/babel-preset-gatsby-package/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"extends": "../../tsconfig.json",
"strict": true
"strict": true,
"compilerOptions": {
"allowJs": true,
"checkJs": true
}
}