Skip to content

Commit

Permalink
refactor(types): more types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 28, 2023
1 parent a126050 commit 0bc0339
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/css/CssGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CssGenerator extends Generator {
generate(module, generateContext) {
const originalSource = module.originalSource();
const source = new ReplaceSource(originalSource);
/** @type {InitFragment[]} */
const initFragments = [];
const cssExports = new Map();

Expand All @@ -51,6 +52,9 @@ class CssGenerator extends Generator {
cssExports
};

/**
* @param {Dependency} dependency dependency
*/
const handleDependency = dependency => {
const constructor = /** @type {new (...args: any[]) => Dependency} */ (
dependency.constructor
Expand Down
34 changes: 27 additions & 7 deletions lib/css/CssModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
const CssExportsGenerator = require("./CssExportsGenerator");
const CssGenerator = require("./CssGenerator");
const CssParser = require("./CssParser");
const WebpackError = require("../WebpackError");

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").CssExperimentOptions} CssExperimentOptions */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */

Expand Down Expand Up @@ -275,6 +278,12 @@ class CssModulesPlugin {
);
}

/**
* @param {Chunk} chunk chunk
* @param {Iterable<Module>} modules unordered modules
* @param {Compilation} compilation compilation
* @returns {Module[]} ordered modules
*/
getModulesInOrder(chunk, modules, compilation) {
if (!modules) return [];

Expand Down Expand Up @@ -320,6 +329,7 @@ class CssModulesPlugin {
// done, everything empty
break;
}
/** @type {Module} */
let selectedModule = list[list.length - 1];
let hasFailed = undefined;
outer: for (;;) {
Expand All @@ -345,18 +355,17 @@ class CssModulesPlugin {
if (compilation) {
// TODO print better warning
compilation.warnings.push(
new Error(
`chunk ${
chunk.name || chunk.id
}\nConflicting order between ${hasFailed.readableIdentifier(
compilation.requestShortener
)} and ${selectedModule.readableIdentifier(
new WebpackError(
`chunk ${chunk.name || chunk.id}\nConflicting order between ${
/** @type {Module} */
(hasFailed).readableIdentifier(compilation.requestShortener)
} and ${selectedModule.readableIdentifier(
compilation.requestShortener
)}`
)
);
}
selectedModule = hasFailed;
selectedModule = /** @type {Module} */ (hasFailed);
}
// Insert the selected module into the final modules list
finalModules.push(selectedModule);
Expand All @@ -374,6 +383,12 @@ class CssModulesPlugin {
return finalModules;
}

/**
* @param {Chunk} chunk chunk
* @param {ChunkGraph} chunkGraph chunk graph
* @param {Compilation} compilation compilation
* @returns {Module[]} ordered css modules
*/
getOrderedChunkCssModules(chunk, chunkGraph, compilation) {
return [
...this.getModulesInOrder(
Expand Down Expand Up @@ -492,6 +507,11 @@ class CssModulesPlugin {
}
}

/**
* @param {Chunk} chunk chunk
* @param {ChunkGraph} chunkGraph chunk graph
* @returns {boolean} true, when the chunk has css
*/
static chunkHasCss(chunk, chunkGraph) {
return (
!!chunkGraph.getChunkModulesIterableBySourceType(chunk, "css") ||
Expand Down
23 changes: 22 additions & 1 deletion lib/css/CssParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const OPTIONALLY_VENDOR_PREFIXED_KEYFRAMES_AT_RULE = /^@(-\w+-)?keyframes$/;
const OPTIONALLY_VENDOR_PREFIXED_ANIMATION_PROPERTY =
/^(-\w+-)?animation(-name)?$/i;

/**
* @param {string} str url string
* @param {boolean} isString is url wrapped in quotes
* @returns {string} normalized url
*/
const normalizeUrl = (str, isString) => {
// Remove extra spaces and newlines:
// `url("im\
Expand Down Expand Up @@ -71,13 +76,20 @@ const normalizeUrl = (str, isString) => {
};

class LocConverter {
/**
* @param {string} input input
*/
constructor(input) {
this._input = input;
this.line = 1;
this.column = 0;
this.pos = 0;
}

/**
* @param {number} pos position
* @returns {LocConverter} location converter
*/
get(pos) {
if (this.pos !== pos) {
if (this.pos < pos) {
Expand Down Expand Up @@ -112,6 +124,10 @@ const CSS_MODE_AT_IMPORT_EXPECT_URL = 3;
const CSS_MODE_AT_IMPORT_EXPECT_LAYER_OR_SUPPORTS_OR_MEDIA = 4;
const CSS_MODE_AT_OTHER = 5;

/**
* @param {number} mode current mode
* @returns {string} description of mode
*/
const explainMode = mode => {
switch (mode) {
case CSS_MODE_TOP_LEVEL:
Expand All @@ -127,7 +143,7 @@ const explainMode = mode => {
case CSS_MODE_AT_OTHER:
return "parsing at-rule";
default:
return mode;
return "parsing css";
}
};

Expand Down Expand Up @@ -163,11 +179,16 @@ class CssParser extends Parser {
const declaredCssVariables = new Set();

const locConverter = new LocConverter(source);
/** @type {number} */
let mode = CSS_MODE_TOP_LEVEL;
/** @type {number} */
let modeNestingLevel = 0;
let modeData = undefined;
/** @type {string | boolean | undefined} */
let singleClassSelector = undefined;
/** @type {[number, number] | undefined} */
let lastIdentifier = undefined;
/** @type {boolean} */
let awaitRightParenthesis = false;
/** @type [string, number, number][] */
let balanced = [];
Expand Down

0 comments on commit 0bc0339

Please sign in to comment.