diff --git a/.gitignore b/.gitignore index 0f66536c..69df09ba 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ /node_modules/ cypress/screenshots cypress/videos +tmp/ +lib/ +build/ diff --git a/package-lock.json b/package-lock.json index d9f975f9..399cab6e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,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", @@ -184,6 +185,16 @@ "node": "^10 || ^12.20.0 || ^14.13.0 || >=15.0.0" } }, + "node_modules/@discoveryjs/scan-fs": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@discoveryjs/scan-fs/-/scan-fs-4.0.0.tgz", + "integrity": "sha512-+dT8eeygTmbobGkUKA58mjhmJdXvXOyCnIybzOKnPF0XUTLskej71kmYk6FJFOlIIv8NAQSrCPioSwWkjngO0w==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^12.20.0 || ^14.13.0 || >=15.0.0" + } + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.23.0", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.23.0.tgz", @@ -4492,6 +4503,12 @@ "resolved": "https://registry.npmjs.org/@discoveryjs/natural-compare/-/natural-compare-1.1.0.tgz", "integrity": "sha512-yuctPJs5lRXoI8LkpVZGAV6n+DKOuEsfpfcIDQ8ZjWHwazqk1QjBc4jMlof0UlZHyUqv4dwsOTooMiAmtzvwXA==" }, + "@discoveryjs/scan-fs": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@discoveryjs/scan-fs/-/scan-fs-4.0.0.tgz", + "integrity": "sha512-+dT8eeygTmbobGkUKA58mjhmJdXvXOyCnIybzOKnPF0XUTLskej71kmYk6FJFOlIIv8NAQSrCPioSwWkjngO0w==", + "dev": true + }, "@esbuild/aix-ppc64": { "version": "0.23.0", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.23.0.tgz", diff --git a/package.json b/package.json index 1444ad8e..84de8403 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -48,6 +57,6 @@ }, "files": [ "dist", - "src" + "lib" ] } diff --git a/scripts/bake-version.cjs b/scripts/bake-version.cjs deleted file mode 100644 index 74be5dfe..00000000 --- a/scripts/bake-version.cjs +++ /dev/null @@ -1,7 +0,0 @@ -const { writeFileSync } = require('fs'); - -const content = process.argv[2] === '--rollback' - ? 'export { version } from \'../package.json\';\n' - : `export const version = ${JSON.stringify(require('../package.json').version)};\n`; - -writeFileSync('./src/version.js', content); diff --git a/scripts/transpile.js b/scripts/transpile.js new file mode 100644 index 00000000..5fc547ee --- /dev/null +++ b/scripts/transpile.js @@ -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();