Use dynamic imports for mathjs

This commit is contained in:
Peter Stuifzand 2021-08-15 23:52:43 +02:00
parent 5850c556c8
commit c16074cabf
2 changed files with 36 additions and 32 deletions

View File

@ -6,7 +6,6 @@ import search from "./search";
import axios from 'axios';
import qs from 'querystring'
import $ from 'jquery';
import Mustache from 'mustache';
import getCaretCoordinates from './caret-position'
import moment from 'moment'
// import mermaid from 'mermaid'
@ -20,9 +19,6 @@ import moment from 'moment'
// import 'prismjs/components/prism-markup-templating'
// import 'prismjs/components/prism-jq'
import MD from './markdown'
import he from 'he'
import {all, create} from 'mathjs'
import formulaFunctions from './formula'
import actions from './actions'
moment.locale('nl')
@ -319,26 +315,27 @@ function Editor(holder, input) {
})
}
function transformMathExpression(converted, scope) {
try {
async function transformMathExpression(converted, scope) {
return Promise.all([
import(/* webpackChunkName: "mathjs" */ 'mathjs'),
import(/* webpackChunkName: "mathjs" */ './formula')
]).then((promises) => {
const mathjs = promises[0]
if (math === undefined) {
math = create(all)
math.import(formulaFunctions)
math = mathjs.create(mathjs.all)
math.import(promises[1])
}
let expr = converted.substring(1);
let parsedExpr = math.parse(expr)
let compiled = parsedExpr.compile()
let evaluated = compiled.evaluate(scope);
if (parsedExpr.isAssignmentNode) {
converted = parsedExpr.object.name + " = <i>" + evaluated.toString() + "</i>"
} else {
converted = "<span class='expression'>" + expr + " = </span><i>" + evaluated.toString() + "</i>"
return parsedExpr.object.name + " = <i>" + evaluated.toString() + "</i>"
}
} catch (e) {
converted = converted + ' <span style="background: red; color: white;">' + e.message + '</span>';
}
return converted;
return "<span class='expression'>" + expr + " = </span><i>" + evaluated.toString() + "</i>"
}).catch(e => {
return converted + ' <span style="background: red; color: white;">' + e.message + '</span>';
})
}
function transform(text, element, id, editor, scope) {
@ -359,7 +356,14 @@ function Editor(holder, input) {
} else if (converted.startsWith("```", 0) || converted.startsWith("$$", 0)) {
converted = MD.render(converted)
} else if (converted.startsWith("=", 0)) {
converted = transformMathExpression(converted, scope);
transformMathExpression(converted, scope)
.then(converted => {
MD.options.html = true
converted = MD.renderInline(converted)
MD.options.html = false
element.html(converted)
})
} else {
let re = /^([A-Z0-9 ]+)::\s*(.*)$/i;
let res = text.match(re)

View File

@ -59,21 +59,21 @@ module.exports = env => {
//
optimization: {
usedExports: true,
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
},
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true,
},
}
}
// splitChunks: {
// cacheGroups: {
// commons: {
// test: /[\\/]node_modules[\\/]/,
// name: 'vendors',
// chunks: 'all'
// },
// styles: {
// name: 'styles',
// test: /\.css$/,
// chunks: 'all',
// enforce: true,
// },
// }
// }
}
}
};