Skip to content

Commit

Permalink
Merge pull request #848 from balena-io/esm
Browse files Browse the repository at this point in the history
20.x: Convert to ESM
  • Loading branch information
Page- authored Jan 10, 2025
2 parents 223a1ec + bb75825 commit 74ecda3
Show file tree
Hide file tree
Showing 96 changed files with 473 additions and 610 deletions.
31 changes: 9 additions & 22 deletions Gruntfile.ts → Gruntfile.cts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { WebpackPluginInstance } from 'webpack';

import _ from 'lodash';
import TerserPlugin from 'terser-webpack-plugin';
import browserConfig from './build/browser';
import moduleConfig from './build/module';
import serverConfig from './build/server';
import browserConfig from './build/browser.cts';
import moduleConfig from './build/module.cts';
import serverConfig from './build/server.cts';

const serverConfigs = {
browser: browserConfig,
Expand Down Expand Up @@ -110,30 +110,18 @@ export = (grunt: typeof Grunt) => {
return renames;
})(),

replace: {
'pine.js': {
replace: _.mapValues(serverConfigs, (_config, task) => {
return {
src: 'out/pine.js',
overwrite: true,
replacements: [
{
from: /nodeRequire/g,
to: 'require',
from: /sourceMappingURL=pine.js.map/g,
to: `sourceMappingURL=pine-${task}-<%= grunt.option('version') %>.js.map`,
},
],
},
..._.mapValues(serverConfigs, (_config, task) => {
return {
src: 'out/pine.js',
overwrite: true,
replacements: [
{
from: /sourceMappingURL=pine.js.map/g,
to: `sourceMappingURL=pine-${task}-<%= grunt.option('version') %>.js.map`,
},
],
};
}),
},
};
}),

webpack: serverConfigs,

Expand Down Expand Up @@ -167,7 +155,6 @@ export = (grunt: typeof Grunt) => {
'webpack:' + task,
'gitinfo:describe',
'version',
'replace:pine.js',
`replace:${task}`,
`concat:${task}`,
`rename:${task}`,
Expand Down
2 changes: 1 addition & 1 deletion bin/abstract-sql-compiler.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../out/bin/abstract-sql-compiler');
import '../out/bin/abstract-sql-compiler.js';
2 changes: 1 addition & 1 deletion bin/odata-compiler.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../out/bin/odata-compiler');
import '../out/bin/odata-compiler.js';
2 changes: 1 addition & 1 deletion bin/sbvr-compiler.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../out/bin/sbvr-compiler');
import '../out/bin/sbvr-compiler.js';
2 changes: 1 addition & 1 deletion build/browser.ts → build/browser.cts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpack from 'webpack';
import type { Configuration } from 'webpack';
import sharedConfig from './config';
import sharedConfig from './config.cts';

if (typeof sharedConfig.externals !== 'object') {
throw new Error('Expected externals to be an object');
Expand Down
6 changes: 5 additions & 1 deletion build/config.ts → build/config.cts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RequiredField } from '../src/sbvr-api/common-types';
import type { RequiredField } from '../src/sbvr-api/common-types.js' with { 'resolution-mode': 'import' };

import * as path from 'path';
import * as webpack from 'webpack';
Expand Down Expand Up @@ -46,6 +46,10 @@ const config: RequiredField<
},
resolve: {
extensions: ['.js', '.ts'],
extensionAlias: {
'.js': ['.ts', '.js'],
'.mjs': ['.mts', '.mjs'],
},
},
plugins: [new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })],
module: {
Expand Down
2 changes: 1 addition & 1 deletion build/module.ts → build/module.cts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpack from 'webpack';
import type { Configuration } from 'webpack';
import sharedConfig from './config';
import sharedConfig from './config.cts';

const config: Configuration = {
...sharedConfig,
Expand Down
2 changes: 1 addition & 1 deletion build/server.ts → build/server.cts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpack from 'webpack';
import type { Configuration } from 'webpack';
import sharedConfig from './config';
import sharedConfig from './config.cts';

const config: Configuration = {
...sharedConfig,
Expand Down
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@balena/pinejs",
"version": "19.7.5",
"main": "out/server-glue/module",
"type": "commonjs",
"main": "out/server-glue/module.js",
"type": "module",
"repository": "git@github.com:balena-io/pinejs.git",
"license": "Apache-2.0",
"bin": {
Expand All @@ -13,16 +13,16 @@
"scripts": {
"prepublish": "require-npm4-to-publish",
"prepare": "node -e \"try { (await import('husky')).default() } catch (e) { if (e.code !== 'ERR_MODULE_NOT_FOUND') throw e }\" --input-type module && npm run build",
"build": "grunt build",
"webpack-browser": "grunt browser",
"webpack-module": "grunt module",
"webpack-server": "grunt server",
"build": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts build",
"webpack-browser": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts browser",
"webpack-module": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts module",
"webpack-server": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts server",
"webpack-build": "npm run webpack-browser && npm run webpack-module && npm run webpack-server",
"lint": "balena-lint -t tsconfig.dev.json -e js -e ts src build typings Gruntfile.ts && npx tsc --project tsconfig.dev.json --noEmit",
"lint": "balena-lint -t tsconfig.dev.json -e js -e ts src build typings Gruntfile.cts && npx tsc --project tsconfig.dev.json --noEmit",
"test": "npm run build && npm run lint && npm run webpack-build && npm run test:compose && npm run test:generated-types",
"test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 PINEJS_QUEUE_CONCURRENCY=1 TZ=UTC npx mocha",
"test:generated-types": "npm run generate-types && git diff --exit-code ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts",
"lint-fix": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.ts",
"lint-fix": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.cts",
"generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && node ./bin/sbvr-compiler.js generate-types ./src/tasks/tasks.sbvr ./src/tasks/tasks.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
},
"dependencies": {
Expand Down Expand Up @@ -54,7 +54,7 @@
"@types/websql": "^0.0.30",
"ajv": "^8.17.1",
"busboy": "^1.6.0",
"commander": "^12.1.0",
"commander": "^13.0.0",
"cron-parser": "^4.9.0",
"deep-freeze": "^0.0.1",
"eventemitter3": "^5.0.1",
Expand All @@ -69,17 +69,18 @@
"devDependencies": {
"@balena/lint": "^8.2.8",
"@balena/pinejs": "file:./",
"@balena/pinejs-webresource-s3": "^1.0.2",
"@faker-js/faker": "^9.3.0",
"@types/busboy": "^1.5.4",
"@types/chai": "^4.3.20",
"@types/chai": "^5.0.1",
"@types/grunt": "^0.4.31",
"@types/mocha": "^10.0.10",
"@types/on-finished": "^2.3.4",
"@types/request": "^2.48.12",
"@types/supertest": "^6.0.2",
"@types/terser-webpack-plugin": "^5.2.0",
"@types/webpack": "^5.28.5",
"chai": "^4.5.0",
"chai": "^5.1.2",
"grunt": "^1.6.1",
"grunt-check-dependencies": "^1.0.0",
"grunt-cli": "^1.5.0",
Expand Down Expand Up @@ -126,7 +127,7 @@
"serve-static": "^1.16.2"
},
"engines": {
"node": ">=20.14.0",
"node": "^20.14.0 || ^22.0.0",
"npm": ">=10.7.0"
},
"lint-staged": {
Expand All @@ -141,7 +142,7 @@
"extension": [
".test.ts"
],
"require": "ts-node/register/transpile-only",
"loader": "ts-node/esm/transpile-only",
"exit": true,
"timeout": 60000,
"recursive": true
Expand Down
6 changes: 3 additions & 3 deletions src/bin/abstract-sql-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {
version,
writeAll,
writeSqlModel,
} from './utils';
} from './utils.js';

import { program } from 'commander';

const runCompile = async (inputFile: string, outputFile?: string) => {
const { generateSqlModel } = await import('../sbvr-api/sbvr-utils.js');
const abstractSql = getAbstractSqlModelFromFile(
const abstractSql = await getAbstractSqlModelFromFile(
inputFile,
program.opts().model,
);
Expand All @@ -28,7 +28,7 @@ const generateTypes = async (
const { abstractSqlToTypescriptTypes } = await import(
'@balena/abstract-sql-to-typescript/generate'
);
const abstractSql = getAbstractSqlModelFromFile(
const abstractSql = await getAbstractSqlModelFromFile(
inputFile,
program.opts().model,
);
Expand Down
6 changes: 3 additions & 3 deletions src/bin/odata-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAbstractSqlModelFromFile, version, writeAll } from './utils';
import { getAbstractSqlModelFromFile, version, writeAll } from './utils.js';
import type {
AbstractSqlModel,
SqlResult,
Expand Down Expand Up @@ -44,7 +44,7 @@ const translateOData = async (
outputFile?: string,
) => {
const request = await generateAbstractSqlQuery(
getAbstractSqlModelFromFile(modelFile, program.opts().model),
await getAbstractSqlModelFromFile(modelFile, program.opts().model),
odata,
);
const json = JSON.stringify(request.abstractSqlQuery, null, 2);
Expand All @@ -68,7 +68,7 @@ const compileOData = async (
outputFile?: string,
) => {
const translatedRequest = await generateAbstractSqlQuery(
getAbstractSqlModelFromFile(modelFile, program.opts().model),
await getAbstractSqlModelFromFile(modelFile, program.opts().model),
odata,
);
const { compileRequest } = await import('../sbvr-api/abstract-sql.js');
Expand Down
4 changes: 2 additions & 2 deletions src/bin/sbvr-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { version, writeAll, writeSqlModel } from './utils';
import { version, writeAll, writeSqlModel } from './utils.js';
import { program } from 'commander';
import * as fs from 'fs';
import fs from 'fs';

const getSE = (inputFile: string) => fs.readFileSync(inputFile, 'utf8');

Expand Down
38 changes: 23 additions & 15 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
process.env.PINEJS_CACHE_FILE =
process.env.PINEJS_CACHE_FILE || __dirname + '/.pinejs-cache.json';
process.env.PINEJS_CACHE_FILE ||
fileURLToPath(new URL('.pinejs-cache.json', import.meta.url));

import type { SqlModel } from '@balena/abstract-sql-compiler';
import type { Config, Model } from '../config-loader/config-loader';
import type * as SbvrUtils from '../sbvr-api/sbvr-utils';
import type { Config, Model } from '../config-loader/config-loader.js';
import type { AbstractSqlModel } from '@balena/abstract-sql-compiler';

import * as fs from 'fs';
import * as path from 'path';
import '../server-glue/sbvr-loader';
import fs from 'fs';
import path from 'path';
import '../server-glue/sbvr-loader.js';
import { fileURLToPath } from 'url';
import { loadSBVR } from '../server-glue/sbvr-loader.js';

export { version } from '../config-loader/env';
export { version } from '../config-loader/env.js';

export const writeAll = (output: string, outputFile?: string): void => {
if (outputFile) {
Expand Down Expand Up @@ -68,15 +70,21 @@ const getConfigModel = (
return fileContents;
};

export const getAbstractSqlModelFromFile = (
export const getAbstractSqlModelFromFile = async (
modelFile: string,
modelName: string | undefined,
): AbstractSqlModel => {
): Promise<AbstractSqlModel> => {
let fileContents: string | Model | AbstractSqlModel | Config;
try {
fileContents = require(path.resolve(modelFile));
fileContents = await import(path.resolve(modelFile));
} catch {
fileContents = fs.readFileSync(require.resolve(modelFile), 'utf8');
fileContents = await fs.promises.readFile(path.resolve(modelFile), 'utf8');
try {
// Try to parse the file as JSON
fileContents = JSON.parse(fileContents);
} catch {
// Ignore error as it's likely just a text sbvr file
}
}
let seModel: string;
if (fileContents == null) {
Expand All @@ -94,16 +102,16 @@ export const getAbstractSqlModelFromFile = (
} else if ('modelText' in configModel && configModel.modelText != null) {
seModel = configModel.modelText;
} else if ('modelFile' in configModel && configModel.modelFile != null) {
seModel = fs.readFileSync(require.resolve(configModel.modelFile), 'utf8');
seModel = await loadSBVR(configModel.modelFile, import.meta);
} else {
throw new Error('Unrecognized config file');
}
} else {
throw new Error('Unrecognized config file');
}
const { generateLfModel, generateAbstractSqlModel } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../sbvr-api/sbvr-utils') as typeof SbvrUtils;
const { generateLfModel, generateAbstractSqlModel } = await import(
'../sbvr-api/sbvr-utils.js'
);
let lfModel;
try {
lfModel = generateLfModel(seModel);
Expand Down
Loading

0 comments on commit 74ecda3

Please sign in to comment.