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
  • Loading branch information
eyelidlessness committed Jan 13, 2023
1 parent 874b4b8 commit 2a41ca0
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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();
39 changes: 39 additions & 0 deletions config/build.shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// @ts-check

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

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
*/
const readFile = (path) => fs.readFileSync(resolvePath(path), 'utf-8');

export const config = JSON.parse(readFile('./config/config.json'));
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
26 changes: 16 additions & 10 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'path';
import { defineConfig } from 'vitest/config';
import { VitePluginNode } from 'vite-plugin-node';
import config from './config/config.json';
import { external } from './config/build.shared';

export default defineConfig({
assetsInclude: ['**/*.xml', '**/*.xsl'],
Expand All @@ -12,19 +12,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 +56,6 @@ export default defineConfig({
globals: true,
include: ['test/**/*.spec.ts'],
reporters: 'verbose',
sequence: { shuffle: true },
sequence: { shuffle: false },
},
});

0 comments on commit 2a41ca0

Please sign in to comment.