@datadog/build-plugin
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -22,2 +22,10 @@ "use strict"; | ||
}); | ||
test('It should return the size of a module', () => { | ||
const { getModuleSize } = require('../helpers'); | ||
const module1 = { size: 1 }; | ||
const module2 = { size: () => 2 }; | ||
expect(getModuleSize(module1)).toBe(1); | ||
expect(getModuleSize(module2)).toBe(2); | ||
expect(getModuleSize()).toBe(0); | ||
}); | ||
}); |
@@ -5,41 +5,44 @@ "use strict"; | ||
// Copyright 2019-Present Datadog, Inc. | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
describe('Modules', () => { | ||
test('It should filter modules the same with Webpack 5 and 4', () => { | ||
const { Modules } = require('../modules'); | ||
// Webpack5 is actually throwing an error when using this property. | ||
const getThrowingDependency = (dep) => { | ||
Object.defineProperty(dep, 'module', { | ||
get: () => { | ||
throw new Error(); | ||
}, | ||
}); | ||
return dep; | ||
}; | ||
const mockedModules = [ | ||
{ | ||
name: 'moduleWebpack4', | ||
dependencies: [ | ||
{ name: 'dep1', module: { name: 'dep1' } }, | ||
{ name: 'dep2' }, | ||
{ name: 'dep3', module: { name: 'dep3' } }, | ||
], | ||
const { Modules } = require('../modules'); | ||
// Webpack5 is actually throwing an error when using this property. | ||
const getThrowingDependency = (dep) => { | ||
Object.defineProperty(dep, 'module', { | ||
get: () => { | ||
throw new Error(); | ||
}, | ||
{ | ||
name: 'moduleWebpack5', | ||
dependencies: [ | ||
getThrowingDependency({ name: 'dep1' }), | ||
getThrowingDependency({ name: 'dep2' }), | ||
getThrowingDependency({ name: 'dep3' }), | ||
], | ||
}); | ||
return dep; | ||
}; | ||
const mockedModules = [ | ||
{ | ||
name: 'moduleWebpack4', | ||
size: 50, | ||
dependencies: [ | ||
{ name: 'dep1', module: { name: 'dep1' }, size: 1 }, | ||
{ name: 'dep2', size: 2 }, | ||
{ name: 'dep3', module: { name: 'dep3' }, size: 3 }, | ||
], | ||
}, | ||
{ | ||
name: 'moduleWebpack5', | ||
size: () => 50, | ||
dependencies: [ | ||
getThrowingDependency({ name: 'dep1', size: () => 1 }), | ||
getThrowingDependency({ name: 'dep2', size: () => 2 }), | ||
getThrowingDependency({ name: 'dep3', size: () => 3 }), | ||
], | ||
}, | ||
]; | ||
const mockCompilation = { | ||
moduleGraph: { | ||
getModule(dep) { | ||
return mockedModules[0].dependencies.find((d) => d.name === dep.name && d.module); | ||
}, | ||
]; | ||
const mockCompilation = { | ||
moduleGraph: { | ||
getModule(dep) { | ||
return mockedModules[0].dependencies.find((d) => d.name === dep.name && d.module); | ||
}, | ||
}, | ||
}; | ||
const modules = new Modules(); | ||
modules.afterOptimizeTree({}, mockedModules, '/', mockCompilation); | ||
}, | ||
}; | ||
const modules = new Modules(); | ||
modules.afterOptimizeTree({}, mockedModules, '/', mockCompilation); | ||
test('It should filter modules the same with Webpack 5 and 4', () => { | ||
const modulesWebpack4 = modules.storedModules['moduleWebpack4'].dependencies; | ||
@@ -49,2 +52,8 @@ const modulesWebpack5 = modules.storedModules['moduleWebpack5'].dependencies; | ||
}); | ||
test('It should add module size to the results', () => { | ||
const results = modules.getResults(); | ||
for (const module of Object.values(results.modules)) { | ||
expect(module.size).toBeDefined(); | ||
} | ||
}); | ||
}); |
@@ -8,3 +8,4 @@ import { Module } from './types'; | ||
export declare const getModuleName: (module: Module, context: string) => string; | ||
export declare const getModuleSize: (module: Module) => number; | ||
export declare const formatLoaderName: (loader: string) => string; | ||
export declare const getLoaderNames: (module: Module) => string[]; |
@@ -44,2 +44,11 @@ "use strict"; | ||
}; | ||
exports.getModuleSize = (module) => { | ||
if (!module) { | ||
return 0; | ||
} | ||
if (typeof module.size === 'function') { | ||
return module.size(); | ||
} | ||
return module.size; | ||
}; | ||
// Format the loader's name by extracting it from the query. | ||
@@ -46,0 +55,0 @@ // "[...]/node_modules/babel-loader/lib/index.js" => babel-loader |
@@ -211,3 +211,3 @@ "use strict"; | ||
const treeSize = tree.reduce((previous, current) => { | ||
return previous + (current ? current.size : 0); | ||
return previous + helpers_1.getModuleSize(current); | ||
}, 0); | ||
@@ -214,0 +214,0 @@ return [ |
@@ -13,15 +13,17 @@ "use strict"; | ||
afterOptimizeTree(chunks, modules, context, compilation) { | ||
const moduleMap = {}; | ||
// In Webpack 5, using dep.module throws an error. | ||
// It's advised to use ModuleGraph API instead (not available in previous versions). | ||
const getModule = (dep) => { | ||
var _a; | ||
try { | ||
return dep.module; | ||
} | ||
catch (e) { | ||
return (_a = compilation.moduleGraph) === null || _a === void 0 ? void 0 : _a.getModule(dep); | ||
} | ||
}; | ||
for (const module of modules) { | ||
const moduleName = helpers_1.getModuleName(module, context); | ||
// In Webpack 5, using dep.module throws an error. | ||
// It's advised to use ModuleGraph API instead (not available in previous versions). | ||
const getModule = (dep) => { | ||
var _a; | ||
try { | ||
return dep.module; | ||
} | ||
catch (e) { | ||
return (_a = compilation.moduleGraph) === null || _a === void 0 ? void 0 : _a.getModule(dep); | ||
} | ||
}; | ||
moduleMap[moduleName] = module; | ||
let dependencies = module.dependencies | ||
@@ -41,2 +43,3 @@ // Ensure it's a module because webpack register as dependency | ||
name: helpers_1.getDisplayName(moduleName), | ||
size: helpers_1.getModuleSize(module), | ||
dependencies, | ||
@@ -59,2 +62,3 @@ dependents: [], | ||
name: storedDepName, | ||
size: helpers_1.getModuleSize(moduleMap[storedDepName]), | ||
dependencies: [], | ||
@@ -61,0 +65,0 @@ dependents: [], |
@@ -197,3 +197,3 @@ export declare type HOOKS = 'output'; | ||
moduleGraph?: ModuleGraph; | ||
size: number; | ||
size: () => number | number; | ||
loaders: { | ||
@@ -241,2 +241,3 @@ loader: string; | ||
name: string; | ||
size: number; | ||
dependencies: string[]; | ||
@@ -243,0 +244,0 @@ dependents: string[]; |
{ | ||
"name": "@datadog/build-plugin", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Datadog", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94031
2232