Skip to content

Commit

Permalink
fix: cjs export and types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jan 17, 2022
1 parent ce2cb04 commit e836a00
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 150 deletions.
40 changes: 29 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/* 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,
ABSOLUTE_PUBLIC_PATH,
SINGLE_DOT_PATH_SEGMENT,
compareModulesByIdentifier,
getUndoPath,
} from "./utils";
} = require("./utils");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -480,6 +480,11 @@ class MiniCssExtractPlugin {
baseDataPath: "options",
});

/**
* @private
* @type {WeakMap<Chunk, Set<TODO>>}
* @private
*/
this._sortedModulesCache = new WeakMap();

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

if (usedModules || !modules) {
// @ts-ignore
return usedModules;
}

Expand Down Expand Up @@ -1119,7 +1125,8 @@ class MiniCssExtractPlugin {
* @param {Module} m
* @returns {boolean}
*/
const unusedModulesFilter = (m) => !usedModules.has(m);
const unusedModulesFilter = (m) =>
!(/** @type {Set<Module>} */ (usedModules).has(m));

while (usedModules.size < modulesList.length) {
let success = false;
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -1215,7 +1227,11 @@ class MiniCssExtractPlugin {
);
}

usedModules.add(fallbackModule);
usedModules.add(
/** @type {Module & { content: Buffer, media: string, sourceMap?: Buffer, supports?: string, layer?: string }} */ (
fallbackModule
)
);
}
}

Expand Down Expand Up @@ -1356,6 +1372,8 @@ class MiniCssExtractPlugin {
}
}

MiniCssExtractPlugin.pluginName = pluginName;
MiniCssExtractPlugin.pluginSymbol = pluginSymbol;
MiniCssExtractPlugin.loader = require.resolve("./loader");

export default MiniCssExtractPlugin;
module.exports = MiniCssExtractPlugin;
81 changes: 44 additions & 37 deletions src/loader.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -65,11 +65,13 @@ function hotLoader(content, context) {
* @this {import("webpack").LoaderContext<LoaderOptions>}
* @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(
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -332,7 +334,7 @@ export function pitch(request) {
const childCompiler =
/** @type {Compilation} */
(this._compilation).createChildCompiler(
`${pluginName} ${request}`,
`${MiniCssExtractPlugin.pluginName} ${request}`,
outputOptions
);

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

childCompiler.hooks.thisCompilation.tap(
`${pluginName} loader`,
`${MiniCssExtractPlugin.pluginName} loader`,
/**
* @param {Compilation} compilation
*/
(compilation) => {
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,
};
});
}
}
});
);
}
);

/** @type {string | Buffer} */
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);
});
});
});
});
}
);
}
);

Expand Down Expand Up @@ -478,5 +486,4 @@ export function pitch(request) {
});
}

// eslint-disable-next-line func-names
export default function () {}
module.exports = { default: function loader() {}, pitch };
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -204,7 +204,7 @@ function getUndoPath(filename, outputPath, enforceRelative) {
: append;
}

export {
module.exports = {
trueFn,
findModuleById,
evalModuleCode,
Expand Down
Loading

0 comments on commit e836a00

Please sign in to comment.