mirror of
https://github.com/pezkuwichain/pezkuwi-dev.git
synced 2026-04-21 23:48:03 +00:00
cb6801a3cc
- Updated all copyright headers from 2025 to 2026 (system date shows 2026)
- Added build output files to eslint ignore list in packages/dev/config/eslint.js
- Added build output patterns to .gitignore
- Ignored: packages/*/*.{d.ts,js,mjs,cjs}, packages/*/cjs/**, packages/*/env/**,
packages/*/rootJs/**, packages/*/rootStatic/**
- Successfully resolved 521 lint errors by properly ignoring generated files
- Build outputs should not be linted (source files are linted instead)
- Lint and build now pass successfully
90 lines
2.5 KiB
TypeScript
90 lines
2.5 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/dev-ts authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import crypto from 'node:crypto';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import ts from 'typescript';
|
|
|
|
import { EXT_TS_REGEX, loaderOptions } from './common.js';
|
|
|
|
interface Loaded {
|
|
format: 'commonjs' | 'module';
|
|
shortCircuit?: boolean;
|
|
source: string;
|
|
}
|
|
|
|
type NexLoad = (url: string, context: Record<string, unknown>) => Promise<Loaded>;
|
|
|
|
/**
|
|
* Load all TypeScript files, compile via tsc on-the-fly
|
|
**/
|
|
export async function load (url: string, context: Record<string, unknown>, nextLoad: NexLoad): Promise<Loaded> {
|
|
if (EXT_TS_REGEX.test(url)) {
|
|
// used the chained loaders to retrieve
|
|
const { source } = await nextLoad(url, {
|
|
...context,
|
|
format: 'module'
|
|
});
|
|
|
|
// we use a hash of the source to determine caching
|
|
const sourceHash = `//# sourceHash=${crypto.createHash('sha256').update(source).digest('hex')}`;
|
|
const compiledFile = url.includes('/src/')
|
|
? fileURLToPath(
|
|
url
|
|
.replace(/\.tsx?$/, '.js')
|
|
.replace('/src/', '/build-loader/')
|
|
)
|
|
: null;
|
|
|
|
if (loaderOptions.isCached && compiledFile && fs.existsSync(compiledFile)) {
|
|
const compiled = fs.readFileSync(compiledFile, 'utf-8');
|
|
|
|
if (compiled.includes(sourceHash)) {
|
|
return {
|
|
format: 'module',
|
|
source: compiled
|
|
};
|
|
}
|
|
}
|
|
|
|
// compile via typescript
|
|
const { outputText } = ts.transpileModule(source.toString(), {
|
|
compilerOptions: {
|
|
...(
|
|
url.endsWith('.tsx')
|
|
? { jsx: ts.JsxEmit.ReactJSX }
|
|
: {}
|
|
),
|
|
esModuleInterop: true,
|
|
importHelpers: true,
|
|
inlineSourceMap: true,
|
|
module: ts.ModuleKind.ESNext,
|
|
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
skipLibCheck: true,
|
|
// Aligns with packages/dev/scripts/pezkuwi-dev-build-ts & packages/dev/config/tsconfig
|
|
target: ts.ScriptTarget.ES2022
|
|
},
|
|
fileName: fileURLToPath(url)
|
|
});
|
|
|
|
if (loaderOptions.isCached && compiledFile) {
|
|
const compiledDir = path.dirname(compiledFile);
|
|
|
|
if (!fs.existsSync(compiledDir)) {
|
|
fs.mkdirSync(compiledDir, { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(compiledFile, `${outputText}\n${sourceHash}`, 'utf-8');
|
|
}
|
|
|
|
return {
|
|
format: 'module',
|
|
source: outputText
|
|
};
|
|
}
|
|
|
|
return nextLoad(url, context);
|
|
}
|