Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert to JS #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
862 changes: 201 additions & 661 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@
"index.dev.js"
],
"devDependencies": {
"@rollup/plugin-replace": "^2.3.3",
"@rollup/plugin-typescript": "^6.0.0",
"@types/mocha": "^8.0.3",
"@types/node": "^14.11.1",
"kleur": "^4.1.1",
"@rollup/plugin-buble": "^0.21.3",
"@rollup/plugin-replace": "^2.3.4",
"@types/node": "^14.14.20",
"kleur": "^4.1.3",
"locate-character": "^2.0.5",
"pretty-ms": "^7.0.0",
"rollup": "^2.27.1",
"pretty-ms": "^7.0.1",
"rollup": "^2.36.0",
"rollup-plugin-terser": "^7.0.2",
"sirv-cli": "^1.0.6",
"sucrase": "^3.15.0",
"surge": "^0.21.6",
"typescript": "^4.0.3",
"uvu": "^0.3.3"
"sirv-cli": "^1.0.10",
"surge": "^0.21.7",
"uvu": "^0.5.1"
},
"scripts": {
"dev": "rollup -cw",
"build": "rollup -c",
"test": "uvu -r sucrase/register/ts test test.ts",
"test": "uvu test test.mjs",
"prepublishOnly": "npm test && npm run build",
"deploy": "npm run build && npm run demos:build && surge demos --domain shimport-demos.surge.sh",
"demos:build": "cp index.js demos/shimport.js && cp index.dev.js demos/shimport.dev.js && node demos/build.js",
Expand Down
11 changes: 5 additions & 6 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import typescript from '@rollup/plugin-typescript';
import buble from '@rollup/plugin-buble';
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';
import pkg from './package.json';

const config = dev => ({
input: 'src/index.ts',
input: 'src/index.js',
output: [
{
file: dev ? 'index.dev.js' : pkg.main,
format: 'iife',
name: '__shimport__'
name: '__shimport__',
esModule: false
}
],
plugins: [
replace({
__VERSION__: pkg.version
}),
typescript({
typescript: require('typescript')
}),
buble(),
!dev && terser({
output: {
comments: false
Expand Down
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ts-check
import { transform } from './transform.js';
import { define, load } from './load.js';

if (typeof document !== 'undefined') {
const scr = document.querySelector('[data-main]');
if (scr) {
load(new URL(scr.getAttribute('data-main'), document.baseURI).toString());
}
}

const VERSION = "__VERSION__";

export { transform, define, load, VERSION };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[comment]
would be nice to see this diff as a git mv, but minimal changes so it looks fine

17 changes: 0 additions & 17 deletions src/index.ts

This file was deleted.

67 changes: 67 additions & 0 deletions src/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// @ts-check
import { transform } from './transform.js';

/** @type {Record<string, Promise<any>>} */
const promises = {};

/** @typedef {(id: string) => Promise<any>} __Import */
/** @typedef {Record<string, any>} __Exports */

/**
* @param {string} id
* @param {string[]} deps
* @param {(__import: __Import, __exports: __Exports, ...deps: any[]) => void} factory
*/
export function define(id, deps, factory) {
const __import = (dep) => load(new URL(dep, id).toString());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[comment]
just making a note that toString was added, but it looks like the typescript version expected a string | URL but then just cast to string, so expected functionality should be the same


return Promise.all(deps.map(__import)).then(__deps => {
const __exports = {};

factory(__import, __exports, ...__deps);
return __exports;
});
}

/**
* @param {string} url
*/
export function load(url) {
return promises[url] || (
promises[url] = fetch(url)
.then(r => r.text())
.then(text => evaluate(transform(text, url)))
);
}

let uid = 1;

/**
* @param {string} code
*/
function evaluate(code) {
if (typeof document !== 'undefined' && typeof URL !== 'undefined') {
return new Promise(fulfil => {
const id = `__shimport__${uid++}`;

// creating a script tag gives us proper stack traces
const blob = new Blob([`${id}=${code}`], {
type: 'application/javascript'
});

const script = document.createElement('script');
script.src = URL.createObjectURL(blob);

script.onload = () => {
fulfil(window[id]);
delete window[id];
};

document.head.appendChild(script);
});

} else {
// for browsers without `URL`
return (0,eval)(code);
}
}
58 changes: 0 additions & 58 deletions src/load.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Loading