'use strict'; var fs = require('@shgysk8zer0/npm-utils/fs'); var yaml = require('@shgysk8zer0/npm-utils/yaml'); var json = require('@shgysk8zer0/npm-utils/json'); var consts = require('@shgysk8zer0/npm-utils/consts'); var importmap = require('@shgysk8zer0/npm-utils/importmap'); var utils = require('@shgysk8zer0/npm-utils/utils'); var mimes = require('@shgysk8zer0/consts/mimes'); var url = require('@shgysk8zer0/npm-utils/url'); var node_url = require('node:url'); var node_path = require('node:path'); var MagicString = require('magic-string'); /* eslint-env node */ 'process' in globalThis && process.cwd instanceof Function ? node_url.pathToFileURL(`${process.cwd()}/`) : 'document' in globalThis ? new URL(document.baseURI) : null; const dirname = path => node_path.dirname(path) + '/'; /* eslint-env node */ const JS_MIMES = ['text/javascript', mimes.JS]; const isJS = type => JS_MIMES.includes(type.toLowerCase()); const cached = new Map(); async function getFile(pathname) { if (! await fs.fileExists(pathname)) { throw new Error(`${pathname} not found.`); } else if (yaml.isYAMLFile(pathname)) { return yaml.readYAMLFile(pathname); } else if (json.isJSONFile(pathname)) { return json.readJSONFile(pathname); } else { throw new TypeError(`Unsupported file type for ${pathname}.`); } } function rollupImport(importMaps = []) { const importmap$1 = 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 fs.readFile(path).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 => utils.isString(entry) ? getFile(entry) // Load from file : entry // Use the Object ); await importmap.buildImportmap(importmap$1, mappings); const err = importmap.getInvalidMapError(importmap$1); 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, consts.ROOT.href).href, external: false }; } else if (utils.isBare(id)) { const match = importmap.resolveImport(id, importmap$1); if (match instanceof URL) { return { id: match.href, external: false }; } else { return null; } } else { return { id: url.pathToURL(id, dirname(src)).href, external: false }; } }, }; } /* eslint-env node */ /* global process */ 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 }; } } function rollupImportMeta({ baseURL = process?.env?.URL, projectRoot = new URL(`${process.cwd()}/`, 'file://').href, } = {}) { if (! url.validateURL(baseURL)) { throw new TypeError(`baseURL: "${baseURL}"" is not a valid URL.`); } else if (! url.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)(`, }); } } }; } } exports.rollupImport = rollupImport; exports.rollupImportMeta = rollupImportMeta;