Skip to content
This repository was archived by the owner on May 3, 2026. It is now read-only.

Commit 62eb470

Browse files
committed
feat: created rollup plugin, it'll replace webpack soon
1 parent 17a0a5a commit 62eb470

12 files changed

Lines changed: 1008 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@tsparticles/rollup-plugin",
3+
"version": "3.4.14",
4+
"description": "Rollup build utilities for tsParticles",
5+
"type": "module",
6+
"main": "dist/index.cjs",
7+
"module": "dist/index.js",
8+
"types": "dist/index.d.ts",
9+
"exports": {
10+
".": {
11+
"import": "./dist/index.js",
12+
"require": "./dist/index.cjs",
13+
"types": "./dist/index.d.ts"
14+
}
15+
},
16+
"files": [
17+
"dist"
18+
],
19+
"scripts": {
20+
"clean": "rimraf dist",
21+
"build": "pnpm clean && rollup -c rollup.config.mjs",
22+
"watch": "rollup -c rollup.config.mjs -w"
23+
},
24+
"peerDependencies": {
25+
"rollup": "^4"
26+
},
27+
"dependencies": {
28+
"@rollup/plugin-replace": "^6.0.3",
29+
"@rollup/plugin-terser": "^1.0.0",
30+
"rollup-plugin-visualizer": "^7.0.1"
31+
},
32+
"devDependencies": {
33+
"@rollup/plugin-typescript": "^12.3.0",
34+
"@types/node": "^25.6.0",
35+
"typescript": "^6.0.3",
36+
"rimraf": "^6.1.3"
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import typescript from "@rollup/plugin-typescript";
2+
import { builtinModules } from "node:module";
3+
4+
export default {
5+
input: "src/index.ts",
6+
external: [
7+
...builtinModules,
8+
"rollup",
9+
"node:path",
10+
"@rollup/plugin-replace",
11+
"@rollup/plugin-terser",
12+
"rollup-plugin-visualizer"
13+
],
14+
output: [
15+
{ file: "dist/index.js", format: "esm" },
16+
{ file: "dist/index.cjs", format: "cjs" }
17+
],
18+
plugins: [typescript()]
19+
};
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
export type ParticlesBuildType =
2+
| "bundle"
3+
| "effect"
4+
| "engine"
5+
| "interaction"
6+
| "interactionExternal"
7+
| "interactionParticles"
8+
| "palette"
9+
| "path"
10+
| "plugin"
11+
| "pluginEasing"
12+
| "pluginEmittersShape"
13+
| "pluginExport"
14+
| "preset"
15+
| "shape"
16+
| "template"
17+
| "updater";
18+
19+
import type { ParticlesBuildParams } from "./types";
20+
21+
interface BuildDefinition {
22+
format: string;
23+
hasBundle?: boolean;
24+
banner: (p: ParticlesBuildParams) => string;
25+
minBanner: (p: ParticlesBuildParams) => string;
26+
}
27+
28+
export const buildMap: Record<ParticlesBuildType, BuildDefinition> = {
29+
bundle: {
30+
format: "",
31+
hasBundle: true,
32+
banner: ({ version }) => `tsParticles v${version}`,
33+
minBanner: ({ version, bundleName }) =>
34+
`tsParticles ${bundleName ?? ""} v${version}`,
35+
},
36+
effect: {
37+
format: "effect",
38+
banner: ({ version }) => `Effect v${version}`,
39+
minBanner: ({ version, effectName }) =>
40+
`tsParticles ${effectName} Effect v${version}`,
41+
},
42+
engine: {
43+
format: "engine",
44+
banner: ({ version }) => `tsParticles Engine v${version}`,
45+
minBanner: ({ version }) => `tsParticles Engine v${version}`,
46+
},
47+
interaction: {
48+
format: "interaction",
49+
banner: ({ version }) => `Interaction v${version}`,
50+
minBanner: ({ version }) => `Interaction v${version}`,
51+
},
52+
interactionExternal: {
53+
format: "interaction.external",
54+
banner: ({ version }) => `External Interaction v${version}`,
55+
minBanner: ({ version }) => `External Interaction v${version}`,
56+
},
57+
interactionParticles: {
58+
format: "interaction.particles",
59+
banner: ({ version }) => `Particles Interaction v${version}`,
60+
minBanner: ({ version }) => `Particles Interaction v${version}`,
61+
},
62+
palette: {
63+
format: "palette",
64+
banner: ({ version }) => `Palette v${version}`,
65+
minBanner: ({ version }) => `Palette v${version}`,
66+
},
67+
path: {
68+
format: "path",
69+
banner: ({ version }) => `Path v${version}`,
70+
minBanner: ({ version }) => `Path v${version}`,
71+
},
72+
plugin: {
73+
format: "plugin",
74+
banner: ({ version }) => `Plugin v${version}`,
75+
minBanner: ({ version, pluginName }) =>
76+
`tsParticles ${pluginName} Plugin v${version}`,
77+
},
78+
pluginEasing: {
79+
format: "plugin.easing",
80+
banner: ({ version }) => `Easing Plugin v${version}`,
81+
minBanner: ({ version }) => `Easing Plugin v${version}`,
82+
},
83+
pluginEmittersShape: {
84+
format: "plugin.emitters.shape",
85+
banner: ({ version }) => `Emitters Shape v${version}`,
86+
minBanner: ({ version }) => `Emitters Shape v${version}`,
87+
},
88+
pluginExport: {
89+
format: "plugin.export",
90+
banner: ({ version }) => `Export Plugin v${version}`,
91+
minBanner: ({ version }) => `Export Plugin v${version}`,
92+
},
93+
preset: {
94+
format: "preset",
95+
hasBundle: true,
96+
banner: ({ version }) => `Preset v${version}`,
97+
minBanner: ({ version }) => `Preset v${version}`,
98+
},
99+
shape: {
100+
format: "shape",
101+
banner: ({ version }) => `Shape v${version}`,
102+
minBanner: ({ version }) => `Shape v${version}`,
103+
},
104+
template: {
105+
format: "template",
106+
banner: ({ version }) => `Template v${version}`,
107+
minBanner: ({ version }) => `Template v${version}`,
108+
},
109+
updater: {
110+
format: "updater",
111+
banner: ({ version }) => `Updater v${version}`,
112+
minBanner: ({ version }) => `Updater v${version}`,
113+
},
114+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { ConfigParams } from "../types";
2+
import type { RollupOptions } from "rollup";
3+
import { createSingleConfig } from "./createSingleConfig";
4+
5+
export const createConfig = (params: ConfigParams): RollupOptions[] => {
6+
return [
7+
createSingleConfig(params, false, false),
8+
createSingleConfig(params, true, false),
9+
createSingleConfig(params, false, true),
10+
createSingleConfig(params, true, true),
11+
];
12+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import path from "node:path";
2+
import replace from "@rollup/plugin-replace";
3+
import terser from "@rollup/plugin-terser";
4+
import { visualizer } from "rollup-plugin-visualizer";
5+
import { getEntry } from "./entry";
6+
import { getExternal, getGlobals } from "./externals";
7+
import type { ConfigParams } from "../types";
8+
import type { RollupOptions } from "rollup";
9+
10+
export const createSingleConfig = (
11+
params: ConfigParams,
12+
min: boolean,
13+
lazy: boolean
14+
): RollupOptions => {
15+
const {
16+
additionalExternals,
17+
banner,
18+
bundle,
19+
dir,
20+
entry,
21+
minBanner,
22+
version,
23+
} = params,
24+
{ name, input } = getEntry({ ...entry, min, lazy });
25+
26+
return {
27+
input,
28+
external: getExternal({ bundle, additionalExternals }),
29+
plugins: [
30+
replace({
31+
preventAssignment: true,
32+
__VERSION__: JSON.stringify(version),
33+
}),
34+
!min &&
35+
visualizer({
36+
filename: path.resolve(dir, "dist/report.html"),
37+
}),
38+
min && terser(),
39+
].filter(Boolean),
40+
output: {
41+
file: path.resolve(dir, "dist", `${name}.js`),
42+
format: "umd",
43+
name: "tsParticles",
44+
globals: getGlobals(additionalExternals, bundle),
45+
banner: min ? minBanner : banner,
46+
inlineDynamicImports: true,
47+
},
48+
};
49+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface EntryParams {
2+
bundle: boolean;
3+
format: string;
4+
lazy: boolean;
5+
min: boolean;
6+
name?: string;
7+
}
8+
9+
export const getEntry = (data: EntryParams) => {
10+
const { bundle, format, lazy, min, name } = data,
11+
fileName = bundle ? "bundle" : "index",
12+
completeFileName = lazy ? `${fileName}.lazy` : fileName,
13+
fixFormat = format ? `.${format}` : "",
14+
fixName = name ? `.${name}` : "",
15+
fixMin = min ? ".min" : "",
16+
fixLazy = lazy ? ".lazy" : "";
17+
18+
return {
19+
name: `tsparticles${fixFormat}${fixName}${fixLazy}${fixMin}`,
20+
input: `./dist/browser/${completeFileName}.js`,
21+
};
22+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ExternalData } from "../types";
2+
3+
interface Params {
4+
additionalExternals?: ExternalData[];
5+
bundle?: boolean;
6+
}
7+
8+
export const getExternal = ({ bundle, additionalExternals = [] }: Params) => {
9+
if (bundle) {
10+
return [];
11+
}
12+
13+
return [
14+
...additionalExternals.map(e => e.name),
15+
/^tsparticles$/,
16+
/^tsparticles-/,
17+
/^@tsparticles\//,
18+
];
19+
};
20+
21+
export const getGlobals = (
22+
additionalExternals: ExternalData[] = [],
23+
bundle?: boolean
24+
) => {
25+
if (bundle) {
26+
return {};
27+
}
28+
29+
return Object.fromEntries(
30+
additionalExternals.map(e => [e.name, "window"])
31+
);
32+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { buildMap, type ParticlesBuildType } from "./buildMap";
2+
import { createConfig } from "./config/createConfig";
3+
import type { ParticlesBuildParams } from "./types";
4+
5+
export const createParticlesBuild = (
6+
type: ParticlesBuildType,
7+
params: ParticlesBuildParams
8+
) => {
9+
const def = buildMap[type];
10+
11+
if (!def) {
12+
throw new Error(`Unknown build type: ${type}`);
13+
}
14+
15+
const dir = params.dir,
16+
version = params.version,
17+
additionalExternals = params.additionalExternals,
18+
moduleName = params.moduleName,
19+
bundle = params.bundle,
20+
banner = def.banner(params),
21+
minBanner = def.minBanner(params),
22+
base = createConfig({
23+
entry: {
24+
format: def.format,
25+
name: moduleName,
26+
bundle: false,
27+
},
28+
version,
29+
banner,
30+
minBanner,
31+
dir,
32+
bundle: false,
33+
additionalExternals,
34+
});
35+
36+
if (def.hasBundle && (bundle ?? true)) {
37+
return [
38+
...base,
39+
...createConfig({
40+
entry: {
41+
format: def.format,
42+
name: moduleName
43+
? `${moduleName}.bundle`
44+
: "bundle",
45+
bundle: true,
46+
},
47+
version,
48+
banner,
49+
minBanner,
50+
dir,
51+
bundle: true,
52+
additionalExternals,
53+
}),
54+
];
55+
}
56+
57+
return base;
58+
};

0 commit comments

Comments
 (0)