Mirror: The small sibling of the graphql package, slimmed down for client-side libraries.

Add babel transform to strip devAssert calls

Changed files
+86
scripts
+1
package.json
···
"@babel/core": "^7.15.0",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@sucrase/jest-plugin": "^2.1.1",
"babel-plugin-modular-graphql": "^1.0.1",
"jest": "^27.0.6",
···
"@babel/core": "^7.15.0",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-node-resolve": "^13.0.4",
+
"@rollup/plugin-replace": "^3.0.0",
"@sucrase/jest-plugin": "^2.1.1",
"babel-plugin-modular-graphql": "^1.0.1",
"jest": "^27.0.6",
+56
scripts/babel/transformDevAssert.mjs
···
···
+
const visited = 'visitedByTransformDevAssert';
+
+
const warningDevCheckTemplate = `
+
if (process.env.NODE_ENV !== 'production') {
+
NODE;
+
}
+
`.trim();
+
+
const plugin = ({ template, types: t }) => {
+
const wrapWithDevCheck = template(warningDevCheckTemplate, {
+
placeholderPattern: /^NODE$/,
+
});
+
+
return {
+
visitor: {
+
CallExpression(path) {
+
const { name } = path.node.callee;
+
if (path.node[visited]) return;
+
+
if (name === 'devAssert') {
+
path.node[visited] = true;
+
+
// The production-check may be hoisted if the parent
+
// is already an if-statement only containing the
+
// warn call
+
let p = path;
+
while (t.isExpressionStatement(p.parentPath.node)) {
+
if (
+
t.isBlockStatement(p.parentPath.parentPath.node) &&
+
p.parentPath.parentPath.node.body.length === 1 &&
+
p.parentPath.parentPath.node.body[0] === path.parentPath.node &&
+
t.isIfStatement(p.parentPath.parentPath.parentPath.node) &&
+
p.parentPath.parentPath.parentPath.node.consequent ===
+
p.parentPath.parentPath.node &&
+
!p.parentPath.parentPath.node.alternate
+
) {
+
p = p.parentPath.parentPath.parentPath;
+
} else if (
+
t.isIfStatement(p.parentPath.parentPath.node) &&
+
p.parentPath.parentPath.node.consequent === p.parentPath.node &&
+
!p.parentPath.parentPath.node.alternate
+
) {
+
p = path.parentPath.parentPath;
+
} else {
+
break;
+
}
+
}
+
+
p.replaceWith(wrapWithDevCheck({ NODE: p.node }));
+
}
+
},
+
},
+
};
+
};
+
+
export default plugin;
+9
scripts/rollup/config.mjs
···
import * as path from 'path';
import resolve from '@rollup/plugin-node-resolve';
import { babel } from '@rollup/plugin-babel';
import { terser } from 'rollup-plugin-terser';
const cwd = process.cwd();
const externalModules = ['dns', 'fs', 'path', 'url'];
···
input: {
graphql: './alias/index.mjs',
},
external(id) {
return externalPredicate.test(id);
},
···
babelHelpers: 'bundled',
presets: [],
plugins: [
'babel-plugin-modular-graphql',
'reghex/babel',
],
···
strict: false,
format: 'esm',
plugins: [
terser({
warnings: true,
ecma: 5,
···
import * as path from 'path';
import resolve from '@rollup/plugin-node-resolve';
+
import replace from '@rollup/plugin-replace';
import { babel } from '@rollup/plugin-babel';
import { terser } from 'rollup-plugin-terser';
+
+
import babelTransformDevAssert from '../babel/transformDevAssert.mjs';
const cwd = process.cwd();
const externalModules = ['dns', 'fs', 'path', 'url'];
···
input: {
graphql: './alias/index.mjs',
},
+
onwarn() {},
external(id) {
return externalPredicate.test(id);
},
···
babelHelpers: 'bundled',
presets: [],
plugins: [
+
babelTransformDevAssert,
'babel-plugin-modular-graphql',
'reghex/babel',
],
···
strict: false,
format: 'esm',
plugins: [
+
replace({
+
'process.env.NODE_ENV': JSON.stringify('production')
+
}),
+
terser({
warnings: true,
ecma: 5,
+20
yarn.lock
···
is-module "^1.0.0"
resolve "^1.19.0"
"@rollup/pluginutils@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
···
dependencies:
yallist "^4.0.0"
make-dir@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
···
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
sprintf-js@~1.0.2:
version "1.0.3"
···
is-module "^1.0.0"
resolve "^1.19.0"
+
"@rollup/plugin-replace@^3.0.0":
+
version "3.0.0"
+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-3.0.0.tgz#3a4c9665d4e7a4ce2c360cf021232784892f3fac"
+
integrity sha512-3c7JCbMuYXM4PbPWT4+m/4Y6U60SgsnDT/cCyAyUKwFHg7pTSfsSQzIpETha3a3ig6OdOKzZz87D9ZXIK3qsDg==
+
dependencies:
+
"@rollup/pluginutils" "^3.1.0"
+
magic-string "^0.25.7"
+
"@rollup/pluginutils@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
···
dependencies:
yallist "^4.0.0"
+
magic-string@^0.25.7:
+
version "0.25.7"
+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
+
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
+
dependencies:
+
sourcemap-codec "^1.4.4"
+
make-dir@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
···
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
+
+
sourcemap-codec@^1.4.4:
+
version "1.4.8"
+
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
+
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==
sprintf-js@~1.0.2:
version "1.0.3"