Skip to content

Commit

Permalink
Charts: add back Rollup (#41234)
Browse files Browse the repository at this point in the history
* add back rollup for charts build

* clean before build

* changelog
  • Loading branch information
kangzj authored Jan 22, 2025
1 parent c153c70 commit 4ff799c
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 10 deletions.
60 changes: 60 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions projects/js-packages/charts/changelog/update-add-back-rollup
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Changed back to build with Rollup
22 changes: 16 additions & 6 deletions projects/js-packages/charts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"clean": "rm -rf node_modules",
"clean": "rm -rf dist",
"test": "jest --config=tests/jest.config.cjs",
"test-coverage": "pnpm run test --coverage",
"storybook": "cd ../storybook && pnpm run storybook:dev",
"compile-ts": "tsc --pretty",
"clean-build": "rm -rf dist/",
"build": "pnpm run build:prod",
"build:prod": "pnpm run clean-build && webpack --config webpack.config.cjs --mode production && bash ./tools/fixup.sh && pnpm run build:types",
"build:prod": "pnpm run clean && rollup -c --environment NODE_ENV:production && bash ./tools/fixup.sh",
"build:dev": "rollup -c --environment NODE_ENV:development",
"build:webpack": "pnpm run clean-build && webpack --config webpack.config.cjs --mode production && bash ./tools/fixup.sh && pnpm run build:types",
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types"
},
"main": "./dist/cjs/index.js",
Expand All @@ -35,14 +37,13 @@
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/types/*.d.ts"
"types": "./dist/index.d.ts"
},
"./components/*": {
"import": "./dist/mjs/components/*/index.js",
"require": "./dist/cjs/components/*/index.js",
"types": "./dist/types/*.d.ts"
"require": "./dist/cjs/components/*/index.js"
},
"./style.css": "./dist/cjs/style.css"
"./style.css": "./dist/mjs/style.css"
},
"dependencies": {
"@babel/runtime": "7.26.0",
Expand All @@ -68,6 +69,11 @@
"@babel/preset-env": "7.26.0",
"@babel/preset-react": "7.26.3",
"@babel/preset-typescript": "7.26.0",
"@rollup/plugin-commonjs": "26.0.1",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-terser": "0.4.3",
"@rollup/plugin-typescript": "12.1.0",
"@storybook/blocks": "8.4.7",
"@storybook/react": "8.4.7",
"@types/react": "18.3.18",
Expand All @@ -85,6 +91,10 @@
"postcss-modules": "6.0.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"rollup": "3.29.5",
"rollup-plugin-dts": "6.1.1",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-postcss": "4.0.2",
"sass": "1.64.1",
"sass-embedded": "1.83.0",
"sass-loader": "^13.0.0",
Expand Down
142 changes: 142 additions & 0 deletions projects/js-packages/charts/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Import necessary plugins for building the library
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import { defineConfig } from 'rollup';
import dts from 'rollup-plugin-dts';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';

// Common plugins used across all build configurations
const commonPlugins = [
// Automatically externalize peer dependencies
peerDepsExternal( {
includeDependencies: true,
} ),
// Locate and bundle third-party dependencies from node_modules
resolve( {
preferBuiltins: true,
extensions: [ '.tsx', '.ts', '.js', '.jsx' ],
} ),
// Convert CommonJS modules to ES6
commonjs(),
// Allow importing JSON files
json(),
// Process SCSS/CSS modules
postcss( {
// Configure CSS modules with scoped names
modules: {
generateScopedName: '[name]__[local]__[hash:base64:5]',
},
extract: 'style.css',
autoModules: false,
use: [ 'sass' ],
} ),
];

// Main bundle configuration for the entire library
const mainConfig = {
// Entry point for the bundle
input: 'src/index.ts',
// Output configuration for different module formats
output: [
{
file: './dist/cjs/index.js',
format: 'cjs', // CommonJS format for Node.js
sourcemap: true,
sourcemapPathTransform: relativeSourcePath => {
return `/@automattic/charts/${ relativeSourcePath }`;
},
},
{
file: './dist/mjs/index.js',
format: 'esm', // ES modules for modern bundlers
sourcemap: true,
},
],
// Mark all dependencies as external to avoid bundling them
external: [ 'react', 'react-dom', /^@visx\/.*/, '@react-spring/web', 'clsx', 'tslib' ],
plugins: [
...commonPlugins,
// TypeScript compilation
typescript( {
tsconfig: './tsconfig.json',
declaration: false, // Declarations handled by dts plugin
sourceMap: true,
compilerOptions: {
verbatimModuleSyntax: true,
},
} ),
terser(),
],
// Handle circular dependencies warning
onwarn( warning, warn ) {
if ( warning.code === 'CIRCULAR_DEPENDENCY' ) {
return;
}
warn( warning );
},
};

// List of components to build individually
const components = [
'components/bar-chart',
'components/line-chart',
'components/pie-chart',
'components/pie-semi-circle-chart',
'components/tooltip',
'components/legend',
'components/grid-control',
'providers/theme',
];

// Generate individual bundles for each component
const componentConfigs = components.map( component => ( {
// Component entry point - try both .tsx and .ts extensions
input: `src/${ component }/index`,
// Output both ESM and CJS formats
output: [
{
file: `dist/mjs/${ component }/index.js`,
format: 'esm',
sourcemap: true,
},
{
file: `dist/cjs/${ component }/index.js`,
format: 'cjs',
sourcemap: true,
},
],
// Same external config as main bundle
external: [ 'react', 'react-dom', /^@visx\/.*/, '@react-spring/web', 'clsx', 'tslib' ],
plugins: [
...commonPlugins,
typescript( {
tsconfig: './tsconfig.json',
declaration: false,
sourceMap: true,
compilerOptions: {
verbatimModuleSyntax: true,
},
} ),
terser(),
],
} ) );

// Configuration for generating TypeScript declaration files
const typesConfig = {
input: 'src/index.ts',
output: [ { file: 'dist/index.d.ts', format: 'es' } ],
plugins: [
dts( {
respectExternal: true,
} ),
],
// Don't include style imports in type definitions
external: [ /\.scss$/, /\.css$/, 'react', /@types\/.*/, /^@visx\/.*/, 'react/jsx-runtime' ],
};

// Export all configurations to be built in parallel
export default defineConfig( [ mainConfig, ...componentConfigs, typesConfig ] );
7 changes: 3 additions & 4 deletions projects/js-packages/charts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"extends": "jetpack-js-tools/tsconfig.tsc-declaration-only.json",
"extends": "jetpack-js-tools/tsconfig.base.json",
"compilerOptions": {
"typeRoots": [ "./node_modules/@types/", "src/*" ],
"outDir": "./dist/types"
"typeRoots": [ "./node_modules/@types/", "src/*" ]
},
// List all sources and source-containing subdirs.
"include": [ "./src/index.ts" ],
"include": [ "./src" ],
"exclude": [ "node_modules", "dist", "**/stories/**" ]
}

0 comments on commit 4ff799c

Please sign in to comment.