Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jspsych-builder

bjoluc54MIT4.4.0TypeScript support: included

A CLI utility to easily develop and package jsPsych experiments

readme

jsPsych Builder

npm (tag) GitHub Workflow Status code style: prettier semantic-release

A CLI utility to easily develop and package jsPsych experiments.

Focus on writing your timeline – let jsPsych Builder do the rest.

Motivation

jsPsych can be loaded in three different ways: Via a CDN, via standalone scripts, or via NPM (ES6). The latter option, while very convenient, is the hardest to manually set up. jsPsych Builder solves this by internally configuring common development tools (webpack, Babel, etc.) and exposing them via a simple CLI. Most notably, it:

  • sets up the HTML markup
  • provides a development mode with automated browser refreshing (using webpack-dev-server)
  • provides Sass support
  • helps with media preloading for custom plugins (by compiling lists of file paths to be preloaded)
  • transpiles, bundles, and minifies scripts to guarantee wide browser compatibility and short loading times (using webpack and Babel)
  • provides TypeScript and React support – simply rename your files to *.ts, *.tsx, or *.jsx.
  • supports module-style imports of non-module plugins from @jspsych-contrib
  • offers to bundle all the required files for deployment, yielding a zip archive
  • offers to package experiments for JATOS

Requirements

jsPsych Builder requires Node.js >= 14 to be installed on your machine.

Getting started

Attention: Starting with version 3, jsPsych Builder exclusively supports jsPsych v7. If you need to work with jsPsych v6, consider using jsPsych Builder v2.1.0 via npx jspsych-builder@v2 init.

Create a new directory, open it in a terminal, and issue

npx jspsych-builder init

This will ask you a few questions and set up a new jsPsych project for you. Within that project, jsPsych Builder installs itself as a development dependency, so no global package installation is required.

Once the project has been initialized, you can run npm start to start a development server for your experiment. You may then open http://localhost:3000/ to see your experiment in action. Whenever you make changes to your source files, the experiment will be updated in the browser as well.

Experiments built with jsPsych Builder adhere to the following directory structure:

├── assets
├── node_modules
├── package.json
├── package-lock.json
├── src
│   └── experiment.js
└── styles
    └── main.scss

assets is the place for your media files, where you are free to add nested directories. package.json and package-lock.json are files created and maintained by npm, a JavaScript package manager. You should leave them in place, as well as node_modules, the directory into which npm installs packages. This is also where jsPsych has been saved to.

The src directory is where you write your actual experiments, and styles is the place for your custom stylesheets. Within src, there can be multiple experiment files, as well as arbitrary directories and JavaScript files that you can import in your experiment files. experiment.js is just the default name for the first experiment file. All jsPsych Builder commands take an experiment-file argument to specify which experiment file shall be used. By default, that option is set to experiment. Changing it to my-experiment (for instance via npm start my-experiment) would make jsPsych Builder load the src/my-experiment.js file instead of src/experiment.js. This allows you to have multiple related experiments in one place and share code between them.

Writing experiments

If you are new to jsPsych, you might take a look at the jsPsych demo experiment tutorial. Note that the tutorial loads jsPsych via a CDN. You will have to npm install and import plugins instead, similar to the NPM version of the hello-world tutorial

Experiment files

Experiment files need to export an asynchronous run function that initializes a JsPsych instance, runs the experiment with it, and optionally returns the JsPsych instance at the end. You can check the experiment template file for an example. If the run function returns the JsPsych instance, jsPsych Builder will display the results in the browser window at the end (or save them to JATOS when an experiment is served by JATOS). Remove the return statement from the run function if you don't want jsPsych Builder to handle result data.

The top of the experiment file contains a special section ("docblock") with meta information ("pragmas"). This is where you specify the title, description, and version of your experiment, as well as any asset files and directories.

Assets

The @assets pragma allows to include arbitrary asset files (like images, videos, etc.) in the build to use them in your experiment. The default value

@assets assets/

includes all files within the assets directory. You can also list individual files and directories, separated by commas. For instance,

@assets assets/my-experiment,assets/fixmark.png,test.html

would include all files within assets/my-experiment, as well as assets/fixmark.png, and test.html.

The paths of all matched asset files are provided to the run function via the assetPaths parameter. They are grouped by their media type (images, video, audio, misc), so you can preload media files with jsPsych's preload plugin if you need to.

Migration notice:

If you were previously using the @imagesDir, @audioDir, @videoDir, and @miscDir pragmas, you can migrate to the @assets pragma as shown in the following example:

- @imagesDir images
- @audioDir audio/common,audio/my-experiment
+ @assets media/images,media/audio/common,media/audio/my-experiment

Note that @assets doesn't prefix paths with media/ like the deprecated @...Dir pragmas did.

Styles

You can write your style sheets using plain CSS or Sass (.scss). You may also import style sheets from node packages. Note that you have to import your styles (or a root style sheet that imports the others) within your experiment file to make the build system include them.

Packaging experiments

Once you have finished an experiment, you can run npm run build. This will create a zip file containing all the files required to serve the experiment on any machine. If you want to serve your experiment using JATOS, run npm run jatos instead to create a JATOS study file (.jzip) that can be imported via the JATOS web interface.

Installing the jsPsych Builder CLI globally

In case you'd like to have direct access (without npx or NPM scripts) to the jsPsych Builder CLI, you can also install jsPsych Builder globally. Depending on your system configuration, you may need admin rights to do so:

npm install -g jspsych-builder

If you are working on Linux or OSX and bash is your shell, you may enable command completion by running jspsych completion >> ~/.bashrc (Linux) or jspsych completion >> ~/.bash_profile (OSX).

A detailed list of sub commands and their respective options can be displayed by running jspsych without any options, or jspsych --help with the name of a sub command.

Customizing the webpack configuration

If you decide to take this path, be aware that even minor and patch releases of jsPsych Builder may break your setup, since the webpack config can be subject to change without notice – you have been warned!

If you need to, you can customize jsPsych Builder's internal webpack configuration to fit your needs: In the root directory of your project (next to your package.json), create a builder.config.mjs file containing

/** @param {import("webpack").Configuration} config */
export function webpack(config) {
  return config;
}

The function that you export gets the webpack config that jsPsych Builder has assembled internally. You can modify it and return your modified version so jsPsych Builder will use it instead of its own config. Similarly, if you need to alter webpack's DevServer configuration, you can do so via

/** @param {import("webpack-dev-server").Configuration} devServerConfig */
export function webpackDevServer(devServerConfig) {
  return devServerConfig;
}

in builder.config.mjs.

changelog

Changelog

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

4.4.0 (2023-07-07)

Features

  • Allow modifying the webpack DevServer configuration in builder.config.mjs (099a495), closes #49 #49

4.3.3 (2023-02-20)

Bug Fixes

  • User Types: Fix the return type of the run function to allow Promise<void> (f9a7cca)

4.3.2 (2022-12-06)

Bug Fixes

  • Fix security vulnerabilities by updating dependencies (84bd8ed)

4.3.1 (2022-07-01)

Bug Fixes

  • Remove obsolete Babel plugins which broke TypeScript parameter properties (5fcfe6b)

4.3.0 (2022-05-16)

Features

  • Support module-style imports of non-module plugins from [@jspsych-contrib](https://github.com/jspsych-contrib) (439e838)

4.2.1 (2022-03-26)

Bug Fixes

  • Fix file/module not found errors due to paths that were hardcoded at compile time (86ca71b), closes #30

4.2.0 (2022-03-25)

Features

  • run command: Add a port option and search for the first free port >= 3000 by default (#28) (55e606d), closes #19 #19
  • Allow webpack config customizations via builder.config.js (06cc282), closes #21
  • CLI: Show webpack build progress (d1597bb)
  • Export TypeScript types for the run function (2e66331)
  • Introduce a general assets pragma and deprecate the individual media pragmas (263db46), closes #18 #18
  • Pass the title and version pragmas as options to the experiment's run function (1f53433), closes #22

Bug Fixes

  • run command: Fix release asset watching of nested directories on MacOS and Windows (03798b5), closes #13
  • Circumvent size warning due to inlined web fonts in jsPsych's stylesheet (0db636c)

4.1.2 (2022-02-09)

Bug Fixes

  • Dependencies: Update dependency versions and replace outdated dependencies (67d78c0)

4.1.1 (2021-12-13)

Bug Fixes

  • Fix file paths on Windows – thanks @Haffi921! (#12) (e8e6184), closes #11 #11

4.1.0 (2021-11-27)

Features

  • Add TypeScript and React support (3668350), closes #10

Bug Fixes

  • Template: Fix jatos script in package.json (1a0db16)
  • Template: Set private flag in package.json (533fa1a)

4.0.1 (2021-10-10)

Bug Fixes

  • Fix asset paths compilation (03499f9)

4.0.0 (2021-10-10)

⚠ BREAKING CHANGES

  • The initOptions argument to the run() function has been replaced by assetPaths, since preloading is no longer supported via init options since jsPsych v6.3.0. Please refer to the experiment file template for an up-to-date usage example.

Bug Fixes

  • Replace initOptions run() argument by assetPaths (78a8687)

3.0.0 (2021-10-09)

⚠ BREAKING CHANGES

  • CLI: The jspsych jatos command is no longer supported. Please use jspsych build --jatos instead.
  • Instead of a createTimeline function, experiment files are now expected to export an async run function that initializes jsPsych and runs the experiment. Please refer to the experiment template file for an example. Furthermore, jsPsych Builder no longer handles named exports other than run, and the jsPsych style sheet is not automatically imported anymore.
  • Dependencies: The minimum supported Node.js version is now v14

Features

  • CLI: Remove jspsych jatos alias (6f778e2)
  • CLI: Slim down jspsych run console output (7231cdf)
  • Development mode: Show fullscreen overlays for errors/warnings (974cfa5)
  • Support jsPsych v7 (ba8725e)

Miscellaneous Chores

  • Dependencies: Update dependencies (829f95f)

2.1.0 (2021-05-11)

Features

  • CLI: Implement shell completion for the [experiment-file] option (6f64ac0)

2.0.0 (2021-02-11)

⚠ BREAKING CHANGES

  • Node.js v10 is no longer supported.

Bug Fixes

1.4.2 (2021-02-05)

Bug Fixes

  • Template: Set the version pragma to 0.1.0 for new experiments (039abc0)
  • Template: Update jsPsych version to v6.2.0 (699d166)

1.4.1 (2020-10-23)

Bug Fixes

  • webpack: Revert to webpack v4 until v5 is more stable (855d686)

1.4.0 (2020-10-23)

Features

  • run command: Implement asset watching (dd9fa09), closes #5
  • run command: Implement pragma watching (835835a), closes #5

1.3.0 (2020-09-19)

Features

  • CLI: Enhance CLI (arguments, help messages, bash completion) (4e7512b)
  • CLI: Improve error presentation (3df327c)

1.2.1 (2020-09-18)

Bug Fixes

  • Template: Use jsPsych GitHub repo in package.json (a74be9c)
  • Webpack Config: Increase size warning threshold (a0cab37)

1.2.0 (2020-04-30)

Features

  • Experiments: Support inclusion of arbitrary files via @miscDir (e698c18)

1.1.0 (2020-04-23)

Features

  • init command: Include jspsych-builder as local dev dependency (2726171)
  • CLI: Add update notification using update-notifier (7b21875)
  • Experiments: Support custom on_finish and on_finish_jatos functions (6f11826)

Bug Fixes

  • init command: Optionally include -e option in success message (823db5b)
  • CLI: Throw error if required pragma is missing in experiment file (e3d5db3)

1.0.2 (2020-04-22)

Bug Fixes

  • Assets: Fix asset resolution (43d51be)

1.0.1 (2020-04-22)

Bug Fixes

  • dependencies: Replace node-sass with sass (c38c5e4)

1.0.0 (2020-04-22)

Features