Skip to content

Commit

Permalink
fix: ensure full URLs for unnamed imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Apr 11, 2019
1 parent 2d23896 commit 2314f61
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ function run(url, str) {

var txt = imports(
// Ensure full URLs & Gather static imports
str.replace(/(import\s*.*\s*from\s*)['"]([^'"]+)['"];?/gi, function (_, state, loc) {
loc = "'" + toURL(loc, url) + "'";
return state + (/\s*from\s*/.test(state) ? "'$dimport[" + (urls.push(loc) - 1) + "]';" : loc + ";");
str.replace(/(^|\s|;)(import\s*)(\(|.*from\s*|)['"]([^'"]+)['"];?/gi, function (_, pre, req, type, loc) {
loc = "'" + new URL(loc, url).href + "'";
return pre + req + type + (type == '(' ? loc : `'$dimport[${urls.push(loc) - 1}]';`);
})

// Attach ourself for dynamic imports
// Ensure we caught all dynamics (multi-line clause)
.replace(/(^|\s|;)(import)(?=\()/g, '$1window.dimport')

// Exports
Expand Down
4 changes: 2 additions & 2 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function toBlob(url, txt) {
return URL.createObjectURL(
new Blob([
// Ensure full URLs & rewrite dynamic imports
txt.replace(/(^|\s|;)(import\s*\(|import\s*.*\s*from\s*)['"]([^'"]+)['"]/gi, function (_, pre, req, loc) {
return pre + (/\s+from\s*/.test(req) ? req : 'window.dimport(') + "'" + new URL(loc, url) + "'";
txt.replace(/(^|\s|;)(import\s*)(\(|.*from\s*|)['"]([^'"]+)['"]/gi, function (_, pre, req, type, loc) {
return pre + (type == '(' ? 'window.dimport' : req) + type + "'" + new URL(loc, url) + "'";
})
// Ensure we caught all dynamics (multi-line clauses)
.replace(/(^|\s|;)(import)(?=\()/g, '$1window.dimport')
Expand Down
6 changes: 3 additions & 3 deletions src/nomodule.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ function run(url, str) {

var txt = imports(
// Ensure full URLs & Gather static imports
str.replace(/(import\s*.*\s*from\s*)['"]([^'"]+)['"];?/gi, function (_, state, loc) {
str.replace(/(^|\s|;)(import\s*)(\(|.*from\s*|)['"]([^'"]+)['"];?/gi, function (_, pre, req, type, loc) {
loc = "'" + new URL(loc, url).href + "'";
return state + (/\s*from\s*/.test(state) ? "'$dimport[" + (urls.push(loc) - 1) + "]';" : loc + ";");
return pre + req + type + (type == '(' ? loc : `'$dimport[${urls.push(loc) - 1}]';`);
})

// Attach ourself for dynamic imports
// Ensure we caught all dynamics (multi-line clause)
.replace(/(^|\s|;)(import)(?=\()/g, '$1window.dimport')

// Exports
Expand Down

0 comments on commit 2314f61

Please sign in to comment.