|
| 1 | +import { dirname, resolve } from 'node:path'; |
| 2 | +import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'; |
| 3 | +import { build, defineConfig } from 'vite'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | +import { generateDtsBundle } from 'dts-bundle-generator'; |
| 6 | + |
| 7 | +function buildPackageJson(packageName) { |
| 8 | + /* |
| 9 | + Define the contents of the package's package.json here. |
| 10 | + */ |
| 11 | + return { |
| 12 | + name: packageName, |
| 13 | + "author": "Mark Silverwood", |
| 14 | + "repository": { |
| 15 | + "type": "git", |
| 16 | + "url": "https://github.com/slicedsilver/lwc-plugin-visible-price-range-util.git" |
| 17 | + }, |
| 18 | + version: '0.1.0', |
| 19 | + keywords: ['lwc-plugin', 'lightweight-charts'], |
| 20 | + type: 'module', |
| 21 | + main: `./${packageName}.umd.cjs`, |
| 22 | + module: `./${packageName}.js`, |
| 23 | + types: `./${packageName}.d.ts`, |
| 24 | + exports: { |
| 25 | + import: { |
| 26 | + types: `./${packageName}.d.ts`, |
| 27 | + default: `./${packageName}.js`, |
| 28 | + }, |
| 29 | + require: { |
| 30 | + types: `./${packageName}.d.cts`, |
| 31 | + default: `./${packageName}.umd.cjs`, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +const __filename = fileURLToPath(import.meta.url); |
| 38 | +const currentDir = dirname(__filename); |
| 39 | + |
| 40 | +const pluginFileName = 'visible-price-range-util'; |
| 41 | +const pluginFile = resolve(currentDir, 'src', `${pluginFileName}.ts`); |
| 42 | + |
| 43 | +const pluginsToBuild = [ |
| 44 | + { |
| 45 | + filepath: pluginFile, |
| 46 | + exportName: 'lwc-plugin-visible-price-range-util', |
| 47 | + name: 'VisiblePriceRangeUtil', |
| 48 | + }, |
| 49 | +]; |
| 50 | + |
| 51 | +const compiledFolder = resolve(currentDir, 'dist'); |
| 52 | +if (!existsSync(compiledFolder)) { |
| 53 | + mkdirSync(compiledFolder); |
| 54 | +} |
| 55 | + |
| 56 | +const buildConfig = ({ |
| 57 | + filepath, |
| 58 | + name, |
| 59 | + exportName, |
| 60 | + formats = ['es', 'umd'], |
| 61 | +}) => { |
| 62 | + return defineConfig({ |
| 63 | + publicDir: false, |
| 64 | + build: { |
| 65 | + outDir: `dist`, |
| 66 | + emptyOutDir: true, |
| 67 | + copyPublicDir: false, |
| 68 | + lib: { |
| 69 | + entry: filepath, |
| 70 | + name, |
| 71 | + formats, |
| 72 | + fileName: exportName, |
| 73 | + }, |
| 74 | + rollupOptions: { |
| 75 | + external: ['lightweight-charts', 'fancy-canvas'], |
| 76 | + output: { |
| 77 | + globals: { |
| 78 | + 'lightweight-charts': 'LightweightCharts', |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }); |
| 84 | +}; |
| 85 | + |
| 86 | +const startTime = Date.now().valueOf(); |
| 87 | +console.log('⚡️ Starting'); |
| 88 | +console.log('Bundling the plugin...'); |
| 89 | +const promises = pluginsToBuild.map(file => { |
| 90 | + return build(buildConfig(file)); |
| 91 | +}); |
| 92 | +await Promise.all(promises); |
| 93 | +console.log('Generating the package.json file...'); |
| 94 | +pluginsToBuild.forEach(file => { |
| 95 | + const packagePath = resolve(compiledFolder, 'package.json'); |
| 96 | + const content = JSON.stringify( |
| 97 | + buildPackageJson(file.exportName), |
| 98 | + undefined, |
| 99 | + 4 |
| 100 | + ); |
| 101 | + writeFileSync(packagePath, content, { encoding: 'utf-8' }); |
| 102 | +}); |
| 103 | +console.log('Generating the typings files...'); |
| 104 | +pluginsToBuild.forEach(file => { |
| 105 | + try { |
| 106 | + const esModuleTyping = generateDtsBundle([ |
| 107 | + { |
| 108 | + filePath: `./typings/${pluginFileName}.d.ts`, |
| 109 | + }, |
| 110 | + ]); |
| 111 | + const typingFilePath = resolve(compiledFolder, `${file.exportName}.d.ts`); |
| 112 | + writeFileSync(typingFilePath, esModuleTyping.join('\n'), { |
| 113 | + encoding: 'utf-8', |
| 114 | + }); |
| 115 | + copyFileSync(typingFilePath, resolve(compiledFolder, `${file.exportName}.d.cts`)); |
| 116 | + } catch (e) { |
| 117 | + console.error('Error generating typings for: ', file.exportName); |
| 118 | + } |
| 119 | +}); |
| 120 | +const endTime = Date.now().valueOf(); |
| 121 | +console.log(`🎉 Done (${endTime - startTime}ms)`); |
0 commit comments