Skip to content

Commit

Permalink
fix: make named imports work from ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Oct 16, 2023
1 parent 53dc895 commit bf3849f
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 20 deletions.
3 changes: 2 additions & 1 deletion __tests__/library.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const core = require('../core');
const pretty = require('../dist/openchemlib-full.pretty');
const full = require('../full');
const pretty = require('../full.pretty');
const minimal = require('../minimal');

const minimalAPI = [
'default',
'Molecule',
'Reaction',
'RingCollection',
Expand Down
2 changes: 1 addition & 1 deletion benchmark/diastereoID.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { getDiastereotopicAtomIDs } = require('openchemlib-utils');

const { Molecule, version } = require('../dist/openchemlib-full');
const { Molecule, version } = require('../full');

async function doAll() {
console.log('OCL version', version);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/molfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const Benchmark = require('benchmark');

const OCLNew = require('../dist/openchemlib-full.pretty');
const OCLOld = require('../distold/openchemlib-full.pretty');
const OCLNew = require('../full.pretty');

const idcode = 'enYXNH@MHDAELem`OCIILdhhdiheCDlieKDdefndZRVVjjfjjfjihJBbb@@@';
const mol = OCLNew.Molecule.fromIDCode(idcode);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/sssearcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const Benchmark = require('benchmark');

const OCLNew = require('../dist/openchemlib-full.pretty');
const OCLOld = require('../distold/openchemlib-full.pretty');
const OCLNew = require('../full.pretty');

let benzeneFragmentNew = OCLNew.Molecule.fromSmiles('c1ccccc1');
benzeneFragmentNew.setFragment(true);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/wikipedia.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { Molecule, version } = require('../dist/openchemlib-full');
const { Molecule, version } = require('../full');

async function doAll() {
console.log('OCL version', version);
Expand Down
23 changes: 22 additions & 1 deletion core.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
'use strict';

module.exports = require('./dist/openchemlib-core.js');
const OCL = require('./dist/openchemlib-core.js');

exports.default = OCL;
exports.CanonizerUtil = OCL.CanonizerUtil;
exports.ConformerGenerator = OCL.ConformerGenerator;
exports.DrugScoreCalculator = OCL.DrugScoreCalculator;
exports.DruglikenessPredictor = OCL.DruglikenessPredictor;
exports.ForceFieldMMFF94 = OCL.ForceFieldMMFF94;
exports.Molecule = OCL.Molecule;
exports.MoleculeProperties = OCL.MoleculeProperties;
exports.Reaction = OCL.Reaction;
exports.ReactionEncoder = OCL.ReactionEncoder;
exports.Reactor = OCL.Reactor;
exports.RingCollection = OCL.RingCollection;
exports.SDFileParser = OCL.SDFileParser;
exports.SSSearcher = OCL.SSSearcher;
exports.SSSearcherWithIndex = OCL.SSSearcherWithIndex;
exports.SmilesParser = OCL.SmilesParser;
exports.ToxicityPredictor = OCL.ToxicityPredictor;
exports.Transformer = OCL.Transformer;
exports.Util = OCL.Util;
exports.version = OCL.version;
2 changes: 1 addition & 1 deletion examples/v3000/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { readFileSync } = require('fs');
const { join } = require('path');

const { Molecule } = require('../../dist/openchemlib-full.pretty');
const { Molecule } = require('../../full.pretty');

const molfile = readFileSync(join(__dirname, 'molfile.txt'), 'utf8');
const molecule = Molecule.fromMolfile(molfile);
Expand Down
26 changes: 25 additions & 1 deletion full.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
'use strict';

module.exports = require('./dist/openchemlib-full.js');
const OCL = require('./dist/openchemlib-full.js');

exports.default = OCL;
exports.CanonizerUtil = OCL.CanonizerUtil;
exports.ConformerGenerator = OCL.ConformerGenerator;
exports.DrugScoreCalculator = OCL.DrugScoreCalculator;
exports.DruglikenessPredictor = OCL.DruglikenessPredictor;
exports.ForceFieldMMFF94 = OCL.ForceFieldMMFF94;
exports.Molecule = OCL.Molecule;
exports.MoleculeProperties = OCL.MoleculeProperties;
exports.Reaction = OCL.Reaction;
exports.ReactionEncoder = OCL.ReactionEncoder;
exports.Reactor = OCL.Reactor;
exports.RingCollection = OCL.RingCollection;
exports.SDFileParser = OCL.SDFileParser;
exports.SSSearcher = OCL.SSSearcher;
exports.SSSearcherWithIndex = OCL.SSSearcherWithIndex;
exports.SVGRenderer = OCL.SVGRenderer;
exports.SmilesParser = OCL.SmilesParser;
exports.StructureEditor = OCL.StructureEditor;
exports.StructureView = OCL.StructureView;
exports.ToxicityPredictor = OCL.ToxicityPredictor;
exports.Transformer = OCL.Transformer;
exports.Util = OCL.Util;
exports.version = OCL.version;
26 changes: 25 additions & 1 deletion full.pretty.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
'use strict';

module.exports = require('./dist/openchemlib-full.pretty.js');
const OCL = require('./dist/openchemlib-full.pretty.js');

exports.default = OCL;
exports.CanonizerUtil = OCL.CanonizerUtil;
exports.ConformerGenerator = OCL.ConformerGenerator;
exports.DrugScoreCalculator = OCL.DrugScoreCalculator;
exports.DruglikenessPredictor = OCL.DruglikenessPredictor;
exports.ForceFieldMMFF94 = OCL.ForceFieldMMFF94;
exports.Molecule = OCL.Molecule;
exports.MoleculeProperties = OCL.MoleculeProperties;
exports.Reaction = OCL.Reaction;
exports.ReactionEncoder = OCL.ReactionEncoder;
exports.Reactor = OCL.Reactor;
exports.RingCollection = OCL.RingCollection;
exports.SDFileParser = OCL.SDFileParser;
exports.SSSearcher = OCL.SSSearcher;
exports.SSSearcherWithIndex = OCL.SSSearcherWithIndex;
exports.SVGRenderer = OCL.SVGRenderer;
exports.SmilesParser = OCL.SmilesParser;
exports.StructureEditor = OCL.StructureEditor;
exports.StructureView = OCL.StructureView;
exports.ToxicityPredictor = OCL.ToxicityPredictor;
exports.Transformer = OCL.Transformer;
exports.Util = OCL.Util;
exports.version = OCL.version;
13 changes: 12 additions & 1 deletion minimal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
'use strict';

module.exports = require('./dist/openchemlib-minimal.js');
const OCL = require('./dist/openchemlib-minimal.js');

exports.default = OCL;
exports.Molecule = OCL.Molecule;
exports.Reaction = OCL.Reaction;
exports.RingCollection = OCL.RingCollection;
exports.SDFileParser = OCL.SDFileParser;
exports.SSSearcher = OCL.SSSearcher;
exports.SSSearcherWithIndex = OCL.SSSearcherWithIndex;
exports.SmilesParser = OCL.SmilesParser;
exports.Util = OCL.Util;
exports.version = OCL.version;
30 changes: 25 additions & 5 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ function compile(mode) {
}
}

function build() {
async function build() {
let prom = [];
fs.mkdirSync('dist', { recursive: true });
for (let k = 0; k < modules.length; k++) {
let mod = modules[k];
log(`Exporting module ${mod.name}`);
log('Exporting modules');
for (const mod of modules) {
log(`Exporting module ${mod.name}${suffix}`);
let warDir = path.join('war', mod.war);
let files = fs.readdirSync(warDir);
let file;
Expand All @@ -230,7 +230,27 @@ function build() {
}),
);
}
return Promise.all(prom);
await Promise.all(prom);

log('Creating ESM-compatible entry points');
for (const mod of modules) {
log(`Creating ESM-compatible entry point for module ${mod.name}${suffix}`);
const moduleInstance = require(`../dist/openchemlib-${mod.name}${suffix}.js`);
const moduleExports = Object.keys(moduleInstance).map(
(moduleExport) => `exports.${moduleExport} = OCL.${moduleExport};`,
);
const facade = `'use strict';
const OCL = require('./dist/openchemlib-${mod.name}${suffix}.js');
exports.default = OCL;
${moduleExports.join('\n')}
`;
fs.writeFileSync(
path.join(__dirname, `../${mod.name}${suffix}.js`),
facade,
);
}
}

function log(value) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import OCL from '../dist/openchemlib-full.js';
import OCL from '../full.js';

let molecule = OCL.Molecule.fromSmiles('C[C@H](Cl)CC');
molecule = OCL.Molecule.fromSmiles('CC=C(O)CC');
Expand Down
7 changes: 3 additions & 4 deletions tests/transformer.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import OCL from '../dist/openchemlib-full.pretty.js';
const { Molecule, Transformer } = OCL;
import { Molecule, Transformer } from '../full.pretty.js';

const reagent = OCL.Molecule.fromSmiles('CCO');
const reagent = Molecule.fromSmiles('CCO');

reagent.setFragment(true);
for (let i = 0; i < 3; i++) {
reagent.setAtomMapNo(i, i);
}
const product = OCL.Molecule.fromSmiles('C=C.O');
const product = Molecule.fromSmiles('C=C.O');
product.setFragment(true);
for (let i = 0; i < 3; i++) {
product.setAtomMapNo(i, i);
Expand Down

0 comments on commit bf3849f

Please sign in to comment.