Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(css): rewrite @layer/@supports implementation
- Loading branch information
Showing
6 changed files
with
168 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Aurélien Manca @noreiller | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const NormalModule = require("../NormalModule"); | ||
const makeSerializable = require("../util/makeSerializable"); | ||
|
||
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ | ||
/** @typedef {import("../Compilation")} Compilation */ | ||
/** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */ | ||
/** @typedef {import("../NormalModule").NormalModuleCreateData} NormalModuleCreateData */ | ||
/** @typedef {import("../RequestShortener")} RequestShortener */ | ||
/** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */ | ||
/** @typedef {import("../WebpackError")} WebpackError */ | ||
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */ | ||
|
||
/** | ||
* @typedef {Object} Decoration | ||
* @property {string=} layer | ||
* @property {string=} media | ||
* @property {string=} supports | ||
*/ | ||
|
||
const SOURCE_TYPES = new Set(["css"]); | ||
|
||
class CssImportModule extends NormalModule { | ||
/** | ||
* @param {NormalModuleCreateData} createData createData object | ||
* @param {Decoration[]} decorations Decoration list | ||
*/ | ||
constructor(createData, decorations) { | ||
super(createData); | ||
|
||
/** @type {[string, string][]} */ | ||
this.decorations = decorations.reduce((acc, { layer, supports, media }) => { | ||
if (layer) { | ||
acc.push([`@layer ${layer} {`, `}`]); | ||
} | ||
|
||
if (supports) { | ||
acc.push([`@supports(${supports}) {`, `}`]); | ||
} | ||
|
||
if (media) { | ||
acc.push([`@media ${media} {`, `}`]); | ||
} | ||
|
||
return acc; | ||
}, []); | ||
} | ||
|
||
/** | ||
* @returns {Set<string>} types available (do not mutate) | ||
*/ | ||
getSourceTypes() { | ||
return SOURCE_TYPES; | ||
} | ||
|
||
serialize(context) { | ||
const { write } = context; | ||
|
||
write(this.decorations); | ||
|
||
super.serialize(context); | ||
} | ||
|
||
static deserialize(context) { | ||
const { read } = context; | ||
|
||
this.decorations = read(); | ||
|
||
return super.deserialize(context); | ||
} | ||
} | ||
|
||
makeSerializable(CssImportModule, "webpack/lib/css/CssImportModule"); | ||
|
||
module.exports = CssImportModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.