6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
node-version: 17.x
cache: "yarn"
- run: yarn --frozen-lockfile
- uses: actions/cache@v1
- uses: actions/cache@v3
with:
path: .eslintcache
key: lint-${{ env.GITHUB_SHA }}
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
- run: yarn --frozen-lockfile
- run: yarn link --frozen-lockfile || true
- run: yarn link webpack --frozen-lockfile
- uses: actions/cache@v1
- uses: actions/cache@v3
with:
path: .jest-cache
key: jest-unit-${{ env.GITHUB_SHA }}
Expand Down Expand Up @@ -101,7 +101,7 @@ jobs:
- run: yarn --frozen-lockfile
- run: yarn link --frozen-lockfile || true
- run: yarn link webpack --frozen-lockfile
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: .jest-cache
key: jest-integration-${{ env.GITHUB_SHA }}
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ or are automatically applied via regex from your webpack configuration.

#### Transpiling

| Name | Status | Install Size | Description |
| :--------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------: | :------------: | :------------------------------------------------------------------------------------------------ |
| <a href="https://github.com/babel/babel-loader"><img width="48" height="48" title="babel-loader" src="https://worldvectorlogo.com/logos/babel-10.svg"></a> | ![babel-npm] | ![babel-size] | Loads ES2015+ code and transpiles to ES5 using <a href="https://github.com/babel/babel">Babel</a> |
| <a href="https://github.com/TypeStrong/ts-loader"><img width="48" height="48" src="https://cdn.rawgit.com/Microsoft/TypeScript/master/doc/logo.svg"></a> | ![type-npm] | ![type-size] | Loads TypeScript like JavaScript |
| <a href="https://github.com/webpack-contrib/coffee-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/coffeescript.svg"></a> | ![coffee-npm] | ![coffee-size] | Loads CoffeeScript like JavaScript |
| Name | Status | Install Size | Description |
| :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------: | :------------: | :------------------------------------------------------------------------------------------------ |
| <a href="https://github.com/babel/babel-loader"><img width="48" height="48" title="babel-loader" src="https://worldvectorlogo.com/logos/babel-10.svg"></a> | ![babel-npm] | ![babel-size] | Loads ES2015+ code and transpiles to ES5 using <a href="https://github.com/babel/babel">Babel</a> |
| <a href="https://github.com/TypeStrong/ts-loader"><img width="48" height="48" src="https://raw.githubusercontent.com/microsoft/TypeScript-Website/f407e1ae19e5e990d9901ac8064a32a8cc60edf0/packages/typescriptlang-org/static/branding/ts-logo-128.svg"></a> | ![type-npm] | ![type-size] | Loads TypeScript like JavaScript |
| <a href="https://github.com/webpack-contrib/coffee-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/coffeescript.svg"></a> | ![coffee-npm] | ![coffee-size] | Loads CoffeeScript like JavaScript |

[babel-npm]: https://img.shields.io/npm/v/babel-loader.svg
[babel-size]: https://packagephobia.com/badge?p=babel-loader
Expand All @@ -175,7 +175,7 @@ or are automatically applied via regex from your webpack configuration.

| Name | Status | Install Size | Description |
| :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------: | :--------------: | :-------------------------------------------------------------------------------------- |
| <a href="https://github.com/webpack-contrib/html-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/html5.svg"></a> | ![html-npm] | ![html-size] | Exports HTML as string, requires references to static resources |
| <a href="https://github.com/webpack-contrib/html-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/html5-2.svg"></a> | ![html-npm] | ![html-size] | Exports HTML as string, requires references to static resources |
| <a href="https://github.com/pugjs/pug-loader"><img width="48" height="48" src="https://cdn.rawgit.com/pugjs/pug-logo/master/SVG/pug-final-logo-_-colour-128.svg"></a> | ![pug-npm] | ![pug-size] | Loads Pug templates and returns a function |
| <a href="https://github.com/webdiscus/pug-loader"><img width="48" height="48" src="https://cdn.rawgit.com/pugjs/pug-logo/master/SVG/pug-final-logo-_-colour-128.svg"></a> | ![pug3-npm] | ![pug3-size] | Compiles Pug to a function or HTML string, useful for use with Vue, React, Angular |
| <a href="https://github.com/peerigon/markdown-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/markdown.svg"></a> | ![md-npm] | ![md-size] | Compiles Markdown to HTML |
Expand Down
6 changes: 6 additions & 0 deletions declarations/LoaderContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ export interface LoaderRunnerLoaderContext<OptionsType> {
* Example: "/abc/resource.js?query#frag"
*/
resource: string;

/**
* Target of compilation.
* Example: "web"
*/
target: string;
}

type AdditionalData = {
Expand Down
12 changes: 7 additions & 5 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ class NormalModule extends Module {
this._isEvaluatingSideEffects = false;
/** @type {WeakSet<ModuleGraph> | undefined} */
this._addedSideEffectsBailout = undefined;
/** @type {Map<string, any>} */
this._codeGeneratorData = new Map();
}

/**
Expand Down Expand Up @@ -1188,11 +1190,9 @@ class NormalModule extends Module {
runtimeRequirements.add(RuntimeGlobals.thisAsExports);
}

/** @type {Map<string, any>} */
let data;
/** @type {function(): Map<string, any>} */
const getData = () => {
if (data === undefined) data = new Map();
return data;
return this._codeGeneratorData;
};

const sources = new Map();
Expand Down Expand Up @@ -1223,7 +1223,7 @@ class NormalModule extends Module {
const resultEntry = {
sources,
runtimeRequirements,
data
data: this._codeGeneratorData
};
return resultEntry;
}
Expand Down Expand Up @@ -1371,6 +1371,7 @@ class NormalModule extends Module {
write(this.error);
write(this._lastSuccessfulBuildMeta);
write(this._forceBuild);
write(this._codeGeneratorData);
super.serialize(context);
}

Expand Down Expand Up @@ -1403,6 +1404,7 @@ class NormalModule extends Module {
this.error = read();
this._lastSuccessfulBuildMeta = read();
this._forceBuild = read();
this._codeGeneratorData = read();
super.deserialize(context);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/dependencies/ImportParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ImportParserPlugin {
if (importOptions.webpackInclude !== undefined) {
if (
!importOptions.webpackInclude ||
importOptions.webpackInclude.constructor.name !== "RegExp"
!(importOptions.webpackInclude instanceof RegExp)
) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
Expand All @@ -146,13 +146,13 @@ class ImportParserPlugin {
)
);
} else {
include = new RegExp(importOptions.webpackInclude);
include = importOptions.webpackInclude;
}
}
if (importOptions.webpackExclude !== undefined) {
if (
!importOptions.webpackExclude ||
importOptions.webpackExclude.constructor.name !== "RegExp"
!(importOptions.webpackExclude instanceof RegExp)
) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
Expand All @@ -161,7 +161,7 @@ class ImportParserPlugin {
)
);
} else {
exclude = new RegExp(importOptions.webpackExclude);
exclude = importOptions.webpackExclude;
}
}
if (importOptions.webpackExports !== undefined) {
Expand Down
18 changes: 14 additions & 4 deletions lib/javascript/JavascriptParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3635,17 +3635,27 @@ class JavascriptParser extends Parser {
return EMPTY_COMMENT_OPTIONS;
}
let options = {};
/** @type {unknown[]} */
let errors = [];
for (const comment of comments) {
const { value } = comment;
if (value && webpackCommentRegExp.test(value)) {
// try compile only if webpack options comment is present
try {
const val = vm.runInNewContext(`(function(){return {${value}};})()`);
Object.assign(options, val);
for (let [key, val] of Object.entries(
vm.runInNewContext(`(function(){return {${value}};})()`)
)) {
if (typeof val === "object" && val !== null) {
if (val.constructor.name === "RegExp") val = new RegExp(val);
else val = JSON.parse(JSON.stringify(val));
}
options[key] = val;
}
} catch (e) {
e.comment = comment;
errors.push(e);
const newErr = new Error(String(e.message));
newErr.stack = String(e.stack);
Object.assign(newErr, { comment });
errors.push(newErr);
}
}
}
Expand Down
60 changes: 45 additions & 15 deletions lib/optimize/RealContentHashPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,43 @@ class RealContentHashPlugin {
}
}
if (hashToAssets.size === 0) return;
const hashRegExp = new RegExp(
Array.from(hashToAssets.keys(), quoteMeta).join("|"),
"g"
const hashRegExps = Array.from(hashToAssets.keys(), quoteMeta).map(
hash => new RegExp(hash, "g")
);

/**
* @param {string} str string to be matched against all hashRegExps
* @returns {string[] | null} matches found
*/
const hashMatch = str => {
/** @type {string[]} */
const results = [];
for (const hashRegExp of hashRegExps) {
const matches = str.match(hashRegExp);
if (matches) {
matches.forEach(match => results.push(match));
}
}
if (results.length) {
return results;
} else {
return null;
}
};

/**
* @param {string} str string to be replaced with all hashRegExps
* @param {function(string): string} fn replacement function to use when a hash is found
* @returns {string} replaced content
*/
const hashReplace = (str, fn) => {
let result = str;
for (const hashRegExp of hashRegExps) {
result = result.replace(hashRegExp, fn);
}
return result;
};

await Promise.all(
assetsWithInfo.map(async asset => {
const { name, source, content, hashes } = asset;
Expand All @@ -198,7 +231,7 @@ class RealContentHashPlugin {
await cacheAnalyse.providePromise(name, etag, () => {
const referencedHashes = new Set();
let ownHashes = new Set();
const inContent = content.match(hashRegExp);
const inContent = hashMatch(content);
if (inContent) {
for (const hash of inContent) {
if (hashes.has(hash)) {
Expand Down Expand Up @@ -298,7 +331,7 @@ ${referencingAssets
identifier,
etag,
() => {
const newContent = asset.content.replace(hashRegExp, hash =>
const newContent = hashReplace(asset.content, hash =>
hashToNewHash.get(hash)
);
return new RawSource(newContent);
Expand All @@ -323,15 +356,12 @@ ${referencingAssets
identifier,
etag,
() => {
const newContent = asset.content.replace(
hashRegExp,
hash => {
if (asset.ownHashes.has(hash)) {
return "";
}
return hashToNewHash.get(hash);
const newContent = hashReplace(asset.content, hash => {
if (asset.ownHashes.has(hash)) {
return "";
}
);
return hashToNewHash.get(hash);
});
return new RawSource(newContent);
}
);
Expand All @@ -342,7 +372,6 @@ ${referencingAssets
for (const oldHash of hashesInOrder) {
const assets = hashToAssets.get(oldHash);
assets.sort(comparator);
const hash = createHash(this._hashFunction);
await Promise.all(
assets.map(asset =>
asset.ownHashes.has(oldHash)
Expand All @@ -363,6 +392,7 @@ ${referencingAssets
});
let newHash = hooks.updateHash.call(assetsContent, oldHash);
if (!newHash) {
const hash = createHash(this._hashFunction);
for (const content of assetsContent) {
hash.update(content);
}
Expand All @@ -374,7 +404,7 @@ ${referencingAssets
await Promise.all(
assetsWithInfo.map(async asset => {
await computeNewContent(asset);
const newName = asset.name.replace(hashRegExp, hash =>
const newName = hashReplace(asset.name, hash =>
hashToNewHash.get(hash)
);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"less": "^4.0.0",
"less-loader": "^8.0.0",
"lint-staged": "^11.0.0",
"loader-utils": "^2.0.0",
"loader-utils": "^2.0.3",
"lodash": "^4.17.19",
"lodash-es": "^4.17.15",
"memfs": "^3.2.0",
Expand Down
Loading