From 2355bc2cf356d7d25e5a8dff7d5481b7717f5ff5 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Wed, 10 Jul 2024 14:45:35 +0200 Subject: [PATCH] chore: set up eslint --- .eslintignore | 6 - .eslintrc | 16 - eslint.config.js | 29 ++ fixtures/modules/basic/main.js | 4 +- fixtures/modules/file/main.js | 24 +- fixtures/modules/simple/app/app.js | 32 +- fixtures/modules/simple/app/views.js | 4 +- fixtures/modules/simple/data/data.js | 16 +- fixtures/modules/simple/main.js | 22 +- fixtures/modules/simple/utils/dom.js | 6 +- package.json | 10 +- rollup.config.js | 14 +- src/plugin.js | 113 ++++--- tap-snapshots/test/plugin.js.test.cjs | 100 ++++--- test/plugin.js | 413 ++++++++++++++------------ 15 files changed, 441 insertions(+), 368 deletions(-) delete mode 100644 .eslintignore delete mode 100644 .eslintrc create mode 100644 eslint.config.js diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index d7256cb..0000000 --- a/.eslintignore +++ /dev/null @@ -1,6 +0,0 @@ -tap-snapshots -node_modules -modules -utils -dist -tmp \ No newline at end of file diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index b53fbca..0000000 --- a/.eslintrc +++ /dev/null @@ -1,16 +0,0 @@ -{ - "env": { - "es6": true, - "node": true, - "browser": false - }, - "parserOptions": { - "ecmaVersion": 2020 - }, - "extends": "airbnb-base", - "rules": { - "indent": [1, 4], - "import/prefer-default-export": [0], - "import/extensions": [0] - } -} diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..98196d4 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,29 @@ +import prettierConfig from "eslint-config-prettier"; +import prettierPlugin from "eslint-plugin-prettier/recommended"; +import globals from "globals"; +import js from "@eslint/js"; + +export default [ + js.configs.recommended, + prettierConfig, + prettierPlugin, + { + languageOptions: { + globals: { + ...globals.node, + ...globals.browser, + global: true, + }, + }, + }, + { + ignores: [ + "tap-snapshots/*", + "node_modules/*", + "modules/*", + "utils/*", + "dist/*", + "tmp/*", + ], + }, +]; diff --git a/fixtures/modules/basic/main.js b/fixtures/modules/basic/main.js index 382a68b..1ac5232 100644 --- a/fixtures/modules/basic/main.js +++ b/fixtures/modules/basic/main.js @@ -1,6 +1,6 @@ -import { html } from 'lit-element'; +import { html } from "lit-element"; const render = (world) => { - return html`

Hello ${world}!

`; + return html`

Hello ${world}!

`; }; render(); diff --git a/fixtures/modules/file/main.js b/fixtures/modules/file/main.js index 09566ed..1794915 100644 --- a/fixtures/modules/file/main.js +++ b/fixtures/modules/file/main.js @@ -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`

Hello ${world}!

`; - } + render(world) { + return html`

Hello ${world}!

`; + } } diff --git a/fixtures/modules/simple/app/app.js b/fixtures/modules/simple/app/app.js index b042816..787f884 100644 --- a/fixtures/modules/simple/app/app.js +++ b/fixtures/modules/simple/app/app.js @@ -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); + } } diff --git a/fixtures/modules/simple/app/views.js b/fixtures/modules/simple/app/views.js index 2462283..7b354de 100644 --- a/fixtures/modules/simple/app/views.js +++ b/fixtures/modules/simple/app/views.js @@ -1,5 +1,5 @@ -import { html, css } from 'lit-element'; +import { html } from "lit-element"; export default function view(items) { - return html`

Hello ${items[0]}!

`; + return html`

Hello ${items[0]}!

`; } diff --git a/fixtures/modules/simple/data/data.js b/fixtures/modules/simple/data/data.js index d1e16d2..f5a8420 100644 --- a/fixtures/modules/simple/data/data.js +++ b/fixtures/modules/simple/data/data.js @@ -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), + ]; } diff --git a/fixtures/modules/simple/main.js b/fixtures/modules/simple/main.js index 929fc84..8eff3f9 100644 --- a/fixtures/modules/simple/main.js +++ b/fixtures/modules/simple/main.js @@ -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(); diff --git a/fixtures/modules/simple/utils/dom.js b/fixtures/modules/simple/utils/dom.js index 1c822c2..b9ea410 100644 --- a/fixtures/modules/simple/utils/dom.js +++ b/fixtures/modules/simple/utils/dom.js @@ -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; } diff --git a/package.json b/package.json index f0fe591..e57de64 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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" diff --git a/rollup.config.js b/rollup.config.js index f301761..ecf0d4b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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" }], }; diff --git a/src/plugin.js b/src/plugin.js index 2d9ed0e..25f099e 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -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; + }, + }; } diff --git a/tap-snapshots/test/plugin.js.test.cjs b/tap-snapshots/test/plugin.js.test.cjs index 3f4606a..1ff0479 100644 --- a/tap-snapshots/test/plugin.js.test.cjs +++ b/tap-snapshots/test/plugin.js.test.cjs @@ -11,13 +11,19 @@ import { css } from 'lit-html'; import { LitElement } from 'https://cdn.eik.dev/lit-element/v2'; class Inner extends LitElement { - static get styles() { - return [css\`:host { color: red; }\`]; - } - - render(world) { - return html\`

Hello \${world}!

\`; - } + static get styles() { + return [ + css\` + :host { + color: red; + } + \`, + ]; + } + + render(world) { + return html\`

Hello \${world}!

\`; + } } export { Inner as default }; @@ -30,13 +36,19 @@ import { css } from 'lit-html'; import { LitElement } from 'https://cdn.eik.dev/lit-element/v2'; class Inner extends LitElement { - static get styles() { - return [css\`:host { color: red; }\`]; - } - - render(world) { - return html\`

Hello \${world}!

\`; - } + static get styles() { + return [ + css\` + :host { + color: red; + } + \`, + ]; + } + + render(world) { + return html\`

Hello \${world}!

\`; + } } export { Inner as default }; @@ -49,13 +61,19 @@ import { css } from 'lit-html'; import { LitElement } from 'https://cdn.eik.dev/lit-element/v2'; class Inner extends LitElement { - static get styles() { - return [css\`:host { color: red; }\`]; - } - - render(world) { - return html\`

Hello \${world}!

\`; - } + static get styles() { + return [ + css\` + :host { + color: red; + } + \`, + ]; + } + + render(world) { + return html\`

Hello \${world}!

\`; + } } export { Inner as default }; @@ -68,13 +86,19 @@ import { css } from 'https://cdn.eik.dev/lit-html/v1'; import { LitElement } from 'https://cdn.eik.dev/lit-element/v2'; class Inner extends LitElement { - static get styles() { - return [css\`:host { color: red; }\`]; - } - - render(world) { - return html\`

Hello \${world}!

\`; - } + static get styles() { + return [ + css\` + :host { + color: red; + } + \`, + ]; + } + + render(world) { + return html\`

Hello \${world}!

\`; + } } export { Inner as default }; @@ -87,13 +111,19 @@ import { css } from 'https://cdn.eik.dev/lit-html/v1'; import { LitElement } from 'https://cdn.eik.dev/lit-element/v2'; class Inner extends LitElement { - static get styles() { - return [css\`:host { color: red; }\`]; - } - - render(world) { - return html\`

Hello \${world}!

\`; - } + static get styles() { + return [ + css\` + :host { + color: red; + } + \`, + ]; + } + + render(world) { + return html\`

Hello \${world}!

\`; + } } export { Inner as default }; diff --git a/test/plugin.js b/test/plugin.js index da2c27b..0f11be9 100644 --- a/test/plugin.js +++ b/test/plugin.js @@ -1,258 +1,299 @@ -import { rollup } from 'rollup'; -import { URL } from 'node:url'; -import fastify from 'fastify'; -import path from 'node:path'; -import tap from 'tap'; -import fs from 'node:fs'; +import { rollup } from "rollup"; +import { URL } from "node:url"; +import fastify from "fastify"; +import path from "node:path"; +import tap from "tap"; +import fs from "node:fs"; -import plugin from '../src/plugin.js'; +import plugin from "../src/plugin.js"; -const FILE = new URL('../fixtures/modules/file/main.js', import.meta.url).pathname; +const FILE = new URL("../fixtures/modules/file/main.js", import.meta.url) + .pathname; /* * When running tests on Windows, the output code get some extra \r on each line. * Remove these so snapshots work on all OSes. */ -const clean = (str) => str.split('\r').join(''); - -tap.test('plugin() - import map fetched from a URL', async (t) => { - const app = fastify(); - app.server.keepAliveTimeout = 20; - app.get('/one', (request, reply) => { - reply.send({ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v2', - }, - }); +const clean = (str) => str.split("\r").join(""); + +tap.test("plugin() - import map fetched from a URL", async (t) => { + const app = fastify(); + app.server.keepAliveTimeout = 20; + app.get("/one", (request, reply) => { + reply.send({ + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v2", + }, + }); + }); + app.get("/two", (request, reply) => { + reply.send({ + imports: { + "lit-html": "https://cdn.eik.dev/lit-html/v1", + }, }); - app.get('/two', (request, reply) => { - reply.send({ + }); + const address = await app.listen(); + + const options = { + input: FILE, + onwarn: () => { + // Supress logging + }, + plugins: [ + plugin({ + maps: [ + { imports: { - 'lit-html': 'https://cdn.eik.dev/lit-html/v1', + "lit-html/lit-html": "https://cdn.eik.dev/lit-html/v2", }, - }); - }); - const address = await app.listen(); - - const options = { - input: FILE, - onwarn: () => { - // Supress logging - }, - plugins: [plugin({ - maps: [{ - imports: { - 'lit-html/lit-html': 'https://cdn.eik.dev/lit-html/v2', - }, - }], - urls: [`${address}/one`, `${address}/two`], - })], - }; - - const bundle = await rollup(options); - const { output } = await bundle.generate({ format: 'esm' }); - - t.matchSnapshot(clean(output[0].code), 'import maps from urls'); - await app.close(); - t.end(); + }, + ], + urls: [`${address}/one`, `${address}/two`], + }), + ], + }; + + const bundle = await rollup(options); + const { output } = await bundle.generate({ format: "esm" }); + + t.matchSnapshot(clean(output[0].code), "import maps from urls"); + await app.close(); + t.end(); }); -tap.test('plugin() - import map fetched from a URL via eik.json', async (t) => { - const app = fastify(); - app.server.keepAliveTimeout = 20; - app.get('/one', (request, reply) => { - reply.send({ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v2', - 'lit-html': 'https://cdn.eik.dev/lit-html/v1', - 'lit-html/lit-html': 'https://cdn.eik.dev/lit-html/v2', - }, - }); +tap.test("plugin() - import map fetched from a URL via eik.json", async (t) => { + const app = fastify(); + app.server.keepAliveTimeout = 20; + app.get("/one", (request, reply) => { + reply.send({ + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v2", + "lit-html": "https://cdn.eik.dev/lit-html/v1", + "lit-html/lit-html": "https://cdn.eik.dev/lit-html/v2", + }, }); - const address = await app.listen(); - - await fs.promises.writeFile(path.join(process.cwd(), 'eik.json'), JSON.stringify({ - name: 'test', - server: address, - version: '1.0.0', - files: { - '/css': '/src/css/**/*', - '/js': '/src/js/**/*', - }, - 'import-map': `${address}/one`, - })); - - const options = { - input: FILE, - onwarn: () => { - // Supress logging - }, - plugins: [plugin()], - }; - - const bundle = await rollup(options); - const { output } = await bundle.generate({ format: 'esm' }); - - t.matchSnapshot(clean(output[0].code), 'eik.json import-map string'); - await app.close(); - await fs.promises.unlink(path.join(process.cwd(), 'eik.json')); - t.end(); + }); + const address = await app.listen(); + + await fs.promises.writeFile( + path.join(process.cwd(), "eik.json"), + JSON.stringify({ + name: "test", + server: address, + version: "1.0.0", + files: { + "/css": "/src/css/**/*", + "/js": "/src/js/**/*", + }, + "import-map": `${address}/one`, + }), + ); + + const options = { + input: FILE, + onwarn: () => { + // Supress logging + }, + plugins: [plugin()], + }; + + const bundle = await rollup(options); + const { output } = await bundle.generate({ format: "esm" }); + + t.matchSnapshot(clean(output[0].code), "eik.json import-map string"); + await app.close(); + await fs.promises.unlink(path.join(process.cwd(), "eik.json")); + t.end(); }); -tap.test('plugin() - Import map defined through constructor "maps" argument take precedence over import map defined in eik.json', async (t) => { +tap.test( + 'plugin() - Import map defined through constructor "maps" argument take precedence over import map defined in eik.json', + async (t) => { const app = fastify(); app.server.keepAliveTimeout = 20; - app.get('/one', (request, reply) => { - reply.send({ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v1', - }, - }); + app.get("/one", (request, reply) => { + reply.send({ + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v1", + }, + }); }); const address = await app.listen(); - await fs.promises.writeFile(path.join(process.cwd(), 'eik.json'), JSON.stringify({ - name: 'test', + await fs.promises.writeFile( + path.join(process.cwd(), "eik.json"), + JSON.stringify({ + name: "test", server: address, - version: '1.0.0', + version: "1.0.0", files: { - '/css': '/src/css/', - '/js': '/src/js/', + "/css": "/src/css/", + "/js": "/src/js/", }, - 'import-map': `${address}/one`, - })); + "import-map": `${address}/one`, + }), + ); const options = { - input: FILE, - onwarn: () => { - // Supress logging - }, - plugins: [plugin({ - maps: [{ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v2', - }, - }], - })], + input: FILE, + onwarn: () => { + // Supress logging + }, + plugins: [ + plugin({ + maps: [ + { + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v2", + }, + }, + ], + }), + ], }; const bundle = await rollup(options); - const { output } = await bundle.generate({ format: 'esm' }); + const { output } = await bundle.generate({ format: "esm" }); - t.matchSnapshot(clean(output[0].code), 'Should rewrite import statement to https://cdn.eik.dev/lit-element/v2'); + t.matchSnapshot( + clean(output[0].code), + "Should rewrite import statement to https://cdn.eik.dev/lit-element/v2", + ); await app.close(); - await fs.promises.unlink(path.join(process.cwd(), 'eik.json')); + await fs.promises.unlink(path.join(process.cwd(), "eik.json")); t.end(); -}); + }, +); -tap.test('plugin() - Import map defined through constructor "urls" argument take precedence over import map defined in eik.json', async (t) => { +tap.test( + 'plugin() - Import map defined through constructor "urls" argument take precedence over import map defined in eik.json', + async (t) => { const app = fastify(); app.server.keepAliveTimeout = 20; - app.get('/one', (request, reply) => { - reply.send({ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v1', - }, - }); + app.get("/one", (request, reply) => { + reply.send({ + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v1", + }, + }); }); - app.get('/two', (request, reply) => { - reply.send({ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v2', - }, - }); + app.get("/two", (request, reply) => { + reply.send({ + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v2", + }, + }); }); const address = await app.listen(); - await fs.promises.writeFile(path.join(process.cwd(), 'eik.json'), JSON.stringify({ - name: 'test', + await fs.promises.writeFile( + path.join(process.cwd(), "eik.json"), + JSON.stringify({ + name: "test", server: address, - version: '1.0.0', + version: "1.0.0", files: { - '/css': '/src/css/', - '/js': '/src/js/', + "/css": "/src/css/", + "/js": "/src/js/", }, - 'import-map': `${address}/one`, - })); + "import-map": `${address}/one`, + }), + ); const options = { - input: FILE, - onwarn: () => { - // Supress logging - }, - plugins: [plugin({ - urls: [ - `${address}/two`, - ], - })], + input: FILE, + onwarn: () => { + // Supress logging + }, + plugins: [ + plugin({ + urls: [`${address}/two`], + }), + ], }; const bundle = await rollup(options); - const { output } = await bundle.generate({ format: 'esm' }); + const { output } = await bundle.generate({ format: "esm" }); - t.matchSnapshot(clean(output[0].code), 'Should rewrite import statement to https://cdn.eik.dev/lit-element/v2'); + t.matchSnapshot( + clean(output[0].code), + "Should rewrite import statement to https://cdn.eik.dev/lit-element/v2", + ); await app.close(); - await fs.promises.unlink(path.join(process.cwd(), 'eik.json')); + await fs.promises.unlink(path.join(process.cwd(), "eik.json")); t.end(); -}); + }, +); -tap.test('plugin() - Import map defined through constructor "maps" argument take precedence over import map defined through constructor "urls" argument', async (t) => { +tap.test( + 'plugin() - Import map defined through constructor "maps" argument take precedence over import map defined through constructor "urls" argument', + async (t) => { const app = fastify(); app.server.keepAliveTimeout = 20; - app.get('/one', (request, reply) => { - reply.send({ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v0', - }, - }); + app.get("/one", (request, reply) => { + reply.send({ + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v0", + }, + }); }); - app.get('/two', (request, reply) => { - reply.send({ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v1', - }, - }); + app.get("/two", (request, reply) => { + reply.send({ + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v1", + }, + }); }); const address = await app.listen(); - await fs.promises.writeFile(path.join(process.cwd(), 'eik.json'), JSON.stringify({ - name: 'test', + await fs.promises.writeFile( + path.join(process.cwd(), "eik.json"), + JSON.stringify({ + name: "test", server: address, - version: '1.0.0', + version: "1.0.0", files: { - '/css': '/src/css/', - '/js': '/src/js/', + "/css": "/src/css/", + "/js": "/src/js/", }, - 'import-map': `${address}/one`, - })); + "import-map": `${address}/one`, + }), + ); const options = { - input: FILE, - onwarn: () => { - // Supress logging - }, - plugins: [plugin({ - maps: [{ - imports: { - 'lit-element': 'https://cdn.eik.dev/lit-element/v2', - }, - }], - urls: [ - `${address}/two`, - ], - })], + input: FILE, + onwarn: () => { + // Supress logging + }, + plugins: [ + plugin({ + maps: [ + { + imports: { + "lit-element": "https://cdn.eik.dev/lit-element/v2", + }, + }, + ], + urls: [`${address}/two`], + }), + ], }; const bundle = await rollup(options); - const { output } = await bundle.generate({ format: 'esm' }); + const { output } = await bundle.generate({ format: "esm" }); - t.matchSnapshot(clean(output[0].code), 'Should rewrite import statement to https://cdn.eik.dev/lit-element/v2'); + t.matchSnapshot( + clean(output[0].code), + "Should rewrite import statement to https://cdn.eik.dev/lit-element/v2", + ); await app.close(); - await fs.promises.unlink(path.join(process.cwd(), 'eik.json')); + await fs.promises.unlink(path.join(process.cwd(), "eik.json")); t.end(); -}); + }, +);