Skip to content

Commit e836a00

Browse files
fix: cjs export and types
1 parent ce2cb04 commit e836a00

File tree

5 files changed

+203
-150
lines changed

5 files changed

+203
-150
lines changed

src/index.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/* eslint-disable class-methods-use-this */
22

3-
import path from "path";
3+
const path = require("path");
44

5-
import { validate } from "schema-utils";
5+
const { validate } = require("schema-utils");
66

7-
import schema from "./plugin-options.json";
8-
import {
7+
const schema = require("./plugin-options.json");
8+
const {
99
trueFn,
1010
MODULE_TYPE,
1111
AUTO_PUBLIC_PATH,
1212
ABSOLUTE_PUBLIC_PATH,
1313
SINGLE_DOT_PATH_SEGMENT,
1414
compareModulesByIdentifier,
1515
getUndoPath,
16-
} from "./utils";
16+
} = require("./utils");
1717

1818
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
1919
/** @typedef {import("webpack").Compiler} Compiler */
@@ -69,8 +69,8 @@ import {
6969

7070
/** @typedef {any} TODO */
7171

72-
export const pluginName = "mini-css-extract-plugin";
73-
export const pluginSymbol = Symbol(pluginName);
72+
const pluginName = "mini-css-extract-plugin";
73+
const pluginSymbol = Symbol(pluginName);
7474

7575
const DEFAULT_FILENAME = "[name].css";
7676
const TYPES = new Set([MODULE_TYPE]);
@@ -480,6 +480,11 @@ class MiniCssExtractPlugin {
480480
baseDataPath: "options",
481481
});
482482

483+
/**
484+
* @private
485+
* @type {WeakMap<Chunk, Set<TODO>>}
486+
* @private
487+
*/
483488
this._sortedModulesCache = new WeakMap();
484489

485490
/**
@@ -1049,6 +1054,7 @@ class MiniCssExtractPlugin {
10491054
let usedModules = this._sortedModulesCache.get(chunk);
10501055

10511056
if (usedModules || !modules) {
1057+
// @ts-ignore
10521058
return usedModules;
10531059
}
10541060

@@ -1119,7 +1125,8 @@ class MiniCssExtractPlugin {
11191125
* @param {Module} m
11201126
* @returns {boolean}
11211127
*/
1122-
const unusedModulesFilter = (m) => !usedModules.has(m);
1128+
const unusedModulesFilter = (m) =>
1129+
!(/** @type {Set<Module>} */ (usedModules).has(m));
11231130

11241131
while (usedModules.size < modulesList.length) {
11251132
let success = false;
@@ -1129,6 +1136,7 @@ class MiniCssExtractPlugin {
11291136
// get first module where dependencies are fulfilled
11301137
for (const list of modulesByChunkGroup) {
11311138
// skip and remove already added modules
1139+
// @ts-ignore
11321140
while (list.length > 0 && usedModules.has(list[list.length - 1])) {
11331141
list.pop();
11341142
}
@@ -1151,7 +1159,11 @@ class MiniCssExtractPlugin {
11511159

11521160
if (failedDeps.length === 0) {
11531161
// use this module and remove it from list
1154-
usedModules.add(list.pop());
1162+
usedModules.add(
1163+
/** @type {Module & { content: Buffer, media: string, sourceMap?: Buffer, supports?: string, layer?: string }} */ (
1164+
list.pop()
1165+
)
1166+
);
11551167
success = true;
11561168
break;
11571169
}
@@ -1215,7 +1227,11 @@ class MiniCssExtractPlugin {
12151227
);
12161228
}
12171229

1218-
usedModules.add(fallbackModule);
1230+
usedModules.add(
1231+
/** @type {Module & { content: Buffer, media: string, sourceMap?: Buffer, supports?: string, layer?: string }} */ (
1232+
fallbackModule
1233+
)
1234+
);
12191235
}
12201236
}
12211237

@@ -1356,6 +1372,8 @@ class MiniCssExtractPlugin {
13561372
}
13571373
}
13581374

1375+
MiniCssExtractPlugin.pluginName = pluginName;
1376+
MiniCssExtractPlugin.pluginSymbol = pluginSymbol;
13591377
MiniCssExtractPlugin.loader = require.resolve("./loader");
13601378

1361-
export default MiniCssExtractPlugin;
1379+
module.exports = MiniCssExtractPlugin;

src/loader.js

+44-37
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import path from "path";
1+
const path = require("path");
22

3-
import {
3+
const {
44
findModuleById,
55
evalModuleCode,
66
AUTO_PUBLIC_PATH,
77
ABSOLUTE_PUBLIC_PATH,
88
SINGLE_DOT_PATH_SEGMENT,
99
stringifyRequest,
10-
} from "./utils";
11-
import schema from "./loader-options.json";
10+
} = require("./utils");
11+
const schema = require("./loader-options.json");
1212

13-
import MiniCssExtractPlugin, { pluginName, pluginSymbol } from "./index";
13+
const MiniCssExtractPlugin = require("./index");
1414

1515
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
1616
/** @typedef {import("webpack").Compiler} Compiler */
@@ -65,11 +65,13 @@ function hotLoader(content, context) {
6565
* @this {import("webpack").LoaderContext<LoaderOptions>}
6666
* @param {string} request
6767
*/
68-
export function pitch(request) {
68+
function pitch(request) {
6969
// @ts-ignore
7070
const options = this.getOptions(/** @type {Schema} */ (schema));
7171
const callback = this.async();
72-
const optionsFromPlugin = /** @type {TODO} */ (this)[pluginSymbol];
72+
const optionsFromPlugin = /** @type {TODO} */ (this)[
73+
MiniCssExtractPlugin.pluginSymbol
74+
];
7375

7476
if (!optionsFromPlugin) {
7577
callback(
@@ -240,7 +242,7 @@ export function pitch(request) {
240242
? `\nexport {};`
241243
: "";
242244

243-
let resultSource = `// extracted by ${pluginName}`;
245+
let resultSource = `// extracted by ${MiniCssExtractPlugin.pluginName}`;
244246

245247
resultSource += this.hot
246248
? hotLoader(result, { context: this.context, options, locals })
@@ -332,7 +334,7 @@ export function pitch(request) {
332334
const childCompiler =
333335
/** @type {Compilation} */
334336
(this._compilation).createChildCompiler(
335-
`${pluginName} ${request}`,
337+
`${MiniCssExtractPlugin.pluginName} ${request}`,
336338
outputOptions
337339
);
338340

@@ -377,52 +379,58 @@ export function pitch(request) {
377379
const { NormalModule } = webpack;
378380

379381
childCompiler.hooks.thisCompilation.tap(
380-
`${pluginName} loader`,
382+
`${MiniCssExtractPlugin.pluginName} loader`,
381383
/**
382384
* @param {Compilation} compilation
383385
*/
384386
(compilation) => {
385387
const normalModuleHook =
386388
NormalModule.getCompilationHooks(compilation).loader;
387389

388-
normalModuleHook.tap(`${pluginName} loader`, (loaderContext, module) => {
389-
if (module.request === request) {
390-
// eslint-disable-next-line no-param-reassign
391-
module.loaders = loaders.map((loader) => {
392-
return {
393-
type: null,
394-
// @ts-ignore
395-
loader: loader.path,
396-
options: loader.options,
397-
ident: loader.ident,
398-
};
399-
});
390+
normalModuleHook.tap(
391+
`${MiniCssExtractPlugin.pluginName} loader`,
392+
(loaderContext, module) => {
393+
if (module.request === request) {
394+
// eslint-disable-next-line no-param-reassign
395+
module.loaders = loaders.map((loader) => {
396+
return {
397+
type: null,
398+
// @ts-ignore
399+
loader: loader.path,
400+
options: loader.options,
401+
ident: loader.ident,
402+
};
403+
});
404+
}
400405
}
401-
});
406+
);
402407
}
403408
);
404409

405410
/** @type {string | Buffer} */
406411
let source;
407412

408413
childCompiler.hooks.compilation.tap(
409-
pluginName,
414+
MiniCssExtractPlugin.pluginName,
410415
/**
411416
* @param {Compilation} compilation
412417
*/
413418
(compilation) => {
414-
compilation.hooks.processAssets.tap(pluginName, () => {
415-
source =
416-
compilation.assets[childFilename] &&
417-
compilation.assets[childFilename].source();
418-
419-
// Remove all chunk assets
420-
compilation.chunks.forEach((chunk) => {
421-
chunk.files.forEach((file) => {
422-
compilation.deleteAsset(file);
419+
compilation.hooks.processAssets.tap(
420+
MiniCssExtractPlugin.pluginName,
421+
() => {
422+
source =
423+
compilation.assets[childFilename] &&
424+
compilation.assets[childFilename].source();
425+
426+
// Remove all chunk assets
427+
compilation.chunks.forEach((chunk) => {
428+
chunk.files.forEach((file) => {
429+
compilation.deleteAsset(file);
430+
});
423431
});
424-
});
425-
});
432+
}
433+
);
426434
}
427435
);
428436

@@ -478,5 +486,4 @@ export function pitch(request) {
478486
});
479487
}
480488

481-
// eslint-disable-next-line func-names
482-
export default function () {}
489+
module.exports = { default: function loader() {}, pitch };

src/utils.js

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

44
/** @typedef {import("webpack").Compilation} Compilation */
55
/** @typedef {import("webpack").Module} Module */
@@ -204,7 +204,7 @@ function getUndoPath(filename, outputPath, enforceRelative) {
204204
: append;
205205
}
206206

207-
export {
207+
module.exports = {
208208
trueFn,
209209
findModuleById,
210210
evalModuleCode,

0 commit comments

Comments
 (0)