mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-10 11:10:54 +00:00
move citation build logic from rollup to esbuild
This commit is contained in:
parent
a2e7918773
commit
8bbf9b73c8
3
.github/workflows/build.yml
vendored
3
.github/workflows/build.yml
vendored
@ -35,6 +35,9 @@ jobs:
|
|||||||
- name: Check types
|
- name: Check types
|
||||||
run: npm run tsc
|
run: npm run tsc
|
||||||
|
|
||||||
|
- name: Prebuild citation
|
||||||
|
run: npm run prebuild
|
||||||
|
|
||||||
- name: Build packages
|
- name: Build packages
|
||||||
run: npm run build
|
run: npm run build
|
||||||
|
|
||||||
|
4797
package-lock.json
generated
4797
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -7,6 +7,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"test:watch": "npm test -- --watch",
|
"test:watch": "npm test -- --watch",
|
||||||
|
"prebuild": "node generateCitation.js",
|
||||||
"build": "turbo run build",
|
"build": "turbo run build",
|
||||||
"build:archive": "gulp createCoreDistArchive",
|
"build:archive": "gulp createCoreDistArchive",
|
||||||
"update-unpkg-links": "gulp updateUnpkgLinks",
|
"update-unpkg-links": "gulp updateUnpkgLinks",
|
||||||
@ -54,9 +55,10 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@citation-js/core": "^0.7.14",
|
"@citation-js/core": "^0.7.14",
|
||||||
"@citation-js/plugin-software-formats": "^0.6.1",
|
|
||||||
"@citation-js/plugin-bibtex": "^0.7.14",
|
"@citation-js/plugin-bibtex": "^0.7.14",
|
||||||
"@citation-js/plugin-csl": "^0.7.14",
|
"@citation-js/plugin-csl": "^0.7.14",
|
||||||
|
"@citation-js/plugin-software-formats": "^0.6.1",
|
||||||
|
"link": "^2.1.1",
|
||||||
"symlink": "^2.1.0"
|
"symlink": "^2.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
80
packages/config/generateCitation.js
Normal file
80
packages/config/generateCitation.js
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
import "@citation-js/plugin-bibtex";
|
||||||
|
import "@citation-js/plugin-software-formats";
|
||||||
|
import "@citation-js/plugin-csl";
|
||||||
|
|
||||||
|
import fs from "node:fs";
|
||||||
|
|
||||||
|
import { Cite } from "@citation-js/core";
|
||||||
|
import yaml from "yaml";
|
||||||
|
|
||||||
|
export default function generateCitation() {
|
||||||
|
let preferredCitation = false;
|
||||||
|
|
||||||
|
// Try to find CITATION.cff file and look for preferred-citation
|
||||||
|
const citationCff = (() => {
|
||||||
|
try {
|
||||||
|
const rawCff = fs.readFileSync("./CITATION.cff", "utf-8").toString();
|
||||||
|
const cffData = yaml.parse(rawCff);
|
||||||
|
if (cffData["preferred-citation"]) {
|
||||||
|
console.log("Found 'preferred-citation' in CITATION.cff");
|
||||||
|
preferredCitation = true;
|
||||||
|
} else {
|
||||||
|
console.log("No 'preferred-citation' found in CITATION.cff");
|
||||||
|
}
|
||||||
|
return yaml.stringify(rawCff);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`Error finding CITATION.cff: ${error.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
if (!citationCff) {
|
||||||
|
return JSON.stringify({ apa: "", bibtex: "" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to convert CITATION.cff to APA string
|
||||||
|
const citationApa = (() => {
|
||||||
|
try {
|
||||||
|
const apaCite = new Cite(citationCff);
|
||||||
|
apaCite["data"] = preferredCitation ? apaCite["data"].slice(1) : apaCite["data"];
|
||||||
|
return (citationApa = apaCite.format("bibliography", {
|
||||||
|
format: "text",
|
||||||
|
template: "apa",
|
||||||
|
lang: "en-us",
|
||||||
|
}));
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`Error converting CITATION.cff to APA string: ${error.message}`);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
// Try to convert CITATION.cff to bibtex string
|
||||||
|
const citationBibtex = (() => {
|
||||||
|
try {
|
||||||
|
const bibtexCite = new Cite(citationCff);
|
||||||
|
bibtexCite["data"] = preferredCitation ? bibtexCite["data"].slice(1) : bibtexCite["data"];
|
||||||
|
return (citationBibtex = bibtexCite.format("bibtex", {
|
||||||
|
format: "text",
|
||||||
|
template: "bibtex",
|
||||||
|
lang: "en-us",
|
||||||
|
}));
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`Error converting CITATION.cff to bibtex string: ${error.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
// Return formatted citation data
|
||||||
|
const citationData = {
|
||||||
|
apa: citationApa.replace(/\n/g, " "),
|
||||||
|
bibtex: citationBibtex.replace(/\n/g, " "),
|
||||||
|
};
|
||||||
|
|
||||||
|
return JSON.stringify(citationData);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute and write to file
|
||||||
|
const citationData = generateCitationData();
|
||||||
|
fs.writeFileSync("citation-data.json", citationData);
|
||||||
|
|
||||||
|
console.log("Citation data generated and saved to citation-data.json");
|
@ -59,7 +59,8 @@
|
|||||||
"rollup-plugin-node-externals": "7.1.3",
|
"rollup-plugin-node-externals": "7.1.3",
|
||||||
"sucrase": "3.34.0",
|
"sucrase": "3.34.0",
|
||||||
"tslib": "2.6.2",
|
"tslib": "2.6.2",
|
||||||
"typescript": "^5.2.2"
|
"typescript": "^5.2.2",
|
||||||
|
"npm-link": "^0.0.4"
|
||||||
},
|
},
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"alias-hq": {
|
"alias-hq": {
|
||||||
|
@ -1,98 +0,0 @@
|
|||||||
import "@citation-js/plugin-bibtex";
|
|
||||||
import "@citation-js/plugin-software-formats";
|
|
||||||
import "@citation-js/plugin-csl";
|
|
||||||
|
|
||||||
import fs from "node:fs";
|
|
||||||
import { extname } from "path";
|
|
||||||
|
|
||||||
import { Cite } from "@citation-js/core";
|
|
||||||
import { createFilter } from "@rollup/pluginutils";
|
|
||||||
import MagicString from "magic-string";
|
|
||||||
import yaml from "yaml";
|
|
||||||
|
|
||||||
export default function cffToJsonPlugin() {
|
|
||||||
const options = { include: ["**/index*"], exclude: [], sourcemap: false };
|
|
||||||
let filter = createFilter(options.include, options.exclude);
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: "rollup-plugin-cff-to-json",
|
|
||||||
version: "1.0.0",
|
|
||||||
transform: function (code, id) {
|
|
||||||
if (!filter(id) || (extname(id) !== ".js" && extname(id) !== ".ts")) return;
|
|
||||||
const magicString = new MagicString(code);
|
|
||||||
let preferredCitation = false;
|
|
||||||
|
|
||||||
// Try to find CITATION.cff file and look for preferred-citation
|
|
||||||
const citationCff = (() => {
|
|
||||||
try {
|
|
||||||
const rawCff = fs.readFileSync("./CITATION.cff", "utf-8").toString();
|
|
||||||
const cffData = yaml.parse(rawCff);
|
|
||||||
if (cffData["preferred-citation"]) {
|
|
||||||
console.log("Found 'preferred-citation' in CITATION.cff");
|
|
||||||
preferredCitation = true;
|
|
||||||
} else {
|
|
||||||
console.log("No 'preferred-citation' found in CITATION.cff");
|
|
||||||
}
|
|
||||||
return yaml.stringify(rawCff);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`Error finding CITATION.cff: ${error.message}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
// Try to convert CITATION.cff to APA string
|
|
||||||
const citationApa = (() => {
|
|
||||||
try {
|
|
||||||
const apaCite = new Cite(citationCff);
|
|
||||||
apaCite["data"] = preferredCitation ? apaCite["data"].slice(1) : apaCite["data"];
|
|
||||||
const citationApa = apaCite.format("bibliography", {
|
|
||||||
format: "text",
|
|
||||||
template: "apa",
|
|
||||||
lang: "en-us",
|
|
||||||
});
|
|
||||||
return citationApa;
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`Error converting CITATION.cff to APA string: ${error.message}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
// Try to convert CITATION.cff to bibtex string
|
|
||||||
const citationBibtex = (() => {
|
|
||||||
try {
|
|
||||||
const bibtexCite = new Cite(citationCff);
|
|
||||||
bibtexCite["data"] = preferredCitation ? bibtexCite["data"].slice(1) : bibtexCite["data"];
|
|
||||||
const citationBibtex = bibtexCite.format("bibtex", {
|
|
||||||
format: "text",
|
|
||||||
template: "bibtex",
|
|
||||||
lang: "en-us",
|
|
||||||
});
|
|
||||||
return citationBibtex;
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`Error converting CITATION.cff to bibtex string: ${error.message}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
// Replace target string with citation APA and bibtex strings
|
|
||||||
if (!citationApa && !citationBibtex) {
|
|
||||||
return { code: code };
|
|
||||||
}
|
|
||||||
//console.log(`citation: {\napa: "${citationApa.replace(/\n/g, ' ')}", \nbibtex: "${citationBibtex.replace(/\n/g, ' ')}"\n}`);
|
|
||||||
const citationString = `citation:\n { apa: "${citationApa.replace(
|
|
||||||
/\n/g,
|
|
||||||
" "
|
|
||||||
)}",\n bibtex: "${citationBibtex.replace(/\n/g, " ")}"\n }`;
|
|
||||||
console.log(citationString);
|
|
||||||
const targetString = "citation: []";
|
|
||||||
const startIndex = code.indexOf(targetString);
|
|
||||||
if (startIndex !== -1) {
|
|
||||||
magicString.overwrite(startIndex, startIndex + targetString.length, citationString);
|
|
||||||
return { code: magicString.toString() };
|
|
||||||
} else {
|
|
||||||
this.error(`Error replacing citation string in ${id}`);
|
|
||||||
return { code: code };
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
@ -10,7 +10,7 @@ import esbuild from "rollup-plugin-esbuild";
|
|||||||
import externals from "rollup-plugin-node-externals";
|
import externals from "rollup-plugin-node-externals";
|
||||||
import ts from "typescript";
|
import ts from "typescript";
|
||||||
|
|
||||||
import cffToJsonPlugin from "./rollup-plugin-build-citation.js";
|
import generateCitation from "./generateCitation.js";
|
||||||
|
|
||||||
const getTsCompilerOptions = () => {
|
const getTsCompilerOptions = () => {
|
||||||
const cwd = process.cwd();
|
const cwd = process.cwd();
|
||||||
@ -30,12 +30,12 @@ const makeConfig = ({
|
|||||||
outputOptions = {},
|
outputOptions = {},
|
||||||
globalOptions = {},
|
globalOptions = {},
|
||||||
iifeOutputOptions = {},
|
iifeOutputOptions = {},
|
||||||
additionalPlugins = [],
|
|
||||||
isNodeOnlyBuild = false,
|
isNodeOnlyBuild = false,
|
||||||
}) => {
|
}) => {
|
||||||
const input = "src/index.ts";
|
const input = "src/index.ts";
|
||||||
const destinationDirectory = "dist";
|
const destinationDirectory = "dist";
|
||||||
const destination = `${destinationDirectory}/index`;
|
const destination = `${destinationDirectory}/index`;
|
||||||
|
const citationData = JSON.parse(fs.readFileSync("citation-data.json", "utf-8"));
|
||||||
|
|
||||||
outputOptions = {
|
outputOptions = {
|
||||||
sourcemap: true,
|
sourcemap: true,
|
||||||
@ -44,7 +44,10 @@ const makeConfig = ({
|
|||||||
|
|
||||||
/** @type{import("rollup-plugin-esbuild").Options} */
|
/** @type{import("rollup-plugin-esbuild").Options} */
|
||||||
const esBuildPluginOptions = {
|
const esBuildPluginOptions = {
|
||||||
//loaders: { ".json": "json" },
|
define: {
|
||||||
|
__APACITATION__: citationData.apa,
|
||||||
|
__BIBTEXCITATION__: citationData.bibtex,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @type{import("@rollup/plugin-commonjs").RollupCommonJSOptions} */
|
/** @type{import("@rollup/plugin-commonjs").RollupCommonJSOptions} */
|
||||||
@ -74,7 +77,6 @@ const makeConfig = ({
|
|||||||
...globalOptions,
|
...globalOptions,
|
||||||
input,
|
input,
|
||||||
plugins: [
|
plugins: [
|
||||||
...additionalPlugins,
|
|
||||||
externals(),
|
externals(),
|
||||||
json(),
|
json(),
|
||||||
esbuild({ ...esBuildPluginOptions, target: "node18" }),
|
esbuild({ ...esBuildPluginOptions, target: "node18" }),
|
||||||
@ -101,7 +103,6 @@ const makeConfig = ({
|
|||||||
...globalOptions,
|
...globalOptions,
|
||||||
input,
|
input,
|
||||||
plugins: [
|
plugins: [
|
||||||
...additionalPlugins,
|
|
||||||
externals({ deps: false }),
|
externals({ deps: false }),
|
||||||
resolve({ preferBuiltins: false }),
|
resolve({ preferBuiltins: false }),
|
||||||
json(),
|
json(),
|
||||||
@ -122,7 +123,6 @@ const makeConfig = ({
|
|||||||
...globalOptions,
|
...globalOptions,
|
||||||
input,
|
input,
|
||||||
plugins: [
|
plugins: [
|
||||||
...additionalPlugins,
|
|
||||||
externals({ deps: false }),
|
externals({ deps: false }),
|
||||||
resolve({ preferBuiltins: false }),
|
resolve({ preferBuiltins: false }),
|
||||||
json(),
|
json(),
|
||||||
@ -155,7 +155,6 @@ export const makeRollupConfig = (iifeName) =>
|
|||||||
exports: "default",
|
exports: "default",
|
||||||
globals: { jspsych: "jsPsychModule" },
|
globals: { jspsych: "jsPsychModule" },
|
||||||
},
|
},
|
||||||
additionalPlugins: [cffToJsonPlugin()],
|
|
||||||
globalOptions: { external: ["jspsych"] },
|
globalOptions: { external: ["jspsych"] },
|
||||||
iifeOutputOptions: { name: iifeName },
|
iifeOutputOptions: { name: iifeName },
|
||||||
});
|
});
|
||||||
@ -175,8 +174,10 @@ export const makeCoreRollupConfig = () =>
|
|||||||
/**
|
/**
|
||||||
* Returns the rollup configuration for Node.js-only packages
|
* Returns the rollup configuration for Node.js-only packages
|
||||||
*/
|
*/
|
||||||
export const makeNodeRollupConfig = () =>
|
export const makeNodeRollupConfig = () => {
|
||||||
makeConfig({
|
console.log("jspsych Roll up config called");
|
||||||
|
return makeConfig({
|
||||||
globalOptions: { external: ["jspsych"] },
|
globalOptions: { external: ["jspsych"] },
|
||||||
isNodeOnlyBuild: true,
|
isNodeOnlyBuild: true,
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user