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

Request: Add purify.min.js and purify.js to package.json exports #1050

Closed
Aetherinox opened this issue Dec 13, 2024 · 7 comments
Closed

Request: Add purify.min.js and purify.js to package.json exports #1050

Aetherinox opened this issue Dec 13, 2024 · 7 comments

Comments

@Aetherinox
Copy link
Contributor

Aetherinox commented Dec 13, 2024

This issue proposes a feature which...

Adds two new entries to the package.json export entrypoints list.


Background & Context

At present, using DOMpurify in combination with webpack 5. To control the dev and production environment, being able to use an alias, and to decide when to minify and when to not, I would need access to each of the two files individually.

{
	alias: {
	    jquery: `jquery/dist/jquery${bDevMode? '' : '.min'}.js`,
	    dompurify: `dompurify/dist/purify${bDevMode? '' : '.min'}.js`,
	}
}



Feature

In the package.json file, change the code:

From:

  "exports": {
    ".": {
      "import": {
        "types": "./dist/purify.es.d.mts",
        "default": "./dist/purify.es.mjs"
      },
      "default": {
        "types": "./dist/purify.cjs.d.ts",
        "default": "./dist/purify.cjs.js"
      }
    }
  },

To:

  "exports": {
    ".": {
      "import": {
        "types": "./dist/purify.es.d.mts",
        "default": "./dist/purify.es.mjs"
      },
      "default": {
        "types": "./dist/purify.cjs.d.ts",
        "default": "./dist/purify.cjs.js"
      }
    },
    "./purify.min.js": "./dist/purify.min.js",
    "./purify.js": "./dist/purify.js"
  },

After adding the changes locally, webpack works great and grabs each file depending on the environment.

Without this change, webpack does not build, and throws a failure error:

Package path ./dist/purify.min.js is not exported from package node_modules\dompurify (see exports field in node_modules\dompurify\package.json)

According to the documentation, webpack has a way to ignore the export fields by defining exportsFields: [], but it is global only. It cannot be controlled based on each alias. You either get all or nothing. And that would kill many other packages using exports.

    resolve: {
		exportsFields: [],
		alias: {
		    jquery: `jquery/dist/jquery${bDevMode? '' : '.min'}.js`,
		    dompurify: `dompurify/dist/purify${bDevMode? '' : '.min'}.js`,
		}
	}

Without this change, the project would either be stuck at v3.1.7, or removal of the alias from webpack all-together.

@reduckted
Copy link
Contributor

Why do you need to require/import the minified file? Webpack should take care of minifying the cjs or esm files that are normally required/imported based on the package exports.

@Aetherinox
Copy link
Contributor Author

Aetherinox commented Dec 14, 2024

Correct, webpack by itself will do the minifying, however, in dev mode, I do not want them modified. And to accomplish that, a conditional is needed within the alias to determine when the app is in dev or production mode. While other stuff in dev mode stays minified. The webpack optimization module is global and can't be defined on a per package basis other than using an alias.

There are other packages that are significantly larger when unpacked in dev mode that don't need to be unpacked, and dramatically increase sizes.

@cure53
Copy link
Owner

cure53 commented Dec 18, 2024

Hey all, I am not sure what to do here, any advice? :D

@Aetherinox
Copy link
Contributor Author

In a perfect world, these two files could be added to the package, and I'd forever hold a man crush for you. But your project, up to you.

@cure53
Copy link
Owner

cure53 commented Dec 24, 2024

What would break if we did that? If the risk for breakage is low, we can look at a PR of yours, maybe?

@Aetherinox
Copy link
Contributor Author

Aetherinox commented Dec 24, 2024

Shouldn't have any conflicts at all since I have not modified your default.s I've been using a modified copy since I put my issue in. exports is just the modern way to define what can be loaded with import and just replaces the old main setup.

Only the modules listed in your package exports will be allowed to be utilized by outside packages, and any others will just get ModuleNotFound . And with the code above, I did not modify what you've defined as your defaults, so existing users will not see any change. Changing the defaults would have caused breakage. It just allows people to actually call your minified / prettified versions.

And with my code above, I just added two additional exports. I forget, but you guys made this change over from main to exports a few versions ago, which is when it stopped working, pretty much just need to re-define them in the new exports.

And yeah, if you want me to do a PR I can, I figured since it's a few lines of code, whoever wants to. But I can if you need.

Edit: Added a pr. Appreciate it.

@cure53
Copy link
Owner

cure53 commented Dec 25, 2024

Cool, thanks 😄

@cure53 cure53 closed this as completed Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants