Skip to content

Commit

Permalink
Basic transpilation
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Aug 7, 2024
1 parent 055cf96 commit 503a572
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/node_modules/
cypress/screenshots
cypress/videos
tmp/
lib/
build/
17 changes: 17 additions & 0 deletions package-lock.json

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

19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@
"repository": "discoveryjs/discovery",
"keywords": [],
"type": "module",
"main": "src/lib.js",
"main": "lib/lib.js",
"unpkg": "dist/discovery.js",
"jsdelivr": "dist/discovery.js",
"typings": "./lib/lib.d.ts",
"exports": {
"./dist/*": "./dist/*",
"./lib/*": "./lib/*",
"./src/*": "./lib/*",
"./package.json": "./package.json"
},
"scripts": {
"lint": "eslint cypress models src",
"typecheck": "tsc --noEmit",
"emit:types": "tsc --emitDeclarationOnly",
"build": "npm run build:js && npm run build:css && npm run build:preloader && npm run build:embed-host",
"build:js": "esbuild src/lib.js --outfile=dist/discovery.js --bundle --define:global=window --format=esm --minify --sourcemap=external",
"build:js": "esbuild src/lib.ts --outfile=dist/discovery.js --bundle --define:global=window --format=esm --minify --sourcemap=external",
"build:css": "esbuild src/lib.css --outfile=dist/discovery.css --bundle --minify --loader:.svg=dataurl",
"build:embed-host": "esbuild src/extensions/embed-host.js --outfile=dist/discovery-embed-host.js --bundle --define:global=window --format=esm --minify --sourcemap=external",
"build:preloader": "npm run build:preloader:js && npm run build:preloader:css",
"build:preloader:js": "esbuild src/preloader.js --outfile=dist/discovery-preloader.js --bundle --define:global=window --format=esm --minify --sourcemap=external",
"build:preloader:css": "esbuild src/preloader.css --outfile=dist/discovery-preloader.css --bundle --minify --loader:.svg=dataurl",
"build-gh-pages": "discovery-build -o .gh-pages --clean",
"prepublishOnly": "node scripts/bake-version.cjs && npm run build",
"postpublish": "node scripts/bake-version.cjs --rollback",
"transpile": "node scripts/transpile.js",
"prepublishOnly": "npm run transpile && npm run build",
"cypress": "npx cypress open",
"cypress:server": "discovery -c ./cypress/fixtures/single-model/.discoveryrc.cjs -p 8124",
"start": "discovery -c models/index.cjs",
Expand All @@ -40,6 +48,7 @@
},
"devDependencies": {
"@discoveryjs/cli": "^2.8.0",
"@discoveryjs/scan-fs": "^4.0.0",
"cypress": "^13.3.0",
"esbuild": "~0.23.0",
"eslint": "^8.50.0",
Expand All @@ -48,6 +57,6 @@
},
"files": [
"dist",
"src"
"lib"
]
}
7 changes: 0 additions & 7 deletions scripts/bake-version.cjs

This file was deleted.

53 changes: 53 additions & 0 deletions scripts/transpile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { scanFs } from '@discoveryjs/scan-fs';
import path from 'node:path';
import fs from 'node:fs';
import { execSync } from 'child_process';
import esbuild from 'esbuild';

const srcpath = resolvePath('../src');
const dstpath = resolvePath('../lib');

function resolvePath(p) {
return new URL(p, import.meta.url).pathname;
}

export async function compile() {
const startTime = Date.now();

for (const file of (await scanFs(srcpath)).files) {
const srcAbsPath = path.join(srcpath, file.path);
let dstAbsPath = path.join(dstpath, file.path);

if (path.extname(srcAbsPath) === '.ts') {
// ignore .ts files when .js file exists
if (fs.existsSync(srcAbsPath.replace(/\.ts$/, '.js'))) {
continue;
}

const { code } = await esbuild.transform(fs.readFileSync(srcAbsPath), {
loader: 'ts'
});

dstAbsPath = dstAbsPath.replace(/\.ts$/, '.js');
await fs.promises.mkdir(path.dirname(dstAbsPath), { recursive: true });
await fs.promises.writeFile(dstAbsPath, code);
} else if (path.basename(srcAbsPath) === 'version.js') {
await fs.promises.mkdir(path.dirname(dstAbsPath), { recursive: true });
await fs.promises.writeFile(dstAbsPath, `export const version = "${JSON.parse(fs.readFileSync(resolvePath('../package.json'))).version}";\n`);
} else {
await fs.promises.mkdir(path.dirname(dstAbsPath), { recursive: true });
await fs.promises.copyFile(srcAbsPath, dstAbsPath);
}
}

try {
execSync('npm run emit:types', {
cwd: resolvePath('..'),
stdio: 'inherit'
});
} catch {}

console.log(`Compiled in ${Date.now() - startTime} ms`);
}

compile();

0 comments on commit 503a572

Please sign in to comment.