Skip to content

Commit

Permalink
chore: set up eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Jul 10, 2024
1 parent 9d62f7f commit 2355bc2
Show file tree
Hide file tree
Showing 15 changed files with 441 additions and 368 deletions.
6 changes: 0 additions & 6 deletions .eslintignore

This file was deleted.

16 changes: 0 additions & 16 deletions .eslintrc

This file was deleted.

29 changes: 29 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import prettierConfig from "eslint-config-prettier";

Check failure on line 1 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
import prettierPlugin from "eslint-plugin-prettier/recommended";

Check failure on line 2 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
import globals from "globals";

Check failure on line 3 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
import js from "@eslint/js";

Check failure on line 4 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`

Check failure on line 5 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
export default [

Check failure on line 6 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
js.configs.recommended,

Check failure on line 7 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
prettierConfig,

Check failure on line 8 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
prettierPlugin,

Check failure on line 9 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
{

Check failure on line 10 in eslint.config.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Delete `␍`
languageOptions: {
globals: {
...globals.node,
...globals.browser,
global: true,
},
},
},
{
ignores: [
"tap-snapshots/*",
"node_modules/*",
"modules/*",
"utils/*",
"dist/*",
"tmp/*",
],
},
];
4 changes: 2 additions & 2 deletions fixtures/modules/basic/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { html } from 'lit-element';
import { html } from "lit-element";

const render = (world) => {
return html`<p>Hello ${world}!</p>`;
return html`<p>Hello ${world}!</p>`;
};
render();
24 changes: 15 additions & 9 deletions fixtures/modules/file/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { html } from 'lit-html/lit-html';
import { css } from 'lit-html';
import { LitElement } from 'lit-element';
import { html } from "lit-html/lit-html";
import { css } from "lit-html";
import { LitElement } from "lit-element";

export default class Inner extends LitElement {
static get styles() {
return [css`:host { color: red; }`];
}
static get styles() {
return [
css`
:host {
color: red;
}
`,
];
}

render(world) {
return html`<p>Hello ${world}!</p>`;
}
render(world) {
return html`<p>Hello ${world}!</p>`;
}
}
32 changes: 16 additions & 16 deletions fixtures/modules/simple/app/app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { replaceElement } from '../utils/dom.js';
import view from './views.js';
import data from '../data/data.js';
import { replaceElement } from "../utils/dom.js";
import view from "./views.js";
import data from "../data/data.js";

export default class App {
constructor(root) {
this.root = root;
}
constructor(root) {
this.root = root;
}

render() {
const items = data();
const el = view(items);
this.root = replaceElement(this.root, el);
}
render() {
const items = data();
const el = view(items);
this.root = replaceElement(this.root, el);
}

update() {
setInterval(() => {
this.render();
}, 1000);
}
update() {
setInterval(() => {
this.render();
}, 1000);
}
}
4 changes: 2 additions & 2 deletions fixtures/modules/simple/app/views.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { html, css } from 'lit-element';
import { html } from "lit-element";

export default function view(items) {
return html`<p>Hello ${items[0]}!</p>`;
return html`<p>Hello ${items[0]}!</p>`;
}
16 changes: 8 additions & 8 deletions fixtures/modules/simple/data/data.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function random(min, max) {
return Math.floor(min + Math.random() * (max + 1 - min));
return Math.floor(min + Math.random() * (max + 1 - min));
}

export default function data() {
return [
random(0, 20),
random(20, 40),
random(40, 60),
random(60, 80),
random(80, 100),
];
return [
random(0, 20),
random(20, 40),
random(40, 60),
random(60, 80),
random(80, 100),
];
}
22 changes: 11 additions & 11 deletions fixtures/modules/simple/main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { firstElement } from './utils/dom.js';
import App from './app/app.js';
import { firstElement } from "./utils/dom.js";
import App from "./app/app.js";

const ready = () => {
return new Promise((resolve) => {
document.addEventListener('DOMContentLoaded', () => {
const el = document.getElementById('app');
resolve(firstElement(el));
});
return new Promise((resolve) => {
document.addEventListener("DOMContentLoaded", () => {
const el = document.getElementById("app");
resolve(firstElement(el));
});
});
};

const start = async () => {
const el = await ready();
const app = new App(el);
app.render();
app.update();
const el = await ready();
const app = new App(el);
app.render();
app.update();
};
start();
6 changes: 3 additions & 3 deletions fixtures/modules/simple/utils/dom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export function replaceElement(target, element) {
target.replaceWith(element);
return element;
target.replaceWith(element);
return element;
}

export function firstElement(element) {
return element.firstElementChild;
return element.firstElementChild;
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"scripts": {
"prepare": "npm run -s build",
"test": "tap --disable-coverage --allow-empty-coverage",
"lint": "eslint . --ext=js",
"lint:fix": "eslint . --fix --ext=js",
"lint": "eslint .",
"start": "node --experimental-modules ./example/server.mjs",
"build": "rollup -c"
},
Expand All @@ -46,10 +45,11 @@
"devDependencies": {
"@semantic-release/changelog": "6.0.3",
"@semantic-release/git": "10.0.1",
"eslint": "8.57.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.29.1",
"eslint": "9.6.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"fastify": "4.28.1",
"globals": "15.8.0",
"rollup": "4.18.0",
"semantic-release": "23.1.1",
"tap": "20.0.3"
Expand Down
14 changes: 3 additions & 11 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
export default {
input: 'src/plugin.js',
external: [
'rollup-plugin-import-map',
'undici',
'path',
'url',
'fs',
],
output: [
{ file: 'dist/plugin.cjs', format: 'cjs' },
],
input: "src/plugin.js",
external: ["rollup-plugin-import-map", "undici", "path", "url", "fs"],
output: [{ file: "dist/plugin.cjs", format: "cjs" }],
};
113 changes: 55 additions & 58 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
/* eslint-disable no-restricted-syntax */

import { rollupImportMapPlugin as importMapPlugin } from 'rollup-plugin-import-map';
import { helpers } from '@eik/common';
import { request } from 'undici';
import { rollupImportMapPlugin as importMapPlugin } from "rollup-plugin-import-map";
import { helpers } from "@eik/common";
import { request } from "undici";

const fetchImportMaps = async (urls = []) => {
try {
const maps = urls.map(async (map) => {
const {
statusCode,
body,
} = await request(map, { maxRedirections: 2 });
try {
const maps = urls.map(async (map) => {
const { statusCode, body } = await request(map, {
maxRedirections: 2,
});

if (statusCode === 404) {
throw new Error('Import map could not be found on server');
} else if (statusCode >= 400 && statusCode < 500) {
throw new Error('Server rejected client request');
} else if (statusCode >= 500) {
throw new Error('Server error');
}
return body.json();
});
return await Promise.all(maps);
} catch (err) {
throw new Error(
`Unable to load import map file from server: ${err.message}`,
);
}
if (statusCode === 404) {
throw new Error("Import map could not be found on server");
} else if (statusCode >= 400 && statusCode < 500) {
throw new Error("Server rejected client request");
} else if (statusCode >= 500) {
throw new Error("Server error");
}
return body.json();
});
return await Promise.all(maps);
} catch (err) {
throw new Error(
`Unable to load import map file from server: ${err.message}`,
);
}
};

export default function esmImportToUrl({
path = process.cwd(),
maps = [],
urls = [],
path = process.cwd(),
maps = [],
urls = [],
} = {}) {
const pMaps = Array.isArray(maps) ? maps : [maps];
const pUrls = Array.isArray(urls) ? urls : [urls];
let plugin;
const pMaps = Array.isArray(maps) ? maps : [maps];
const pUrls = Array.isArray(urls) ? urls : [urls];
let plugin;

return {
name: 'eik-rollup-plugin',
return {
name: "eik-rollup-plugin",

async buildStart(options) {
// Load eik config from eik.json or package.json
const config = await helpers.getDefaults(path);
this.debug(`Loaded eik config ${JSON.stringify(config, null, 2)}`);
async buildStart(options) {
// Load eik config from eik.json or package.json
const config = await helpers.getDefaults(path);
this.debug(`Loaded eik config ${JSON.stringify(config, null, 2)}`);

// Fetch import maps from the server
try {
const fetched = await fetchImportMaps([...config.map, ...pUrls]);
for (const map of fetched) {
this.debug(`Fetched import map ${JSON.stringify(map, null, 2)}`);
}
plugin = importMapPlugin([...fetched, ...pMaps]);
await plugin.buildStart(options);
} catch (err) {
this.error(err.message);
}
},
// Fetch import maps from the server
try {
const fetched = await fetchImportMaps([...config.map, ...pUrls]);
for (const map of fetched) {
this.debug(`Fetched import map ${JSON.stringify(map, null, 2)}`);
}
plugin = importMapPlugin([...fetched, ...pMaps]);
await plugin.buildStart(options);
} catch (err) {
this.error(err.message);
}
},

resolveId(importee) {
const resolved = plugin.resolveId(importee);
if (resolved) {
this.debug(`Resolved ${importee} to ${resolved.id}`);
}
return resolved;
},
};
resolveId(importee) {
const resolved = plugin.resolveId(importee);
if (resolved) {
this.debug(`Resolved ${importee} to ${resolved.id}`);
}
return resolved;
},
};
}
Loading

0 comments on commit 2355bc2

Please sign in to comment.