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

chore(package.json): add purify.js and purify.min.js to exports as targetable modules #1053

Merged
merged 1 commit into from
Dec 25, 2024

Conversation

Aetherinox
Copy link
Contributor

@Aetherinox Aetherinox commented Dec 25, 2024

Summary

This PR is per discussions had in the issue:


Adds purify.js and minified purify.min.js to the package.json exports field, allowing for each script to be called as it was prior to DOMPurify v3.2.0.

DOMPurify was migrated over to utilizing exports in commit faf296da9b412d93023dde1c8f86599cb7b13beb and is provided in the link below:

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

DOMPurify v3.1.7 is the last version that utilized the old main field without exports.

This PR simply allows for the above two files to be targeted. The new entries were added as their own named exports, and do not override the defaults configured by DOMPurify; so existing users will have no issues after updating, all imports will remain the same.

Tested this on both a project utilizing webpack (all existing DOMPurify calls continued to work as normal), and also tested on a new project not utilizing Webpack, and only using the existing exports and all functionality continued to work.

As we know, exports was introduced in Node v12.7.0 as a succession to the older main functionality, allowing developers to control that aspects of a library can be called.

With the edits made in the PR, webpack behaves as it originally did where each file can now be targeted and loaded when desired.




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, we 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`,
	}
}

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)

To temporarily circumvent this issue, we had to do away with calling the uncompressed and minified files all-together, which means we cannot specify which files get minified and which ones do not.

According to the webpack documentation, webpack has a way to ignore the export fields by defining exportsFields: [] in the webpack.config.js file, 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`,
		}
	}

Webpack's documentation makes note of the behavior it takes in regards to exports:

As of version 5.94.0, webpack's behavior has been updated to align with Node's behavior. It now selects the first valid path without attempting further resolutions and throws an error if the path cannot be resolved.

At the time of writing this, I am currently on webpack 5.97.1.




Tasks

  • Install PR
  • Import DOMPurify
  • Using any of the existing methods should continue to work (ie: DOMPurify.sanitize)

For webpack users, they can configure DOMPurify as a normal alias within webpack.config.js

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

Dependencies

  • Resolved dependency
  • Open dependency

can now directly import both minified and uncompressed library scripts
@Aetherinox Aetherinox changed the title chore(package.json): add purify.js and purify.min.js to exports chore(package.json): add purify.js and purify.min.js to exports as targetable modules Dec 25, 2024
@cure53 cure53 merged commit 0ce2d47 into cure53:main Dec 25, 2024
8 checks passed
@cure53
Copy link
Owner

cure53 commented Dec 25, 2024

LGTM, thx 🙂

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

Successfully merging this pull request may close these issues.

2 participants