Skip to content

Commit

Permalink
Merge pull request #119 from sendbird/irene/AC-1658
Browse files Browse the repository at this point in the history
chore: apply dynamic import for index script caching
  • Loading branch information
AhyoungRyu authored Mar 15, 2024
2 parents 23d801e + 983b531 commit ff86bc0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/self-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "rm -rf dist && tsc-silent -p './tsconfig.json' --suppress @ && vite build",
"build:post": "node scripts/generate-index.js",
"build": "rm -rf dist && tsc-silent -p './tsconfig.json' --suppress @ && vite build && npm run build:post",
"preview": "vite preview",
"lint": "npx eslint src",
"lint:fix": "npm run lint -- --fix",
Expand Down
17 changes: 17 additions & 0 deletions packages/self-service/scripts/generate-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from 'node:fs';

const packageLock = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'));
const version = packageLock.dependencies["@sendbird/chat-ai-widget"].version;

if (!version) {
console.error('Error: No version found for @sendbird/chat-ai-widget. Please check the package-lock.json file.');
process.exit(1);
}

const content = `import(\`/output.js?v=${version}\`).then(() => console.log("AI chatbot module has been successfully loaded"));`;

// For development
fs.writeFileSync('dist/index-dev.js', content);

// For production
fs.writeFileSync('dist/index.js', content);
2 changes: 1 addition & 1 deletion packages/self-service/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineConfig({
rollupOptions: {
output: {
manualChunks: undefined,
entryFileNames: `[name].js`,
entryFileNames: `output.js`,
chunkFileNames: `[name].js`,
assetFileNames: `[name].[ext]`,
globals: {
Expand Down

0 comments on commit ff86bc0

Please sign in to comment.