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

Add build pipeline to create output mirroring graphql

Changed files
+187 -167
alias
scripts
-21
alias/index.mjs
···
-
export { version, versionInfo } from 'graphql/version';
-
-
export {
-
parse,
-
parseValue,
-
parseType,
-
print,
-
visit,
-
visitInParallel,
-
getVisitFn,
-
Kind,
-
} from 'graphql/language';
-
-
/** Create, format, and print GraphQL errors. */
-
export {
-
GraphQLError,
-
syntaxError,
-
locatedError,
-
printError,
-
formatError,
-
} from 'graphql/error';
+1 -2
package.json
···
"main": "index.js",
"license": "MIT",
"scripts": {
-
"build": "rollup -c scripts/rollup/config.mjs"
+
"build": "rollup -c scripts/rollup/config.js"
},
"devDependencies": {
"@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",
+1 -1
scripts/babel/transformDevAssert.mjs
···
const visited = 'visitedByTransformDevAssert';
const warningDevCheckTemplate = `
-
if (process.env.NODE_ENV !== 'production') {
+
if (false) {
NODE;
}
`.trim();
+185
scripts/rollup/config.js
···
+
import * as path from 'path';
+
+
import resolve from '@rollup/plugin-node-resolve';
+
import { babel } from '@rollup/plugin-babel';
+
import { terser } from 'rollup-plugin-terser';
+
+
import babelModularGraphQL from 'babel-plugin-modular-graphql';
+
import babelTransformDevAssert from '../babel/transformDevAssert.mjs';
+
import babelTransformObjectFreeze from '../babel/transformObjectFreeze.mjs';
+
+
const cwd = process.cwd();
+
const graphqlModule = path.join(cwd, 'node_modules/graphql/');
+
const virtualModule = path.join(cwd, 'virtual/');
+
const aliasModule = path.join(cwd, 'alias/');
+
+
const EXTERNAL = 'graphql';
+
const externalModules = ['dns', 'fs', 'path', 'url'];
+
const externalPredicate = new RegExp(`^(${externalModules.join('|')})($|/)`);
+
+
const exports = {};
+
const importMap = require('babel-plugin-modular-graphql/import-map.json');
+
const excludeMap = new Set(['Lexer']);
+
+
for (const key in importMap) {
+
const { from, local } = importMap[key];
+
if (/\/jsutils\//g.test(from) || excludeMap.has(key)) continue;
+
+
const name = from.replace(/^graphql\//, '');
+
exports[name] = (exports[name] || '') + `export { ${key} } from '${EXTERNAL}'\n`;
+
+
const parts = name.split('/');
+
for (let i = parts.length - 1; i > 0; i--) {
+
const name = `${parts.slice(0, i).join('/')}/index`;
+
const from = `./${parts.slice(i).join('/')}`;
+
exports[name] = (exports[name] || '') + `export { ${local} } from '${from}'\n`;
+
}
+
+
const index = `export { ${local} } from './${name}'\n`;
+
exports.index = (exports.index || '') + index;
+
}
+
+
const manualChunks = (id, utils) => {
+
let chunk;
+
if (id.startsWith(graphqlModule)) {
+
chunk = id.slice(graphqlModule.length);
+
} else if (id.startsWith(virtualModule)) {
+
chunk = id.slice(virtualModule.length);
+
} else if (id.startsWith(aliasModule)) {
+
chunk = id.slice(aliasModule.length);
+
}
+
+
if (chunk) {
+
return chunk.replace(/\.m?js$/, '');
+
}
+
+
const { importers } = utils.getModuleInfo(id);
+
return importers.length === 1
+
? manualChunks(importers[0], utils)
+
: 'shared';
+
};
+
+
export default {
+
input:
+
Object.keys(exports).reduce((input, key) => {
+
input[key] = path.join('./virtual', key);
+
return input;
+
}, {}),
+
external(id) {
+
return externalPredicate.test(id);
+
},
+
treeshake: {
+
unknownGlobalSideEffects: false,
+
tryCatchDeoptimization: false,
+
moduleSideEffects: false,
+
},
+
plugins: [
+
{
+
async load(id) {
+
if (!id.startsWith(virtualModule))
+
return null;
+
+
const entry = path.relative(virtualModule, id).replace(/\.m?js$/, '');
+
return exports[entry] || null;
+
},
+
+
async resolveId(source, importer) {
+
if (!source.startsWith('.') && !source.startsWith('virtual/'))
+
return null;
+
+
const target = path.join(importer ? path.dirname(importer) : cwd, source);
+
+
const virtualEntry = path.relative(virtualModule, target);
+
if (!virtualEntry.startsWith('../')) {
+
const aliasSource = path.join(aliasModule, virtualEntry);
+
const alias = await this.resolve(aliasSource, undefined, { skipSelf: true });
+
return alias || target;
+
}
+
+
const graphqlEntry = path.relative(graphqlModule, target);
+
if (!graphqlEntry.startsWith('../')) {
+
const aliasSource = path.join(aliasModule, graphqlEntry);
+
const alias = await this.resolve(aliasSource, undefined, { skipSelf: true });
+
return alias || target;
+
}
+
+
return null;
+
},
+
},
+
+
resolve({
+
extensions: ['.mjs', '.js'],
+
mainFields: ['module', 'browser', 'main'],
+
preferBuiltins: false,
+
browser: true,
+
}),
+
+
babel({
+
babelrc: false,
+
babelHelpers: 'bundled',
+
presets: [],
+
plugins: [
+
babelTransformDevAssert,
+
babelTransformObjectFreeze,
+
babelModularGraphQL,
+
'reghex/babel',
+
],
+
}),
+
+
terser({
+
warnings: true,
+
ecma: 5,
+
keep_fnames: true,
+
ie8: false,
+
compress: {
+
pure_getters: true,
+
toplevel: true,
+
booleans_as_integers: false,
+
keep_fnames: true,
+
keep_fargs: true,
+
if_return: false,
+
ie8: false,
+
sequences: false,
+
loops: false,
+
conditionals: false,
+
join_vars: false
+
},
+
mangle: {
+
module: true,
+
keep_fnames: true,
+
},
+
output: {
+
beautify: true,
+
braces: true,
+
indent_level: 2
+
}
+
}),
+
],
+
+
treeshake: 'smallest',
+
shimMissingExports: false,
+
preserveEntrySignatures: 'allow-extension',
+
+
output: [
+
{
+
chunkFileNames: '[name].js',
+
entryFileNames: '[name].js',
+
dir: './dist',
+
exports: 'named',
+
format: 'cjs',
+
minifyInternalExports: false,
+
hoistTransitiveImports: false,
+
manualChunks,
+
},
+
{
+
chunkFileNames: '[name].mjs',
+
entryFileNames: '[name].mjs',
+
dir: './dist',
+
exports: 'named',
+
format: 'esm',
+
minifyInternalExports: false,
+
hoistTransitiveImports: false,
+
manualChunks,
+
},
+
],
+
};
-123
scripts/rollup/config.mjs
···
-
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 babelModularGraphQL from 'babel-plugin-modular-graphql';
-
import babelTransformDevAssert from '../babel/transformDevAssert.mjs';
-
import babelTransformObjectFreeze from '../babel/transformObjectFreeze.mjs';
-
-
const cwd = process.cwd();
-
const externalModules = ['dns', 'fs', 'path', 'url'];
-
const externalPredicate = new RegExp(`^(${externalModules.join('|')})($|/)`);
-
-
const config = {
-
input: {
-
graphql: './alias/index.mjs',
-
},
-
onwarn() {},
-
external(id) {
-
return externalPredicate.test(id);
-
},
-
treeshake: {
-
unknownGlobalSideEffects: false,
-
tryCatchDeoptimization: false,
-
moduleSideEffects: false,
-
},
-
};
-
-
export default {
-
...config,
-
shimMissingExports: true,
-
plugins: [
-
{
-
async resolveId(source, importer) {
-
if (source.startsWith('.') && importer) {
-
source = path.resolve(path.dirname(importer), source);
-
}
-
-
const base = path.join(cwd, 'node_modules/graphql/');
-
const baseSource = path.relative(base, source);
-
if (baseSource.startsWith('..')) {
-
return null;
-
}
-
-
const aliasSource = path.join(cwd, 'alias/', baseSource);
-
return this.resolve(aliasSource, importer, { skipSelf: true });
-
},
-
},
-
-
resolve({
-
extensions: ['.mjs', '.js'],
-
mainFields: ['module', 'browser', 'main'],
-
preferBuiltins: false,
-
browser: true,
-
}),
-
-
babel({
-
babelrc: false,
-
babelHelpers: 'bundled',
-
presets: [],
-
plugins: [
-
babelTransformDevAssert,
-
babelTransformObjectFreeze,
-
babelModularGraphQL,
-
'reghex/babel',
-
],
-
}),
-
],
-
-
output: [
-
{
-
chunkFileNames: '[hash].mjs',
-
entryFileNames: '[name].mjs',
-
dir: './dist',
-
exports: 'named',
-
externalLiveBindings: false,
-
sourcemap: true,
-
esModule: false,
-
indent: false,
-
freeze: false,
-
strict: false,
-
format: 'esm',
-
},
-
{
-
chunkFileNames: '[hash].min.mjs',
-
entryFileNames: '[name].min.mjs',
-
dir: './dist',
-
exports: 'named',
-
externalLiveBindings: false,
-
sourcemap: true,
-
esModule: false,
-
indent: false,
-
freeze: false,
-
strict: false,
-
format: 'esm',
-
plugins: [
-
replace({
-
'process.env.NODE_ENV': JSON.stringify('production')
-
}),
-
-
terser({
-
warnings: true,
-
ecma: 5,
-
ie8: false,
-
toplevel: true,
-
compress: {
-
keep_infinity: true,
-
pure_getters: true,
-
passes: 10
-
},
-
mangle: {
-
module: true,
-
},
-
output: {
-
comments: false
-
}
-
}),
-
],
-
}
-
],
-
};
-20
yarn.lock
···
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"