Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update internals to be esm #137

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
os: [ubuntu-latest, macOS-latest]
node-version: [18, 20]
runs-on: ${{ matrix.os }}

Expand Down
107 changes: 1 addition & 106 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,112 +1,7 @@
node_modules
node_modules/**/*
.DS_Store
tmp/**/*
.idea
# Logs
logs
.idea/**/*
.tap
*.iml
*.log
npm-debug.log*
package-lock.json
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"src"
],
"scripts": {
"test": "tap --no-coverage",
"test:snapshot": "TAP_SNAPSHOT=1 tap --no-coverage",
"test": "tap --disable-coverage --allow-empty-coverage",
"test:snapshot": "TAP_SNAPSHOT=1 tap --allow-empty-coverage",
"lint": "eslint ."
},
"repository": {
Expand Down Expand Up @@ -40,12 +40,12 @@
"eslint-plugin-prettier": "5.1.3",
"fastify": "4.28.1",
"globals": "15.8.0",
"semantic-release": "21.1.2",
"tap": "16.3.10",
"semantic-release": "24.0.0",
"tap": "21.0.0",
"memfs": "4.9.3"
},
"dependencies": {
"@eik/common": "3.0.1",
"undici": "5.28.4"
"undici": "6.19.4"
}
}
11 changes: 4 additions & 7 deletions src/builders.cjs → src/builders.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const parsers = require("./parsers.cjs");
import * as parsers from "./parsers.js";

const RX_SIDE_EFFECTS_IMPORT = parsers.sideEffectsImport();
const RX_STANDARD_IMPORT = parsers.standardImport();
const RX_DYNAMIC_IMPORT = parsers.dynamicImport();

const standardImport = ({ dictionary = new Map(), source = "" }) => {
export const standardImport = ({ dictionary = new Map(), source = "" }) => {
const result = source.replace(
RX_STANDARD_IMPORT,
(replacer, g1, g2, g3, g4) => {
Expand All @@ -18,9 +18,8 @@ const standardImport = ({ dictionary = new Map(), source = "" }) => {
dictionary,
};
};
module.exports.standardImport = standardImport;

const dynamicImport = ({ dictionary = new Map(), source = "" }) => {
export const dynamicImport = ({ dictionary = new Map(), source = "" }) => {
const result = source.replace(RX_DYNAMIC_IMPORT, (replacer, g1, g2) => {
const dep = dictionary.get(g2) || g2;
return `${g1}('${dep}')`;
Expand All @@ -31,9 +30,8 @@ const dynamicImport = ({ dictionary = new Map(), source = "" }) => {
dictionary,
};
};
module.exports.dynamicImport = dynamicImport;

const sideEffectsImport = ({ dictionary = new Map(), source = "" }) => {
export const sideEffectsImport = ({ dictionary = new Map(), source = "" }) => {
const result = source.replace(RX_SIDE_EFFECTS_IMPORT, (replacer, g1, g2) => {
const dep = dictionary.get(g2) || g2;
return `${g1} '${dep}'`;
Expand All @@ -44,4 +42,3 @@ const sideEffectsImport = ({ dictionary = new Map(), source = "" }) => {
dictionary,
};
};
module.exports.sideEffectsImport = sideEffectsImport;
11 changes: 5 additions & 6 deletions src/loader.cjs → src/loader.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { helpers } = require("@eik/common");
const utils = require("./utils.cjs");
const {
import { helpers } from "@eik/common";
import * as utils from "./utils.js";
import {
standardImport,
dynamicImport,
sideEffectsImport,
} = require("./builders.cjs");
} from "./builders.js";

const dictionary = new Map();
let cold = true;

async function loader(source) {
export default async function loader(source) {
const options = this.getOptions();
const callback = this.async();

Expand Down Expand Up @@ -65,4 +65,3 @@ async function loader(source) {
callback(null, obj.source);
});
}
module.exports = loader;
14 changes: 0 additions & 14 deletions src/parsers.cjs

This file was deleted.

11 changes: 11 additions & 0 deletions src/parsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const standardImport = (flags = "sgm") =>
new RegExp(
"(import)\\s*([\\w{},\\n\\s\\[\\]\\*\\.]+?)\\s*(from)\\s*['\"]([\\w@\\-\\./]+?)['\"]",
flags,
);

export const dynamicImport = (flags = "gm") =>
new RegExp("(import)[(]['\"](.+?)['\"][)]", flags);

export const sideEffectsImport = (flags = "gm") =>
new RegExp("(import)\\s*['\"](.+?)['\"]", flags);
33 changes: 24 additions & 9 deletions src/utils.cjs → src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const { request } = require("undici");
import { request } from "undici";

const isBare = (str) => {
/**
* Whether or not a string looks like a bare import.
* @param {string} str
* @returns {boolean}
*/
export const isBare = (str) => {
if (
str.startsWith("/") ||
str.startsWith("./") ||
Expand All @@ -12,12 +17,19 @@ const isBare = (str) => {
}
return true;
};
module.exports.isBare = isBare;

const isString = (str) => typeof str === "string";
module.exports.isString = isString;
/**
* Runs typeof
* @param {any} str
* @returns {boolean}
*/
export const isString = (str) => typeof str === "string";

const validate = (map) =>
/**
* @param {any} map
* @returns {Array<{ key: string; value: string }>}
*/
export const validate = (map) =>
Object.keys(map.imports).map((key) => {
const value = map.imports[key];

Expand All @@ -29,9 +41,13 @@ const validate = (map) =>

return { key, value };
});
module.exports.validate = validate;

const fetchImportMaps = async (urls = []) => {
/**
*
* @param {string[]} [urls=[]]
* @returns
*/
export const fetchImportMaps = async (urls = []) => {
try {
const maps = urls.map(async (map) => {
const { statusCode, body } = await request(map, { maxRedirections: 2 });
Expand All @@ -53,4 +69,3 @@ const fetchImportMaps = async (urls = []) => {
);
}
};
module.exports.fetchImportMaps = fetchImportMaps;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/builders.js TAP .dynamicImport() - Replace module name > should replaced matched values with dictionary values 1`] = `
exports[`test/builders.test.js > TAP > .dynamicImport() - Replace module name > should replaced matched values with dictionary values 1`] = `

var a = import('./REPLACED-A.js')
let b = await import('module-b');
Expand All @@ -15,18 +15,18 @@ exports[`test/builders.js TAP .dynamicImport() - Replace module name > should re

`

exports[`test/builders.js TAP .sideEffectsImport() - Replace module name > should replaced matched values with dictionary values 1`] = `
exports[`test/builders.test.js > TAP > .sideEffectsImport() - Replace module name > should replaced matched values with dictionary values 1`] = `

import './REPLACED-A.js';import 'module-b'
import './REPLACED-C.js';

`

exports[`test/builders.js TAP .standardImport() - Replace module name > should replaced matched values with dictionary values 1`] = `
exports[`test/builders.test.js > TAP > .standardImport() - Replace module name > should replaced matched values with dictionary values 1`] = `

import { export1 } from './REPLACED-A.js';
import {export2} from 'module-b';
import {
import {
export3,
export4,
} from './REPLACED-C.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/loader.js TAP loader() - Import map defined through option "maps" argument take precedence over import map defined through option "urls" argument > Should rewrite import statement to https://cdn.eik.dev/lit-element/v2 1`] = `
exports[`test/loader.test.js > TAP > loader() - Import map defined through option "maps" argument take precedence over import map defined through option "urls" argument > Should rewrite import statement to https://cdn.eik.dev/lit-element/v2 1`] = `
import*as t from"https://cdn.eik.dev/lit-element/v2";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const r=(o={html:()=>t.html},n={},e.d(n,o),n);var o,n;function a(t,e){return Math.floor(t+Math.random()*(e+1-t))}class d{constructor(t){this.root=t}render(){const t=(e=[a(0,20),a(20,40),a(40,60),a(60,80),a(80,100)],r.html\`<p>Hello \${e[0]}!</p>\`);var e,o;this.root=(o=t,this.root.replaceWith(o),o)}update(){setInterval((()=>{this.render()}),1e3)}}(async()=>{const t=await new Promise((t=>{document.addEventListener("DOMContentLoaded",(()=>{const e=document.getElementById("app");t(e.firstElementChild)}))})),e=new d(t);e.render(),e.update()})();
`

exports[`test/loader.js TAP loader() - Import map defined through option "maps" take precedence over import map defined in eik.json > Should rewrite import statement to https://cdn.eik.dev/lit-element/v2 1`] = `
exports[`test/loader.test.js > TAP > loader() - Import map defined through option "maps" take precedence over import map defined in eik.json > Should rewrite import statement to https://cdn.eik.dev/lit-element/v2 1`] = `
import*as t from"https://cdn.eik.dev/lit-element/v2";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const r=(o={html:()=>t.html},n={},e.d(n,o),n);var o,n;function a(t,e){return Math.floor(t+Math.random()*(e+1-t))}class d{constructor(t){this.root=t}render(){const t=(e=[a(0,20),a(20,40),a(40,60),a(60,80),a(80,100)],r.html\`<p>Hello \${e[0]}!</p>\`);var e,o;this.root=(o=t,this.root.replaceWith(o),o)}update(){setInterval((()=>{this.render()}),1e3)}}(async()=>{const t=await new Promise((t=>{document.addEventListener("DOMContentLoaded",(()=>{const e=document.getElementById("app");t(e.firstElementChild)}))})),e=new d(t);e.render(),e.update()})();
`

exports[`test/loader.js TAP loader() - Import map defined through option "urls" argument take precedence over import map defined in eik.json > Should rewrite import statement to https://cdn.eik.dev/lit-element/v2 1`] = `
exports[`test/loader.test.js > TAP > loader() - Import map defined through option "urls" argument take precedence over import map defined in eik.json > Should rewrite import statement to https://cdn.eik.dev/lit-element/v2 1`] = `
import*as t from"https://cdn.eik.dev/lit-element/v2";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const r=(o={html:()=>t.html},n={},e.d(n,o),n);var o,n;function a(t,e){return Math.floor(t+Math.random()*(e+1-t))}class d{constructor(t){this.root=t}render(){const t=(e=[a(0,20),a(20,40),a(40,60),a(60,80),a(80,100)],r.html\`<p>Hello \${e[0]}!</p>\`);var e,o;this.root=(o=t,this.root.replaceWith(o),o)}update(){setInterval((()=>{this.render()}),1e3)}}(async()=>{const t=await new Promise((t=>{document.addEventListener("DOMContentLoaded",(()=>{const e=document.getElementById("app");t(e.firstElementChild)}))})),e=new d(t);e.render(),e.update()})();
`

exports[`test/loader.js TAP loader() - import map fetched from a URL > import maps from urls 1`] = `
exports[`test/loader.test.js > TAP > loader() - import map fetched from a URL > import maps from urls 1`] = `
import*as t from"https://cdn.eik.dev/lit-element/v2";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const r=(o={html:()=>t.html},n={},e.d(n,o),n);var o,n;function a(t,e){return Math.floor(t+Math.random()*(e+1-t))}class d{constructor(t){this.root=t}render(){const t=(e=[a(0,20),a(20,40),a(40,60),a(60,80),a(80,100)],r.html\`<p>Hello \${e[0]}!</p>\`);var e,o;this.root=(o=t,this.root.replaceWith(o),o)}update(){setInterval((()=>{this.render()}),1e3)}}(async()=>{const t=await new Promise((t=>{document.addEventListener("DOMContentLoaded",(()=>{const e=document.getElementById("app");t(e.firstElementChild)}))})),e=new d(t);e.render(),e.update()})();
`

exports[`test/loader.js TAP loader() - import map fetched from a URL via eik.json > eik.json import-map string 1`] = `
exports[`test/loader.test.js > TAP > loader() - import map fetched from a URL via eik.json > eik.json import-map string 1`] = `
import*as t from"https://cdn.eik.dev/lit-element/v2";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const r=(o={html:()=>t.html},n={},e.d(n,o),n);var o,n;function a(t,e){return Math.floor(t+Math.random()*(e+1-t))}class d{constructor(t){this.root=t}render(){const t=(e=[a(0,20),a(20,40),a(40,60),a(60,80),a(80,100)],r.html\`<p>Hello \${e[0]}!</p>\`);var e,o;this.root=(o=t,this.root.replaceWith(o),o)}update(){setInterval((()=>{this.render()}),1e3)}}(async()=>{const t=await new Promise((t=>{document.addEventListener("DOMContentLoaded",(()=>{const e=document.getElementById("app");t(e.firstElementChild)}))})),e=new d(t);e.render(),e.update()})();
`
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/parsers.js TAP .standardImport() - Multiline "export" values > should be the "export" group 1`] = `
exports[`test/parsers.test.js > TAP > .standardImport() - Multiline "export" values > should be the "export" group 1`] = `
{
export1,
export2,
export3,
}
`

exports[`test/parsers.js TAP .standardImport() - Multiple import statements concatinated > should be the "export" group 1`] = `
exports[`test/parsers.test.js > TAP > .standardImport() - Multiple import statements concatinated > should be the "export" group 1`] = `
{
export3,
export4,
}
`

exports[`test/parsers.js TAP .standardImport() - Multiple import statements on new lines > should be the "export" group 1`] = `
exports[`test/parsers.test.js > TAP > .standardImport() - Multiple import statements on new lines > should be the "export" group 1`] = `
{
export3,
export4,
Expand Down
Loading