Skip to content

Commit

Permalink
fixed an issue with the currentScript not spotted before
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Aug 5, 2024
1 parent a9577a9 commit 9e13dfe
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions esm/custom.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@ungap/with-resolvers';
import { $$ } from 'basic-devtools';

import { JSModules, assign, create, createOverload, createResolved, dedent, defineProperty, nodeInfo, registerJSModules } from './utils.js';
import { JSModules, isSync, assign, create, createOverload, createResolved, dedent, defineProperty, nodeInfo, registerJSModules } from './utils.js';
import { getDetails } from './script-handler.js';
import { registry as defaultRegistry, prefixes, configs } from './interpreters.js';
import { getRuntimeID } from './loader.js';
Expand Down Expand Up @@ -69,7 +69,7 @@ export const handleCustomType = async (node) => {
type: runtime,
custom: type,
config: node.getAttribute('config') || config || {},
async: node.getAttribute('async') !== 'false',
async: !isSync(node),
serviceWorker: node.getAttribute('service-worker'),
});
defineProperty(node, 'xworker', { value: xworker });
Expand Down
23 changes: 11 additions & 12 deletions esm/script-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import $xworker from './worker/class.js';
import workerURL from './worker/url.js';
import { getRuntime, getRuntimeID } from './loader.js';
import { registry } from './interpreters.js';
import { JSModules, all, dispatch, resolve, defineProperty, nodeInfo, registerJSModules } from './utils.js';
import { JSModules, isSync, all, dispatch, resolve, defineProperty, nodeInfo, registerJSModules } from './utils.js';

const getRoot = (script) => {
let parent = script;
Expand Down Expand Up @@ -55,12 +55,6 @@ const execute = async (currentScript, source, XWorker, isAsync) => {
source,
]);
try {
// temporarily override inherited document.currentScript in a non writable way
// but it deletes it right after to preserve native behavior (as it's sync: no trouble)
defineProperty(document, 'currentScript', {
configurable: true,
get: () => currentScript,
});
registerJSModules(type, module, interpreter, JSModules);
module.registerJSModule(interpreter, 'polyscript', {
XWorker,
Expand All @@ -69,10 +63,16 @@ const execute = async (currentScript, source, XWorker, isAsync) => {
workers: workersHandler,
});
dispatch(currentScript, type, 'ready');
const result = module[isAsync ? 'runAsync' : 'run'](interpreter, content);
// temporarily override inherited document.currentScript in a non writable way
// but it deletes it right after to preserve native behavior (as it's sync: no trouble)
defineProperty(document, 'currentScript', {
configurable: true,
get: () => currentScript,
});
const done = dispatch.bind(null, currentScript, type, 'done');
if (isAsync) result.then(done);
else done();
let result = module[isAsync ? 'runAsync' : 'run'](interpreter, content);
if (isAsync) result = await result;
done();
return result;
} finally {
delete document.currentScript;
Expand Down Expand Up @@ -125,7 +125,6 @@ export const handle = async (script) => {
// and/or source code with different config or interpreter
const {
attributes: {
async: asyncAttribute,
config,
env,
name: wn,
Expand All @@ -138,7 +137,7 @@ export const handle = async (script) => {
} = script;

/* c8 ignore start */
const isAsync = asyncAttribute?.value !== 'false';
const isAsync = !isSync(script);

const versionValue = version?.value;
const name = getRuntimeID(type, versionValue);
Expand Down
4 changes: 4 additions & 0 deletions esm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export const importCSS = href => new Promise((onload, onerror) => {
});

export const isCSS = source => /\.css$/i.test(new URL(source).pathname);

export const isSync = element =>
/^(?:false|0|no)$/i.test(element.getAttribute('async'));

/* c8 ignore stop */

export {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polyscript",
"version": "0.14.5",
"version": "0.14.6-rc1",
"description": "PyScript single core to rule them all",
"main": "./esm/index.js",
"types": "./types/polyscript/esm/index.d.ts",
Expand Down

0 comments on commit 9e13dfe

Please sign in to comment.