Skip to content

Commit

Permalink
Minimal working version of inline-mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Jan 21, 2024
1 parent 4c3b80d commit 9f7f5e0
Show file tree
Hide file tree
Showing 18 changed files with 369 additions and 131 deletions.
24 changes: 24 additions & 0 deletions examples/inline-mod/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions examples/inline-mod/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions examples/inline-mod/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@example/inline-mod",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"build": "tsc && vite build",
"dev": "vite",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.2.2",
"@inox-tools/inline-mod": "workspace:*",
"vite": "^5.0.8"
}
}
1 change: 1 addition & 0 deletions examples/inline-mod/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions examples/inline-mod/src/counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {interceptCounter} from 'virtual:interceptors';

export function setupCounter(element: HTMLButtonElement) {
let counter = 0
const setCounter = (count: number) => {
counter = interceptCounter(count);
element.innerHTML = `count is ${counter}`;
}
element.addEventListener('click', () => setCounter(counter + 1))
setCounter(0)
}
24 changes: 24 additions & 0 deletions examples/inline-mod/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import './style.css'
import typescriptLogo from './typescript.svg'
import viteLogo from '/vite.svg'
import { setupCounter } from './counter.ts'

document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="${viteLogo}" class="logo" alt="Vite logo" />
</a>
<a href="https://www.typescriptlang.org/" target="_blank">
<img src="${typescriptLogo}" class="logo vanilla" alt="TypeScript logo" />
</a>
<h1>Vite + TypeScript</h1>
<div class="card">
<button id="counter" type="button"></button>
</div>
<p class="read-the-docs">
Click on the Vite and TypeScript logos to learn more
</p>
</div>
`

setupCounter(document.querySelector<HTMLButtonElement>('#counter')!)
96 changes: 96 additions & 0 deletions examples/inline-mod/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #3178c6aa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
1 change: 1 addition & 0 deletions examples/inline-mod/src/typescript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/inline-mod/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
23 changes: 23 additions & 0 deletions examples/inline-mod/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
13 changes: 13 additions & 0 deletions examples/inline-mod/vite.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'vite';
import inlineModPlugin, {inlineMod} from "@inox-tools/inline-mod/vite";

inlineMod({
constExports: {
interceptCounter: count => count % 13,
},
modName: 'virtual:interceptors',
})

export default defineConfig({
plugins: [inlineModPlugin({})]
})
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"esbuild": "^0.19.11",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-prettier": "^5.1.3",
"organize-imports-cli": "^0.10.0",
"prettier": "^3.1.1",
"prettier-plugin-astro": "^0.12.3",
"turbo": "^1.11.3",
"prettier": "^3.2.4",
"prettier-plugin-astro": "^0.13.0",
"turbo": "^1",
"typescript": "^5.3.3"
},
"packageManager": "pnpm@^8"
Expand Down
11 changes: 6 additions & 5 deletions packages/inline-mod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@inox-tools/inline-mod",
"version": "0.0.1",
"version": "0.0.4",
"description": "Define a virtual module inline with any reference to buildtime values",
"keywords": [
"vite-plugin"
Expand All @@ -10,20 +10,21 @@
"author": "Luiz Ferraz <[email protected]>",
"type": "module",
"exports": {
".": "./dist/index.js",
"./vite": {
"types": "./dist/vite.d.ts",
"default": "./dist/index.js"
"default": "./dist/vite.js"
}
},
"files": [
"README.md",
"dist"
"src"
],
"scripts": {
"build": "tsup",
"test": "vitest",
"dev": "vite --host",
"prepublish": "npm run clean && npm run build"
"dev": "tsup --watch",
"prepublish": "pnpm run build"
},
"dependencies": {
"typescript": "^5"
Expand Down
5 changes: 5 additions & 0 deletions packages/inline-mod/src/closure/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export class EntryRegistry<K> {
this.inner.set(key, entry);
}

public addUnchecked(key: K, entry: Entry) {
this.remove(key);
this.add(key, entry);
}

public prepare(key: K) {
// Pending entry is intentionally not constructable without a cast
this.add(key, { type: 'pending' } as Entry<'pending'>);
Expand Down
16 changes: 8 additions & 8 deletions packages/inline-mod/src/closure/inspectCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as modules from 'node:module';
import upath from 'upath';
import * as upath from 'node:path';
import { Entry, EntryRegistry } from './entry.js';
import { Lazy } from './lazy.js';
import { getModuleFromPath } from './package.js';
Expand Down Expand Up @@ -878,30 +878,30 @@ class GlobalCache {
// Add entries to allow proper serialization over generators and iterators.
const emptyGenerator = function* (): any {};

this.cache.add(Object.getPrototypeOf(emptyGenerator), {
this.cache.addUnchecked(Object.getPrototypeOf(emptyGenerator), {
type: 'expr',
value: 'Object.getPrototypeOf(function*(){})',
});
this.cache.add(Object.getPrototypeOf(emptyGenerator.prototype), {
this.cache.addUnchecked(Object.getPrototypeOf(emptyGenerator.prototype), {
type: 'expr',
value: 'Object.getPrototypeOf((function*(){}).prototype)',
});
this.cache.add(Symbol.iterator, { type: 'expr', value: 'Symbol.iterator' });
this.cache.addUnchecked(Symbol.iterator, { type: 'expr', value: 'Symbol.iterator' });

this.cache.add(process.env, Entry.expr('process.env'));
this.cache.addUnchecked(process.env, Entry.expr('process.env'));
}

private addGlobalInfo(key: string) {
const globalObj = (global as any)[key];
const text = utils.isLegalMemberName(key) ? `global.${key}` : `global[${JSON.stringify(key)}]`;

if (globalObj !== undefined && globalObj !== null) {
this.cache.add(globalObj, { type: 'expr', value: text });
this.cache.add(Object.getPrototypeOf(globalObj), {
this.cache.addUnchecked(globalObj, { type: 'expr', value: text });
this.cache.addUnchecked(Object.getPrototypeOf(globalObj), {
type: 'expr',
value: `Object.getPrototypeOf(${text})`,
});
this.cache.add(globalObj.prototype, {
this.cache.addUnchecked(globalObj.prototype, {
type: 'expr',
value: `${text}.prototype`,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/inline-mod/src/closure/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import * as upath from 'upath';
import * as upath from 'node:path';

type Exports = string | { [key: string]: SubExports };
type SubExports = string | { [key: string]: SubExports } | null;
Expand Down
Loading

0 comments on commit 9f7f5e0

Please sign in to comment.