diff --git a/packages/eslint/package.json b/packages/eslint/package.json index 119e15c3e..9e23d91c8 100755 --- a/packages/eslint/package.json +++ b/packages/eslint/package.json @@ -70,6 +70,8 @@ "eslint": "^9.5.0" }, "devDependencies": { + "@rollup/plugin-commonjs": "^26.0.1", + "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.0.0", "@rollup/plugin-typescript": "^9.0.1", "@types/eslint": "^8.56.10", diff --git a/packages/eslint/test/test.mjs b/packages/eslint/test/legacy.mjs similarity index 67% rename from packages/eslint/test/test.mjs rename to packages/eslint/test/legacy.mjs index 51aaf13cd..25a185da0 100755 --- a/packages/eslint/test/test.mjs +++ b/packages/eslint/test/legacy.mjs @@ -1,12 +1,74 @@ import fs from 'fs'; - import { createRequire } from 'module'; +import { resolve } from 'path'; + import test from 'ava'; import nodeResolve from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs' +import json from '@rollup/plugin-json' import { rollup } from 'rollup'; -import eslint from 'current-package'; +import rollupConfig from '../rollup.config.mjs'; + +const require = createRequire(import.meta.url); +const __dirname = new URL('.', import.meta.url).pathname; +/** + * @type {import('current-package').default} + */ +let eslint; + +test.before('bundle @rollup/plugin-eslint for legacy eslint tests', async () => { + const workspaceRoot = resolve(__dirname, '../../../'); + const workspaceEslintPath = resolve(workspaceRoot, 'node_modules/eslint'); + const { main, devDependencies, dependencies } = JSON.parse(fs.readFileSync(resolve(workspaceEslintPath, 'package.json'), 'utf8')); + const workspaceEslintEntry = resolve(workspaceEslintPath, main) + await rollup({ + ...rollupConfig, + external: (rollupConfig.external ?? []).filter((id) => id !== 'eslint'), + plugins: [ + ...(rollupConfig.plugins ?? []), + json(), + nodeResolve({ + // glob v7 causes a circular dependency error + resolveOnly: [ + ...Object.keys(devDependencies ?? {}).filter((id) => id !== 'glob'), + ...Object.keys(dependencies ?? {}).filter((id) => id !== 'glob') + ] + }), + commonjs(), + { + name: 'resolve-eslint', + // to bundle eslint v8 + resolveId(id) { + if (id === 'eslint') { + return workspaceEslintEntry; + } + } + }, + { + name: 'hack-commonjs-dynamic-require', + // a hack solution for dynamic requiring eslint formatters + footer: ` + function commonjsRequire(path) { + path = path.replace(/^\\/test/, '') + return require(resolve(${JSON.stringify(workspaceEslintPath)}, path)) + } + ` + } + ] + }) + .then((b) => b.write({ format: 'cjs', sourcemap: true, file: resolve(__dirname, 'temp.js') })) + eslint = require(resolve(__dirname, 'temp.js')) +}); + +test.after.always('cleanup', async () => { + [resolve(__dirname, 'temp.js'), resolve(__dirname, 'temp.js.map')].forEach((file) => { + if (fs.existsSync(file)) { + fs.unlinkSync(file); + } + }) +}); test('should lint files', async (t) => { let count = 0; @@ -201,24 +263,3 @@ test('should fix source code', async (t) => { fs.unlinkSync('./test/fixtures/fixable-clone.js'); }); - -test('works with cjs plugin', async (t) => { - const require = createRequire(import.meta.url); - const eslintPluginCjs = require('current-package'); - let count = 0; - await rollup({ - input: './test/fixtures/undeclared.js', - plugins: [ - eslintPluginCjs({ - formatter: (results) => { - count += results[0].messages.length; - // eslint-disable-next-line prefer-destructuring - const { message } = results[0].messages[0]; - t.is(message, "'x' is not defined."); - } - }) - ] - }); - - t.is(count, 1); -}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9a5c753a5..1ba5d1f09 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -352,6 +352,12 @@ importers: specifier: ^9.5.0 version: 9.5.0 devDependencies: + '@rollup/plugin-commonjs': + specifier: ^26.0.1 + version: 26.0.1(rollup@4.0.0-24) + '@rollup/plugin-json': + specifier: ^6.1.0 + version: 6.1.0(rollup@4.0.0-24) '@rollup/plugin-node-resolve': specifier: ^15.0.0 version: 15.0.0(rollup@4.0.0-24) @@ -2283,7 +2289,6 @@ packages: strip-ansi-cjs: /strip-ansi@6.0.1 wrap-ansi: 8.1.0 wrap-ansi-cjs: /wrap-ansi@7.0.0 - dev: false /@istanbuljs/load-nyc-config@1.1.0: resolution: {integrity: sha512-VjeHSlIzpv/NyD3N0YuHfXOPDIixcA1q2ZV98wsMqcYlPmv2n3Yb2lYP9XMElnaFVXg5A7YLTeLu6V84uQDjmQ==} @@ -2373,7 +2378,6 @@ packages: resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==} engines: {node: '>=14'} requiresBuild: true - dev: false optional: true /@rollup/plugin-alias@4.0.0(rollup@4.0.0-24): @@ -2441,6 +2445,24 @@ packages: rollup: 4.0.0-24 dev: true + /@rollup/plugin-commonjs@26.0.1(rollup@4.0.0-24): + resolution: {integrity: sha512-UnsKoZK6/aGIH6AdkptXhNvhaqftcjq3zZdT+LY5Ftms6JR06nADcDsYp5hTU9E2lbJUEOhdlY5J4DNTneM+jQ==} + engines: {node: '>=16.0.0 || 14 >= 14.17'} + peerDependencies: + rollup: ^2.68.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + dependencies: + '@rollup/pluginutils': 5.1.0(rollup@4.0.0-24) + commondir: 1.0.1 + estree-walker: 2.0.2 + glob: 10.4.1 + is-reference: 1.2.1 + magic-string: 0.30.3 + rollup: 4.0.0-24 + dev: true + /@rollup/plugin-json@5.0.0(rollup@4.0.0-24): resolution: {integrity: sha512-LsWDA5wJs/ggzakVuKQhZo7HPRcQZgBa3jWIVxQSFxaRToUGNi8ZBh3+k/gQ+1eInVYJgn4WBRCUkmoDrmmGzw==} engines: {node: '>=14.0.0'} @@ -2454,6 +2476,19 @@ packages: rollup: 4.0.0-24 dev: true + /@rollup/plugin-json@6.1.0(rollup@4.0.0-24): + resolution: {integrity: sha512-EGI2te5ENk1coGeADSIwZ7G2Q8CJS2sF120T7jLw4xFw9n7wIOXHo+kIYRAoVpJAN+kmqZSoO3Fp4JtoNF4ReA==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + dependencies: + '@rollup/pluginutils': 5.1.0(rollup@4.0.0-24) + rollup: 4.0.0-24 + dev: true + /@rollup/plugin-node-resolve@15.0.0(rollup@4.0.0-24): resolution: {integrity: sha512-iwJbzfTzlzDDQcGmkS7EkCKwe2kSkdBrjX87Fy/KrNjr6UNnLpod0t6X66e502LRe5JJCA4FFqrEscWPnZAkig==} engines: {node: '>=14.0.0'} @@ -2554,7 +2589,6 @@ packages: estree-walker: 2.0.2 picomatch: 2.3.1 rollup: 4.0.0-24 - dev: false /@rollup/rollup-android-arm-eabi@4.0.0-24: resolution: {integrity: sha512-19cF3V1fHfzPzwu0cgZEdWLMdNkqSmKOhidqQv1CkUqAMcb7etA7WLx8YrX5ob31ruI0BYYrUDBunlIuMHHUrg==} @@ -3361,7 +3395,6 @@ packages: /ansi-styles@6.2.1: resolution: {integrity: sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==} engines: {node: '>=12'} - dev: false /any-promise@1.3.0: resolution: {integrity: sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==} @@ -5059,7 +5092,6 @@ packages: dependencies: cross-spawn: 7.0.3 signal-exit: 4.1.0 - dev: false /fromentries@1.3.2: resolution: {integrity: sha512-cHEpEQHUg0f8XdtZCc2ZAhrHzKzT0MrFUTcvx+hfxYu7rGMDc5SKoXFh+n4YigxsHXRzc6OrCshdR1bWH6HHyg==} @@ -5167,7 +5199,6 @@ packages: minimatch: 9.0.4 minipass: 7.1.2 path-scurry: 1.11.1 - dev: false /glob@7.1.6: resolution: {integrity: sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==} @@ -5783,7 +5814,6 @@ packages: '@isaacs/cliui': 8.0.2 optionalDependencies: '@pkgjs/parseargs': 0.11.0 - dev: false /js-sdsl@4.1.5: resolution: {integrity: sha512-08bOAKweV2NUC1wqTtf3qZlnpOX/R2DU9ikpjOHs0H+ibQv3zpncVQg6um4uYtRtrwIX8M4Nh3ytK4HGlYAq7Q==} @@ -6055,7 +6085,6 @@ packages: /lru-cache@10.2.2: resolution: {integrity: sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==} engines: {node: 14 || >=16.14} - dev: false /lru-cache@6.0.0: resolution: {integrity: sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==} @@ -6081,7 +6110,6 @@ packages: engines: {node: '>=12'} dependencies: '@jridgewell/sourcemap-codec': 1.4.15 - dev: false /make-dir@2.1.0: resolution: {integrity: sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==} @@ -6255,7 +6283,6 @@ packages: engines: {node: '>=16 || 14 >=14.17'} dependencies: brace-expansion: 2.0.1 - dev: false /minimist-options@4.1.0: resolution: {integrity: sha512-Q4r8ghd80yhO/0j1O3B2BjweX3fiHg9cdOwjJd2J76Q135c+NDxGCqdYKQ1SKBuFfgWbAUzBfvYjPUEeNgqN1A==} @@ -6272,7 +6299,6 @@ packages: /minipass@7.1.2: resolution: {integrity: sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==} engines: {node: '>=16 || 14 >=14.17'} - dev: false /ms@2.0.0: resolution: {integrity: sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==} @@ -6684,7 +6710,6 @@ packages: dependencies: lru-cache: 10.2.2 minipass: 7.1.2 - dev: false /path-to-regexp@1.8.0: resolution: {integrity: sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==} @@ -7623,7 +7648,6 @@ packages: /signal-exit@4.1.0: resolution: {integrity: sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==} engines: {node: '>=14'} - dev: false /sinon@14.0.1: resolution: {integrity: sha512-JhJ0jCiyBWVAHDS+YSjgEbDn7Wgz9iIjA1/RK+eseJN0vAAWIWiXBdrnb92ELPyjsfreCYntD1ORtLSfIrlvSQ==} @@ -7847,7 +7871,6 @@ packages: engines: {node: '>=12'} dependencies: ansi-regex: 6.0.1 - dev: false /strip-bom-buf@2.0.0: resolution: {integrity: sha512-gLFNHucd6gzb8jMsl5QmZ3QgnUJmp7qn4uUSHNwEXumAp7YizoGYw19ZUVfuq4aBOQUtyn2k8X/CwzWB73W2lQ==} @@ -8326,7 +8349,6 @@ packages: ansi-styles: 6.2.1 string-width: 5.1.2 strip-ansi: 7.1.0 - dev: false /wrappy@1.0.2: resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==}