Skip to content

Commit

Permalink
Merge pull request #48 from shgysk8zer0/bug/42
Browse files Browse the repository at this point in the history
Update to treat file paths as `file:`  URLs
  • Loading branch information
shgysk8zer0 authored Oct 6, 2023
2 parents 18996b4 + 75222e4 commit 9a00454
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ test/
.nvmrc
importmap.json
importmap.yaml
importmap.yml
rollup.config.js
test.config.js
*.tgz
*.log
*.bak
*.min.js
*.map

8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v1.2.2] - 2023-10-05

### Changed
- Handle file paths as `file:` URLs to avoid inconsistency

### Fixed
- Fixed handling Windows file paths [shgysk8zer0/npm-utils#19](https://github.com/shgysk8zer0/npm-utils/pull/19)

## [v1.2.1] - 2023-10-02

### Changed
Expand Down
18 changes: 12 additions & 6 deletions import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
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 { ROOT } from '@shgysk8zer0/npm-utils/consts';
import { buildImportmap, getInvalidMapError, resolveImport } from '@shgysk8zer0/npm-utils/importmap';
import { isString, isBare } from '@shgysk8zer0/npm-utils/utils';
import { JS as JS_MIME } from '@shgysk8zer0/consts/mimes';
import { pathToURL } from '@shgysk8zer0/npm-utils/url';
import { dirname } from '@shgysk8zer0/npm-utils/path';

const JS_MIMES = ['text/javascript', JS_MIME];

Expand Down Expand Up @@ -33,11 +35,11 @@ export function rollupImport(importMaps = []) {
name: '@shgysk8zer0/rollup-import',
async load(path) {
if (cached.has(path)) {
return cached.get(path);
return new URL(cached.get(path));
} else {
switch(new URL(path).protocol) {
case 'file:':
return readFile(path.replace('file://', '')).then(content => {
return readFile(path).then(content => {
cached.set(path, content);
return content;
});
Expand Down Expand Up @@ -79,13 +81,17 @@ export function rollupImport(importMaps = []) {
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 };
return { id: new URL(id, ROOT.href).href, external: false };
} else if (isBare(id)) {
const match = resolveImport(id, importmap, { base: src });
const match = resolveImport(id, importmap);

return match instanceof URL ? { id: match.href, external: false } : null;
if (match instanceof URL) {
return { id: match.href, external: false };
} else {
return null;
}
} else {
return { id: pathToURL(id, src).href, external: false };
return { id: pathToURL(id, dirname(src)).href, external: false };
}
},
};
Expand Down
10 changes: 0 additions & 10 deletions importmap.json

This file was deleted.

7 changes: 0 additions & 7 deletions importmap.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions importmap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
imports:
'@shgysk8zer0/kazoo/': https://unpkg.com/@shgysk8zer0/[email protected]/
'@shgysk8zer0/konami': https://unpkg.com/@shgysk8zer0/[email protected]/konami.js
'@shgysk8zer0/polyfills': https://unpkg.com/@shgysk8zer0/[email protected]/all.min.js
'@shgysk8zer0/polyfills/': https://unpkg.com/@shgysk8zer0/[email protected]/
'@shgysk8zer0/jswaggersheets': https://unpkg.com/@shgysk8zer0/[email protected]/swagger.js
'@shgysk8zer0/jswaggersheets/': https://unpkg.com/@shgysk8zer0/[email protected]/
'@shgysk8zer0/jss/': https://unpkg.com/@shgysk8zer0/[email protected]/
'@shgysk8zer0/http-status': https://unpkg.com/@shgysk8zer0/[email protected]/http-status.js
'@shgysk8zer0/components/': https://unpkg.com/@shgysk8zer0/[email protected]/
'@kernvalley/components/': https://unpkg.com/@kernvalley/[email protected]/
'@webcomponents/custom-elements': >-
https://unpkg.com/@webcomponents/[email protected]/custom-elements.min.js
leaflet: https://unpkg.com/[email protected]/dist/leaflet-src.esm.js
firebase/: https://www.gstatic.com/firebasejs/9.23.0/
urlpattern-polyfill: https://unpkg.com/[email protected]/index.js
highlight.js: https://unpkg.com/@highlightjs/[email protected]/es/highlight.min.js
highlight.js/: https://unpkg.com/@highlightjs/[email protected]/
marked: https://unpkg.com/[email protected]/lib/marked.esm.js
marked-highlight: https://unpkg.com/[email protected]/src/index.js
17 changes: 8 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shgysk8zer0/rollup-import",
"version": "1.2.1",
"version": "1.2.2",
"description": "A RollUp plugin for importing modules from URLs, paths, and bare specifiers using import maps.",
"type": "module",
"engines": {
Expand Down Expand Up @@ -64,7 +64,7 @@
"homepage": "https://github.com/shgysk8zer0/rollup-import#readme",
"dependencies": {
"@shgysk8zer0/consts": "^1.0.7",
"@shgysk8zer0/npm-utils": "^1.0.5",
"@shgysk8zer0/npm-utils": "^1.1.2",
"magic-string": "^0.30.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions test.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { rollupImport } from './import.js';
import { rollupImportMeta } from './meta.js';
// import { config } from 'dotenv';
import { getConfig } from '@shgysk8zer0/js-utils/rollup';
// config();

export default getConfig('./test/index.js', {
format: 'cjs',
plugins: [
rollupImport(['importmap.json']),
rollupImport(['importmap.yml']),
rollupImportMeta({
baseURL: 'https://example.com/',
}),
Expand Down

0 comments on commit 9a00454

Please sign in to comment.