Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 1 addition & 29 deletions packages/instantsearch-ui-components/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
import { createESMConfig, createCJSConfig, createJsxPragmaFixPlugins } from '../../scripts/build/rollup.base.mjs';
import { readdirSync } from 'node:fs';
import { extname, join } from 'node:path';
import { createESMConfig, createCJSConfig, createJsxPragmaFixPlugins, collectSourceEntries } from '../../scripts/build/rollup.base.mjs';
import pkg from './package.json' with { type: 'json' };

const SOURCE_ROOT = 'src';
const SOURCE_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx']);
const IGNORED_DIRS = new Set(['__tests__', '__mocks__']);

function collectSourceEntries(dir = SOURCE_ROOT) {
const entries = [];
const dirEntries = readdirSync(dir, { withFileTypes: true });

for (const entry of dirEntries) {
const filePath = join(dir, entry.name);

if (entry.isDirectory()) {
if (!IGNORED_DIRS.has(entry.name)) {
entries.push(...collectSourceEntries(filePath));
}
continue;
}

if (entry.isFile() && SOURCE_EXTENSIONS.has(extname(entry.name))) {
entries.push(filePath);
}
}

return entries;
}

const input = collectSourceEntries();

// This package is framework-agnostic and uses /** @jsx createElement */ pragmas
Expand Down
58 changes: 5 additions & 53 deletions packages/instantsearch.js/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* - CJS: preserved modules
* - UMD: bundled for browsers
*/
import { existsSync, readFileSync, readdirSync } from 'fs';
import { dirname, extname, join, resolve } from 'path';
import { existsSync, readFileSync } from 'fs';
import { dirname, resolve } from 'path';

import {
createESMConfig,
createCJSConfig,
createUMDConfig,
createBanner,
collectSourceEntries,
} from '../../scripts/build/rollup.base.mjs';

const pkg = JSON.parse(readFileSync('./package.json', 'utf8'));
Expand All @@ -25,9 +26,6 @@ const isESM = process.env.BUILD_FORMAT === 'esm';
const isCJS = process.env.BUILD_FORMAT === 'cjs';
const isUMD = process.env.BUILD_FORMAT === 'umd';

const SOURCE_ROOT = 'src';
const SOURCE_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx']);
const IGNORED_DIRS = new Set(['__tests__', '__mocks__']);
const RESOLVABLE_EXTENSIONS = ['.ts', '.tsx', '.jsx'];

function resolveJsImportsToSource() {
Expand Down Expand Up @@ -58,46 +56,6 @@ function resolveJsImportsToSource() {
};
}

function collectSourceEntries({ includeIndex, includeIndexEs }) {
const entries = [];

function walk(dir) {
const dirEntries = readdirSync(dir, { withFileTypes: true });
for (const entry of dirEntries) {
if (entry.isDirectory()) {
if (IGNORED_DIRS.has(entry.name)) {
continue;
}
walk(join(dir, entry.name));
continue;
}

if (!entry.isFile()) {
continue;
}

if (!SOURCE_EXTENSIONS.has(extname(entry.name))) {
continue;
}

const filePath = join(dir, entry.name);
const relativePath = filePath.slice(SOURCE_ROOT.length + 1);

if (!includeIndexEs && relativePath === 'index.es.ts') {
continue;
}
if (!includeIndex && relativePath === 'index.ts') {
continue;
}

entries.push(filePath);
}
}

walk(SOURCE_ROOT);
return entries;
}

const configs = [];

if (isESM || (!isESM && !isCJS && !isUMD)) {
Expand All @@ -119,10 +77,7 @@ if (isESM || (!isESM && !isCJS && !isUMD)) {
configs.push(
esmEntryConfig,
createESMConfig({
input: collectSourceEntries({
includeIndex: false,
includeIndexEs: false,
}),
input: collectSourceEntries({ exclude: ['index.ts', 'index.es.ts'] }),
pkg,
outputDir: 'es',
preserveModules: true,
Expand All @@ -134,10 +89,7 @@ if (isESM || (!isESM && !isCJS && !isUMD)) {
if (isCJS || (!isESM && !isCJS && !isUMD)) {
configs.push(
createCJSConfig({
input: collectSourceEntries({
includeIndex: true,
includeIndexEs: false,
}),
input: collectSourceEntries({ exclude: ['index.es.ts'] }),
pkg,
outputDir: 'cjs',
preserveModules: true,
Expand Down
29 changes: 1 addition & 28 deletions packages/react-instantsearch-core/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,10 @@ import {
createCJSConfig,
createUMDConfig,
createBanner,
collectSourceEntries,
} from '../../scripts/build/rollup.base.mjs';
import { readdirSync } from 'node:fs';
import { extname, join } from 'node:path';
import pkg from './package.json' with { type: 'json' };

const SOURCE_ROOT = 'src';
const SOURCE_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx']);
const IGNORED_DIRS = new Set(['__tests__', '__mocks__']);

function collectSourceEntries(dir = SOURCE_ROOT) {
const entries = [];
const dirEntries = readdirSync(dir, { withFileTypes: true });

for (const entry of dirEntries) {
const filePath = join(dir, entry.name);

if (entry.isDirectory()) {
if (!IGNORED_DIRS.has(entry.name)) {
entries.push(...collectSourceEntries(filePath));
}
continue;
}

if (entry.isFile() && SOURCE_EXTENSIONS.has(extname(entry.name))) {
entries.push(filePath);
}
}

return entries;
}

const moduleInput = collectSourceEntries();
const umdInput = 'src/index.ts';
const isESM = process.env.BUILD_FORMAT === 'esm';
Expand Down
4 changes: 2 additions & 2 deletions packages/react-instantsearch-nextjs/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createESMConfig, createCJSConfig } from '../../scripts/build/rollup.base.mjs';
import { createESMConfig, createCJSConfig, collectSourceEntries } from '../../scripts/build/rollup.base.mjs';

import pkg from './package.json' with { type: 'json' };

const input = 'src/index.ts';
const input = collectSourceEntries();
const isESM = process.env.BUILD_FORMAT === 'esm';
const isCJS = process.env.BUILD_FORMAT === 'cjs';

Expand Down
4 changes: 3 additions & 1 deletion packages/react-instantsearch-router-nextjs/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
createESMConfig,
createCJSConfig,
collectSourceEntries,
} from '../../scripts/build/rollup.base.mjs';

import pkg from './package.json' with { type: 'json' };

const input = 'src/index.ts';
const input = collectSourceEntries();
const isESM = process.env.BUILD_FORMAT === 'esm';
const isCJS = process.env.BUILD_FORMAT === 'cjs';

Expand All @@ -29,6 +30,7 @@ if (isCJS || (!isESM && !isCJS)) {
input,
pkg,
outputDir: 'dist/cjs',
preserveModules: true,
// Replace instantsearch.js/es imports with instantsearch.js/cjs for CJS build
replaceImports: {
'instantsearch.js/es': 'instantsearch.js/cjs',
Expand Down
29 changes: 1 addition & 28 deletions packages/react-instantsearch/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,10 @@ import {
createCJSConfig,
createUMDConfig,
createBanner,
collectSourceEntries,
} from '../../scripts/build/rollup.base.mjs';
import { readdirSync } from 'node:fs';
import { extname, join } from 'node:path';
import pkg from './package.json' with { type: 'json' };

const SOURCE_ROOT = 'src';
const SOURCE_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx']);
const IGNORED_DIRS = new Set(['__tests__', '__mocks__']);

function collectSourceEntries(dir = SOURCE_ROOT) {
const entries = [];
const dirEntries = readdirSync(dir, { withFileTypes: true });

for (const entry of dirEntries) {
const filePath = join(dir, entry.name);

if (entry.isDirectory()) {
if (!IGNORED_DIRS.has(entry.name)) {
entries.push(...collectSourceEntries(filePath));
}
continue;
}

if (entry.isFile() && SOURCE_EXTENSIONS.has(extname(entry.name))) {
entries.push(filePath);
}
}

return entries;
}

const moduleInput = collectSourceEntries();
const umdInput = 'src/index.ts';
const isESM = process.env.BUILD_FORMAT === 'esm';
Expand Down
83 changes: 75 additions & 8 deletions scripts/build/rollup.base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
*
* Usage in package rollup.config.mjs:
*
* import { createESMConfig, createCJSConfig, createUMDConfig } from '../../scripts/build/rollup.base.mjs';
* import { createESMConfig, createCJSConfig, createUMDConfig, collectSourceEntries } from '../../scripts/build/rollup.base.mjs';
* import pkg from './package.json' with { type: 'json' };
*
* const input = collectSourceEntries();
*
* export default [
* createESMConfig({ input: 'src/index.ts', pkg }),
* createCJSConfig({ input: 'src/index.ts', pkg }),
* createESMConfig({ input, pkg }),
* createCJSConfig({ input, pkg }),
* createUMDConfig({
* input: 'src/index.ts',
* pkg,
Expand All @@ -18,6 +20,9 @@
* ];
*/

import { readdirSync } from 'node:fs';
import { extname, join } from 'node:path';

import {
createSwcPlugin,
createCommonjsPlugin,
Expand All @@ -39,7 +44,7 @@ import { extensionResolver } from './rollup-plugin-extension-resolver.mjs';
* @param {string} [options.outputDir='dist/es'] - Output directory
* @param {string[]} [options.external] - External dependencies (auto-detected from pkg if not provided)
* @param {Object[]} [options.plugins] - Additional plugins to append
* @param {boolean} [options.preserveModules=false] - Whether to preserve module structure
* @param {boolean} [options.preserveModules=true] - Whether to preserve module structure
* @returns Rollup configuration object
*/
export function createESMConfig({
Expand All @@ -48,7 +53,7 @@ export function createESMConfig({
outputDir = 'dist/es',
external,
plugins = [],
preserveModules = false,
preserveModules = true,
swc = {},
preSwcPlugins = [],
}) {
Expand Down Expand Up @@ -97,7 +102,7 @@ export function createESMConfig({
* @param {string[]} [options.external] - External dependencies (auto-detected from pkg if not provided)
* @param {Object[]} [options.plugins] - Additional plugins to append
* @param {Object} [options.replaceImports] - Import path replacements (e.g., { 'pkg/es': 'pkg/cjs' })
* @param {boolean} [options.preserveModules=false] - Whether to preserve module structure
* @param {boolean} [options.preserveModules=true] - Whether to preserve module structure
* @returns Rollup configuration object
*/
export function createCJSConfig({
Expand All @@ -107,7 +112,7 @@ export function createCJSConfig({
external,
plugins = [],
replaceImports = {},
preserveModules = false,
preserveModules = true,
swc = {},
preSwcPlugins = [],
}) {
Expand All @@ -129,7 +134,13 @@ export function createCJSConfig({
createResolvePlugin(),
createCommonjsPlugin(),
...preSwcPlugins,
createSwcPlugin(swc),
createSwcPlugin({
module: {
type: 'commonjs',
noInterop: false,
},
...swc,
}),
createWrapWarningsWithDevCheckPlugin(),
createReplacePlugin({ mode: 'production', additional: replaceImports }),
createStripJsxPragmaPlugin(),
Expand Down Expand Up @@ -239,6 +250,62 @@ export function createUMDConfig({
return [devConfig, prodConfig];
}

const DEFAULT_SOURCE_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx']);
const DEFAULT_IGNORED_DIRS = new Set(['__tests__', '__mocks__']);

/**
* Collects all source files from a directory recursively.
* @param {Object} [options] - Configuration options
* @param {string} [options.sourceRoot='src'] - Root directory to scan
* @param {Set<string>} [options.extensions] - File extensions to include
* @param {Set<string>} [options.ignoredDirs] - Directory names to skip
* @param {string[]} [options.exclude] - File paths (relative to sourceRoot) to exclude
* @returns {string[]} Array of file paths
*/
export function collectSourceEntries({
sourceRoot = 'src',
extensions = DEFAULT_SOURCE_EXTENSIONS,
ignoredDirs = DEFAULT_IGNORED_DIRS,
exclude = [],
} = {}) {
const excludeSet = new Set(exclude);
const entries = [];

function walk(dir) {
const dirEntries = readdirSync(dir, { withFileTypes: true });

for (const entry of dirEntries) {
const filePath = join(dir, entry.name);

if (entry.isDirectory()) {
if (!ignoredDirs.has(entry.name)) {
walk(filePath);
}
continue;
}

if (!entry.isFile()) {
continue;
}

if (!extensions.has(extname(entry.name))) {
continue;
}

// Check if this file should be excluded
const relativePath = filePath.slice(sourceRoot.length + 1);
if (excludeSet.has(relativePath)) {
continue;
}

entries.push(filePath);
}
}

walk(sourceRoot);
return entries;
}

/**
* Extracts external dependencies from package.json.
* Includes dependencies, peerDependencies, and optionalDependencies.
Expand Down
Loading