|
| 1 | +import resolve from "@rollup/plugin-node-resolve" |
| 2 | +import commonjs from "@rollup/plugin-commonjs" |
| 3 | +import { terser } from "rollup-plugin-terser" |
| 4 | +import json from "rollup-plugin-json" |
| 5 | +import copy from "rollup-plugin-copy2" |
| 6 | +import typescript from "@rollup/plugin-typescript" |
| 7 | +import tar from "tar" |
| 8 | +import fs from "fs" |
| 9 | +import pkg from "./package.json" |
| 10 | +import crypto from "crypto" |
| 11 | +import { validate } from "@budibase/backend-core/plugins" |
| 12 | + |
| 13 | +const iconFile = "icon.svg" |
| 14 | +const iconExists = fs.existsSync(iconFile) |
| 15 | +const assets = ["schema.json", "package.json"] |
| 16 | +if (iconExists) { |
| 17 | + assets.push(iconFile) |
| 18 | +} |
| 19 | + |
| 20 | +// Custom plugin to clean the dist folder before building |
| 21 | +const clean = () => ({ |
| 22 | + buildStart() { |
| 23 | + const dist = "./dist/" |
| 24 | + if (fs.existsSync(dist)) { |
| 25 | + fs.readdirSync(dist).forEach(path => { |
| 26 | + if (path.endsWith(".tar.gz")) { |
| 27 | + fs.unlinkSync(dist + path) |
| 28 | + } |
| 29 | + }) |
| 30 | + } |
| 31 | + }, |
| 32 | +}) |
| 33 | + |
| 34 | +// Custom plugin to hash the JS bundle and write it in the schema |
| 35 | +const hash = () => ({ |
| 36 | + writeBundle() { |
| 37 | + // Generate JS hash |
| 38 | + const fileBuffer = fs.readFileSync("dist/plugin.min.js") |
| 39 | + const hashSum = crypto.createHash("sha1") |
| 40 | + hashSum.update(fileBuffer) |
| 41 | + const hex = hashSum.digest("hex") |
| 42 | + |
| 43 | + // Read and parse existing schema from dist folder |
| 44 | + const schema = JSON.parse(fs.readFileSync("./dist/schema.json", "utf8")) |
| 45 | + |
| 46 | + // Write updated schema to dist folder, pretty printed as JSON again |
| 47 | + const newSchema = { |
| 48 | + ...schema, |
| 49 | + hash: hex, |
| 50 | + version: pkg.version, |
| 51 | + } |
| 52 | + fs.writeFileSync("./dist/schema.json", JSON.stringify(newSchema, null, 2)) |
| 53 | + }, |
| 54 | +}) |
| 55 | + |
| 56 | +// Custom plugin to bundle up our files after building |
| 57 | +const bundle = () => ({ |
| 58 | + async writeBundle() { |
| 59 | + const bundleName = `${pkg.name}-${pkg.version}.tar.gz` |
| 60 | + return tar |
| 61 | + .c({ gzip: true, cwd: "dist" }, [...assets, "plugin.min.js"]) |
| 62 | + .pipe(fs.createWriteStream(`dist/${bundleName}`)) |
| 63 | + }, |
| 64 | +}) |
| 65 | + |
| 66 | +const validateSchema = () => ({ |
| 67 | + buildStart() { |
| 68 | + const schema = fs.readFileSync("schema.json", "utf8") |
| 69 | + validate(JSON.parse(schema)) |
| 70 | + } |
| 71 | +}) |
| 72 | + |
| 73 | +export default { |
| 74 | + input: "src/index.ts", |
| 75 | + output: { |
| 76 | + sourcemap: false, |
| 77 | + format: "cjs", |
| 78 | + file: "dist/plugin.min.js", |
| 79 | + inlineDynamicImports: true, |
| 80 | + exports: "default", |
| 81 | + }, |
| 82 | + plugins: [ |
| 83 | + validateSchema(), |
| 84 | + clean(), |
| 85 | + resolve({ |
| 86 | + preferBuiltins: true, |
| 87 | + browser: false, |
| 88 | + exportConditions: ["node"] |
| 89 | + }), |
| 90 | + typescript({ |
| 91 | + compilerOptions: { |
| 92 | + target: "es6", |
| 93 | + module: "esnext", |
| 94 | + lib: ["es2020"], |
| 95 | + allowJs: true, |
| 96 | + strict: true, |
| 97 | + noImplicitAny: true, |
| 98 | + esModuleInterop: true, |
| 99 | + resolveJsonModule: true, |
| 100 | + types: ["node"], |
| 101 | + skipLibCheck: true, |
| 102 | + moduleResolution: "node", |
| 103 | + }, |
| 104 | + include: ["./src/**/*"], |
| 105 | + exclude: ["node_modules", "dist", "**/*.spec.ts", "**/*.spec.js"], |
| 106 | + }), |
| 107 | + commonjs(), |
| 108 | + json(), |
| 109 | + terser(), |
| 110 | + copy({ |
| 111 | + assets, |
| 112 | + }), |
| 113 | + hash(), |
| 114 | + bundle(), |
| 115 | + ], |
| 116 | +} |
0 commit comments