import { babel } from "@rollup/plugin-babel"; import commonjs from "@rollup/plugin-commonjs"; import json from "@rollup/plugin-json"; import resolve from "@rollup/plugin-node-resolve"; import { defineConfig } from "rollup"; import { terser } from "rollup-plugin-terser"; import typescript from "rollup-plugin-typescript2"; import ts from "typescript"; const makeConfig = (outputOptions, globalOptions = {}, iifeOutputOptions = {}) => { const source = "src/index"; const destination = "dist/index"; outputOptions = { sourcemap: true, ...outputOptions, }; const commonConfig = defineConfig({ input: `${source}.ts`, plugins: [ resolve(), typescript({ typescript: ts, tsconfigDefaults: { exclude: ["./tests", "**/*.spec.ts", "**/*.test.ts", "./dist"], }, tsconfigOverride: { compilerOptions: { rootDir: "./src", outDir: "./dist", paths: {}, // Do not include files referenced via `paths` }, }, }), json(), commonjs(), ], ...globalOptions, }); return defineConfig([ { // Non-babel builds ...commonConfig, output: [ { // Build file to be used as an ES import file: `${destination}.js`, format: "esm", ...outputOptions, }, { // Build commonjs module (for tools that do not fully support ES6 modules) file: `${destination}.cjs`, format: "cjs", ...outputOptions, }, { // Build file to be used for tinkering in modern browsers file: `${destination}.browser.js`, format: "iife", ...outputOptions, ...iifeOutputOptions, }, ], }, { // Babel build ...commonConfig, plugins: commonConfig.plugins.concat( babel({ babelHelpers: "bundled", extends: "@jspsych/config/babel", }) ), output: [ { // Minified production build file file: `${destination}.browser.min.js`, format: "iife", plugins: [terser()], ...outputOptions, ...iifeOutputOptions, }, ], }, ]); }; /** * Returns a Rollup configuration object for a JsPsych plugin or extension that is written in * TypeScript * * @param {string} iifeName The variable name that will identify the plugin or extension in the * global scope in browser builds */ export const makeRollupConfig = (iifeName) => makeConfig( { exports: "default", globals: { jspsych: "jsPsychModule" }, }, { external: ["jspsych"] }, { name: iifeName } ); /** * Returns the rollup configuration for the core `jspsych` package. */ export const makeCoreRollupConfig = () => makeConfig( { exports: "named", name: "jsPsychModule", }, {}, { footer: "var initJsPsych = jsPsychModule.initJsPsych;" } );