This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
generated from HugoDF/microbundle-ts-pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from HugoDF/feat-warn-version-mismatch
feat(log-warn-on-cdn-npm-version-mismatch)
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<head> | ||
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v1.x.x/dist/alpine.min.js" defer></script> | ||
</head> | ||
<body> | ||
<div x-data="{}"></div> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* | ||
* @param {Document} document - document from which Alpine components are being loaded from | ||
* @param {string} alpineVersion - Alpine.js version from NPM | ||
* @returns {void} | ||
*/ | ||
function checkVersionMismatch(document, alpineVersion) { | ||
if (document.scripts.length === 0) return; | ||
const alpineScript = [...document.scripts].find( | ||
(s) => s.src.includes('dist/alpine') || s.src.includes('alpinejs/alpine') | ||
); | ||
if (!alpineScript) return; | ||
// Match v1.x.x, v2.x.x etc (the bit between @ and /)from | ||
// `https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js` | ||
const [jsDelivrVersion] = alpineScript.src.match(/(?<=@v)[a-z|\d.]*(?=\/)/gm); | ||
const cdnMajorVersion = jsDelivrVersion[0]; | ||
if (!alpineVersion.startsWith(cdnMajorVersion)) { | ||
console.warn( | ||
`alpine-test-utils: Alpine.js version is different to CDN one, requested "${jsDelivrVersion}", testing with "${alpineVersion}"` | ||
); | ||
} | ||
} | ||
|
||
module.exports = {checkVersionMismatch}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import test from 'ava'; | ||
import path from 'path'; | ||
import {load} from '../src/main'; | ||
import Alpine from 'alpinejs'; | ||
|
||
const stub = (fn) => { | ||
const calls = []; | ||
const callable = (...args) => { | ||
calls.push(args); | ||
fn(...args); | ||
}; | ||
|
||
callable.calls = calls; | ||
callable.firstCall = () => calls[0]; | ||
return callable; | ||
}; | ||
|
||
test('load - Alpine.js version mismatch', async (t) => { | ||
console.warn = stub(() => {}); | ||
const component = await load( | ||
path.join(__dirname, '../fixtures/version.html') | ||
); | ||
t.is(component, `<div x-data="{}"></div>`); | ||
t.is( | ||
console.warn.firstCall()[0], | ||
`alpine-test-utils: Alpine.js version is different to CDN one, requested "1.x.x", testing with "${Alpine.version}"` | ||
); | ||
}); |