Skip to content

Commit

Permalink
convert to type "module", continue working on new version of build sc…
Browse files Browse the repository at this point in the history
…ript
  • Loading branch information
patinthehat committed Dec 21, 2023
1 parent 5e6d9fb commit dcd479c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ module.exports = {
'array-bracket-newline': [ 'warn', { multiline: true, minItems: 6 }],
'array-bracket-spacing': [ 'warn', 'always', { objectsInArrays: false }],
'array-element-newline': [ 'warn', { multiline: true, minItems: 6 }],
'eqeqeq': [ 'error', 'smart' ],
'indent': [ 'warn', 4, { SwitchCase: 1 }],
eqeqeq: [ 'error', 'smart' ],
indent: [ 'warn', 4, { SwitchCase: 1 }],
'no-eval': 'error',
'no-var': 'error',
'object-curly-newline': [
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"lint:staged": "lint-staged",
"fix": "npm run fmt && npm run lint:fix",
"build:api-docs": "typedoc --plugin typedoc-plugin-markdown --out docs/api src/index.ts",
"build:dev": "node scripts/build.cjs",
"build:prod": "node scripts/build.cjs --production",
"build:binaries": "node scripts/build.cjs --binaries",
"build:dev": "node --no-warnings scripts/build.cjs",
"build:prod": "node --no-warnings scripts/build.cjs --production",
"build:binaries": "node --no-warnings scripts/build.cjs --binaries",
"build:dts": "dts-bundle-generator -o dist/index.d.ts src/index.ts",
"build": "npm run build:dev && npm run build:dts",
"dev": "npm run build:dev && node dist/index.js",
Expand Down
40 changes: 17 additions & 23 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import esbuild from 'esbuild';
import { execSync, spawnSync } from 'node:child_process';
import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
import { basename, resolve } from 'node:path';
import { basename, join, dirname } from 'node:path';

import pkg from '../package.json';

const platformToTarget = platform => {
return `${platform.name}${platform.version}`;
};
import pkg from '../package.json' assert { type: 'json' };

const platformToTarget = platform => `${platform.name}${platform.version}`;
const isValidFormat = format => [ 'esm', 'cjs', 'iife' ].includes(format);
const parentDir = dir => dirname(dirname(dir));
const projectPath = (...parts) => join(...[ parentDir(import.meta.url).replace('file://', ''), ...parts ]);

/** @type { import('esbuild').BuildOptions } */
const buildConfig = {
basePath: resolve(import.meta.url, '..'),
basePath: projectPath(),
bundle: true,
constants: {},
entry: 'src/index.ts',
Expand Down Expand Up @@ -48,26 +47,22 @@ class Builder {
absWorkingDir: buildConfig.basePath,
allowOverwrite: true,
bundle: buildConfig.bundle,
define: {
__APP_VERSION__: `'${pkg.version}'`,
__COMPILED_AT__: `'${new Date().toUTCString()}'`,
...buildConfig.constants,
},
define: buildConfig.constants,
entryPoints: [ buildConfig.entry ],
format: buildConfig.format,
logLevel: 'silent',
metafile: true,
minify: buildConfig.minify,
outdir: buildConfig.outdir,
platform: buildConfig.platform.name,
tsconfig: projectPath('tsconfig.json'),
plugins: [
// esbuildPluginDecorator({
// compiler: 'tsc',
// tsconfigPath: `${buildConfig.basePath}/tsconfig.json`,
// }),
],
target: platformToTarget(buildConfig.platform),
experimentalOptimizeImports: true,
});

return new Promise(resolve => resolve(result));
Expand Down Expand Up @@ -120,16 +115,15 @@ class Builder {
return buildConfig.format;
};

process.argv
.slice(2)
.map(arg => {
const hasMappedArg = typeof argMap[arg] === 'undefined';
return hasMappedArg ? { name: arg.replace(/^-+/, ''), value: true } : argMap[arg];
})
.forEach(data => {
if ([ 'esm', 'cjs' ].includes(data.name)) setConfigOutputFormat(data.name);
})
.forEach(data => (this.config[data.name] = data.value));
const args = process.argv.slice(2).map(arg => {
const hasMappedArg = typeof argMap[arg] === 'undefined';
return hasMappedArg ? { name: arg.replace(/^-+/, ''), value: true } : argMap[arg];
});

args.forEach(data => {
if ([ 'esm', 'cjs' ].includes(data.name)) setConfigOutputFormat(data.name);
});
args.forEach(data => (this.config[data.name] = data.value));

buildConfig.format = getFormatType();

Expand Down
2 changes: 1 addition & 1 deletion tests/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-undef */

import { add } from '../src/index';
import { add } from '@/index.js';

test('two plus two is four', () => {
expect(2 + 2).toBe(4);
Expand Down
9 changes: 5 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "esnext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"module": "NodeNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["esnext"] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
"checkJs": true /* Report errors in .js files. */,

// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
Expand Down Expand Up @@ -43,7 +44,7 @@
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */

/* Module Resolution Options */
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"moduleResolution": "NodeNext" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"baseUrl": "." /* Base directory to resolve non-absolute module names. */,
"paths": {
"@/*": ["src/*"],
Expand All @@ -56,7 +57,7 @@
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

"moduleDetection": "auto",
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
Expand All @@ -72,5 +73,5 @@
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"resolveJsonModule": true
},
"exclude": ["configure-package.js", "src/scripts/*", "features/*", "*.config.js", ".eslintrc.cjs", "scripts/*"]
"exclude": ["configure-package.js", "features/*", "*.config.js", ".eslintrc.cjs", "scripts/*"]
}

0 comments on commit dcd479c

Please sign in to comment.