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

♻️ Add module / CJS and types to purifier npm package #26297

Merged
merged 6 commits into from
Jan 15, 2020

Conversation

fstanis
Copy link
Contributor

@fstanis fstanis commented Jan 10, 2020

No description provided.

@jridgewell
Copy link
Contributor

/to @choumx

@jridgewell jridgewell requested review from dreamofabear and removed request for jridgewell January 10, 2020 18:44
"description": "AMP-specific sanitization library",
"author": "The AMP HTML Authors",
"license": "Apache-2.0",
"main": "dist/purifier.js",
"files": ["dist/*"],
"main": "dist/purifier.mjs",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is only having an "mjs" output typical?

Copy link
Member

@samouri samouri Jan 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My impression was that main: should be CJS and module: is normally mjs.

From Rollup's documentation:

To make sure your ES modules are immediately usable by tools that work with CommonJS such as Node.js and webpack, you can use Rollup to compile to UMD or CommonJS format, and then point to that compiled version with the main property in your package.json file. If your package.json file also has a module field, ESM-aware tools like Rollup and webpack 2+ will import the ES module version directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, didn't know this. Added CJS output now and used main and module in package.json.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was a bridge until Node.js supported ES6 modules: https://nodejs.org/api/esm.html#esm_dual_commonjs_es_module_packages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading a bit into this, it seems the module + main approach @samouri suggested is still best, since Node.js will just ignore module and use main. The solution from nodejs.org to have a wrapper instead seems only useful to allow ESM support in Node (which is still experimental in LTS) and I fear it will cause complications when used in a browser.

TLDR: I see no real advantages in having an ESM version in Node (for now).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, just sharing for anyone who was also curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it, sorry I misunderstood your previous comment.

@fstanis fstanis force-pushed the pur2 branch 4 times, most recently from ef85108 to b67d9e2 Compare January 14, 2020 15:06
@fstanis
Copy link
Contributor Author

fstanis commented Jan 15, 2020

Update: it seems passing doc.defaultView instead of self is generally a bad idea: defaultView may be unset and this makes DOMPurify think the library is not running in a real browser, making it a singleton.

@fstanis fstanis changed the title ♻️ Use .mjs in purifier and doc.defaultView instead of self for window ♻️ Add module / CJS and types to purifier npm package Jan 15, 2020
@fstanis fstanis merged commit 9e862ca into ampproject:master Jan 15, 2020
@fstanis fstanis deleted the pur2 branch January 15, 2020 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants