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

fix: intellisense for esm build ts #126

Merged
merged 4 commits into from
Dec 25, 2023
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
37 changes: 18 additions & 19 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ export async function build(options: Record<string, any>) {
}
const sernConfig = await getConfig();
let buildConfig: Partial<BuildOptions> = {};

const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`, {
//for some reason, my ignore glob wasn't registering correctly'
ignore: {
ignored: (p) => p.name.endsWith('.d.ts'),
},
});

const buildConfigPath = resolve(options.project ?? 'sern.build.js');

const resolveBuildConfig = (path: string|undefined, language: string) => {
if(language === 'javascript') {
return path ?? resolve('jsconfig.json')
}
return path ?? resolve('tsconfig.json')
}

const defaultBuildConfig = {
defineVersion: true,
format: options.format ?? 'esm',
Expand All @@ -76,28 +80,23 @@ export async function build(options: Record<string, any>) {
env: options.env ?? resolve('.env'),
};
if (pathExistsSync(buildConfigPath)) {
try {
buildConfig = {
...defaultBuildConfig,
...(await import('file:///' + buildConfigPath)).default,
};
} catch (e) {
console.log(e);
process.exit(1);
}
} else {
//throwable, buildConfigPath may not exist
buildConfig = {
...defaultBuildConfig,
...(await import('file:///' + buildConfigPath)).default,
};
} else {
buildConfig = defaultBuildConfig;
console.log('No build config found, defaulting');
}

let env = {} as Record<string, string>;
configDotenv({ path: buildConfig.env, processEnv: env });

if (env.MODE && !env.NODE_ENV) {
const modeButNotNodeEnvExists = env.MODE && !env.NODE_ENV;
if (modeButNotNodeEnvExists) {
console.warn('Use NODE_ENV instead of MODE');
console.warn('MODE has no effect.');
console.warn(`https://nodejs.dev/en/learn/nodejs-the-difference-between-development-and-production/`);
console.warn(`https://nodejs.org/en/learn/getting-started/nodejs-the-difference-between-development-and-production`);
}

if (env.NODE_ENV) {
Expand All @@ -110,7 +109,7 @@ export async function build(options: Record<string, any>) {

try {
let config = require(buildConfig.tsconfig!);
config.extends && console.warn("Please ensure to extend the generated tsconfig")
config.extends && console.warn("Extend the generated tsconfig")
} catch(e) {
console.warn("no tsconfig / jsconfig found");
console.warn(`Please create a ${sernConfig.language === 'javascript' ? 'jsconfig.json' : 'tsconfig.json' }`);
Expand All @@ -130,11 +129,11 @@ export async function build(options: Record<string, any>) {
console.log(' ', magentaBright('env'), buildConfig.env);

const sernDir = resolve('.sern'),
genDir = resolve(sernDir, 'generated'),
ambientFilePath = resolve(sernDir, 'ambient.d.ts'),
packageJsonPath = resolve('package.json'),
sernTsConfigPath = resolve(sernDir, 'tsconfig.json'),
packageJson = () => require(packageJsonPath);
genDir = resolve(sernDir, 'generated'),
ambientFilePath = resolve(sernDir, 'ambient.d.ts'),
packageJsonPath = resolve('package.json'),
sernTsConfigPath = resolve(sernDir, 'tsconfig.json'),
packageJson = () => require(packageJsonPath);

if (!(await pathExists(genDir))) {
console.log('Making .sern/generated dir, does not exist');
Expand Down
9 changes: 6 additions & 3 deletions src/utilities/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const processEnvType = (env: NodeJS.ProcessEnv) => {
const envBuilder = new StringWriter();

for (const key of entries) {
envBuilder.tab();
envBuilder.tab();
envBuilder.envField(key);
envBuilder.tab()
.tab()
.envField(key);
}
return envBuilder.build();
};
Expand Down Expand Up @@ -40,6 +40,8 @@ const writeTsConfig = async (format: 'cjs' | 'esm', configPath: string, fw: File
const target = format === 'esm' ? { target: 'esnext' } : {};
const sernTsConfig = {
compilerOptions: {
//module determines top level await. CJS doesn't have that abliity afaik
module: format === 'cjs' ? 'node' : 'esnext',
moduleResolution: 'node',
strict: true,
skipLibCheck: true,
Expand Down Expand Up @@ -69,6 +71,7 @@ class StringWriter {
return this;
}
envField(key: string) {
//if env field has space or parens, wrap key in ""
if (/\s|\(|\)/g.test(key)) {
this.fileString += `"${key}": string`;
} else {
Expand Down