kristian / minify-xml Goto Github PK
View Code? Open in Web Editor NEWFast XML minifier / compressor / uglifier with a command-line
License: Other
Fast XML minifier / compressor / uglifier with a command-line
License: Other
When I try an argument, which is not available normally the help will called again with the error like "Unknown --my-flag
. Just an Idea. So I would expect, when I had a typo, what can happen with such long arguments like --remove-witespace-between-tags (h is missing here) I should get a hint, that this is an unknown flag.
My NetBeans plugin has a JEditorPane for smth called "Fixed header content". I would like to add such extra stuff to the output file automatically after minifying. The old Java APIs are not working with files, but with streams and they can add the stuff as a prepend textstream to the output stream. The CLIs like google closure compiler, postcss with cssnano and others are working, most of the time with input/output files. So I would like to ask, whether you can add a new CLI argument to add extra content to the output file.
So my case will be: minify-xml --input: my.xml --extra (or whatever) "comments or extra xml" --output: my.min.xml.
For sure I can handle this in an other way, but it would be easier to have such an argument. What do you think?
Hello, in the project I am using "node 16" and when I went to install the library it gave an incompatibility error requiring version ">= 18", can you tell me how dangerous it is for me to follow the installation even using node 16?
Unfortunately I can't update the version to 18 at the moment, I joined the project in progress and I don't know how much it would affect.
Anyway thanks in advance for making the library available, making life easier for many developers out there ๐.
I tried the latest version 2.3.1 and used it like this:
minify-xml foo.xml -o foo.min.xml --remove-whitespace-between-tags true
This is my input file: https://pastebin.com/Dighb1Ek
and this is my output file: https://pastebin.com/wLEr6w79
As you can see, it was minified, and the comments were removed, but the problem is that the space is still there. So I thought the flag --remove-whitespace-between-tags true
will fix this but no. Did I miss a flag for this?
using minifyXML(xml, {removeWhitespaceBetweenTags: 'strict', collapseEmptyElements: false})
is removing whitespace between elements.
input:
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:t xml:space="preserve"> </w:t>
<w:t xml:space="preserve">
</w:t>
<w:t xml:space="preserve"> </w:t>
<w:t xml:space="preserve"> </w:t>
<w:t xml:space="preserve"></w:t>
</w:document>
output:
'<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:t xml:space="preserve"></w:t><w:t xml:space="preserve"></w:t><w:t xml:space="preserve"></w:t><w:t xml:space="preserve"></w:t><w:t xml:space="preserve"></w:t></w:document>'
expected:
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:t xml:space="preserve"> </w:t><w:t xml:space="preserve">
</w:t><w:t xml:space="preserve"> </w:t><w:t xml:space="preserve"> </w:t><w:t xml:space="preserve"></w:t></w:document>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://test.com/index.html</loc>
<lastmod>2023-10-28T18:30:00Z</lastmod>
</url>
</urlset>
could be minified to:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://test.com/index.html</loc>
<lastmod>2023-10-28T18:30:00Z</lastmod>
</url>
</urlset>
The regular expression which is used to detect the used tag namespaces fails when there is a sequence of opening tags without namespace, e.g. for an XML like
<App><Page><customHeader><f:ShellBar title="{i18n>TITLE}" homeIcon="./img/logo_ui5.png"/>
The string <App><Page><customHeader><f
is extracts as a used namespace. But only f
should be extracted.
I think, the character '>' needs to be added to the character set of the RegExp.
I had a file which is around 3.2GB and the minify-xml
had failed to process this file.
The exception message:
RangeError [ERR_FS_FILE_TOO_LARGE]: File size (3441779227) is greater than 2 GB
Stacktrace:
node:fs:416
throw new ERR_FS_FILE_TOO_LARGE(size);
^
RangeError [ERR_FS_FILE_TOO_LARGE]: File size (3441779227) is greater than 2 GB
at new NodeError (node:internal/errors:363:5)
at tryCreateBuffer (node:fs:416:13)
at Object.readFileSync (node:fs:461:14)
at Object.<anonymous> (C:\Users\mofaisal\AppData\Roaming\npm\node_modules\minify-xml\cli.js:106:23)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:816:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
code: 'ERR_FS_FILE_TOO_LARGE'
}
Is it possible to enhance this tool to process large files?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://test.com/index.html</loc>
<lastmod>2023-10-28T18:30:00Z</lastmod>
</url>
</urlset>
could be minified to:
<?xml version="1.0" encoding="UTF-8">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://test.com/index.html</loc>
<lastmod>2023-10-28T18:30:00Z</lastmod>
</url>
</urlset>
https://www.w3.org/TR/xml/#sec-rmd
If there are no external markup declarations, the standalone document declaration has no meaning.
https://stackoverflow.com/questions/5578645/what-does-the-standalone-directive-mean-in-xml
Even after yesterday's fix, it seems that the regular expression still fails to detect the usage of namespaces in attributes.
For example the customData
namespace in <smartTable:SmartTable id="employee_table" customData:p13nDialogSettings='{filter:{visible:false}}' />
.
As far as I can see, the only CLI argument is -i --in-place. But all the options, that I can use in the config file, are needed as a separate cli argument.
example:
minify-xml bla.xml --remove-comments=false
While using this package I found out that it doesn't remove line breaks. Since XML does not require a specific form of line break for correct documents, this would be very handy. Most other minification libraries for JSON, CSS, etc. in fact remove line breaks.
Right now I use this workaround. This does keep text in CDATA sections intact.
MinifyXML.minify(input, { considerPreserveWhitespace: false, shortenNamespaces: false }).replace(/(?<cdata>\<!\[CDATA\[.*?]]>)|(?:\r?\n)/gs, (match: string, cdata: any) => cdata ?? '');
While using this package in browsers despite not using streams, I'm getting errors since Buffer and process are not defined in this environment.
ReferenceError: Buffer is not defined
at node_modules/duplexify/index.js (index.js:6:21)
at __require2 (chunk-62VDRKYJ.js?v=80dff520:19:50)
at node_modules/pumpify/index.js (index.js:3:17)
at __require2 (chunk-62VDRKYJ.js?v=80dff520:19:50)
at node_modules/minify-xml/index.js (index.js:244:17)
at __require2 (chunk-62VDRKYJ.js?v=80dff520:19:50)
at dep:minify-xml:1:16
This is because this package uses duplexify and pumpify packages, which require the Buffer class and the process object.
Is there a possibility for a browser-compatible version? My project has a lot of workers and to fix this issue I have to do weird things like this:
import { Buffer } from 'buffer';
(window as any).Buffer = Buffer;
(window as any).process = { version: 'browser' };
I'm aware there are other workarounds like polyfills, Vite plugins and other solutions, unfortunately they are not always fully reliable.
Please consider including browser-friendly export for this great library.
Node 20 is now the active LTS Iron.
https://nodejs.org/en/about/previous-releases
minify-xml/.github/workflows/node.js.yml
Line 19 in c775553
Hello, I'm trying to use the library but I'm getting this error:
var import_minify_xml = __toESM(require("minify-xml"));
Error [ERR_REQUIRE_ESM]: require() of ES Module
My project is in Typescript, maybe it's some incorrect configuration on my part, but I'd like to ask if you've ever experienced this error?
I'm importing the library this way:
import minifyXML from 'minify-xml';
In dist it looks like this:
var import_minify_xml = __toESM(require("minify-xml"))
As minify-xml does not analyze the XML structure of a document, it also processes the content of CDATA sections, which it shouldn't touch.
<api>
<description>
<![CDATA[
<a
href = "https://github.com"
target = "_blank"
>
**** Hello, world! ****
</a>
]]></description>
</api>
First of all, the implementation contains a typo. It checks for a non-empty array of used
namespaces before removing the unused ones. I guess the intention was to check for unused
to be non-empty.
Fails to remove namespace 'n' from
<a xmlns:n="..." />
Second, the implementation only checks for namespaces that are used for elements, not for namespaces that are used for attributes.
Unintentionally removes namespace 'n' from
<a xmlns:n="..." n:attr="value" />
last but not least, as the regexp based approach does not understand the structure of the XML, it cannot detected when a local namespace is unnecessary in a subtree, it can only decide globally.
Does not remove 2nd declaration of namespace 'n' in
<a>
<b1 xmlns:n="...">
<n:c/>
</b1>
<b2 xmlns:n="...">
</b2>
</a>
This does not cause errors, but does not achieve the optimal reduction.
The 2nd issue is the most important one for our use case in UI5 as we have some special namespaces that are used only for attributes (e.g. for custom data).
Hey, I will use this npm package in my Apache NetBeans plugin Minifierbeans: https://github.com/Chris2011/minifierbeans. For this, I need the CLI where I call it like this: minify-xml myxmlfile.xml -o newfile.min.xml.
So I need the -o --output argument which is also common in such minifier CLIs. I had a look into -h and just see the -i --in-place argument and nothing else.
It would be great, if you can add a flag to add multiple input files and multiple output files. With my plugin https://github.com/Chris2011/minifierbeans it is possible to minify a whole folder with multiple files. The problem is that everytime it finds a file, it will call the CLI and I would like to refactor the code to just call the CLI onces like: minify-xml --in file1.xml file2.xml file3.xml --out file1.min.xml file2.min.xml file3.min.xml.
What do you think about it? I still have a workaround in my mind if you don't think, that this will fit into your package.
Despite speedy performance on other much larger files, minify-xml has been completely halting its work when trying to process files that, as far as I can tell as their common distinguishing factor anyway, have long strings within tags.
This file took about 15 minutes to process: https://pastebin.com/G4fGQ9DB
This file was sitting for something like 40 minutes before I noticed the halted progress and cancelled it: https://pastebin.com/3XMpZciC
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.