Skip to content

Commit

Permalink
Merge pull request #21 from projectblacklight/rollup
Browse files Browse the repository at this point in the history
Add rollup for bundling
  • Loading branch information
taylor-steve authored Jan 10, 2025
2 parents 7fe50cf + 3728dfd commit 19502e5
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ test/tmp
test/version_tmp
tmp
.internal_test_app

node_modules
package-lock.json
34 changes: 34 additions & 0 deletions app/assets/javascripts/blacklight_oembed/oembed.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/assets/javascripts/blacklight_oembed/oembed.esm.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 38 additions & 27 deletions app/assets/javascripts/blacklight_oembed/oembed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/assets/javascripts/blacklight_oembed/oembed.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions app/javascript/oembed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default function oEmbed(elements) {
if (!elements) return;

// Ensure elements is an array-like collection
elements = elements instanceof NodeList ? Array.from(elements) : [elements];

elements.forEach(function(embedViewer) {
const embedURL = embedViewer.dataset.embedUrl; // Get the embed URL from the data attribute

if (!embedURL) return;

// Fetch JSON data from the embed URL
fetch(embedURL)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data === null) {
return;
}
// Set the inner HTML of the element
embedViewer.innerHTML = data.html;
})
.catch(error => {
console.error('Error fetching embed data:', error);
});
});
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"version": "1.3.0",
"description": "Oembed behavior for Blacklight",
"main": "app/assets/javascripts/blacklight_oembed/oembed.js",
"type": "module",
"files": [
"app/assets/javascripts/blacklight_oembed/*.js"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "rollup --config rollup.config.js --sourcemap && ESM=true rollup --config rollup.config.js --sourcemap"
},
"repository": {
"type": "git",
Expand All @@ -19,5 +21,8 @@
"url": "https://github.com/projectblacklight/blacklight-oembed/issues"
},
"homepage": "https://github.com/projectblacklight/blacklight-oembed#readme",
"dependencies": {}
"dependencies": {
"rollup": "^4.30.1",
"rollup-plugin-includepaths": "^0.2.4"
}
}
27 changes: 27 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import includePaths from 'rollup-plugin-includepaths';


const BUNDLE = process.env.BUNDLE === 'true'
const ESM = process.env.ESM === 'true'

const fileDest = `oembed${ESM ? '.esm' : ''}`

let includePathOptions = {
include: {},
paths: ['app/javascript'],
external: [],
extensions: ['.js']
};

const rollupConfig = {
input: 'app/javascript/oembed.js',
output: {
file: `app/assets/javascripts/blacklight_oembed/${fileDest}.js`,
format: ESM ? 'es' : 'umd',
generatedCode: { preset: 'es2015' },
name: ESM ? undefined : 'BlacklightOembed'
},
plugins: [includePaths(includePathOptions)]
}

export default rollupConfig

0 comments on commit 19502e5

Please sign in to comment.