-
Notifications
You must be signed in to change notification settings - Fork 245
Update node-main to work in (webpacked) browser as well as node, and rename node-main to main
#1566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
ec5e485
c11b909
bc3d466
dc3c97e
21d88a3
137cae2
03efc2d
2ade081
9106384
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import {Locale} from '#js/util/Locale.js'; | ||
|
|
||
| Locale.isComponent = true; | ||
|
|
||
| for (const location of Object.values(Locale.locations)) { | ||
| location[0].replace(/^(?:\.\.\/)*ts\//, '[mathjax]/'); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('./browser.js').default; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
| import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'browser' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; | ||
|
|
||
| export const MathJax: MathJaxObject; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
| import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'browser' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; | ||
|
|
||
| export const MathJax: MathJaxObject; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
| import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'browser' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; | ||
|
|
||
| export const MathJax: MathJaxObject; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { MathJax, combineConfig, combineDefaults } from '#js/components/global.js'; | ||
| import { Loader } from '#js/components/loader.js'; | ||
| import { hasWindow } from '#js/util/context.js'; | ||
|
|
||
| if (hasWindow) { | ||
| window.exports ??= {}; | ||
| } | ||
|
|
||
| /** | ||
| * This funciton is called below and by ../main.js in order to create | ||
| * the needed init() function that is exported for initializing | ||
| * MathJax and returning the completed MathJax object. For use in node, | ||
| * the nodeSetup function is called to do extra setup (like setting the | ||
| * the asyncLoad function and getting the path to the MathJax files), and | ||
| * the adaptor will be added to the loading list if one isnt already | ||
| * inclkuded there. | ||
| * | ||
| * @param {() => void} nodeSetup The function to do extra setup for node | ||
| * @param {string} adaptor The adaptor to use for node | ||
| * @returns {(config} => MathJax} The init() function for export | ||
| */ | ||
| export function makeInit(nodeSetup = null, adaptor = '') { | ||
| return async function init(config, component = 'startup') { | ||
| Loader.preLoaded('loader', 'core'); | ||
| MathJax.config.loader.failed = (err) => {throw err}; | ||
| if (hasWindow) { | ||
| MathJax.config.loader.paths.mathjax = 'https://cdn.jsdelivr.net/npm/mathjax'; | ||
| } | ||
| if (nodeSetup) { | ||
| await nodeSetup(); | ||
| } | ||
| combineConfig(MathJax.config, config); | ||
| if (adaptor) { | ||
| const CONFIG = MathJax.config.loader; | ||
| CONFIG.load ??= []; | ||
| if (CONFIG.load.filter((name) => name.substring(0, 9) === 'adaptors/').length === 0) { | ||
| CONFIG.load.unshift(adaptor); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure I fully understand:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that If a third-party adaptor is included in the I suppose it would be better to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you still want to do this? |
||
| } | ||
| } | ||
| MathJax.loader = Loader; | ||
| if (component) { | ||
| await Loader.load(component); | ||
| } | ||
| await MathJax.startup?.promise ?? Promise.resolve(); | ||
| return MathJax; | ||
| } | ||
| }; | ||
|
|
||
| export const init = MathJax.init = makeInit(); | ||
| export default MathJax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import MathJax from './browser.js'; | ||
|
|
||
| export default MathJax; | ||
| export const init = MathJax.init; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "copy": { | ||
| "to": "[bundle]/main", | ||
| "from": ".", | ||
| "copy": [ | ||
| "browser.mjs", | ||
| "browser.d.mts", | ||
| "browser.d.ts" | ||
| ] | ||
| }, | ||
| "webpack": { | ||
| "name": "main/browser", | ||
| "noDTS": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const json = (file) => fetch(file).then((data) => data.json()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const json: (file: string) => Promise<any>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| const webpack = require('webpack'); | ||
|
|
||
| module.exports = (pkg) => { | ||
| pkg.output.library = { | ||
| type: 'commonjs2', | ||
| export: ['default'], | ||
| }; | ||
| pkg.plugins.push( | ||
| new webpack.NormalModuleReplacementPlugin( | ||
| /mjs\/components\/mjs\/json\.js/, | ||
| '../../../components/mjs/main/browser/json.cjs', | ||
| ), | ||
| new webpack.optimize.LimitChunkCountPlugin({ | ||
| maxChunks: 1, | ||
| }), | ||
| ); | ||
| return pkg; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "copy": { | ||
| "to": "[bundle]/main", | ||
| "from": ".", | ||
| "copy": [ | ||
| "main.mjs", | ||
| "main.d.mts", | ||
| "main.d.ts" | ||
| ] | ||
| }, | ||
| "webpack": { | ||
| "name": "main/main", | ||
| "noDTS": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| let REQUIRE; | ||
| try { REQUIRE = eval("require") } catch (_) { REQUIRE = () => {} } | ||
| export const json = (file) => Promise.resolve().then(() => REQUIRE(file)); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const json: (file: string) => Promise<any>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('./main.js').default; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
| import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'liteDOM' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; | ||
|
|
||
| export const MathJax: MathJaxObject; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
| import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'liteDOM' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; | ||
|
|
||
| export const MathJax: MathJaxObject; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
| import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'liteDOM' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; | ||
|
|
||
| export const MathJax: MathJaxObject; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { MathJax } from '#js/components/global.js'; | ||
| import { hasWindow } from '#js/util/context.js'; | ||
| import { setup, adaptor } from './node/setup.js'; | ||
| import { makeInit } from './browser/browser.js'; | ||
|
|
||
| if (!hasWindow) { | ||
| MathJax.init = makeInit(setup, adaptor); | ||
| } | ||
|
|
||
| export const init = MathJax.init; | ||
| export default MathJax; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import MathJax from './main.js'; | ||
|
|
||
| export default MathJax; | ||
| export const init = MathJax.init; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "copy": { | ||
| "to": "[bundle]/main", | ||
| "from": ".", | ||
| "copy": [ | ||
| "data.cjs", | ||
| "data.d.cts", | ||
| "node.mjs", | ||
| "node.d.mts", | ||
| "node.d.ts" | ||
| ] | ||
| }, | ||
| "webpack": { | ||
| "name": "main/node", | ||
| "noDTS": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| let REQUIRE; | ||
| try { REQUIRE = eval("require") } catch(_) { REQUIRE = () => {} } | ||
| const path = REQUIRE("path"); | ||
| const fs = REQUIRE("fs"); | ||
|
|
||
| let source = null; | ||
| if (fs.existsSync(path.resolve(__dirname, '..', '..', 'source.js'))) { | ||
| source = async () => {return (await import(/* webpackIgnore: true */ '../../source.js')).source}; | ||
| } | ||
|
|
||
| module.exports.REQUIRE = REQUIRE; | ||
| module.exports.path = path; | ||
| module.exports.source = source; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'liteDOM' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('./node.js').default; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { | ||
| COMPONENT_LIST, | ||
| ADAPTOR_LIST, | ||
| MATHJAX_CONFIG, | ||
| MATHJAX_OBJECT, | ||
| MATHJAX_COMPONENTS, | ||
| ADAPTOR_DOM, | ||
| } from '@mathjax/src/mjs/types/mathjax.js'; | ||
| import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js'; | ||
|
|
||
| export function init< | ||
| T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup', | ||
| A extends ADAPTOR_LIST = 'liteDOM' | ||
| >( | ||
| config: MATHJAX_CONFIG<T, A>, | ||
| component?: MATHJAX_COMPONENTS<T, A> | ||
| ): Promise<MATHJAX_OBJECT<T, A>>; | ||
|
|
||
| export const MathJax: MathJaxObject; |
Uh oh!
There was an error while loading. Please reload this page.