Skip to content

Commit

Permalink
feat: add support for rollup v4
Browse files Browse the repository at this point in the history
  • Loading branch information
swiing committed Feb 15, 2024
1 parent f359c33 commit ac498ac
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 37 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"css"
],
"peerDependencies": {
"rollup": "^2.79.1||^3.0.0"
"rollup": "^2.79.1||^3.0.0||^4.0.0"
},
"dependencies": {
"@rollup/plugin-node-resolve": "^15.0.2",
Expand All @@ -57,8 +57,8 @@
"pnpm": "^6.35.1",
"prettier": "^2.8.8",
"prettier-plugin-package": "^1.3.0",
"rollup": "^3.21.6",
"rollup-plugin-prettier": "^2.3.0",
"rollup": "^4.0.0",
"rollup-plugin-prettier": "^4.1.1",
"source-map-support": "^0.5.21"
},
"ava": {
Expand Down
162 changes: 135 additions & 27 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ export default function importAssertions(options = {}) {

// we want to make sure acorn knows how to parse import assertions

// The acorn parser only implements stage 4 js proposals.
// For rollup v2 or v2,
// the acorn parser only implements stage 4 js proposals.
// At the moment "import assertions" are a stage 3 proposal and as such
// cannot be parsed by acorn. However, there exist a plugin,
// so we inject the adhoc plugin into the options
// by leveraging https://rollupjs.org/guide/en/#acorninjectplugins
options(opts) {
// eslint-disable-next-line no-param-reassign
opts.acornInjectPlugins = opts.acornInjectPlugins || [];
if (!opts.acornInjectPlugins.includes(acornImportAssertions)) {
opts.acornInjectPlugins.push(acornImportAssertions);
const rollupMajorVersion = Number(this.meta.rollupVersion.split('.')[0]);

if (rollupMajorVersion <= 3) {
// eslint-disable-next-line no-param-reassign
opts.acornInjectPlugins = opts.acornInjectPlugins || [];
if (!opts.acornInjectPlugins.includes(acornImportAssertions)) {
opts.acornInjectPlugins.push(acornImportAssertions);
}
}

return opts;
},

Expand All @@ -63,9 +69,11 @@ export default function importAssertions(options = {}) {

const moduleInfo = self.getModuleInfo(id);
const assertType =
'assertions' in moduleInfo
'attributes' in moduleInfo
? moduleInfo.attributes.type /* rollup v4 */
: 'assertions' in moduleInfo
? moduleInfo.assertions.type /* rollup v3 */
: moduleInfo.meta['import-assertions'];
: moduleInfo.meta['import-assertions']; /* rollup v<=2 */

if (assertType === 'json')
// from @rollup/plugin-json
Expand Down

0 comments on commit ac498ac

Please sign in to comment.