diff --git a/package.json b/package.json index b9232def6..63bdb5d77 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,6 @@ "@babel/core": "7.16.12", "@babel/preset-env": "7.16.11", "@babel/types": "7.16.8", - "@esbuild-plugins/node-globals-polyfill": "0.1.1", "@esbuild-plugins/node-modules-polyfill": "0.1.4", "@glimmer/reference": "0.83.1", "@types/estree": "0.0.50", diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index 1200e33b9..f3ba00931 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -5,7 +5,6 @@ import builtinModules from "builtin-modules"; import * as babel from "@babel/core"; import esbuild from "esbuild"; import { NodeModulesPolyfillPlugin as esbuildPluginNodeModulePolyfills } from "@esbuild-plugins/node-modules-polyfill"; -import { NodeGlobalsPolyfillPlugin as esbuildPluginNodeGlobalsPolyfills } from "@esbuild-plugins/node-globals-polyfill"; import esbuildPluginTextReplace from "esbuild-plugin-text-replace"; import browserslistToEsbuild from "browserslist-to-esbuild"; import { PROJECT_ROOT, DIST_DIR } from "../utils/index.mjs"; @@ -69,24 +68,29 @@ function getBabelConfig(bundle) { async function* getEsbuildOptions(bundle, options) { const replaceStrings = { - "process.env.PRETTIER_TARGET": JSON.stringify(bundle.target), - "process.env.NODE_ENV": JSON.stringify("production"), // `tslib` exports global variables "createExporter(root": "createExporter({}", }; + + const define = { + "process.env.PRETTIER_TARGET": JSON.stringify(bundle.target), + "process.env.NODE_ENV": JSON.stringify("production"), + }; + if (bundle.target === "universal") { // We can't reference `process` in UMD bundles and this is // an undocumented "feature" - replaceStrings["process.env.PRETTIER_DEBUG"] = "global.PRETTIER_DEBUG"; - // `rollup-plugin-node-globals` replace `__dirname` with the real dirname - // `parser-typescript.js` will contain a path of working directory + replaceStrings["process.env.PRETTIER_DEBUG"] = "globalThis.PRETTIER_DEBUG"; + + define.process = JSON.stringify({ env: {}, argv: [] }); + + // Replace `__dirname` and `__filename` with a fake value + // So `parser-typescript.js` won't contain a path of working directory // See #8268 - replaceStrings.__filename = JSON.stringify( + define.__filename = JSON.stringify( "/prettier-security-filename-placeholder.js" ); - replaceStrings.__dirname = JSON.stringify( - "/prettier-security-dirname-placeholder" - ); + define.__dirname = JSON.stringify("/prettier-security-dirname-placeholder"); } const replaceModule = {}; @@ -139,12 +143,12 @@ async function* getEsbuildOptions(bundle, options) { const esbuildOptions = { entryPoints: [path.join(PROJECT_ROOT, bundle.input)], + define, bundle: true, metafile: true, plugins: [ esbuildPluginEvaluate(), esbuildPluginReplaceModule({ ...replaceModule, ...bundle.replaceModule }), - bundle.target === "universal" && esbuildPluginNodeGlobalsPolyfills(), bundle.target === "universal" && esbuildPluginNodeModulePolyfills(), esbuildPluginTextReplace({ include: /\.[cm]?js$/, diff --git a/scripts/build/config.mjs b/scripts/build/config.mjs index 1ebb4b441..0f554158a 100644 --- a/scripts/build/config.mjs +++ b/scripts/build/config.mjs @@ -43,7 +43,8 @@ const parsers = [ 'require("globby")': "{}", "extra.projects = prepareAndTransformProjects(": "extra.projects = [] || prepareAndTransformProjects(", - "process.versions.node": "'999.999.999'", + "process.versions.node": JSON.stringify("999.999.999"), + "process.cwd()": JSON.stringify("/prettier-security-dirname-placeholder"), // `rollup-plugin-polyfill-node` don't have polyfill for these modules 'require("perf_hooks")': "{}", 'require("inspector")': "{}", @@ -59,6 +60,9 @@ const parsers = [ "typescriptVersionIsAtLeast[version] = semverCheck(version);": "typescriptVersionIsAtLeast[version] = true;", }, + replaceModule: { + [require.resolve("debug")]: require.resolve("./shims/debug.cjs"), + }, }, { input: "src/language-js/parse/espree.js", diff --git a/scripts/build/shims/assert.cjs b/scripts/build/shims/assert.cjs index 286ef6593..42b115991 100644 --- a/scripts/build/shims/assert.cjs +++ b/scripts/build/shims/assert.cjs @@ -1,3 +1,5 @@ +"use strict"; + const assert = () => {}; assert.ok = assert; assert.strictEqual = assert; diff --git a/scripts/build/shims/debug.cjs b/scripts/build/shims/debug.cjs new file mode 100644 index 000000000..9d9e8288e --- /dev/null +++ b/scripts/build/shims/debug.cjs @@ -0,0 +1,3 @@ +"use strict"; + +module.exports = () => () => {}; diff --git a/yarn.lock b/yarn.lock index 05ae97ac2..ea82f4cfd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1188,11 +1188,6 @@ resolved "https://registry.yarnpkg.com/@cspell/dict-vue/-/dict-vue-2.0.1.tgz#7514875f760ae755d2a6ef1fd00917d107682fe1" integrity sha512-n9So2C2Zw+uSDRzb2h9wq3PjZBqoHx+vBvu6a34H2qpumNjZ6HaEronrzX5tXJJXzOtocIQYrLxdd128TAU3+g== -"@esbuild-plugins/node-globals-polyfill@0.1.1": - version "0.1.1" - resolved "https://registry.yarnpkg.com/@esbuild-plugins/node-globals-polyfill/-/node-globals-polyfill-0.1.1.tgz#a313ab3efbb2c17c8ce376aa216c627c9b40f9d7" - integrity sha512-MR0oAA+mlnJWrt1RQVQ+4VYuRJW/P2YmRTv1AsplObyvuBMnPHiizUF95HHYiSsMGLhyGtWufaq2XQg6+iurBg== - "@esbuild-plugins/node-modules-polyfill@0.1.4": version "0.1.4" resolved "https://registry.yarnpkg.com/@esbuild-plugins/node-modules-polyfill/-/node-modules-polyfill-0.1.4.tgz#eb2f55da11967b2986c913f1a7957d1c868849c0"