Skip to content
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

xmldom can not be imported from ES modules #316

Open
amitdhiman000 opened this issue Sep 2, 2021 · 11 comments
Open

xmldom can not be imported from ES modules #316

amitdhiman000 opened this issue Sep 2, 2021 · 11 comments
Labels
awaiting response Maintainers are waiting for information question Contains open questions that need to be answered testing
Milestone

Comments

@amitdhiman000
Copy link

"@xmldom/xmldom": "0.7.4"

Not able to import the modules with web test runner.

SyntaxError: The requested module './node_modules/@xmldom/xmldom/lib/index.js' does not provide an export named 'DOMParser'

facing same issue with older versions as well.

@karfau
Copy link
Member

karfau commented Sep 2, 2021

@amitdhiman000 can you point to something that is available publicly where it is not working?

Or provide more detail how to reproduce it? I'm not aware of the details regarding web test runner.

If you are able to you could even create a branch/PR that adds a github action check which is failing and we can see what we can do about it.

@karfau karfau added awaiting response Maintainers are waiting for information question Contains open questions that need to be answered testing labels Sep 2, 2021
@amitdhiman000
Copy link
Author

@karfau

Code is not available in public domain, it is a private git repo in my organisation.

Here is sample project to demonstrate the issue repo link
npm install
npm run test

@karfau
Copy link
Member

karfau commented Sep 2, 2021

Thank you, I have tried it and followed the documentation a bit.
As far as I understand currently, the issue is that xmldom is only providing CommonJS Modules

Can you check if one of the options listed there work for you?

Since I don't have a lot of experience on that level (and I'm not sure one of the other maintainers has), we might need help to provide other formats and we need to discuss what we want/need to support. In this regard it is also linked to #76

@karfau karfau added this to the before 1.0.0 milestone Sep 2, 2021
@karfau karfau changed the title import { DOMParser } doesn't work. xmldom can not be imported from ES modules Sep 3, 2021
@amitdhiman000
Copy link
Author

amitdhiman000 commented Sep 3, 2021

@karfau

Thanks for your suggestions.
I dig little bit deeper and found the solution for the problem.
I used the commonjs plugin, to load @xmldom/xmldom package.

web-test-runner-config.js

import rollupCommonjs  from '@rollup/plugin-commonjs';
import { fromRollup } from '@web/dev-server-rollup';

const commonjs = fromRollup(rollupCommonjs);

export default {
    nodeResolve: true,
    plugins: [
        commonjs({
            include: ["**/node_modules/@xmldom/xmldom/lib/*.js"]
        })
    ]
};

@karfau
Copy link
Member

karfau commented Sep 4, 2021

Based on my current understanding we should be able to make xmldom available for the ES module world by generating an es module for each commonjs module.
I followed the "rollup track" and it looks like https://github.com/eight04/cjs-es is an option for that.

Before implementing what seems to be the most common use case on our side I filed eight04/cjs-es#27

There are also other tools that have a cli:

Another option might be to use typescript to convert the modules, https://blog.logrocket.com/publishing-node-modules-typescript-es-modules/ contains the basic concepts, here is the related typescript reference https://www.typescriptlang.org/docs/handbook/2/modules.html. (And since we anyways plan to generate our types (see #285), this approach has the lower amount of devDependencies.)

We would also need to decide how to include both formats into the package. There are quite some options:

and potential issues:

and we need to make sure we stay backward compatible. So another option (for the time being) might be to publish the ES Module package under a separate name?

@ anybody: Please share any other ideas of how to solve this specific issue.

@karfau karfau modified the milestone: before 1.0.0 Dec 21, 2021
@stell
Copy link

stell commented Apr 4, 2022

Any news on an ES6 version of this?

@karfau
Copy link
Member

karfau commented Apr 5, 2022

Any news on an ES6 version of this?

My previous comment is still the most up to date one. (Tldr: some tooling needs to currently be applied that is not part of this library.)

But I have added some examples to the repo that show how it is possible to use the library in such setups.

If you have a specific use case/setup that is not covered yet, please provide an example via github, stackblitz or something similar.

And of course if somebody has an idea how to achieve this in a backward compatible way and without a rewrite, PRs are very welcome.

@taisukef
Copy link

I made an ES6 version.
https://github.com/code4fukui/xmldom-es

You can run on Deno REPL

import { DOMParser, XMLSerializer } from "https://code4fukui.github.io/xmldom-es/xmldom.js";

const source = `<xml xmlns="a">
	<child>test</child>
	<child/>
</xml>`;

new DOMParser().parseFromString(source, "text/xml");

@karfau
Copy link
Member

karfau commented Jun 19, 2022

@taisukef thx for providing an example of what needs to be done to switch to modules and support deno.

I still think we need to find a way to additionally continue to support ES5.

I'm guessing that we could migrate the code like you did but put it into mjs files and add a build/transpilation step to continue to support ES5?
Ideally in a way that it doesn't break existing usages.

@fdb
Copy link

fdb commented Apr 2, 2023

Hi,

Using Rollup I also get the same issue that it can't find DOMParser as an export:

src/main.js → dist/bundle.js...
[!] RollupError: "DOMParser" is not exported by "node_modules/@xmldom/xmldom/lib/index.js", imported by "src/main.js".
https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module
src/main.js (1:9)
1: import { DOMParser } from '@xmldom/xmldom';

I made a minimal example project here:

https://stackblitz.com/edit/node-ert3fy?file=src/main.js

My main.js looks like this:

import { DOMParser } from '@xmldom/xmldom';

const source = `<xml xmlns="a"><child>test</child><child/></xml>`;

const dom = new DOMParser().parseFromString(source, 'text/xml');
// .... other code ....

My rollup.config.js looks like this:

import { nodeResolve } from '@rollup/plugin-node-resolve';

export default {
  input: 'src/main.js',
  output: {
    file: 'dist/bundle.js',
    format: 'iife',
    name: 'app',
    sourcemap: true,
  },
  plugins: [nodeResolve()],
};

@karfau
Copy link
Member

karfau commented Apr 5, 2023

@fdb I am not experienced with rollup, but did you try to modify your config in a way that was suggested in this comment: #316 (comment) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Maintainers are waiting for information question Contains open questions that need to be answered testing
Projects
Status: Has Priority
Development

No branches or pull requests

5 participants