1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00

git: add package.json, rollup build script

This commit is contained in:
Sotiri Bakagiannis 2021-04-15 13:46:44 +01:00
parent e1df2bc19c
commit 3587c689e8
2 changed files with 291 additions and 0 deletions

79
package.json Normal file
View File

@ -0,0 +1,79 @@
{
"name": "psychojs",
"version": "2020.2.0",
"private": true,
"description": "Helps run in-browser neuroscience, psychology, and psychophysics experiments",
"license": "MIT",
"author": {
"name": "Alain Pitiot"
},
"type": "module",
"main": "src/index.js",
"scripts": {
"build": "npm run build:js && npm run build:css && npm run build:docs",
"build:css": "postcss -o dist/psychojs-$npm_package_version.css src/index.css",
"build:docs": "jsdoc src -r -d docs",
"build:js": "rollup -c",
"lint": "npm run lint:js && npm run lint:css",
"lint:css": "stylelint src/**/*.css",
"lint:js": "jshint rollup.config.js src/**/*.js",
"start": "npm run build"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"targets": {
"ie": 11
},
"spec": true,
"forceAllTransforms": true,
"debug": true
}
]
]
},
"browserslist": [
"last 2 versions"
],
"stylelint": {
"extends": "stylelint-config-standard",
"rules": {
"no-descending-specificity": [
true,
{
"severity": "warning"
}
]
}
},
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@rollup/plugin-babel": "^5.2.1",
"cssnano": "^4.1.10",
"jsdoc": "^3.6.6",
"jshint": "^2.12.0",
"postcss": "^8.1.3",
"postcss-cli": "^8.1.0",
"postcss-preset-env": "^6.7.0",
"rollup": "^2.32.1",
"stylelint": "^13.7.2",
"stylelint-config-standard": "^20.0.0",
"terser": "^5.3.8"
},
"jshintConfig": {
"esversion": 8
},
"postcss": {
"plugins": {
"postcss-preset-env": {},
"cssnano": {
"autoprefixer": false
}
}
}
}

212
rollup.config.js Normal file
View File

@ -0,0 +1,212 @@
// ES native imports courtesy of using type module in 'package.json'
import path from 'path';
import fs from 'fs';
import { minify } from 'terser';
import babel from '@rollup/plugin-babel';
import pkg from './package.json';
// Manually set default version here for easier
// diffing when comparing to original build script output
const { VERSION: version = pkg.version } = process.env;
// Enabled in the original, even though
// source maps missing for sample provided
const sourcemap = false;
// Might be 'build' or similar
const destination = './dist';
// Could be 'src' or 'lib'
const source = './src';
// Start fresh
try {
if (fs.existsSync(destination)) {
// Clear out JS files before rebuilding
const contents = fs.readdirSync(destination).filter(item => item.endsWith('js'));
for (const item of contents) {
const target = path.join(destination, item);
const stat = fs.statSync(target);
// Delete
fs.unlinkSync(target);
}
} else {
// Create 'dist' if missing
fs.mkdirSync(destination);
}
} catch (error) {
console.error(error);
}
// For sorting legacy/IE11 bundle components
const orderOfAppearance = [ 'util', 'data', 'core', 'visual', 'sound' ];
const last = [ ...orderOfAppearance ].pop();
const footer = `
// Add a few top level variables for convenience, this makes it
// possible to eg. use "return Scheduler.Event.NEXT;" instead of "util.Scheduler.Event.NEXT;"
PsychoJS = core.PsychoJS;
TrialHandler = data.TrialHandler;
Scheduler = util.Scheduler;`;
const plugins = [
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**',
include: `${destination}/*.iife.js`
}),
minifier({
compress: false,
mangle: false,
output: {
beautify: true
},
sourceMap: false,
toplevel: false
})
];
// List source directory contents
const components = fs.readdirSync(source)
// Need subdirectories only
.filter((item) => {
const target = path.join(source, item);
const stat = fs.statSync(target);
return stat.isDirectory();
})
// Put in order
.sort((a, b) => orderOfAppearance.indexOf(a) - orderOfAppearance.indexOf(b))
// Prepare an output object for each component module
.map((component, _, contents) => ({
// So I don't have to specify full paths
external: (id) => {
// Decompose current component path
const segments = id.split('/');
// Mark as external if contents within source
// directory tree, excluding the current component
return contents
.filter(item => item !== component)
.some(item => segments.includes(item));
},
input: `${source}/${component}/index.js`,
// Disable circular dependency warnings
onwarn,
output: [
{
file: `${destination}/${component}-${version}.js`,
format: 'module',
globals: {
performance: 'performance'
},
// Find which module the import points to
// and fix path in place
paths: (id) => {
const name = findName(id, contents);
return `./${name}-${version}.js`;
},
sourcemap,
},
{
esModule: false,
file: `${destination}/${component}-${version}.iife.js`,
format: 'iife',
globals: id => findName(id, contents),
name: component,
paths: (id) => {
const name = findName(id, contents);
return `./${name}-${version}.iife.js`;
},
sourcemap,
plugins: [
appender({
target: `${destination}/psychojs-${version}.js`,
// Mirrors rollup's 'outputOptions' hook
outputOptions: (options) => {
if (options.file.includes(last)) {
options.footer = footer;
}
return options;
}
})
]
}
],
plugins
})
);
export default [
...components,
{
// Add a UMD build for Thomas
input: `${source}/index.js`,
onwarn,
output: {
file: `${destination}/psychojs-${version}.umd.js`,
format: 'umd',
name: 'psychojs'
},
plugins
}
];
// https://rollupjs.org/guide/en/#onwarn
function onwarn(message, warn) {
// Skip circular dependency warnings
if (message.code === 'CIRCULAR_DEPENDENCY') {
return;
}
warn(message);
}
// Helper for extracting module name from contents array by rollup id (path to file)
function findName(id, contents) {
return id.split(path.sep).find(item => contents.includes(item));
}
// Minimal terser plugin
function minifier(options) {
return {
name: 'minifier',
async renderChunk(code) {
try {
// Includes code and map keys
const result = await minify(code, options);
return result;
} catch (error) {
throw error;
}
}
};
}
// Custom plugin for cancatenating IIFE's sans cat(1)
function appender({ target = '', outputOptions = () => {} } = {}) {
return {
name: 'appender',
outputOptions: (options) => outputOptions(options),
async generateBundle(options, bundle) {
const { file } = options;
const id = file.split('/').pop();
const { code } = bundle[id];
// Should be expected to throw if `target` missing
fs.appendFile(target, code, (error) => {
if (error) {
throw error;
}
});
// Prevent write out
delete bundle[id];
}
};
}