Skip to content

Commit

Permalink
Test Issue 12338
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 18, 2022
1 parent ae080df commit 12d9c80
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -1,3 +1,4 @@
.tmp
!/*.js
/tests/format/**/*.js
/tests/integration/cli/
Expand Down
6 changes: 5 additions & 1 deletion .eslintrc.js
Expand Up @@ -167,7 +167,11 @@ module.exports = {
},
},
{
files: ["**/*.mjs", "scripts/release/**/*.js"],
files: [
"**/*.mjs",
"scripts/release/**/*.js",
"scripts/tools/bundle-prettier-with-webpack-test/**/*.js",
],
parserOptions: {
sourceType: "module",
},
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/prod-test.yml
Expand Up @@ -169,3 +169,29 @@ jobs:

- name: Run CLI on Node.js v0.10.48
run: node dist/bin-prettier --version 2>&1 >/dev/null | grep "prettier requires at least version 10.13.0 of Node, please upgrade" || exit 1

webpack-friendly:
name: Bundle Prettier with webpack
runs-on: ubuntu-latest
needs: [build]
defaults:
run:
working-directory: scripts/tools/bundle-prettier-with-webpack-test
steps:
- name: Checkout
uses: actions/checkout@v2.4.0

- name: Setup Node.js
uses: actions/setup-node@v2.5.1

- name: Download Artifact
uses: actions/download-artifact@v2
with:
name: dist
path: dist

- name: Install Dependencies
run: yarn install --frozen-lockfile

- name: Test
run: yarn test
10 changes: 7 additions & 3 deletions .gitignore
@@ -1,12 +1,17 @@
/.cache
# node_modules
# We have `node_modules` directory for tests, don't ignore them all
/node_modules
/website/node_modules
/scripts/release/node_modules
/scripts/tools/bundle-prettier-with-webpack-test/node_modules
/scripts/tools/eslint-plugin-prettier-internal-rules/node_modules

.tmp
*.log
/errors
/test*.*
/.vscode
/dist
/website/node_modules
/website/build
/website/i18n
/website/static/playground.js
Expand All @@ -22,5 +27,4 @@ package-lock.json
!.yarn/versions
.pnp.*
.nyc_output
/scripts/tools/eslint-plugin-prettier-internal-rules/node_modules
.devcontainer
1 change: 1 addition & 0 deletions .prettierignore
@@ -1,3 +1,4 @@
.tmp
dist/
.cache/
coverage/
Expand Down
78 changes: 78 additions & 0 deletions scripts/tools/bundle-prettier-with-webpack-test/index.js
@@ -0,0 +1,78 @@
import { fileURLToPath } from "node:url";
import path from "node:path";
import fs from "node:fs/promises";
import crypto from "node:crypto";
import webpack from "webpack";
import { outdent } from "outdent";
import { DIST_DIR } from "../../../scripts/utils/index.mjs";

async function runWebpack(config) {
return new Promise((resolve, reject) => {
webpack(config, (error, stats) => {
if (error) {
reject(error);
return;
}

if (stats.hasErrors()) {
const { errors } = stats.toJson();
const error = new Error(errors[0].message);
error.errors = errors;
reject(error);
return;
}

resolve(stats);
});
});
}

const getRandomFileName = (prefix) =>
prefix + "-" + crypto.randomBytes(4).toString("hex").slice(0, 4) + ".js";

const TEMPORARY_DIRECTORY = fileURLToPath(new URL("./.tmp", import.meta.url));

(async () => {
try {
await fs.mkdir(TEMPORARY_DIRECTORY);
} catch {
// No op
}

const relativePath = path
.relative(TEMPORARY_DIRECTORY, DIST_DIR)
.replaceAll("\\", "/");

/* `require` in `parser-typescript.js` */

const PROBLEMATIC_WARNING_MESSAGE =
"Critical dependency: require function is used in a way in which dependencies cannot be statically extracted";
const inputFile = path.join(TEMPORARY_DIRECTORY, getRandomFileName("input"));
await fs.writeFile(
inputFile,
`import "${relativePath}/parser-typescript.js"`
);

const stats = await runWebpack({
mode: "production",
entry: inputFile,
output: {
path: TEMPORARY_DIRECTORY,
filename: getRandomFileName("output"),
},
});
const result = stats.toJson();
const { warnings } = result;
const error = warnings.find(
({ message }) => message === PROBLEMATIC_WARNING_MESSAGE
);
if (error) {
throw new Error(outdent`
Unexpected webpack warning:
${error.message}
${error.details}
`);
}
})();
13 changes: 13 additions & 0 deletions scripts/tools/bundle-prettier-with-webpack-test/package.json
@@ -0,0 +1,13 @@
{
"name": "bundle-prettier-with-webpack-test",
"version": "0.0.0",
"private": "true",
"type": "module",
"devDependencies": {
"outdent": "0.8.0",
"webpack": "5.70.0"
},
"scripts": {
"test": "node ./index.js"
}
}

0 comments on commit 12d9c80

Please sign in to comment.