From ffec085cd6a1d7c9b2666f5a36f6c3d274e662de Mon Sep 17 00:00:00 2001 From: swiing Date: Thu, 15 Feb 2024 14:39:09 +0100 Subject: [PATCH] feat: add support for rollup v4 --- package.json | 2 +- src/index.js | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 2e352db..45bd0e9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 415b240..74560d5 100644 --- a/src/index.js +++ b/src/index.js @@ -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; }, @@ -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