Skip to content

Commit 19502e5

Browse files
authored
Merge pull request #21 from projectblacklight/rollup
Add rollup for bundling
2 parents 7fe50cf + 3728dfd commit 19502e5

File tree

8 files changed

+142
-29
lines changed

8 files changed

+142
-29
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ test/tmp
1616
test/version_tmp
1717
tmp
1818
.internal_test_app
19+
20+
node_modules
21+
package-lock.json

app/assets/javascripts/blacklight_oembed/oembed.esm.js

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

app/assets/javascripts/blacklight_oembed/oembed.esm.js.map

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

app/assets/javascripts/blacklight_oembed/oembed.js

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

app/assets/javascripts/blacklight_oembed/oembed.js.map

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

app/javascript/oembed.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default function oEmbed(elements) {
2+
if (!elements) return;
3+
4+
// Ensure elements is an array-like collection
5+
elements = elements instanceof NodeList ? Array.from(elements) : [elements];
6+
7+
elements.forEach(function(embedViewer) {
8+
const embedURL = embedViewer.dataset.embedUrl; // Get the embed URL from the data attribute
9+
10+
if (!embedURL) return;
11+
12+
// Fetch JSON data from the embed URL
13+
fetch(embedURL)
14+
.then(response => {
15+
if (!response.ok) {
16+
throw new Error('Network response was not ok');
17+
}
18+
return response.json();
19+
})
20+
.then(data => {
21+
if (data === null) {
22+
return;
23+
}
24+
// Set the inner HTML of the element
25+
embedViewer.innerHTML = data.html;
26+
})
27+
.catch(error => {
28+
console.error('Error fetching embed data:', error);
29+
});
30+
});
31+
}

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"version": "1.3.0",
44
"description": "Oembed behavior for Blacklight",
55
"main": "app/assets/javascripts/blacklight_oembed/oembed.js",
6+
"type": "module",
67
"files": [
78
"app/assets/javascripts/blacklight_oembed/*.js"
89
],
910
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"test": "echo \"Error: no test specified\" && exit 1",
12+
"prepare": "rollup --config rollup.config.js --sourcemap && ESM=true rollup --config rollup.config.js --sourcemap"
1113
},
1214
"repository": {
1315
"type": "git",
@@ -19,5 +21,8 @@
1921
"url": "https://github.com/projectblacklight/blacklight-oembed/issues"
2022
},
2123
"homepage": "https://github.com/projectblacklight/blacklight-oembed#readme",
22-
"dependencies": {}
24+
"dependencies": {
25+
"rollup": "^4.30.1",
26+
"rollup-plugin-includepaths": "^0.2.4"
27+
}
2328
}

rollup.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import includePaths from 'rollup-plugin-includepaths';
2+
3+
4+
const BUNDLE = process.env.BUNDLE === 'true'
5+
const ESM = process.env.ESM === 'true'
6+
7+
const fileDest = `oembed${ESM ? '.esm' : ''}`
8+
9+
let includePathOptions = {
10+
include: {},
11+
paths: ['app/javascript'],
12+
external: [],
13+
extensions: ['.js']
14+
};
15+
16+
const rollupConfig = {
17+
input: 'app/javascript/oembed.js',
18+
output: {
19+
file: `app/assets/javascripts/blacklight_oembed/${fileDest}.js`,
20+
format: ESM ? 'es' : 'umd',
21+
generatedCode: { preset: 'es2015' },
22+
name: ESM ? undefined : 'BlacklightOembed'
23+
},
24+
plugins: [includePaths(includePathOptions)]
25+
}
26+
27+
export default rollupConfig

0 commit comments

Comments
 (0)