-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(atomic-react): remove traces of npm imports (#4581)
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
1 parent
ebf55db
commit 599b36f
Showing
8 changed files
with
181 additions
and
258 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]); |
Oops, something went wrong.