-
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.
chore(atomic): bundle lit dependencies for CDN build (#4908)
This PR adds a bundling step (with rollup) in the atomic build process. This step only runs for the CDN build, and ensures that dependencies for lit components get bundled in the dist folder. Since this PR does not contain a lit component, you can take a look at #4893 as an example. https://coveord.atlassian.net/browse/KIT-3906
- Loading branch information
Showing
10 changed files
with
175 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,6 @@ headless/ | |
/playwright/.cache/ | ||
|
||
dist-storybook/ | ||
|
||
/dev/bueno/ | ||
/dev/headless/ |
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
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,76 @@ | ||
import resolve from '@rollup/plugin-node-resolve'; | ||
import {readdirSync, statSync} from 'fs'; | ||
import {join, resolve as resolvePath, relative} from 'path'; | ||
import {generateExternalPackageMappings} from './scripts/externalPackageMappings.mjs'; | ||
|
||
const isCDN = process.env.DEPLOYMENT_ENVIRONMENT === 'CDN'; | ||
|
||
const packageMappings = generateExternalPackageMappings(import.meta.dirname); | ||
|
||
const externalizeDependenciesPlugin = () => { | ||
return { | ||
name: 'externalize-dependencies', | ||
resolveId: (source, _importer, _options) => { | ||
const packageMapping = packageMappings[source]; | ||
|
||
if (packageMapping) { | ||
if (!isCDN) { | ||
return false; | ||
} | ||
|
||
return { | ||
id: packageMapping.cdn, | ||
external: 'absolute', | ||
}; | ||
} | ||
|
||
return null; | ||
}, | ||
}; | ||
}; | ||
|
||
const getDirectories = (src) => { | ||
const dirs = []; | ||
const files = readdirSync(src); | ||
files.forEach((file) => { | ||
const fullPath = join(src, file); | ||
if (statSync(fullPath).isDirectory()) { | ||
dirs.push(fullPath); | ||
dirs.push(...getDirectories(fullPath)); | ||
} | ||
}); | ||
return dirs; | ||
}; | ||
|
||
const distDirs = getDirectories(resolvePath('dist/atomic')); | ||
|
||
const inputFiles = distDirs.flatMap((distDir) => { | ||
return readdirSync(distDir) | ||
.filter((file) => file.endsWith('.js')) | ||
.map((file) => join(distDir, file)); | ||
}); | ||
|
||
export default { | ||
input: inputFiles, | ||
output: { | ||
dir: 'dist/atomic', | ||
format: 'esm', | ||
entryFileNames: ({facadeModuleId}) => { | ||
const relativePath = relative(resolvePath('dist/atomic'), facadeModuleId); | ||
return `${relativePath}`; | ||
}, | ||
chunkFileNames: '[name].js', | ||
manualChunks: (id) => { | ||
if (id.includes('node_modules')) { | ||
return ( | ||
'vendor/' + | ||
id.toString().split('node_modules/')[1].split('/')[0].toString() | ||
); | ||
} | ||
}, | ||
}, | ||
plugins: [ | ||
resolve({preserveSymlinks: false}), | ||
externalizeDependenciesPlugin(), | ||
], | ||
}; |
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,81 @@ | ||
import {execSync} from 'child_process'; | ||
import fs from 'fs/promises'; | ||
import ncp from 'ncp'; | ||
import path from 'path'; | ||
|
||
const getVersionFromPackageJson = async (packagePath) => { | ||
const packageJsonPath = path.join(packagePath, 'package.json'); | ||
try { | ||
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')); | ||
return packageJson.version; | ||
} catch (err) { | ||
console.error(`Error reading ${packageJsonPath}: ${err.message}`); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
const copyFiles = async (source, destination) => { | ||
return new Promise((resolve, reject) => { | ||
ncp(source, destination, (err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
const currentDir = import.meta.dirname; | ||
const headlessDir = path.resolve(currentDir, '../../headless'); | ||
const buenoDir = path.resolve(currentDir, '../../bueno'); | ||
const devPublicDir = path.resolve(currentDir, '../dev'); | ||
|
||
const run = async () => { | ||
const headlessVersion = await getVersionFromPackageJson(headlessDir); | ||
const buenoVersion = await getVersionFromPackageJson(buenoDir); | ||
|
||
const directories = [ | ||
`${devPublicDir}/headless/v${headlessVersion}`, | ||
`${devPublicDir}/bueno/v${buenoVersion}`, | ||
]; | ||
|
||
for (const dir of directories) { | ||
if ( | ||
await fs | ||
.access(dir) | ||
.then(() => true) | ||
.catch(() => false) | ||
) { | ||
console.log(`Deleting existing directory: ${dir}`); | ||
await fs.rm(dir, {recursive: true, force: true}); | ||
} | ||
} | ||
|
||
for (const dir of directories) { | ||
console.log(`Creating directory: ${dir}`); | ||
await fs.mkdir(dir, {recursive: true}); | ||
} | ||
|
||
console.log( | ||
`Copying headless files to ${devPublicDir}/headless/v${headlessVersion}` | ||
); | ||
await copyFiles( | ||
path.join(headlessDir, 'dist/browser'), | ||
`${devPublicDir}/headless/v${headlessVersion}` | ||
); | ||
|
||
console.log(`Copying bueno files to ${devPublicDir}/bueno/v${buenoVersion}`); | ||
await copyFiles( | ||
path.join(buenoDir, 'dist/browser'), | ||
`${devPublicDir}/bueno/v${buenoVersion}` | ||
); | ||
|
||
console.log('Starting Vite server...'); | ||
execSync('vite serve dev', {stdio: 'inherit'}); | ||
}; | ||
|
||
run().catch((err) => { | ||
console.error('An error occurred:', err); | ||
process.exit(1); | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
* { | ||
:host { | ||
/* Primary colors */ | ||
--atomic-primary: #1372ec; | ||
--atomic-primary-light: #399ffe; | ||
|