From ddc8bbfce8a6d610771de3ed35663fc618bb39e6 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Wed, 3 Aug 2022 20:18:41 +0900 Subject: [PATCH 1/2] chore: enable node prefix lint --- .eslintrc.cjs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 904873b07043cc..12ae8a255406d4 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,4 +1,5 @@ // @ts-check +const { builtinModules } = require('node:module') const { defineConfig } = require('eslint-define-config') module.exports = defineConfig({ @@ -81,6 +82,10 @@ module.exports = defineConfig({ { prefer: 'type-imports' } ], + 'import/no-nodejs-modules': [ + 'error', + { allow: builtinModules.map((mod) => `node:${mod}`) } + ], 'import/no-duplicates': 'error', 'import/order': 'error', 'sort-imports': [ From 3ae3a4e02d3e1fc44acdb3134df2032ab6e9f2fc Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Wed, 3 Aug 2022 20:25:50 +0900 Subject: [PATCH 2/2] chore: fix lint --- packages/vite/bin/vite.js | 2 +- packages/vite/src/node/http.ts | 6 +++--- packages/vite/src/node/logger.ts | 2 +- packages/vite/src/node/plugins/ssrRequireHook.ts | 4 +++- packages/vite/types/ws.d.ts | 2 +- playground/alias/vite.config.js | 2 +- playground/assets/vite.config-runtime-base.js | 2 +- playground/assets/vite.config.js | 2 +- playground/backend-integration/vite.config.js | 2 +- playground/css-codesplit-cjs/vite.config.js | 2 +- playground/css-codesplit/vite.config.js | 2 +- playground/css/postcss.config.js | 4 ++-- playground/css/vite.config.js | 2 +- playground/dynamic-import/vite.config.js | 4 ++-- playground/fs-serve/root/vite.config.js | 2 +- playground/html/vite.config.js | 2 +- playground/json/server.js | 4 ++-- playground/legacy/vite.config.js | 4 ++-- playground/lib/vite.config.js | 4 ++-- playground/lib/vite.dyimport.config.js | 4 ++-- playground/multiple-entrypoints/vite.config.js | 4 ++-- .../optimize-deps/dep-with-builtin-module-cjs/index.js | 2 ++ .../optimize-deps/dep-with-builtin-module-esm/index.js | 2 ++ playground/optimize-deps/vite.config.js | 2 +- playground/optimize-missing-deps/multi-entry-dep/index.js | 2 +- playground/optimize-missing-deps/server.js | 4 ++-- playground/resolve/browser-field/multiple.dot.path.js | 2 +- playground/resolve/browser-field/not-browser.js | 2 +- playground/ssr-deps/import-builtin-cjs/index.js | 2 +- playground/ssr-deps/read-file-content/index.js | 6 +++--- playground/ssr-deps/require-absolute/index.js | 2 +- playground/ssr-deps/server.js | 2 +- playground/ssr-html/server.js | 2 +- playground/ssr-pug/server.js | 2 +- playground/ssr-react/prerender.js | 2 +- playground/ssr-react/server.js | 2 +- playground/ssr-vue/server.js | 2 +- playground/ssr-vue/vite.config.js | 2 +- playground/ssr-webworker/worker.js | 2 +- playground/worker/vite.config-relative-base.js | 2 +- 40 files changed, 56 insertions(+), 50 deletions(-) diff --git a/packages/vite/bin/vite.js b/packages/vite/bin/vite.js index 5814f260dc427c..13e9529510e4a3 100755 --- a/packages/vite/bin/vite.js +++ b/packages/vite/bin/vite.js @@ -50,7 +50,7 @@ if (profileIndex > 0) { if (next && !next.startsWith('-')) { process.argv.splice(profileIndex, 1) } - const inspector = await import('inspector').then((r) => r.default) + const inspector = await import('node:inspector').then((r) => r.default) const session = (global.__vite_profile_session = new inspector.Session()) session.connect() session.post('Profiler.enable', () => { diff --git a/packages/vite/src/node/http.ts b/packages/vite/src/node/http.ts index 18ce37f6a327e1..f0f53f1d686fd6 100644 --- a/packages/vite/src/node/http.ts +++ b/packages/vite/src/node/http.ts @@ -95,16 +95,16 @@ export async function resolveHttpServer( httpsOptions?: HttpsServerOptions ): Promise { if (!httpsOptions) { - const { createServer } = await import('http') + const { createServer } = await import('node:http') return createServer(app) } // #484 fallback to http1 when proxy is needed. if (proxy) { - const { createServer } = await import('https') + const { createServer } = await import('node:https') return createServer(httpsOptions, app) } else { - const { createSecureServer } = await import('http2') + const { createSecureServer } = await import('node:http2') return createSecureServer( { // Manually increase the session memory to prevent 502 ENHANCE_YOUR_CALM diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts index d0c5d29334c023..326fa0e6dfd778 100644 --- a/packages/vite/src/node/logger.ts +++ b/packages/vite/src/node/logger.ts @@ -1,6 +1,6 @@ /* eslint no-console: 0 */ -import readline from 'readline' +import readline from 'node:readline' import colors from 'picocolors' import type { RollupError } from 'rollup' import type { ResolvedServerUrls } from './server' diff --git a/packages/vite/src/node/plugins/ssrRequireHook.ts b/packages/vite/src/node/plugins/ssrRequireHook.ts index dc51f9114c5ef2..d1173b211ff836 100644 --- a/packages/vite/src/node/plugins/ssrRequireHook.ts +++ b/packages/vite/src/node/plugins/ssrRequireHook.ts @@ -53,7 +53,9 @@ type NodeResolveFilename = ( /** Respect the `resolve.dedupe` option in production SSR. */ function dedupeRequire(dedupe: string[]) { // eslint-disable-next-line no-restricted-globals - const Module = require('module') as { _resolveFilename: NodeResolveFilename } + const Module = require('node:module') as { + _resolveFilename: NodeResolveFilename + } const resolveFilename = Module._resolveFilename Module._resolveFilename = function (request, parent, isMain, options) { if (request[0] !== '.' && request[0] !== '/') { diff --git a/packages/vite/types/ws.d.ts b/packages/vite/types/ws.d.ts index 4a03058d0eeaa2..a06341fca9eeb9 100644 --- a/packages/vite/types/ws.d.ts +++ b/packages/vite/types/ws.d.ts @@ -26,7 +26,7 @@ import type { } from 'node:http' import type { Server as HTTPSServer } from 'node:https' import type { Duplex, DuplexOptions } from 'node:stream' -import type { SecureContextOptions } from 'tls' +import type { SecureContextOptions } from 'node:tls' import type { URL } from 'node:url' import type { ZlibOptions } from 'node:zlib' diff --git a/playground/alias/vite.config.js b/playground/alias/vite.config.js index 634871877a0f56..16c33dd859aa66 100644 --- a/playground/alias/vite.config.js +++ b/playground/alias/vite.config.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('node:path') /** * @type {import('vite').UserConfig} diff --git a/playground/assets/vite.config-runtime-base.js b/playground/assets/vite.config-runtime-base.js index 0d643a6cf6058a..1752e974b8abcd 100644 --- a/playground/assets/vite.config-runtime-base.js +++ b/playground/assets/vite.config-runtime-base.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('node:path') const dynamicBaseAssetsCode = ` globalThis.__toAssetUrl = url => '/' + url diff --git a/playground/assets/vite.config.js b/playground/assets/vite.config.js index c9d821ae3d73ee..700896cc13d50b 100644 --- a/playground/assets/vite.config.js +++ b/playground/assets/vite.config.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('node:path') /** * @type {import('vite').UserConfig} diff --git a/playground/backend-integration/vite.config.js b/playground/backend-integration/vite.config.js index ce3f3361fc70d1..ff5d2c43c3ed04 100644 --- a/playground/backend-integration/vite.config.js +++ b/playground/backend-integration/vite.config.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('node:path') const glob = require('fast-glob') const normalizePath = require('vite').normalizePath diff --git a/playground/css-codesplit-cjs/vite.config.js b/playground/css-codesplit-cjs/vite.config.js index 7cd8967121f021..85a7a0f07eff14 100644 --- a/playground/css-codesplit-cjs/vite.config.js +++ b/playground/css-codesplit-cjs/vite.config.js @@ -1,4 +1,4 @@ -const { resolve } = require('path') +const { resolve } = require('node:path') module.exports = { build: { diff --git a/playground/css-codesplit/vite.config.js b/playground/css-codesplit/vite.config.js index 528a3c7d7f19fe..0bc9cab5d025df 100644 --- a/playground/css-codesplit/vite.config.js +++ b/playground/css-codesplit/vite.config.js @@ -1,4 +1,4 @@ -const { resolve } = require('path') +const { resolve } = require('node:path') module.exports = { build: { diff --git a/playground/css/postcss.config.js b/playground/css/postcss.config.js index 48d5dd23d5a8f1..a9bbd0f2b4e09e 100644 --- a/playground/css/postcss.config.js +++ b/playground/css/postcss.config.js @@ -2,8 +2,8 @@ module.exports = { plugins: [require('postcss-nested'), testDirDep, testSourceInput] } -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const glob = require('fast-glob') const { normalizePath } = require('vite') diff --git a/playground/css/vite.config.js b/playground/css/vite.config.js index 704eb134e25880..d8333b07fb4d63 100644 --- a/playground/css/vite.config.js +++ b/playground/css/vite.config.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('node:path') /** * @type {import('vite').UserConfig} diff --git a/playground/dynamic-import/vite.config.js b/playground/dynamic-import/vite.config.js index 96baaa74798686..f3b7cb1dd049ce 100644 --- a/playground/dynamic-import/vite.config.js +++ b/playground/dynamic-import/vite.config.js @@ -1,5 +1,5 @@ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const vite = require('vite') module.exports = vite.defineConfig({ diff --git a/playground/fs-serve/root/vite.config.js b/playground/fs-serve/root/vite.config.js index 585a91f9d6346d..5712ad5acb3438 100644 --- a/playground/fs-serve/root/vite.config.js +++ b/playground/fs-serve/root/vite.config.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('node:path') /** * @type {import('vite').UserConfig} diff --git a/playground/html/vite.config.js b/playground/html/vite.config.js index 31cc1656d2f19e..8c117aaaa663e7 100644 --- a/playground/html/vite.config.js +++ b/playground/html/vite.config.js @@ -1,4 +1,4 @@ -const { resolve } = require('path') +const { resolve } = require('node:path') /** * @type {import('vite').UserConfig} diff --git a/playground/json/server.js b/playground/json/server.js index d78c7f6246d540..01f7e1482aa9e8 100644 --- a/playground/json/server.js +++ b/playground/json/server.js @@ -1,6 +1,6 @@ // @ts-check -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const express = require('express') const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD diff --git a/playground/legacy/vite.config.js b/playground/legacy/vite.config.js index f793980f365887..bdf89639d57e37 100644 --- a/playground/legacy/vite.config.js +++ b/playground/legacy/vite.config.js @@ -1,5 +1,5 @@ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const legacy = require('@vitejs/plugin-legacy').default module.exports = { diff --git a/playground/lib/vite.config.js b/playground/lib/vite.config.js index d9fb932f535ebb..3e5187b68c55ee 100644 --- a/playground/lib/vite.config.js +++ b/playground/lib/vite.config.js @@ -1,5 +1,5 @@ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') /** * @type {import('vite').UserConfig} diff --git a/playground/lib/vite.dyimport.config.js b/playground/lib/vite.dyimport.config.js index 54a84dbe76c24a..c621c90fb2ed8a 100644 --- a/playground/lib/vite.dyimport.config.js +++ b/playground/lib/vite.dyimport.config.js @@ -1,5 +1,5 @@ -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') /** * @type {import('vite').UserConfig} diff --git a/playground/multiple-entrypoints/vite.config.js b/playground/multiple-entrypoints/vite.config.js index c2a44858a3ce6b..e1289dbf5a0078 100644 --- a/playground/multiple-entrypoints/vite.config.js +++ b/playground/multiple-entrypoints/vite.config.js @@ -1,5 +1,5 @@ -const { resolve } = require('path') -const fs = require('fs') +const { resolve } = require('node:path') +const fs = require('node:fs') module.exports = { build: { diff --git a/playground/optimize-deps/dep-with-builtin-module-cjs/index.js b/playground/optimize-deps/dep-with-builtin-module-cjs/index.js index 7cff6a9e9dac8e..82766ff59a3e8c 100644 --- a/playground/optimize-deps/dep-with-builtin-module-cjs/index.js +++ b/playground/optimize-deps/dep-with-builtin-module-cjs/index.js @@ -1,5 +1,7 @@ // no node: protocol intentionally +// eslint-disable-next-line import/no-nodejs-modules const fs = require('fs') +// eslint-disable-next-line import/no-nodejs-modules const path = require('path') // NOTE: require destructure would error immediately because of how esbuild diff --git a/playground/optimize-deps/dep-with-builtin-module-esm/index.js b/playground/optimize-deps/dep-with-builtin-module-esm/index.js index 5b5df60fad9e15..b7b710ffaab58f 100644 --- a/playground/optimize-deps/dep-with-builtin-module-esm/index.js +++ b/playground/optimize-deps/dep-with-builtin-module-esm/index.js @@ -1,5 +1,7 @@ // no node: protocol intentionally +// eslint-disable-next-line import/no-nodejs-modules import { readFileSync } from 'fs' +// eslint-disable-next-line import/no-nodejs-modules import path from 'path' // access from named import diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js index 091f994e2c6aec..a44ca2ef2449d6 100644 --- a/playground/optimize-deps/vite.config.js +++ b/playground/optimize-deps/vite.config.js @@ -1,4 +1,4 @@ -const fs = require('fs') +const fs = require('node:fs') const vue = require('@vitejs/plugin-vue') // Overriding the NODE_ENV set by vitest diff --git a/playground/optimize-missing-deps/multi-entry-dep/index.js b/playground/optimize-missing-deps/multi-entry-dep/index.js index 0717b87c27c2d8..4b214df5d30765 100644 --- a/playground/optimize-missing-deps/multi-entry-dep/index.js +++ b/playground/optimize-missing-deps/multi-entry-dep/index.js @@ -1,3 +1,3 @@ -const path = require('path') +const path = require('node:path') exports.name = path.normalize('./Server') diff --git a/playground/optimize-missing-deps/server.js b/playground/optimize-missing-deps/server.js index ace9ca52c6f2b3..641c128afbbe26 100644 --- a/playground/optimize-missing-deps/server.js +++ b/playground/optimize-missing-deps/server.js @@ -1,6 +1,6 @@ // @ts-check -const fs = require('fs') -const path = require('path') +const fs = require('node:fs') +const path = require('node:path') const express = require('express') const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD diff --git a/playground/resolve/browser-field/multiple.dot.path.js b/playground/resolve/browser-field/multiple.dot.path.js index b4022f73daaf6e..37bd1099aecc0e 100644 --- a/playground/resolve/browser-field/multiple.dot.path.js +++ b/playground/resolve/browser-field/multiple.dot.path.js @@ -1,2 +1,2 @@ -const fs = require('fs') +const fs = require('node:fs') console.log('this should not run in the browser') diff --git a/playground/resolve/browser-field/not-browser.js b/playground/resolve/browser-field/not-browser.js index b4022f73daaf6e..37bd1099aecc0e 100644 --- a/playground/resolve/browser-field/not-browser.js +++ b/playground/resolve/browser-field/not-browser.js @@ -1,2 +1,2 @@ -const fs = require('fs') +const fs = require('node:fs') console.log('this should not run in the browser') diff --git a/playground/ssr-deps/import-builtin-cjs/index.js b/playground/ssr-deps/import-builtin-cjs/index.js index 149be9e9ed53b8..f3e8d6a4b9031f 100644 --- a/playground/ssr-deps/import-builtin-cjs/index.js +++ b/playground/ssr-deps/import-builtin-cjs/index.js @@ -1,4 +1,4 @@ -exports.stream = require('stream') +exports.stream = require('node:stream') exports.hello = function () { return 'Hello World!' diff --git a/playground/ssr-deps/read-file-content/index.js b/playground/ssr-deps/read-file-content/index.js index c8761b3b4734c1..4cc6c34972defa 100644 --- a/playground/ssr-deps/read-file-content/index.js +++ b/playground/ssr-deps/read-file-content/index.js @@ -1,9 +1,9 @@ -const path = require('path') +const path = require('node:path') module.exports = async function readFileContent(filePath) { const fs = process.versions.node.split('.')[0] >= '14' - ? require('fs/promises') - : require('fs').promises + ? require('node:fs/promises') + : require('node:fs').promises return await fs.readFile(path.resolve(filePath), 'utf-8') } diff --git a/playground/ssr-deps/require-absolute/index.js b/playground/ssr-deps/require-absolute/index.js index c2f844f3e2f6ed..18b3aa936629dc 100644 --- a/playground/ssr-deps/require-absolute/index.js +++ b/playground/ssr-deps/require-absolute/index.js @@ -1,3 +1,3 @@ -const path = require('path') +const path = require('node:path') module.exports.hello = () => require(path.resolve(__dirname, './foo.js')).hello diff --git a/playground/ssr-deps/server.js b/playground/ssr-deps/server.js index aa47a6055321ac..d82f80c599583b 100644 --- a/playground/ssr-deps/server.js +++ b/playground/ssr-deps/server.js @@ -1,7 +1,7 @@ // @ts-check import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'url' +import { fileURLToPath } from 'node:url' import express from 'express' const __dirname = path.dirname(fileURLToPath(import.meta.url)) diff --git a/playground/ssr-html/server.js b/playground/ssr-html/server.js index d1f0ad1a3e0a7e..6c6ddca9addc1d 100644 --- a/playground/ssr-html/server.js +++ b/playground/ssr-html/server.js @@ -1,6 +1,6 @@ import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'url' +import { fileURLToPath } from 'node:url' import express from 'express' const __dirname = path.dirname(fileURLToPath(import.meta.url)) diff --git a/playground/ssr-pug/server.js b/playground/ssr-pug/server.js index d5fe58ee47dbdf..b5d4f340b79b32 100644 --- a/playground/ssr-pug/server.js +++ b/playground/ssr-pug/server.js @@ -1,6 +1,6 @@ // @ts-check import path from 'node:path' -import { fileURLToPath } from 'url' +import { fileURLToPath } from 'node:url' import pug from 'pug' import express from 'express' diff --git a/playground/ssr-react/prerender.js b/playground/ssr-react/prerender.js index 38412028a7fccb..8a18a492ab138c 100644 --- a/playground/ssr-react/prerender.js +++ b/playground/ssr-react/prerender.js @@ -3,7 +3,7 @@ import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'url' +import { fileURLToPath } from 'node:url' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const toAbsolute = (p) => path.resolve(__dirname, p) diff --git a/playground/ssr-react/server.js b/playground/ssr-react/server.js index 3b7f7045f6c95a..1a8d245f8e2d80 100644 --- a/playground/ssr-react/server.js +++ b/playground/ssr-react/server.js @@ -1,6 +1,6 @@ import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'url' +import { fileURLToPath } from 'node:url' import express from 'express' const __dirname = path.dirname(fileURLToPath(import.meta.url)) diff --git a/playground/ssr-vue/server.js b/playground/ssr-vue/server.js index 56961faa40769e..55605387c481f9 100644 --- a/playground/ssr-vue/server.js +++ b/playground/ssr-vue/server.js @@ -1,7 +1,7 @@ // @ts-check import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'url' +import { fileURLToPath } from 'node:url' import express from 'express' const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD diff --git a/playground/ssr-vue/vite.config.js b/playground/ssr-vue/vite.config.js index a313ea7dd479b2..c8be7320c8a9b0 100644 --- a/playground/ssr-vue/vite.config.js +++ b/playground/ssr-vue/vite.config.js @@ -1,4 +1,4 @@ -import path from 'path' +import path from 'node:path' import { defineConfig } from 'vite' import vuePlugin from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' diff --git a/playground/ssr-webworker/worker.js b/playground/ssr-webworker/worker.js index 9904d5994a1319..d353688b0575eb 100644 --- a/playground/ssr-webworker/worker.js +++ b/playground/ssr-webworker/worker.js @@ -1,4 +1,4 @@ -import { fileURLToPath } from 'url' +import { fileURLToPath } from 'node:url' import path from 'node:path' import { Miniflare } from 'miniflare' diff --git a/playground/worker/vite.config-relative-base.js b/playground/worker/vite.config-relative-base.js index 8002883ca4abf1..4c20940749eacc 100644 --- a/playground/worker/vite.config-relative-base.js +++ b/playground/worker/vite.config-relative-base.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('node:path') const vueJsx = require('@vitejs/plugin-vue-jsx') const vite = require('vite')