Skip to content

Commit b6d53d3

Browse files
feat: remove cjs wrapper (#1146)
1 parent 231cc46 commit b6d53d3

22 files changed

+236
-148
lines changed

package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,33 @@
1111
"type": "opencollective",
1212
"url": "https://opencollective.com/webpack"
1313
},
14-
"main": "dist/cjs.js",
15-
"types": "types/cjs.d.ts",
14+
"main": "dist/index.js",
15+
"types": "types/index.d.ts",
1616
"engines": {
1717
"node": ">= 12.13.0"
1818
},
1919
"scripts": {
2020
"commitlint": "commitlint --from=master",
21+
"security": "npm audit --production",
2122
"fmt:check": "prettier \"{**/*,*}.{js,json,md,yml,css}\" --list-different",
2223
"lint:js": "eslint --cache src test",
2324
"lint:types": "tsc --pretty --noEmit",
2425
"lint": "npm-run-all lint:js fmt:check",
2526
"fmt": "npm run fmt:check -- --write",
2627
"fix:js": "npm run lint:js -- --fix",
2728
"fix": "npm-run-all fix:js fmt",
28-
"prepare": "husky install && npm run build",
29+
"clean": "del-cli dist types",
30+
"prebuild": "npm run clean",
2931
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
3032
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
3133
"build": "npm-run-all -p \"build:**\"",
32-
"release": "standard-version",
33-
"security": "npm audit --production",
3434
"test:only": "cross-env NODE_ENV=test jest",
3535
"test:watch": "npm run test:only -- --watch",
3636
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
3737
"pretest": "npm run lint",
38-
"test": "npm run test:coverage"
38+
"test": "npm run test:coverage",
39+
"prepare": "husky install && npm run build",
40+
"release": "standard-version"
3941
},
4042
"files": [
4143
"dist",

src/cjs.js

-3
This file was deleted.

src/index.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { validate } from "schema-utils";
2-
import mime from "mime-types";
1+
const { validate } = require("schema-utils");
2+
const mime = require("mime-types");
33

4-
import middleware from "./middleware";
5-
import getFilenameFromUrl from "./utils/getFilenameFromUrl";
6-
import setupHooks from "./utils/setupHooks";
7-
import setupWriteToDisk from "./utils/setupWriteToDisk";
8-
import setupOutputFileSystem from "./utils/setupOutputFileSystem";
9-
import ready from "./utils/ready";
10-
import schema from "./options.json";
4+
const middleware = require("./middleware");
5+
const getFilenameFromUrl = require("./utils/getFilenameFromUrl");
6+
const setupHooks = require("./utils/setupHooks");
7+
const setupWriteToDisk = require("./utils/setupWriteToDisk");
8+
const setupOutputFileSystem = require("./utils/setupOutputFileSystem");
9+
const ready = require("./utils/ready");
10+
const schema = require("./options.json");
1111

1212
const noop = () => {};
1313

@@ -18,10 +18,9 @@ const noop = () => {};
1818
/** @typedef {import("webpack").Stats} Stats */
1919
/** @typedef {import("webpack").MultiStats} MultiStats */
2020

21-
// TODO fix me
2221
/**
2322
* @typedef {Object} ExtendedServerResponse
24-
* @property {{ webpack?: { devMiddleware?: Context<any, any> } }} [locals]
23+
* @property {{ webpack?: { devMiddleware?: Context<IncomingMessage, ServerResponse> } }} [locals]
2524
*/
2625

2726
/** @typedef {import("http").IncomingMessage} IncomingMessage */
@@ -146,7 +145,7 @@ const noop = () => {};
146145
* @param {Options<Request, Response>} [options]
147146
* @returns {API<Request, Response>}
148147
*/
149-
export default function wdm(compiler, options = {}) {
148+
function wdm(compiler, options = {}) {
150149
validate(/** @type {Schema} */ (schema), options, {
151150
name: "Dev Middleware",
152151
baseDataPath: "options",
@@ -277,3 +276,5 @@ export default function wdm(compiler, options = {}) {
277276

278277
return instance;
279278
}
279+
280+
module.exports = wdm;

src/middleware.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import path from "path";
1+
const path = require("path");
22

3-
import mime from "mime-types";
4-
import parseRange from "range-parser";
3+
const mime = require("mime-types");
4+
const parseRange = require("range-parser");
55

6-
import getFilenameFromUrl from "./utils/getFilenameFromUrl";
7-
import {
6+
const getFilenameFromUrl = require("./utils/getFilenameFromUrl");
7+
const {
88
getHeaderNames,
99
getHeaderFromRequest,
1010
getHeaderFromResponse,
1111
setHeaderForResponse,
1212
setStatusCode,
1313
send,
14-
} from "./utils/compatibleAPI";
15-
import ready from "./utils/ready";
14+
} = require("./utils/compatibleAPI");
15+
const ready = require("./utils/ready");
1616

1717
/** @typedef {import("./index.js").NextFunction} NextFunction */
1818
/** @typedef {import("./index.js").IncomingMessage} IncomingMessage */
@@ -58,7 +58,7 @@ const BYTES_RANGE_REGEXP = /^ *bytes/i;
5858
* @param {import("./index.js").Context<Request, Response>} context
5959
* @return {import("./index.js").Middleware<Request, Response>}
6060
*/
61-
export default function wrapper(context) {
61+
function wrapper(context) {
6262
return async function middleware(req, res, next) {
6363
const acceptedMethods = context.options.methods || ["GET", "HEAD"];
6464

@@ -270,3 +270,5 @@ export default function wrapper(context) {
270270
}
271271
};
272272
}
273+
274+
module.exports = wrapper;

src/utils/getFilenameFromUrl.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import path from "path";
2-
import { parse } from "url";
3-
import querystring from "querystring";
1+
const path = require("path");
2+
const { parse } = require("url");
3+
const querystring = require("querystring");
44

5-
import getPaths from "./getPaths";
5+
const getPaths = require("./getPaths");
66

77
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
88
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
@@ -49,7 +49,7 @@ const memoizedParse = mem(parse);
4949
* @param {string} url
5050
* @returns {string | undefined}
5151
*/
52-
export default function getFilenameFromUrl(context, url) {
52+
function getFilenameFromUrl(context, url) {
5353
const { options } = context;
5454
const paths = getPaths(context);
5555

@@ -142,3 +142,5 @@ export default function getFilenameFromUrl(context, url) {
142142
// eslint-disable-next-line consistent-return
143143
return foundFilename;
144144
}
145+
146+
module.exports = getFilenameFromUrl;

src/utils/getPaths.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @template {ServerResponse} Response
1010
* @param {import("../index.js").Context<Request, Response>} context
1111
*/
12-
export default function getPaths(context) {
12+
function getPaths(context) {
1313
const { stats, options } = context;
1414
/** @type {Stats[]} */
1515
const childStats =
@@ -35,3 +35,5 @@ export default function getPaths(context) {
3535

3636
return publicPaths;
3737
}
38+
39+
module.exports = getPaths;

src/utils/ready.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @param {Request} [req]
1010
* @returns {void}
1111
*/
12-
export default function ready(context, callback, req) {
12+
function ready(context, callback, req) {
1313
if (context.state) {
1414
callback(context.stats);
1515

@@ -22,3 +22,5 @@ export default function ready(context, callback, req) {
2222

2323
context.callbacks.push(callback);
2424
}
25+
26+
module.exports = ready;

src/utils/setupHooks.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import webpack from "webpack";
2-
import { isColorSupported } from "colorette";
1+
const webpack = require("webpack");
2+
const { isColorSupported } = require("colorette");
33

44
/** @typedef {import("webpack").Configuration} Configuration */
55
/** @typedef {import("webpack").Compiler} Compiler */
@@ -20,7 +20,7 @@ import { isColorSupported } from "colorette";
2020
* @template {ServerResponse} Response
2121
* @param {import("../index.js").Context<Request, Response>} context
2222
*/
23-
export default function setupHooks(context) {
23+
function setupHooks(context) {
2424
function invalid() {
2525
if (context.state) {
2626
context.logger.log("Compilation starting...");
@@ -198,3 +198,5 @@ export default function setupHooks(context) {
198198
context.compiler.hooks.invalid.tap("webpack-dev-middleware", invalid);
199199
context.compiler.hooks.done.tap("webpack-dev-middleware", done);
200200
}
201+
202+
module.exports = setupHooks;

src/utils/setupOutputFileSystem.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import path from "path";
2-
3-
import { createFsFromVolume, Volume } from "memfs";
1+
const path = require("path");
2+
const memfs = require("memfs");
43

54
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
65
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
@@ -11,7 +10,7 @@ import { createFsFromVolume, Volume } from "memfs";
1110
* @template {ServerResponse} Response
1211
* @param {import("../index.js").Context<Request, Response>} context
1312
*/
14-
export default function setupOutputFileSystem(context) {
13+
function setupOutputFileSystem(context) {
1514
let outputFileSystem;
1615

1716
if (context.options.outputFileSystem) {
@@ -34,7 +33,7 @@ export default function setupOutputFileSystem(context) {
3433

3534
outputFileSystem = outputFileSystemFromOptions;
3635
} else {
37-
outputFileSystem = createFsFromVolume(new Volume());
36+
outputFileSystem = memfs.createFsFromVolume(new memfs.Volume());
3837
// TODO: remove when we drop webpack@4 support
3938
// @ts-ignore
4039
outputFileSystem.join = path.join.bind(path);
@@ -52,3 +51,5 @@ export default function setupOutputFileSystem(context) {
5251
// eslint-disable-next-line no-param-reassign
5352
context.outputFileSystem = outputFileSystem;
5453
}
54+
55+
module.exports = setupOutputFileSystem;

src/utils/setupWriteToDisk.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from "fs";
2-
import path from "path";
1+
const fs = require("fs");
2+
const path = require("path");
33

44
/** @typedef {import("webpack").Compiler} Compiler */
55
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
@@ -12,7 +12,7 @@ import path from "path";
1212
* @template {ServerResponse} Response
1313
* @param {import("../index.js").Context<Request, Response>} context
1414
*/
15-
export default function setupWriteToDisk(context) {
15+
function setupWriteToDisk(context) {
1616
/**
1717
* @type {Compiler[]}
1818
*/
@@ -113,3 +113,5 @@ export default function setupWriteToDisk(context) {
113113
);
114114
}
115115
}
116+
117+
module.exports = setupWriteToDisk;

test/cjs.test.js

-8
This file was deleted.

test/helpers/runner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const express = require("express");
44
const webpack = require("webpack");
55
const merge = require("deepmerge");
66

7-
const middleware = require("../../dist").default;
7+
const middleware = require("../../dist");
88
const defaultConfig = require("../fixtures/webpack.config");
99

1010
const configEntries = [];

test/utils/setupOutputFileSystem.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ describe("setupOutputFileSystem", () => {
2020
compiler: {},
2121
options: {},
2222
};
23+
2324
setupOutputFileSystem(context);
25+
2426
// make sure that this is the default fs created
2527
expect(context.compiler.outputFileSystem.testFs).toBeTruthy();
2628
expect(context.outputFileSystem.testFs).toBeTruthy();
@@ -36,7 +38,9 @@ describe("setupOutputFileSystem", () => {
3638
},
3739
options: {},
3840
};
41+
3942
setupOutputFileSystem(context);
43+
4044
context.compiler.compilers.forEach((comp) => {
4145
expect(comp.outputFileSystem).toBeTruthy();
4246
});
@@ -49,6 +53,7 @@ describe("setupOutputFileSystem", () => {
4953
outputFileSystem: {},
5054
},
5155
};
56+
5257
expect(() => {
5358
setupOutputFileSystem(context);
5459
}).toThrow(/join/);
@@ -63,6 +68,7 @@ describe("setupOutputFileSystem", () => {
6368
},
6469
},
6570
};
71+
6672
expect(() => {
6773
setupOutputFileSystem(context);
6874
}).toThrow(/mkdirp/);
@@ -78,7 +84,9 @@ describe("setupOutputFileSystem", () => {
7884
},
7985
},
8086
};
87+
8188
setupOutputFileSystem(context);
89+
8290
expect(context.outputFileSystem).toEqual(context.options.outputFileSystem);
8391
});
8492
});

types/cjs.d.ts

-3
This file was deleted.

0 commit comments

Comments
 (0)