Skip to content

Commit a437191

Browse files
committed
Merge branch 'develop' into main
2 parents 6903e3b + 439e838 commit a437191

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

package-lock.json

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
"babel-loader": "^8.2.3",
4040
"core-js": "^3.21.0",
4141
"css-loader": "^6.6.0",
42+
"exports-loader": "^3.1.0",
4243
"html-webpack-plugin": "^5.5.0",
4344
"html-webpack-tags-plugin": "^3.0.2",
45+
"imports-loader": "^3.1.1",
4446
"mini-css-extract-plugin": "^2.5.3",
4547
"regenerator-runtime": "^0.13.9",
4648
"sass": "^1.49.7",

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jsPsych Builder solves this by internally configuring common development tools (
2222
* helps with media preloading for custom plugins (by compiling lists of file paths to be preloaded)
2323
* transpiles, bundles, and minifies scripts to guarantee wide browser compatibility and short loading times (using webpack and Babel)
2424
* provides TypeScript and React support – simply rename your files to `*.ts`, `*.tsx`, or `*.jsx`.
25+
* supports module-style imports of non-module plugins from `@jspsych-contrib`
2526
* offers to bundle all the required files for deployment, yielding a zip archive
2627
* offers to package experiments for [JATOS](https://www.jatos.org/)
2728

src/config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFileSync } from "fs";
12
import path from "path";
23
import { fileURLToPath, pathToFileURL } from "url";
34

@@ -122,6 +123,31 @@ export const getWebpackConfig = (context: BuilderContext) => {
122123
test: /\.s[ac]ss$/i,
123124
use: [{ loader: MiniCssExtractPlugin.loader }, "css-loader", "sass-loader"],
124125
},
126+
127+
// Transform IIFE plugins from `@jspsych-contrib` to modules using `imports-loader` and `exports-loader`
128+
{
129+
test: (resource) => resource.includes("/@jspsych-contrib/plugin-"),
130+
use: (info: any) => {
131+
if (info.descriptionData.main) {
132+
// The `main` field is not set in the IIFE plugin template, so we consider everything with
133+
// a `main` field non-IIFE and ignore it here.
134+
return [];
135+
}
136+
137+
// Extract the global variable name of the plugin
138+
const pluginVarNameMatches = /^var ([a-zA-Z]+) = \(function/m.exec(
139+
readFileSync(info.resource).toString()
140+
);
141+
if (!pluginVarNameMatches) {
142+
return [];
143+
}
144+
145+
return [
146+
"imports-loader?type=commonjs&imports=single|jspsych|jsPsychModule",
147+
`exports-loader?type=commonjs&exports=single|${pluginVarNameMatches[1]}`,
148+
];
149+
},
150+
},
125151
],
126152
},
127153
performance: {

0 commit comments

Comments
 (0)