Skip to content

Commit

Permalink
fix(atomic-react): remove traces of npm imports (#4581)
Browse files Browse the repository at this point in the history
https://coveord.atlassian.net/browse/KIT-3659


### This PR main goal is to fix the ESM output in the CDN by removing
the traces of npm imports.

This is achieved by two things. 

1. Bundle all of atomic-react (both esm and cjs module) in rollup.
Before esm was bundled with tsc and cjs with rollup.

#### before 😆 
```mermaid
graph TD;
    rollup-->CJS;
    tsc-->ESM;
    tsc-->types;
```
#### after 😆 
```mermaid
graph TD;
    rollup-->CJS;
    rollup-->ESM;
    tsc-->types;
```

This is the current state of the dist folder.

```
dist/
├── cjs/
│   ├── atomic-react.cjs
│   ├── recommendation/
│   │   └── atomic-react.cjs
│   └── commerce/
│       └── atomic-react.cjs
├── esm/
│   ├── atomic-react.mjs
│   ├── recommendation/
│   │   └── atomic-react.mjs
│   └── commerce/
│       └── atomic-react.mjs
└── types/
    ├── index.d.ts
    ├── recommendation.index.d.ts
    ├── commerce.index.d.ts
    └── components/
        └── ...
```
2. Add @coveo/atomic/loader to the list of cdn externalizations.
https://github.com/coveo/ui-kit/pull/4581/files#r1812922022

3. Fix the ouptput in the CDN it now looks something like this for the
proper files :

<img width="1531" alt="image"
src="https://github.com/user-attachments/assets/11e8d085-15c1-4eb8-bf31-4a1d64dfb29f">
  • Loading branch information
alexprudhomme authored Oct 28, 2024
1 parent ebf55db commit 599b36f
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 258 deletions.
23 changes: 0 additions & 23 deletions package-lock.json

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

23 changes: 10 additions & 13 deletions packages/atomic-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
"clean": "rimraf -rf dist",
"build:fixLoaderImportPaths": "node ./scripts/fix-loader-import-paths.js",
"build:fixGeneratedImportPaths": "fix-esm-import-path src/components/stencil-generated",
"build:bundles:esm": "tsc -p tsconfig.esm.json",
"build:bundles:iife-cjs": "rollup --config rollup.config.mjs --bundleConfigAsCjs",
"build:bundles": "concurrently \"npm run build:bundles:esm\" \"npm run build:bundles:iife-cjs\"",
"build:bundles": "rollup --config rollup.config.js",
"build:types": "tsc --project tsconfig.types.json",
"publish:npm": "npm run-script -w=@coveo/release npm-publish",
"publish:bump": "npm run-script -w=@coveo/release bump",
"promote:npm:latest": "npm run-script -w=@coveo/release promote-npm-prod",
"build:assets": "ncp ../atomic/dist/atomic/assets dist/assets && ncp ../atomic/dist/atomic/lang dist/lang "
},
"module": "./dist/esm/atomic-react.mjs",
"main": "./dist/cjs/atomic-react.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"types": "./dist/types/index.d.ts",
"files": [
"dist/",
"recommendation/",
Expand All @@ -37,9 +36,7 @@
"@coveo/release": "1.0.0",
"@coveo/rollup-plugin-replace-with-ast": "1.0.0",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-replace": "^5.0.0",
"@rollup/plugin-terser": "0.4.4",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "20.14.12",
Expand All @@ -62,18 +59,18 @@
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"types": "./dist/types/index.d.ts",
"import": "./dist/esm/atomic-react.mjs",
"require": "./dist/cjs/atomic-react.cjs"
},
"./commerce": {
"types": "./dist/commerce.index.d.ts",
"import": "./dist/commerce.index.js",
"types": "./dist/types/commerce.index.d.ts",
"import": "./dist/esm/commerce/atomic-react.mjs",
"require": "./dist/cjs/commerce/atomic-react.cjs"
},
"./recommendation": {
"types": "./dist/recommendation.index.d.ts",
"import": "./dist/recommendation.index.js",
"types": "./dist/types/recommendation.index.d.ts",
"import": "./dist/esm/recommendation/atomic-react.mjs",
"require": "./dist/cjs/recommendation/atomic-react.cjs"
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/atomic-react/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"npm run build:fixLoaderImportPaths",
"npm run build:fixGeneratedImportPaths",
"npm run build:bundles",
"npm run build:types",
"npm run build:assets"
],
"parallel": false,
Expand Down
167 changes: 167 additions & 0 deletions packages/atomic-react/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import {nodeResolve} from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import {readFileSync} from 'fs';
import {join, dirname} from 'path';
import {defineConfig} from 'rollup';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import {fileURLToPath} from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const isCDN = process.env.DEPLOYMENT_ENVIRONMENT === 'CDN';

let headlessVersion;
let atomicVersion;

if (isCDN) {
console.log('Building for CDN');

const headlessPackageJsonPath = join(
__dirname,
'../../packages/headless/package.json'
);
const atomicPackageJsonPath = join(
__dirname,
'../../packages/atomic/package.json'
);

try {
const headlessPackageJson = JSON.parse(
readFileSync(headlessPackageJsonPath, 'utf8')
);
headlessVersion = 'v' + headlessPackageJson.version;
console.log('Using headless version from package.json:', headlessVersion);

const atomicPackageJson = JSON.parse(
readFileSync(atomicPackageJsonPath, 'utf8')
);
atomicVersion = 'v' + atomicPackageJson.version;
console.log('Using atomic version from package.json:', atomicVersion);
} catch (error) {
console.error('Error reading headless package.json:', error);
throw new Error('Error reading headless package.json');
}
}

const packageMappings = {
'@coveo/headless/commerce': {
cdn: `/headless/${headlessVersion}/commerce/headless.esm.js`,
},
'@coveo/headless/insight': {
cdn: `/headless/${headlessVersion}/insight/headless.esm.js`,
},
'@coveo/headless/recommendation': {
cdn: `/headless/${headlessVersion}/recommendation/headless.esm.js`,
},
'@coveo/headless/case-assist': {
cdn: `/headless/${headlessVersion}/case-assist/headless.esm.js`,
},
'@coveo/headless': {
cdn: `/headless/${headlessVersion}/headless.esm.js`,
},
'@coveo/atomic/loader': {
cdn: `/atomic/${atomicVersion}/loader/index.js`,
},
};

const externalizeDependenciesPlugin = () => {
return {
name: 'externalize-dependencies',
resolveId: (source, _importer, _options) => {
const packageMapping = packageMappings[source];

if (packageMapping) {
console.log(`Package cdn import : ${packageMapping.cdn}`);

return {
id: packageMapping.cdn,
external: 'absolute',
};
}

return null;
},
};
};

/** @type {import('rollup').ExternalOption} */
const commonExternal = [
'react',
'react-dom',
'react-dom/client',
'react-dom/server',
'@coveo/atomic/loader',
'@coveo/headless',
'@coveo/headless/recommendation',
'@coveo/headless/commerce',
];

/** @type {import('rollup').ExternalOption} */
const cdnExternal = [
'react',
'react-dom',
'react-dom/client',
'react-dom/server',
];

/** @returns {import('rollup').OutputOptions} */
const outputCJS = ({useCase}) => ({
file: `dist/cjs/${useCase}atomic-react.cjs`,
format: 'cjs',
sourcemap: true,
});

/** @returns {import('rollup').OutputOptions} */
const outputESM = ({useCase}) => ({
file: `dist/esm/${useCase}atomic-react.mjs`,
format: 'esm',
sourcemap: true,
});

/**@type {import('rollup').InputPluginOption} */
const plugins = [
nodePolyfills(),
typescript(),
nodeResolve(),
isCDN && externalizeDependenciesPlugin(),
];

export default defineConfig([
{
input: 'src/index.ts',
output: [outputCJS({useCase: ''})],
external: isCDN ? cdnExternal : commonExternal,
plugins: plugins,
},
{
input: 'src/index.ts',
output: [outputESM({useCase: ''})],
external: isCDN ? cdnExternal : commonExternal,
plugins: plugins,
},
{
input: 'src/recommendation.index.ts',
output: [outputCJS({useCase: 'recommendation/'})],
external: isCDN ? cdnExternal : commonExternal,
plugins: plugins,
},
{
input: 'src/recommendation.index.ts',
output: [outputESM({useCase: 'recommendation/'})],
external: isCDN ? cdnExternal : commonExternal,
plugins: plugins,
},
{
input: 'src/commerce.index.ts',
output: [outputCJS({useCase: 'commerce/'})],
external: isCDN ? cdnExternal : commonExternal,
plugins: plugins,
},
{
input: 'src/commerce.index.ts',
output: [outputESM({useCase: 'commerce/'})],
external: isCDN ? cdnExternal : commonExternal,
plugins: plugins,
},
]);
Loading

0 comments on commit 599b36f

Please sign in to comment.