Skip to content

Commit

Permalink
fix(babel-preset-gatsby): no compile (#32713)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Aug 6, 2021
1 parent 77e9aab commit 850a5ea
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 60 deletions.
2 changes: 0 additions & 2 deletions packages/babel-preset-gatsby-package/.gitignore

This file was deleted.

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
@@ -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
@@ -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
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
@@ -1,4 +1,8 @@
{
"extends": "../../tsconfig.json",
"strict": true
"strict": true,
"compilerOptions": {
"allowJs": true,
"checkJs": true
}
}

0 comments on commit 850a5ea

Please sign in to comment.