diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f0b4b1a..6e283796 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.5.1](https://github.com/webpack-contrib/mini-css-extract-plugin/compare/v2.5.0...v2.5.1) (2022-01-17) + + +### Bug Fixes + +* cjs export and types ([e836a00](https://github.com/webpack-contrib/mini-css-extract-plugin/commit/e836a00cb164fecdb49e670e0552475477fa7994)) + ## [2.5.0](https://github.com/webpack-contrib/mini-css-extract-plugin/compare/v2.4.7...v2.5.0) (2022-01-14) diff --git a/package-lock.json b/package-lock.json index 4cc7286d..ffce6d97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mini-css-extract-plugin", - "version": "2.5.0", + "version": "2.5.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mini-css-extract-plugin", - "version": "2.5.0", + "version": "2.5.1", "license": "MIT", "dependencies": { "schema-utils": "^4.0.0" diff --git a/package.json b/package.json index 91a49410..58c2c942 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mini-css-extract-plugin", - "version": "2.5.0", + "version": "2.5.1", "description": "extracts CSS into separate files", "license": "MIT", "repository": "webpack-contrib/mini-css-extract-plugin", diff --git a/src/index.js b/src/index.js index 9617711f..e7fabd20 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,11 @@ /* eslint-disable class-methods-use-this */ -import path from "path"; +const path = require("path"); -import { validate } from "schema-utils"; +const { validate } = require("schema-utils"); -import schema from "./plugin-options.json"; -import { +const schema = require("./plugin-options.json"); +const { trueFn, MODULE_TYPE, AUTO_PUBLIC_PATH, @@ -13,7 +13,7 @@ import { SINGLE_DOT_PATH_SEGMENT, compareModulesByIdentifier, getUndoPath, -} from "./utils"; +} = require("./utils"); /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ @@ -69,8 +69,8 @@ import { /** @typedef {any} TODO */ -export const pluginName = "mini-css-extract-plugin"; -export const pluginSymbol = Symbol(pluginName); +const pluginName = "mini-css-extract-plugin"; +const pluginSymbol = Symbol(pluginName); const DEFAULT_FILENAME = "[name].css"; const TYPES = new Set([MODULE_TYPE]); @@ -480,6 +480,11 @@ class MiniCssExtractPlugin { baseDataPath: "options", }); + /** + * @private + * @type {WeakMap>} + * @private + */ this._sortedModulesCache = new WeakMap(); /** @@ -1049,6 +1054,7 @@ class MiniCssExtractPlugin { let usedModules = this._sortedModulesCache.get(chunk); if (usedModules || !modules) { + // @ts-ignore return usedModules; } @@ -1119,7 +1125,8 @@ class MiniCssExtractPlugin { * @param {Module} m * @returns {boolean} */ - const unusedModulesFilter = (m) => !usedModules.has(m); + const unusedModulesFilter = (m) => + !(/** @type {Set} */ (usedModules).has(m)); while (usedModules.size < modulesList.length) { let success = false; @@ -1129,6 +1136,7 @@ class MiniCssExtractPlugin { // get first module where dependencies are fulfilled for (const list of modulesByChunkGroup) { // skip and remove already added modules + // @ts-ignore while (list.length > 0 && usedModules.has(list[list.length - 1])) { list.pop(); } @@ -1151,7 +1159,11 @@ class MiniCssExtractPlugin { if (failedDeps.length === 0) { // use this module and remove it from list - usedModules.add(list.pop()); + usedModules.add( + /** @type {Module & { content: Buffer, media: string, sourceMap?: Buffer, supports?: string, layer?: string }} */ ( + list.pop() + ) + ); success = true; break; } @@ -1215,7 +1227,11 @@ class MiniCssExtractPlugin { ); } - usedModules.add(fallbackModule); + usedModules.add( + /** @type {Module & { content: Buffer, media: string, sourceMap?: Buffer, supports?: string, layer?: string }} */ ( + fallbackModule + ) + ); } } @@ -1356,6 +1372,8 @@ class MiniCssExtractPlugin { } } +MiniCssExtractPlugin.pluginName = pluginName; +MiniCssExtractPlugin.pluginSymbol = pluginSymbol; MiniCssExtractPlugin.loader = require.resolve("./loader"); -export default MiniCssExtractPlugin; +module.exports = MiniCssExtractPlugin; diff --git a/src/loader.js b/src/loader.js index 80db2f3d..3a4e9173 100644 --- a/src/loader.js +++ b/src/loader.js @@ -1,16 +1,16 @@ -import path from "path"; +const path = require("path"); -import { +const { findModuleById, evalModuleCode, AUTO_PUBLIC_PATH, ABSOLUTE_PUBLIC_PATH, SINGLE_DOT_PATH_SEGMENT, stringifyRequest, -} from "./utils"; -import schema from "./loader-options.json"; +} = require("./utils"); +const schema = require("./loader-options.json"); -import MiniCssExtractPlugin, { pluginName, pluginSymbol } from "./index"; +const MiniCssExtractPlugin = require("./index"); /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ @@ -65,11 +65,13 @@ function hotLoader(content, context) { * @this {import("webpack").LoaderContext} * @param {string} request */ -export function pitch(request) { +function pitch(request) { // @ts-ignore const options = this.getOptions(/** @type {Schema} */ (schema)); const callback = this.async(); - const optionsFromPlugin = /** @type {TODO} */ (this)[pluginSymbol]; + const optionsFromPlugin = /** @type {TODO} */ (this)[ + MiniCssExtractPlugin.pluginSymbol + ]; if (!optionsFromPlugin) { callback( @@ -240,7 +242,7 @@ export function pitch(request) { ? `\nexport {};` : ""; - let resultSource = `// extracted by ${pluginName}`; + let resultSource = `// extracted by ${MiniCssExtractPlugin.pluginName}`; resultSource += this.hot ? hotLoader(result, { context: this.context, options, locals }) @@ -332,7 +334,7 @@ export function pitch(request) { const childCompiler = /** @type {Compilation} */ (this._compilation).createChildCompiler( - `${pluginName} ${request}`, + `${MiniCssExtractPlugin.pluginName} ${request}`, outputOptions ); @@ -377,7 +379,7 @@ export function pitch(request) { const { NormalModule } = webpack; childCompiler.hooks.thisCompilation.tap( - `${pluginName} loader`, + `${MiniCssExtractPlugin.pluginName} loader`, /** * @param {Compilation} compilation */ @@ -385,20 +387,23 @@ export function pitch(request) { const normalModuleHook = NormalModule.getCompilationHooks(compilation).loader; - normalModuleHook.tap(`${pluginName} loader`, (loaderContext, module) => { - if (module.request === request) { - // eslint-disable-next-line no-param-reassign - module.loaders = loaders.map((loader) => { - return { - type: null, - // @ts-ignore - loader: loader.path, - options: loader.options, - ident: loader.ident, - }; - }); + normalModuleHook.tap( + `${MiniCssExtractPlugin.pluginName} loader`, + (loaderContext, module) => { + if (module.request === request) { + // eslint-disable-next-line no-param-reassign + module.loaders = loaders.map((loader) => { + return { + type: null, + // @ts-ignore + loader: loader.path, + options: loader.options, + ident: loader.ident, + }; + }); + } } - }); + ); } ); @@ -406,23 +411,26 @@ export function pitch(request) { let source; childCompiler.hooks.compilation.tap( - pluginName, + MiniCssExtractPlugin.pluginName, /** * @param {Compilation} compilation */ (compilation) => { - compilation.hooks.processAssets.tap(pluginName, () => { - source = - compilation.assets[childFilename] && - compilation.assets[childFilename].source(); - - // Remove all chunk assets - compilation.chunks.forEach((chunk) => { - chunk.files.forEach((file) => { - compilation.deleteAsset(file); + compilation.hooks.processAssets.tap( + MiniCssExtractPlugin.pluginName, + () => { + source = + compilation.assets[childFilename] && + compilation.assets[childFilename].source(); + + // Remove all chunk assets + compilation.chunks.forEach((chunk) => { + chunk.files.forEach((file) => { + compilation.deleteAsset(file); + }); }); - }); - }); + } + ); } ); @@ -478,5 +486,4 @@ export function pitch(request) { }); } -// eslint-disable-next-line func-names -export default function () {} +module.exports = { default: function loader() {}, pitch }; diff --git a/src/utils.js b/src/utils.js index 7bad0d70..2784f8ee 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,5 @@ -import NativeModule from "module"; -import path from "path"; +const NativeModule = require("module"); +const path = require("path"); /** @typedef {import("webpack").Compilation} Compilation */ /** @typedef {import("webpack").Module} Module */ @@ -204,7 +204,7 @@ function getUndoPath(filename, outputPath, enforceRelative) { : append; } -export { +module.exports = { trueFn, findModuleById, evalModuleCode, diff --git a/types/index.d.ts b/types/index.d.ts index c750f9a1..16b52078 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,3 +1,105 @@ +export = MiniCssExtractPlugin; +declare class MiniCssExtractPlugin { + /** + * @private + * @param {Compiler["webpack"]} webpack + * @returns {typeof CssModule} + */ + private static getCssModule; + /** + * @private + * @param {Compiler["webpack"]} webpack + * @returns {typeof CssDependency} + */ + private static getCssDependency; + /** + * @param {PluginOptions} [options] + */ + constructor(options?: PluginOptions | undefined); + /** + * @private + * @type {WeakMap>} + * @private + */ + private _sortedModulesCache; + /** + * @private + * @type {NormalizedPluginOptions} + */ + private options; + /** + * @private + * @type {RuntimeOptions} + */ + private runtimeOptions; + /** + * @param {Compiler} compiler + */ + apply(compiler: Compiler): void; + /** + * @private + * @param {Chunk} chunk + * @param {ChunkGraph} chunkGraph + * @returns {Iterable} + */ + private getChunkModules; + /** + * @private + * @param {Compilation} compilation + * @param {Chunk} chunk + * @param {Iterable} modules + * @param {Compilation["requestShortener"]} requestShortener + * @returns {Set} + */ + private sortModules; + /** + * @private + * @param {Compiler} compiler + * @param {Compilation} compilation + * @param {Chunk} chunk + * @param {Iterable} modules + * @param {Compiler["requestShortener"]} requestShortener + * @param {string} filenameTemplate + * @param {Parameters['output']['filename'], string | undefined>>[0]} pathData + * @returns {Source} + */ + private renderContentAsset; +} +declare namespace MiniCssExtractPlugin { + export { + pluginName, + pluginSymbol, + loader, + Schema, + Compiler, + Compilation, + ChunkGraph, + Chunk, + ChunkGroup, + Module, + Dependency, + Source, + Configuration, + WebpackError, + AssetInfo, + LoaderOptions, + PluginOptions, + NormalizedPluginOptions, + RuntimeOptions, + TODO, + }; +} +type Compiler = import("webpack").Compiler; +type PluginOptions = { + filename?: Required["output"]["filename"]; + chunkFilename?: Required["output"]["chunkFilename"]; + ignoreOrder?: boolean | undefined; + insert?: string | ((linkTag: any) => void) | undefined; + attributes?: Record | undefined; + linkType?: string | false | undefined; + runtime?: boolean | undefined; + experimentalUseImportModule?: boolean | undefined; +}; /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ /** @typedef {import("webpack").Compilation} Compilation */ @@ -46,22 +148,21 @@ * @property {Record | undefined} attributes */ /** @typedef {any} TODO */ -export const pluginName: "mini-css-extract-plugin"; -export const pluginSymbol: unique symbol; -export default MiniCssExtractPlugin; -export type Schema = import("schema-utils/declarations/validate").Schema; -export type Compiler = import("webpack").Compiler; -export type Compilation = import("webpack").Compilation; -export type ChunkGraph = import("webpack").ChunkGraph; -export type Chunk = import("webpack").Chunk; -export type ChunkGroup = Parameters[0]; -export type Module = import("webpack").Module; -export type Dependency = import("webpack").Dependency; -export type Source = import("webpack").sources.Source; -export type Configuration = import("webpack").Configuration; -export type WebpackError = import("webpack").WebpackError; -export type AssetInfo = import("webpack").AssetInfo; -export type LoaderOptions = { +declare const pluginName: "mini-css-extract-plugin"; +declare const pluginSymbol: unique symbol; +declare var loader: string; +type Schema = import("schema-utils/declarations/validate").Schema; +type Compilation = import("webpack").Compilation; +type ChunkGraph = import("webpack").ChunkGraph; +type Chunk = import("webpack").Chunk; +type ChunkGroup = Parameters[0]; +type Module = import("webpack").Module; +type Dependency = import("webpack").Dependency; +type Source = import("webpack").sources.Source; +type Configuration = import("webpack").Configuration; +type WebpackError = import("webpack").WebpackError; +type AssetInfo = import("webpack").AssetInfo; +type LoaderOptions = { publicPath?: | string | ((resourcePath: string, rootContext: string) => string) @@ -70,17 +171,7 @@ export type LoaderOptions = { esModule?: boolean | undefined; layer?: string | undefined; }; -export type PluginOptions = { - filename?: Required["output"]["filename"]; - chunkFilename?: Required["output"]["chunkFilename"]; - ignoreOrder?: boolean | undefined; - insert?: string | ((linkTag: any) => void) | undefined; - attributes?: Record | undefined; - linkType?: string | false | undefined; - runtime?: boolean | undefined; - experimentalUseImportModule?: boolean | undefined; -}; -export type NormalizedPluginOptions = { +type NormalizedPluginOptions = { filename: Required["output"]["filename"]; chunkFilename?: Required["output"]["chunkFilename"]; ignoreOrder: boolean; @@ -90,73 +181,9 @@ export type NormalizedPluginOptions = { runtime: boolean; experimentalUseImportModule?: boolean | undefined; }; -export type RuntimeOptions = { +type RuntimeOptions = { insert: string | ((linkTag: any) => void) | undefined; linkType: string | false | "text/css"; attributes: Record | undefined; }; -export type TODO = any; -declare class MiniCssExtractPlugin { - /** - * @private - * @param {Compiler["webpack"]} webpack - * @returns {typeof CssModule} - */ - private static getCssModule; - /** - * @private - * @param {Compiler["webpack"]} webpack - * @returns {typeof CssDependency} - */ - private static getCssDependency; - /** - * @param {PluginOptions} [options] - */ - constructor(options?: PluginOptions | undefined); - _sortedModulesCache: WeakMap; - /** - * @private - * @type {NormalizedPluginOptions} - */ - private options; - /** - * @private - * @type {RuntimeOptions} - */ - private runtimeOptions; - /** - * @param {Compiler} compiler - */ - apply(compiler: Compiler): void; - /** - * @private - * @param {Chunk} chunk - * @param {ChunkGraph} chunkGraph - * @returns {Iterable} - */ - private getChunkModules; - /** - * @private - * @param {Compilation} compilation - * @param {Chunk} chunk - * @param {Iterable} modules - * @param {Compilation["requestShortener"]} requestShortener - * @returns {Set} - */ - private sortModules; - /** - * @private - * @param {Compiler} compiler - * @param {Compilation} compilation - * @param {Chunk} chunk - * @param {Iterable} modules - * @param {Compiler["requestShortener"]} requestShortener - * @param {string} filenameTemplate - * @param {Parameters['output']['filename'], string | undefined>>[0]} pathData - * @returns {Source} - */ - private renderContentAsset; -} -declare namespace MiniCssExtractPlugin { - const loader: string; -} +type TODO = any; diff --git a/types/loader.d.ts b/types/loader.d.ts index e45d9978..24586258 100644 --- a/types/loader.d.ts +++ b/types/loader.d.ts @@ -1,9 +1,3 @@ -/** - * @this {import("webpack").LoaderContext} - * @param {string} request - */ -export function pitch(request: string): void; -export default function _default(): void; export type Schema = import("schema-utils/declarations/validate").Schema; export type Compiler = import("webpack").Compiler; export type Compilation = import("webpack").Compilation; @@ -22,3 +16,10 @@ export type Dependency = { supports?: string | undefined; sourceMap?: Buffer | undefined; }; +/** + * @this {import("webpack").LoaderContext} + * @param {string} request + */ +export function pitch(request: string): void; +declare function _default(): void; +export { _default as default };