|
| 1 | +import babel from "@rollup/plugin-babel"; |
| 2 | +import commonjs from "@rollup/plugin-commonjs"; |
| 3 | +import resolve from "@rollup/plugin-node-resolve"; |
| 4 | +import strip from "@rollup/plugin-strip"; |
| 5 | +import terser from "@rollup/plugin-terser"; |
| 6 | +import gulp from "gulp"; |
| 7 | +import { rimraf } from "rimraf"; |
| 8 | +import { rollup } from "rollup"; |
| 9 | + |
| 10 | +const babelConfig = { |
| 11 | + babelHelpers: "bundled", |
| 12 | + ignore: ["node_modules"], |
| 13 | + compact: false, |
| 14 | + extensions: [".js"], |
| 15 | + presets: [ |
| 16 | + [ |
| 17 | + "@babel/preset-env", |
| 18 | + { |
| 19 | + targets: { |
| 20 | + node: "current", |
| 21 | + }, |
| 22 | + }, |
| 23 | + ], |
| 24 | + ], |
| 25 | +}; |
| 26 | + |
| 27 | +const debugRollupPlugins = [resolve(), commonjs(), babel(babelConfig)]; |
| 28 | + |
| 29 | +const prodRollupPlugins = [resolve(), commonjs(), strip(), babel(babelConfig), terser()]; |
| 30 | + |
| 31 | +async function compile(variant) { |
| 32 | + const rollupPlugins = variant === "debug" ? debugRollupPlugins : prodRollupPlugins; |
| 33 | + const rollupOutputName = variant !== "prod" ? `index.${variant}.js` : `index.js`; |
| 34 | + |
| 35 | + const builder = await rollup({ |
| 36 | + input: `./src/index.js`, |
| 37 | + plugins: rollupPlugins, |
| 38 | + external: ["browser-sync"], |
| 39 | + }); |
| 40 | + |
| 41 | + await builder.write({ |
| 42 | + file: `./dist/${rollupOutputName}`, |
| 43 | + name: "gulp-browser-sync", |
| 44 | + format: "esm", |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +// Creates ES module bundles |
| 49 | +export async function buildFlavors() { |
| 50 | + await Promise.all(["debug", "prod"].map(compile)); |
| 51 | +} |
| 52 | + |
| 53 | +export function copyTSDefinition() { |
| 54 | + return gulp |
| 55 | + .src("src/**/*.d.ts", { base: "src/", allowEmpty: true, encoding: false }) |
| 56 | + .pipe(gulp.dest("dist/", { mode: 0o644, dirMode: 0o755 })); |
| 57 | +} |
| 58 | + |
| 59 | +// npm run clean / npx gulp clean: clean 'dist' folder |
| 60 | +export const clean = () => rimraf("./dist/"); |
| 61 | + |
| 62 | +// npm run build / npx gulp build: build plugin |
| 63 | +export const build = gulp.series(clean, buildFlavors, copyTSDefinition); |
| 64 | + |
| 65 | +// watch files for changes and trigger rebuild tasks |
| 66 | +async function watchFiles() { |
| 67 | + gulp.watch("src/**/*.js", buildFlavors); |
| 68 | + gulp.watch("src/**/*.ts", buildFlavors, copyTSDefinition); |
| 69 | +} |
| 70 | + |
| 71 | +// npm run watch / npx gulp watch: continuously update index.html from deps |
| 72 | +export const watch = gulp.series(build, watchFiles); |
| 73 | + |
| 74 | +export default build; |
0 commit comments