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

Replace UMD importing style with ESM style through import map #208

Merged
merged 1 commit into from
Aug 30, 2024
Merged
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
25 changes: 22 additions & 3 deletions repl/importmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
function importFile(content) {
return "data:text/javascript;base64," + btoa(content);
}
const nodeModules = './node_modules/';
const nodeModules = location.href + './node_modules/';
// For React ESM importmap to work you need React ESM: npm i react-es6
const react = {
'prop-types' : nodeModules + 'react-es6/prop-types/index.js',
Expand All @@ -27,14 +27,33 @@ const reactMin = {
const imports = {
"@runtime-type-inspector/runtime" : '../src-runtime/index.js',
"@runtime-type-inspector/transpiler": '../src-transpiler/index.js',
"@babel/parser" : "./babel-parser.js",
"@babel/parser" : importFile(`
// Note: this is still ugly, but Babel 8 should be "nice" ESM (when it's out)
// Test: const {parse} = await import("@babel/parser");
// Now we have this, but requires never npm version:
// https://github.com/kungfooman/babel-8-via-importmap/blob/main/importmap.js
globalThis.exports = {};
const ret = await import("${nodeModules}@babel/parser/lib/index.js");
export const {parse, parseExpression, tokTypes} = globalThis.exports;
export default exports;
`),
"display-anything" : "./node_modules/display-anything/src/index.js",
"worker-with-import-map" : "./node_modules/worker-with-import-map/src/index.js",
"test-import-validation-b" : "../test/typechecking/import-validation/b.js",
//"@babel/helper-plugin-utils" : "./babel-helper-plugin-utils.js",
//"@babel/plugin-syntax-typescript" : "./babel-plugin-syntax-typescript.js",
"fs" : importFile("export default {};"),
"typescript" : importFile("export default ts;"), // UMD import
"typescript" : importFile(`
globalThis.module = {exports: {}};
await import("${nodeModules}typescript/lib/typescript.js");
export default globalThis.module.exports;
`),
"ace-builds": importFile(`
await import("${nodeModules}ace-builds/src/ace.js");
window.ace.config.set('basePath', "${nodeModules}ace-builds/src");
const {ace} = window;
export {ace};
`),
...reactMin,
};
if (location.host.includes('runtimetypeinspector.org') || location.port === '7000') {
Expand Down
2 changes: 0 additions & 2 deletions repl/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<html>
<head>
<script src="importmap.js"></script>
<script src="node_modules/ace-builds/src/ace.js"></script>
<script src="node_modules/typescript/lib/typescript.js"></script>
<link rel="stylesheet" href="./node_modules/display-anything/src/DisplayAnything.css"></link>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions repl/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {parse} from '@babel/parser';
import {ace} from 'ace-builds';
import {
parseJSDoc,
addTypeChecks,
Expand Down