NPM publishing
The current package published to NPM is seriously broken.
Based on your publishing script, what you are publishing to NPM here is the full mxgraph distribution. That is all source code, all build files, all resources in all languages. Additionally the package.json incorrectly contains webpack, grunt etc. as dependencies instead of devDependencies. Thus installing just the mxgraph package as a dependency in a newly created empty folder results in a total of 92 MB of files.
The package.json further defines "main": "./javascript/dist/build.js" to be the entry point of the package. The dist folder is however excluded through the .gitignore file. Thus the mxgraph library actually cannot be included from npm in its current form - the package is broken. The github repository recently received a .npmignore file (https://github.com/jgraph/mxgraph/commit/78a73184b0fd0fcb138bc8ba425d47047282faa6) which mitigates this issue, this is however not in the bitbucket repository yet (although referenced in the deploy script).
Your build script actually also pushes the js code to a separate mxgraph-js repository. This looks a bit more like what an npm package should contain but is also incompatible with npm usage. In order to be compatible with your documentation, the main file should include the UMD loader code you add during the grunt build process. Nevertheless it is of course a cleaner solution to directly publish to npm from the original code.
In order to fix these issues I created a fork with a single additional commit on bitbucket: https://bitbucket.org/lgleim/mxgraph2
Unfortunately I cannot create pull requests there and the github repo has a somewhat different structure.
Since I am not familiar with all the other requirements to be fulfilled in order to execute the build.sh script, I was not able to test the code with your build script. The build does complete successfully with the following steps:
git clone [email protected]:lgleim/mxgraph2.git
cd mxgraph2
sed "s/@VERSION@/3.9.2/" etc/build/mxgraph-package.json > package.json
npm install
# npm publish
So I kindly ask you to review and manually merge my patch and (hopefully) to subsequently publish a working version of mxgraph to the npm registry.
This change changes the behaviour of the deployment to git. It clones the Github version over the release, we currently do it the other way around. Unfortunately, I can't accept this change.
Fine. Just please unpublish your npm package in order to stop confusing people if you do not intend to fix it.
Then we'll just have a pile more requests for publishing to NPM. If someone can submit a PR to the BB repo with a fix that doesn't break our deploy, we're happy to accept it.
Is there already a fix for this issue?
What problem are you seeing?
When I install mxGraph via
npm install mxgraph --save and
and load it in my js using
var mxgraph = require("mxgraph")(
mxImageBasePath: "./src/images",
mxBasePath: "./src"
})
I get the error Module not found
The real issue is that the bundled script, i.e. the package's main script is simply not included in what is published to npm.
And I don't know what that means unfortunately, thus the impass :).
the first hit on google: https://www.sitepoint.com/javascript-modules-bundling-transpiling/ Thus project is using a homegrown Grunt build script (quite ugly) which parses the "./javascript/src/js/mxClient.js" file for all includes it defines and effectively just concats all the files together, but wrapping the concatenated code into a UMD Loader function. (Comparison of Module Loaders, missing native ES6 Modules). This creates the bundle ('./javascript/dist/build.js'), a single file which contains the entire library, which is also the main thing that needs to be published to npm. On a side note - there are way better ways to publish libraries today which allow for lazy loading of modules only if their code is actully needed, thus minimizing site loading times. Also you do not need to write Ecmascript 3 anymore. There are a number of Transpilers available that can take ES2017+ and translate it to legacy ES3 code.
Back to the task at hand: In your current build script the commands npm install and npm publish --access public run the grunt build script, which however fails because some files have wrongly been deleted at that point.
Can we just move lines 64 and 65 line upwards https://bitbucket.org/jgraph/mxgraph2/src/master/etc/build/deploy.sh ?
That keeps the git deploy as it was, which was the problem with the previous patch.
We plan to move this to Github at some point, which will simplify things a little, but it's finding the resources to do it.
I just tried npm install mxgraph on my command line and that works with 3.9.7. I'm assuming this issue is now resolved.
I am getting the same issue with 3.9.8, any pointers on how to fix it?
Just to reiterate - the general issue is the following:
The mxgraph package published to npm does not actually include the main entry file "main": "./javascript/dist/build.js", that is declared in the package.json. Thus (as far I can tell) if it is just installed as a general dependency (npm install without arguments, resolving your projects package.json dependencies), the file will never ever be able to be imported. Thus this issue.
However the package.json includes a prepare script ("prepare": "grunt build --base ./ --gruntfile etc/build/Gruntfile.js"), which is exectued by npm if you specifically manually install the mxgraph package.
npm install mxgraph (or even better npm install github:jgraph/mxgraph) pulls what is published to npm, actually installs all dev dependencies of the mxgraph library and builds it locally. Thus you can get it to work and this process also does not fail deterministically (hail npm) but it is still due to the simple fact, that the way this project is published to npm does not make any sense.
I ended up on this bug while trying to understand how to use the module with webpack.
I think I found a reasonable workaround that allows to use the current npm package, I document it here in case it may be useful for somebody else:
In the webpack configuration I added:
module: {
rules: [
{
test: require.resolve('mxgraph/javascript/mxClient.min.js'),
use: [
'imports-loader?mxBasePath=>"/ui/static/mxgraph/"',
'exports-loader?mxEvent,mxGraph,mxRubberband'
]
}
]
}
This makes sure the mxBasePath is properly set and the required variables are exported from the volume. Notice that I include the minified version to avoid troubles, this is normally not recommended.
Then to make the mxgraph assets available I add the following:
plugins: [
// copy mxgraph assets
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../node_modules/mxgraph/javascript/src'),
to: config.dev.assetsSubDirectory + '/mxgraph',
ignore: ['.*']
}
])
]
With these two changes I can then use the module as follows:
import {mxEvent, mxGraph, mxRubberband} from 'mxgraph/javascript/mxClient.min.js'
var graph = new mxGraph(container)