Skip to content

Commit

Permalink
add related info to asset info
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 2, 2020
1 parent a14f073 commit f5bd621
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
15 changes: 10 additions & 5 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const StatsFactory = require("./stats/StatsFactory");
const StatsPrinter = require("./stats/StatsPrinter");
const AsyncQueue = require("./util/AsyncQueue");
const LazySet = require("./util/LazySet");
const { cachedCleverMerge } = require("./util/cleverMerge");
const {
compareLocations,
concatComparators,
Expand Down Expand Up @@ -177,6 +178,7 @@ const { getRuntimeKey } = require("./util/runtime");
* @property {number=} size size in bytes, only set after asset has been emitted
* @property {boolean=} development true, when asset is only used for development and doesn't count towards user-facing assets
* @property {boolean=} hotModuleReplacement true, when asset ships data for updating an existing application (HMR)
* @property {Record<string, string | string[]>=} related object of pointers to other assets, keyed by type of relation (only points from parent to child)
*/

/**
Expand Down Expand Up @@ -211,6 +213,9 @@ const { getRuntimeKey } = require("./util/runtime");
* @property {string=} url
*/

/** @type {AssetInfo} */
const EMPTY_ASSET_INFO = Object.freeze({});

const esmDependencyCategory = "esm";
// TODO webpack 6: remove
const deprecatedNormalModuleLoaderHook = util.deprecate(
Expand Down Expand Up @@ -2720,13 +2725,13 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
this.assets[file] = newSourceOrFunction;
}
if (assetInfoUpdateOrFunction !== undefined) {
const oldInfo = this.assetsInfo.get(file);
const oldInfo = this.assetsInfo.get(file) || EMPTY_ASSET_INFO;
if (typeof assetInfoUpdateOrFunction === "function") {
this.assetsInfo.set(file, assetInfoUpdateOrFunction(oldInfo || {}));
this.assetsInfo.set(file, assetInfoUpdateOrFunction(oldInfo));
} else {
this.assetsInfo.set(
file,
Object.assign({}, oldInfo, assetInfoUpdateOrFunction)
cachedCleverMerge(oldInfo, assetInfoUpdateOrFunction)
);
}
}
Expand All @@ -2740,7 +2745,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
array.push({
name: assetName,
source: this.assets[assetName],
info: this.assetsInfo.get(assetName) || {}
info: this.assetsInfo.get(assetName) || EMPTY_ASSET_INFO
});
}
}
Expand All @@ -2757,7 +2762,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
return {
name,
source: this.assets[name],
info: this.assetsInfo.get(name) || {}
info: this.assetsInfo.get(name) || EMPTY_ASSET_INFO
};
}

Expand Down
71 changes: 46 additions & 25 deletions lib/SourceMapDevToolPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,27 +182,38 @@ class SourceMapDevToolPlugin {
asyncLib.each(
files,
(file, callback) => {
const asset = compilation.getAsset(file).source;
const asset = compilation.getAsset(file);
if (asset.info.related && asset.info.related.sourceMap) {
fileIndex++;
return callback();
}
const cacheItem = cache.getItemCache(
file,
cache.getLazyHashedEtag(asset)
cache.getLazyHashedEtag(asset.source)
);

cacheItem.get((err, assets) => {
cacheItem.get((err, cacheEntry) => {
if (err) {
return callback(err);
}
/**
* If presented in cache, reassigns assets. Cache assets already have source maps.
*/
if (assets) {
for (const cachedFile in assets) {
if (cacheEntry) {
const { assets, assetsInfo } = cacheEntry;
for (const cachedFile of Object.keys(assets)) {
if (cachedFile === file) {
compilation.updateAsset(cachedFile, assets[cachedFile]);
compilation.updateAsset(
cachedFile,
assets[cachedFile],
assetsInfo[cachedFile]
);
} else {
compilation.emitAsset(cachedFile, assets[cachedFile], {
development: true
});
compilation.emitAsset(
cachedFile,
assets[cachedFile],
assetsInfo[cachedFile]
);
}
/**
* Add file to chunk, if not presented there
Expand Down Expand Up @@ -232,7 +243,7 @@ class SourceMapDevToolPlugin {
/** @type {SourceMapTask | undefined} */
const task = getTaskForFile(
file,
asset,
asset.source,
options,
compilation,
cacheItem
Expand Down Expand Up @@ -340,6 +351,7 @@ class SourceMapDevToolPlugin {
tasks,
(task, callback) => {
const assets = Object.create(null);
const assetsInfo = Object.create(null);
const file = task.file;
const chunk = fileToChunk.get(file);
const sourceMap = task.sourceMap;
Expand Down Expand Up @@ -400,28 +412,36 @@ class SourceMapDevToolPlugin {
dirname(outputFs, `/${file}`),
`/${sourceMapFile}`
);
/**
* Add source map url to compilation asset, if {@link currentSourceMappingURLComment} presented
*/
/** @type {Source} */
let asset = new RawSource(source);
if (currentSourceMappingURLComment !== false) {
const asset = new ConcatSource(
new RawSource(source),
// Add source map url to compilation asset, if currentSourceMappingURLComment is set
asset = new ConcatSource(
asset,
compilation.getPath(
currentSourceMappingURLComment,
Object.assign({ url: sourceMapUrl }, pathParams)
)
);
assets[file] = asset;
compilation.updateAsset(file, asset);
}
/**
* Add source map file to compilation assets and chunk files
*/
const asset = new RawSource(sourceMapString);
assets[sourceMapFile] = asset;
compilation.emitAsset(sourceMapFile, asset, {
const assetInfo = {
related: { sourceMap: sourceMapFile }
};
assets[file] = asset;
assetsInfo[file] = assetInfo;
compilation.updateAsset(file, asset, assetInfo);
// Add source map file to compilation assets and chunk files
const sourceMapAsset = new RawSource(sourceMapString);
const sourceMapAssetInfo = {
development: true
});
};
assets[sourceMapFile] = sourceMapAsset;
assetsInfo[sourceMapFile] = sourceMapAssetInfo;
compilation.emitAsset(
sourceMapFile,
sourceMapAsset,
sourceMapAssetInfo
);
if (chunk !== undefined)
chunk.auxiliaryFiles.add(sourceMapFile);
} else {
Expand All @@ -447,10 +467,11 @@ class SourceMapDevToolPlugin {
)
);
assets[file] = asset;
assetsInfo[file] = undefined;
compilation.updateAsset(file, asset);
}

task.cacheItem.store(assets, err => {
task.cacheItem.store({ assets, assetsInfo }, err => {
reportProgress(
0.5 + (0.5 * ++taskIndex) / tasks.length,
task.file,
Expand Down
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ declare interface AssetInfo {
* true, when asset ships data for updating an existing application (HMR)
*/
hotModuleReplacement?: boolean;

/**
* object of pointers to other assets, keyed by type of relation (only points from parent to child)
*/
related?: Record<string, LibraryExport>;
}
type AssetModuleFilename =
| string
Expand Down

0 comments on commit f5bd621

Please sign in to comment.