Run loader as it normally would

This commit is contained in:
Eloy Durán 2023-06-09 16:51:16 +02:00
Родитель 9b0deb717f
Коммит f26019b824
4 изменённых файлов: 75 добавлений и 18 удалений

Просмотреть файл

@ -16,6 +16,8 @@
},
"devDependencies": {
"@types/jest": "^26.0.22",
"@types/loader-runner": "^2.2.4",
"loader-runner": "^4.3.0",
"monorepo-scripts": "*"
},
"sideEffects": false,

Просмотреть файл

@ -1,57 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`webpackLoader works with documents captured as variable 1`] = `
"
[
"
import { graphql } from "@nova/react";
const doc = require("./__generated__/SomeComponentFragment.graphql").default
const {data} = useFragment(doc, props);
"
",
]
`;
exports[`webpackLoader works with documents with mutation operations 1`] = `
"
[
"
import { graphql } from "@nova/react";
const doc = require("./__generated__/SomeComponentMutation.graphql").default;
console.log()
"
",
]
`;
exports[`webpackLoader works with documents with query operations 1`] = `
"
[
"
import { graphql } from "@nova/react";
const doc = require("./__generated__/SomeComponentQuery.graphql").default;
console.log()
"
",
]
`;
exports[`webpackLoader works with documents with subscription operations 1`] = `
"
[
"
import { graphql } from "@nova/react";
const doc = require("./__generated__/SomeComponentSubscription.graphql").default;
console.log()
"
",
]
`;
exports[`webpackLoader works with files that contain no embedded documents 1`] = `
"
[
"
import { graphql } from "@nova/react";
console.log()
"
",
]
`;
exports[`webpackLoader works with inline documents 1`] = `
"
[
"
import { graphql } from "@nova/react";
const {data} = useFragment(require("./__generated__/SomeComponentFragment.graphql").default, props);
"
",
]
`;
exports[`webpackLoader works with inline documents over multiple lines 1`] = `
"
[
"
import { graphql } from "@nova/react";
const {data} = useFragment(
require("./__generated__/SomeComponentFragment.graphql").default,
props
);
"
",
]
`;

Просмотреть файл

@ -1,7 +1,35 @@
import { webpackLoader } from "../webpack";
import { RunLoaderResult, runLoaders } from "loader-runner";
describe(webpackLoader, () => {
function runLoader(source: string) {
return new Promise<RunLoaderResult>((resolve, reject) => {
runLoaders(
{
resource: "/path/to/your/resource.js",
loaders: [
{
loader: require.resolve("../webpack.ts"),
options: {},
},
],
context: {},
readResource: (_path, callback) => {
callback(null, Buffer.from(source));
},
},
(err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
},
);
});
}
describe("webpackLoader", () => {
it.todo("works with watch query documents");
it.todo("has a dependency on the artefact file");
it.each([
{
@ -84,7 +112,8 @@ describe(webpackLoader, () => {
);
`,
},
])("works with $name", ({ source }) => {
expect(webpackLoader.bind({} as any)(source)).toMatchSnapshot();
])("works with $name", async ({ source }) => {
const result = await runLoader(source);
expect(result.result).toMatchSnapshot();
});
});

Просмотреть файл

@ -3272,6 +3272,13 @@
dependencies:
"@types/node" "*"
"@types/loader-runner@^2.2.4":
version "2.2.4"
resolved "https://registry.yarnpkg.com/@types/loader-runner/-/loader-runner-2.2.4.tgz#42f21c1fe4a1fbc34100857a64db76450cc2ea2b"
integrity sha512-loD63p9KWQPgTvMBbhu9nD/2AeLqzP7ey/h1cWUK3wzLtilRO1kcCtlJvGrT/LEtOM7bjzaFSX1hxXaOdZdt8g==
dependencies:
"@types/node" "*"
"@types/lodash.camelcase@^4.3.6":
version "4.3.6"
resolved "https://registry.yarnpkg.com/@types/lodash.camelcase/-/lodash.camelcase-4.3.6.tgz#393c748b70cd926fc629e6502a9d0929f217d5fd"
@ -8874,6 +8881,11 @@ loader-runner@^4.2.0:
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-4.2.0.tgz#d7022380d66d14c5fb1d496b89864ebcfd478384"
integrity sha512-92+huvxMvYlMzMt0iIOukcwYBFpkYJdpl2xsZ7LrlayO7E8SOv+JJUEK17B/dJIHAOLMfh2dZZ/Y18WgmGtYNw==
loader-runner@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-4.3.0.tgz#c1b4a163b99f614830353b16755e7149ac2314e1"
integrity sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==
loader-utils@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-2.0.0.tgz#e4cace5b816d425a166b5f097e10cd12b36064b0"