-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #24 from shgysk8zer0/feature/import-meta
Add support for `import.meta.url` & `import.meta.resolve()`
- Loading branch information
Showing
13 changed files
with
626 additions
and
171 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
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 |
---|---|---|
|
@@ -133,4 +133,5 @@ dist | |
*.out.js | ||
*.min.js | ||
*.cjs | ||
*.map.* | ||
*.map | ||
*bak |
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
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,90 @@ | ||
/* eslint-env node */ | ||
import { fileExists, readFile } from '@shgysk8zer0/npm-utils/fs'; | ||
import { readYAMLFile, isYAMLFile } from '@shgysk8zer0/npm-utils/yaml'; | ||
import { readJSONFile, isJSONFile } from '@shgysk8zer0/npm-utils/json'; | ||
import { buildImportmap, getInvalidMapError, resolveImport } from '@shgysk8zer0/npm-utils/importmap'; | ||
import { isString, isBare } from '@shgysk8zer0/npm-utils/utils'; | ||
import { JS as JS_MIMES } from '@shgysk8zer0/npm-utils/mimes'; | ||
import { pathToURL } from '@shgysk8zer0/npm-utils/url'; | ||
|
||
const isJS = type => JS_MIMES.includes(type.toLowerCase()); | ||
|
||
const cached = new Map(); | ||
|
||
async function getFile(pathname) { | ||
if (! await fileExists(pathname)) { | ||
throw new Error(`${pathname} not found.`); | ||
} else if (isYAMLFile(pathname)) { | ||
return readYAMLFile(pathname); | ||
} else if (isJSONFile(pathname)) { | ||
return readJSONFile(pathname); | ||
} else { | ||
throw new TypeError(`Unsupported file type for ${pathname}.`); | ||
} | ||
} | ||
|
||
export function rollupImport(importMaps = []) { | ||
const importmap = new Map(); | ||
const maps = Array.isArray(importMaps) ? importMaps : [importMaps]; | ||
|
||
return { | ||
name: '@shgysk8zer0/rollup-import', | ||
async load(path) { | ||
if (cached.has(path)) { | ||
return cached.get(path); | ||
} else { | ||
switch(new URL(path).protocol) { | ||
case 'file:': | ||
return readFile(path.replace('file://', '')).then(content => { | ||
cached.set(path, content); | ||
return content; | ||
}); | ||
|
||
case 'https:': | ||
case 'http:': | ||
return fetch(path).then(async resp => { | ||
if (! resp.ok) { | ||
throw new Error(`<${path}> [${resp.status} ${resp.statusText}]`); | ||
} else if (! isJS(resp.headers.get('Content-Type').split(';')[0].trim())) { | ||
throw new TypeError(`Expected 'application/javascript' but got ${resp.headers.get('Content-Type')}`); | ||
} else { | ||
const content = await resp.text(); | ||
cached.set(path, content); | ||
return content; | ||
} | ||
}); | ||
|
||
default: | ||
throw new TypeError(`Unsupported protocol "${path}."`); | ||
} | ||
} | ||
}, | ||
async buildStart(options) { | ||
if (typeof options !== 'undefined') { | ||
const mappings = maps.map(entry => isString(entry) | ||
? getFile(entry, options) // Load from file | ||
: entry // Use the Object | ||
); | ||
|
||
await buildImportmap(importmap, mappings); | ||
const err = getInvalidMapError(importmap); | ||
|
||
if (err instanceof Error) { | ||
throw err; | ||
} | ||
} | ||
}, | ||
resolveId(id, src, { /*assertions, custom,*/ isEntry }) { | ||
// @TODO: Store `options.external` and use for return value? | ||
if (isEntry) { | ||
return { id: new URL(id, `file://${process.cwd()}/`).href, external: false }; | ||
} else if (isBare(id)) { | ||
const match = resolveImport(id, importmap, { base: src }); | ||
|
||
return match instanceof URL ? { id: match.href, external: false } : null; | ||
} else { | ||
return { id: pathToURL(id, src).href, external: false }; | ||
} | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,91 +1,2 @@ | ||
/* eslint-env node */ | ||
import { fileExists, readFile } from '@shgysk8zer0/npm-utils/fs'; | ||
import { readYAMLFile, isYAMLFile } from '@shgysk8zer0/npm-utils/yaml'; | ||
import { readJSONFile, isJSONFile } from '@shgysk8zer0/npm-utils/json'; | ||
import { buildImportmap, getInvalidMapError, resolveImport } from '@shgysk8zer0/npm-utils/importmap'; | ||
import { isString, isBare } from '@shgysk8zer0/npm-utils/utils'; | ||
import { JS as JS_MIMES } from '@shgysk8zer0/npm-utils/mimes'; | ||
import { pathToURL } from '@shgysk8zer0/npm-utils/url'; | ||
|
||
const isJS = type => JS_MIMES.includes(type.toLowerCase()); | ||
|
||
const cached = new Map(); | ||
|
||
async function getFile(pathname) { | ||
if (! await fileExists(pathname)) { | ||
throw new Error(`${pathname} not found.`); | ||
} else if (isYAMLFile(pathname)) { | ||
return readYAMLFile(pathname); | ||
} else if (isJSONFile(pathname)) { | ||
return readJSONFile(pathname); | ||
} else { | ||
throw new TypeError(`Unsupported file type for ${pathname}.`); | ||
} | ||
} | ||
|
||
export function rollupImport(importMaps = []) { | ||
const importmap = new Map(); | ||
const maps = Array.isArray(importMaps) ? importMaps : [importMaps]; | ||
|
||
return { | ||
name: '@shgysk8zer0/rollup-import', | ||
async load(path) { | ||
if (cached.has(path)) { | ||
return cached.get(path); | ||
} else { | ||
switch(new URL(path).protocol) { | ||
case 'file:': | ||
return readFile(path.replace('file://', '')).then(content => { | ||
cached.set(path, content); | ||
return content; | ||
}); | ||
|
||
case 'https:': | ||
case 'http:': | ||
return fetch(path).then(async resp => { | ||
// console.log({ url: resp.url, status: resp.status, ok: resp.ok }); | ||
if (! resp.ok) { | ||
throw new Error(`<${path}> [${resp.status} ${resp.statusText}]`); | ||
} else if (! isJS(resp.headers.get('Content-Type').split(';')[0].trim())) { | ||
throw new TypeError(`Expected 'application/javascript' but got ${resp.headers.get('Content-Type')}`); | ||
} else { | ||
const content = await resp.text(); | ||
cached.set(path, content); | ||
return content; | ||
} | ||
}); | ||
|
||
default: | ||
throw new TypeError(`Unsupported protocol "${path.protocol}."`); | ||
} | ||
} | ||
}, | ||
async buildStart(options) { | ||
if (typeof options !== 'undefined') { | ||
const mappings = maps.map(entry => isString(entry) | ||
? getFile(entry, options) // Load from file | ||
: entry // Use the Object | ||
); | ||
|
||
await buildImportmap(importmap, mappings); | ||
const err = getInvalidMapError(importmap); | ||
|
||
if (err instanceof Error) { | ||
throw err; | ||
} | ||
} | ||
}, | ||
resolveId(id, src, { /*assertions, custom,*/ isEntry }) { | ||
// @TODO: Store `options.external` and use for return value? | ||
if (isEntry) { | ||
return { id: new URL(id, `file://${process.cwd()}/`).href, external: false }; | ||
} else if (isBare(id)) { | ||
const match = resolveImport(id, importmap, { base: src }); | ||
|
||
return match instanceof URL ? { id: match.href, external: false } : null; | ||
} else { | ||
return { id: pathToURL(id, src).href, external: false }; | ||
} | ||
}, | ||
}; | ||
} | ||
export { rollupImport } from './import.js'; | ||
export { rollupImportMeta } from './meta.js'; |
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,48 @@ | ||
import MagicString from 'magic-string'; | ||
import { validateURL } from '@shgysk8zer0/npm-utils/url'; | ||
|
||
function transformSource(source, id, mapping) { | ||
const ms = new MagicString(source); | ||
|
||
Object.entries(mapping).forEach(([search, replace]) => ms.replaceAll(search, replace)); | ||
|
||
if (ms.hasChanged()) { | ||
const code = ms.toString(); | ||
const map = ms.generateMap({ source: id }); | ||
|
||
return { code, map }; | ||
} | ||
} | ||
|
||
export function rollupImportMeta({ | ||
baseURL = process?.env?.URL, | ||
projectRoot = new URL(`${process.cwd()}/`, 'file://').href, | ||
} = {}) { | ||
if (! validateURL(baseURL)) { | ||
throw new TypeError(`baseURL: "${baseURL}"" is not a valid URL.`); | ||
} else if (! validateURL(projectRoot)) { | ||
throw new TypeError(`projectRoot: "${projectRoot} is not a valid URL."`); | ||
} else { | ||
return { | ||
transform(source, id) { | ||
if (! source.includes('import.meta')) { | ||
return; | ||
} else if (id.startsWith('https:')) { | ||
return transformSource(source, id, { | ||
'${import.meta.url}': id, | ||
'import.meta.url': `'${id}'`, | ||
'import.meta.resolve(': `(path => new URL(path, '${id}').href)(`, | ||
}); | ||
} else if (id.startsWith('file:')) { | ||
const url = new URL(id.replace(projectRoot, baseURL)).href; | ||
|
||
return transformSource(source, id, { | ||
'${import.meta.url}': url, | ||
'import.meta.url': `'${url}'`, | ||
'import.meta.resolve(': `(path => new URL(path, '${url}').href)(`, | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.