Something went wrong. Try again.
Mirror: The spec-compliant minimum of client-side GraphQL.
Something went wrong. Try again.
6.1 kB · 251 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252import fs from 'node:fs/promises';import path from 'node:path/posix';import { readFileSync } from 'node:fs';
import * as prettier from 'prettier';import commonjs from '@rollup/plugin-commonjs';import resolve from '@rollup/plugin-node-resolve';import babel from '@rollup/plugin-babel';import terser from '@rollup/plugin-terser';import cjsCheck from 'rollup-plugin-cjs-check';import dts from 'rollup-plugin-dts';
const normalize = name => [] .concat(name) .join(' ') .replace(/[@\s/.]+/g, ' ') .trim() .replace(/\s+/, '-') .toLowerCase();
const extension = name => { if (/\.d.ts$/.test(name)) { return '.d.ts'; } else { return path.extname(name); }};
const meta = JSON.parse(readFileSync('package.json'));const name = normalize(meta.name);
const externalModules = [ ...Object.keys(meta.dependencies || {}), ...Object.keys(meta.peerDependencies || {}),];
const external = new RegExp(`^(${externalModules.join('|')})($|/)`);
const exports = {};for (const key in meta.exports) { const entry = meta.exports[key]; if (typeof entry === 'object' && !!entry.source) { const entryPath = normalize(key); const entryName = normalize([name, entryPath]); exports[entryName] = { path: entryPath, ...entry, }; }}
const commonConfig = { input: Object.entries(exports).reduce((input, [exportName, entry]) => { input[exportName] = entry.source; return input; }, {}), onwarn: () => {}, external(id) { return external.test(id); }, treeshake: { unknownGlobalSideEffects: false, tryCatchDeoptimization: false, moduleSideEffects: false, },};
const commonPlugins = [ resolve({ extensions: ['.mjs', '.js', '.ts'], mainFields: ['module', 'jsnext', 'main'], preferBuiltins: false, browser: true, }),
commonjs({ ignoreGlobal: true, include: /\/node_modules\//, }),];
const commonOutput = { dir: './', exports: 'auto', sourcemap: true, sourcemapExcludeSources: true, hoistTransitiveImports: false, indent: false, freeze: false, strict: false, generatedCode: { preset: 'es5', reservedNamesAsProps: false, objectShorthand: false, constBindings: false, },};
const outputPlugins = [ { name: 'outputPackageJsons', async writeBundle() { for (const key in exports) { const entry = exports[key]; if (entry.path) { const output = path.relative(entry.path, process.cwd()); const json = JSON.stringify({ name: key, private: true, version: '0.0.0', main: path.join(output, entry.require), module: path.join(output, entry.import), types: path.join(output, entry.types), source: path.join(output, entry.source), exports: { '.': { types: path.join(output, entry.types), import: path.join(output, entry.import), require: path.join(output, entry.require), source: path.join(output, entry.source), }, }, }, null, 2);
await fs.mkdir(entry.path, { recursive: true }); await fs.writeFile(path.join(entry.path, 'package.json'), json); } } }, },
cjsCheck(),
terser({ warnings: true, ecma: 2015, keep_fnames: true, ie8: false, compress: { pure_getters: true, toplevel: true, booleans_as_integers: false, keep_fnames: true, keep_fargs: true, ie8: false, conditionals: false, join_vars: false, }, mangle: { module: true, keep_fnames: true, }, output: { beautify: true, braces: true, indent_level: 2, }, }),];
export default [ { ...commonConfig, plugins: [ ...commonPlugins, babel({ babelrc: false, babelHelpers: 'bundled', extensions: ['mjs', 'js', 'jsx', 'ts', 'tsx'], exclude: 'node_modules/**', presets: [], plugins: [ '@babel/plugin-transform-typescript', '@babel/plugin-transform-block-scoping', ], }), ], output: [ { ...commonOutput, format: 'esm', chunkFileNames(chunk) { return `dist/chunks/[name]-chunk${extension(chunk.name) || '.mjs'}`; }, entryFileNames(chunk) { return chunk.isEntry ? path.normalize(exports[chunk.name].import) : `dist/[name].mjs`; }, plugins: outputPlugins, }, { ...commonOutput, format: 'cjs', esModule: true, externalLiveBindings: true, chunkFileNames(chunk) { return `dist/chunks/[name]-chunk${extension(chunk.name) || '.js'}`; }, entryFileNames(chunk) { return chunk.isEntry ? path.normalize(exports[chunk.name].require) : `dist/[name].js`; }, plugins: outputPlugins, }, ], },
{ ...commonConfig, plugins: [ ...commonPlugins, dts(), ], output: { ...commonOutput, sourcemap: false, format: 'dts', chunkFileNames(chunk) { return `dist/chunks/[name]-chunk${extension(chunk.name) || '.d.ts'}`; }, entryFileNames(chunk) { return chunk.isEntry ? path.normalize(exports[chunk.name].types) : `dist/[name].d.ts`; }, plugins: [ { renderChunk(code, chunk) { if (chunk.fileName.endsWith('d.ts')) { const gqlImportRe = /(import\s+(?:[*\s{}\w\d]+)\s*from\s*'graphql';?)/g; code = code.replace(gqlImportRe, x => '/*@ts-ignore*/\n' + x);
code = prettier.format(code, { filepath: chunk.fileName, parser: 'typescript', singleQuote: true, tabWidth: 2, printWidth: 100, trailingComma: 'es5', });
return code; } }, }, ], }, },];