Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Fix build, a few errors found in core and express
Browse files Browse the repository at this point in the history
- Fixes Vite build
- Fixes `tsc`
    - Adds types for config/build.shared.js
    - Uses shared build config where possible
    - Adds typedef emit
- Fixes reference to `config.json` which works locally but failed in CI
  • Loading branch information
eyelidlessness committed Jan 13, 2023
1 parent 874b4b8 commit 5af98f3
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"error",
{
"devDependencies": [
"app.js",
"app.ts",
"vite.config.ts",
"src/api.ts",
Expand All @@ -59,7 +60,7 @@
"import/no-unresolved": [
"error",
{
"ignore": ["vitest/config"]
"ignore": ["vitest/config", ".*?raw"]
}
],
"import/order": "warn",
Expand Down
63 changes: 63 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// @ts-check

import { createServer } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';
import {
config,
external,
resolvePath,
rootDir,
} from './config/build.shared.js';

const appPath = resolvePath('./app.ts');

const init = async () => {
/** @type {import('vite').UserConfig} */
const baseOptions = {
mode: 'development',
build: {
rollupOptions: {
external,
},
},
optimizeDeps: {
disabled: true,
},
root: rootDir,
ssr: {
target: 'node',
},
};

const servers = await Promise.all([
createServer({
...baseOptions,
configFile: false,
plugins: VitePluginNode({
adapter: 'express',
appPath,
exportName: 'app',
tsCompiler: 'esbuild',
}),
server: {
port: config.port,
},
}),
createServer({
...baseOptions,
configFile: false,
publicDir: resolvePath('./test/forms'),
server: {
port: 8081,
},
}),
]);

await Promise.all(servers.map((server) => server.listen()));

servers.forEach((server) => {
server.printUrls();
});
};

init();
2 changes: 1 addition & 1 deletion app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bodyParser from 'body-parser';
import express from 'express';
import config from './config/config.json';
import { config } from './config/build.shared';
import { api } from './src/api';

const app = express();
Expand Down
7 changes: 7 additions & 0 deletions config/build.shared.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {
config,
external,
resolvePath,
readFile,
rootDir,
} from './build.shared';
42 changes: 42 additions & 0 deletions config/build.shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @ts-check

import fs from 'fs';
import { createRequire } from 'module';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

const require = createRequire(import.meta.url);

export const config = require('./config.json');

export const external = [
'body-parser',
'crypto',
'css.escape',
'express',
'fs',
'language-tags',
'libxslt',
'node1-libxmljsmt-myh',
'path',
'string-direction',
'undici',
'url',
'vite-node',
'vite',
];

export const rootDir = dirname(fileURLToPath(import.meta.url)).replace(
/(\/enketo-transformer)(\/.*$)/,
'$1'
);

/**
* @param {string} path
*/
export const resolvePath = (path) => resolve(rootDir, path);

/**
* @param {string} path
*/
export const readFile = (path) => fs.readFileSync(resolvePath(path), 'utf-8');
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"type": "module",
"main": "./dist/enketo-transformer.umd.cjs",
"module": "./dist/enketo-transformer.js",
"types": "./dist/src/transformer.d.ts",
"exports": {
".": {
"import": "./dist/enketo-transformer.js",
"require": "./dist/enketo-transformer.umd.cjs"
"require": "./dist/enketo-transformer.umd.cjs",
"types": "./dist/src/transformer.d.ts"
}
},
"bugs": {
Expand All @@ -27,15 +29,15 @@
},
"contributors": [],
"scripts": {
"build": "vite build",
"build": "vite build && npm run tsc",
"start": "vite",
"eslint-check": "eslint app.ts src/**/*.ts vite.config.ts test/**/*.ts",
"eslint-fix": "eslint app.ts src/**/*.ts vite.config.ts test/**/*.ts --fix",
"prettier-fix": "prettier --write .",
"test": "vitest run --coverage && npm run prettier-fix && npm run eslint-fix && tsc && node update-readme-with-shield-badge.cjs",
"test": "vitest run --coverage && npm run prettier-fix && npm run eslint-fix && npm run tsc && node update-readme-with-shield-badge.cjs",
"test:watch": "vitest",
"develop": "DEBUG=api,transformer,markdown,language vite & http-server test/forms -p 8081",
"tsc": "tsc --project . && tsc --project ./test"
"tsc": "tsc --project ./tsconfig.json && tsc --project ./tsconfig.typedefs.json && tsc --project ./test/tsconfig.json"
},
"repository": {
"type": "git",
Expand Down
25 changes: 13 additions & 12 deletions src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import fs from 'fs';
import crypto from 'crypto';
import libxslt from 'libxslt';
import type {
Document as XMLJSDocument,
DocumentFragment as XMLJSDocumentFragment,
} from 'libxmljs';
import pkg from '../package.json';
import xslForm from './xsl/openrosa2html5form.xsl?raw';
import xslModel from './xsl/openrosa2xmlmodel.xsl?raw';
import language from './language';
import markdown from './markdown';
import { escapeURLPath, getMediaPath } from './url';

const { libxmljs } = libxslt;

const getXSL = (fileName: string) => {
const { pathname } = new URL(`./xsl/${fileName}`, import.meta.url);

return fs.readFileSync(pathname, 'utf8');
};

const xslForm = getXSL('openrosa2html5form.xsl');
const xslModel = getXSL('openrosa2xmlmodel.xsl');

export const NAMESPACES = {
xmlns: 'http://www.w3.org/2002/xforms',
orx: 'http://openrosa.org/xforms',
Expand Down Expand Up @@ -103,12 +95,19 @@ export const transform = (survey: Survey): Promise<TransformedSurvey> => {

const model = docToString(xmlDoc);

return {
// @ts-expect-error - This fails because `xform` is not optional, but this is API-consistent behavior.
delete survey.xform;
delete survey.media;
delete survey.preprocess;
delete survey.markdown;
delete survey.openclinica;

return Object.assign(survey, {
form,
model,
languageMap,
transformerVersion: PACKAGE_VESION,
};
});
});
};

Expand Down Expand Up @@ -515,6 +514,8 @@ const PACKAGE_VESION = pkg.version;

const VERSION = md5(xslForm + xslModel + PACKAGE_VESION);

export { VERSION as version };

export const sheets = {
xslForm,
xslModel,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "./tsconfig.base.json",
"exclude": ["dist", "node_modules", "./typings/test.d.ts"],
"include": ["app.ts", "src", "typings"],
"include": ["app.ts", "config", "src", "typings"],
"files": ["./typings/env.d.ts"]
}
10 changes: 10 additions & 0 deletions tsconfig.typedefs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"noEmit": false,
"outDir": "./dist"
},
"extends": "./tsconfig.json",
"include": ["./src/transformer.ts", "./typings"]
}
27 changes: 16 additions & 11 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { resolve } from 'path';
import { defineConfig } from 'vitest/config';
import { VitePluginNode } from 'vite-plugin-node';
import config from './config/config.json';
import { config, external } from './config/build.shared';

export default defineConfig({
assetsInclude: ['**/*.xml', '**/*.xsl'],
Expand All @@ -12,19 +11,25 @@ export default defineConfig({
},
minify: false,
outDir: 'dist',
rollupOptions: {
external,
output: {
// This suppresses a warning for modules with both named and
// default exporrs when building for CommonJS (UMD in our
// current build). It's safe to suppress this warning because we
// have explicit tests ensuring both the default and named
// exports are consistent with the existing public API.
exports: 'named',
},
},
sourcemap: true,
},
esbuild: {
sourcemap: 'inline',
},
plugins: [
...VitePluginNode({
adapter: 'express',
appPath: './app.ts',
exportName: 'app',
tsCompiler: 'esbuild',
}),
],
optimizeDeps: {
disabled: true,
},
server: {
port: config.port,
},
Expand All @@ -50,6 +55,6 @@ export default defineConfig({
globals: true,
include: ['test/**/*.spec.ts'],
reporters: 'verbose',
sequence: { shuffle: true },
sequence: { shuffle: false },
},
});

0 comments on commit 5af98f3

Please sign in to comment.