Skip to content

Commit

Permalink
Add Webpack support
Browse files Browse the repository at this point in the history
  • Loading branch information
zauni committed Sep 17, 2024
1 parent 329e383 commit 0194c90
Show file tree
Hide file tree
Showing 9 changed files with 856 additions and 3 deletions.
766 changes: 765 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"./vite": {
"require": "./dist/vite.cjs",
"import": "./dist/vite.js"
},
"./webpack": {
"require": "./dist/webpack.cjs",
"import": "./dist/webpack.js"
}
},
"typesVersions": {
Expand Down Expand Up @@ -66,7 +70,8 @@
"typescript": "^5.6.2",
"unplugin": "^1.14.1",
"vite": "^5.4.4",
"vitest": "^2.0.5"
"vitest": "^2.0.5",
"webpack": "^5.94.0"
},
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions src/__snapshots__/webpack.unit.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Webpack plugin > should handle a JSON schema with a reference 1`] = `"(()=>{"use strict";console.log({openapi:"3.0.0",info:{title:"My great API",description:"Great description",version:"1.0.0"},paths:{"/my/path":{get:{summary:"Some GET request",responses:{200:{description:"Some response",content:{"application/json":{schema:{type:"object",properties:{someKey:{type:"string"}},required:["someKey"]},example:{someKey:"some value"}}}}}}}}})})();"`;

exports[`Webpack plugin > should handle a YAML schema with a reference 1`] = `"(()=>{"use strict";console.log({openapi:"3.0.0",info:{title:"My great API",description:"Great description",version:"1.0.0"},paths:{"/my/path":{get:{summary:"Some GET request",responses:{200:{description:"Some response",content:{"application/json":{schema:{type:"object",properties:{someKey:{type:"string"}},required:["someKey"]},example:{someKey:"some value"}}}}}}}}})})();"`;
3 changes: 3 additions & 0 deletions src/fixtures/webpack/main-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import api from "../api.openapi.json";

console.log(api);
3 changes: 3 additions & 0 deletions src/fixtures/webpack/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import api from "../api.yaml";

console.log(api);
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (opts) => {
esbuild: {
loader: "js",
},
webpack(compiler) {
compiler.options.module.rules.push({
test: (value) =>
(options.extensions ?? []).some((ext) => value.includes(ext)),
type: "javascript/auto", // Treat JSON files as JavaScript modules
});
},
};
};

Expand Down
3 changes: 3 additions & 0 deletions src/webpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { unplugin } from "./index.js";

export default unplugin.webpack as typeof unplugin.webpack;
57 changes: 57 additions & 0 deletions src/webpack.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Volume, createFsFromVolume } from "memfs";
import { describe, expect, it } from "vitest";
import webpack, { type OutputFileSystem } from "webpack";
import webpackPlugin from "./webpack.js";

async function build(config: webpack.Configuration): Promise<string> {
return new Promise((resolve, reject) => {
const compiler = webpack({
mode: "production",
output: {
path: "/", // Set to '/' since we are using an in-memory filesystem
filename: "bundle.js", // Output file name
},
...config,
});
const vol = new Volume();
const memFs = createFsFromVolume(vol);
compiler.outputFileSystem = memFs as OutputFileSystem;

compiler.run((err, stats) => {
if (err || stats?.hasErrors()) {
reject(err || stats?.toJson().errors);
return;
}

const transformedCode = memFs.readFileSync("/bundle.js", "utf-8");

resolve(transformedCode.toString());
});
});
}

describe("Webpack plugin", () => {
it("should handle a YAML schema with a reference", async () => {
const code = await build({
entry: "./src/fixtures/webpack/main.js",
plugins: [webpackPlugin()],
});

expect(code).toMatchSnapshot();
expect(code).toContain('type:"object",');
});

it("should handle a JSON schema with a reference", async () => {
const code = await build({
entry: "./src/fixtures/webpack/main-json.js",
plugins: [
webpackPlugin({
extensions: [".openapi.json"],
}),
],
});

expect(code).toMatchSnapshot();
expect(code).toContain('type:"object",');
});
});
8 changes: 7 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts", "src/esbuild.ts", "src/rollup.ts", "src/vite.ts"],
entry: [
"src/index.ts",
"src/esbuild.ts",
"src/rollup.ts",
"src/vite.ts",
"src/webpack.ts",
],
clean: true,
format: ["cjs", "esm"],
dts: true,
Expand Down

0 comments on commit 0194c90

Please sign in to comment.