Repository URL to install this package:
|
Version:
1.2.8 ▾
|
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const fs_1 = require("fs");
const md5_1 = __importDefault(require("md5"));
const webpack_1 = __importDefault(require("webpack"));
const di_1 = require("@skava/di");
const getConfigForEnv_1 = __importDefault(require("../../../config/oneConfig/getConfigForEnv"));
const utils_1 = require("../../utils");
const appRootDir = di_1.config.get('appRootDir');
const pkg = di_1.config.get('package');
const { DllPlugin } = webpack_1.default;
const logging = {
generating(bundleName) {
utils_1.log({
title: 'vendorDLL',
level: 'warn',
message: `Generating a new "${bundleName}" Vendor DLL for boosted development performance.
The Vendor DLL helps to speed up your development workflow by reducing Webpack build times. It does this by seperating Vendor DLLs from your primary bundles, thereby allowing Webpack to ignore them when having to rebuild your code for changes. We recommend that you add all your client bundle specific dependencies to the Vendor DLL configuration (within /config).`,
});
},
regenerating(bundleName) {
utils_1.log({
title: 'vendorDLL',
level: 'warn',
message: `New "${bundleName}" vendor dependencies detected. Regenerating the vendor dll...`,
});
},
unchanged(bundleName) {
utils_1.log({
title: 'vendorDLL',
level: 'info',
message: `No changes to existing "${bundleName}" vendor dependencies. Using the existing vendor dll.`,
});
},
complete(devDLLDependencies) {
utils_1.log({
title: 'vendorDLL',
level: 'info',
message: `Vendor DLL build complete. The following dependencies have been included:\n\t-${devDLLDependencies.join('\n\t-')}\n`,
});
},
};
function createVendorDLL(bundleName, bundleConfig) {
const dllConfig = getConfigForEnv_1.default('bundles.client.devVendorDLL');
const devDLLDependencies = dllConfig.include.sort();
const { name } = dllConfig;
const { outputPath } = bundleConfig;
const resolveDepAgainstPkg = dep => [
dep,
pkg.dependencies[dep],
pkg.devDependencies[dep],
];
// We calculate a hash of the package.json's dependencies, which we can use
// to determine if dependencies have changed since the last time we built
// the vendor dll.
const dllDepsFromPkg = devDLLDependencies.map(resolveDepAgainstPkg);
const dllDepsAsString = JSON.stringify(dllDepsFromPkg);
// We do this to include any possible version numbers we may have for
// a dependency. If these change then our hash should too, which will
// result in a new dev dll build.
const currentDependenciesHash = md5_1.default(dllDepsAsString);
const vendorDLLHashFilePath = path_1.resolve(appRootDir, outputPath, `${name}_hash`);
function webpackConfigFactory() {
const dllPath = path_1.resolve(appRootDir, outputPath, `./${name}.json`);
return {
// We only use this for development, so lets always include source maps.
devtool: 'inline-source-map',
entry: {
[name]: devDLLDependencies,
},
output: {
path: outputPath,
filename: `${name}.js`,
library: name,
},
plugins: [
new DllPlugin({
path: dllPath,
name,
}),
],
};
}
function buildVendorDLL() {
logging.complete(devDLLDependencies);
const webpackConfig = webpackConfigFactory();
const vendorDLLCompiler = webpack_1.default(webpackConfig);
return new Promise((successfullyCompiled, reject) => {
const onCompiler = (vendorCompilationError, stats = {}) => {
if (vendorCompilationError) {
reject(vendorCompilationError);
}
// Update the dependency hash
fs_1.writeFileSync(vendorDLLHashFilePath, currentDependenciesHash);
successfullyCompiled();
};
return vendorDLLCompiler.run(onCompiler);
});
}
if (!fs_1.existsSync(vendorDLLHashFilePath)) {
// builddll
logging.generating(bundleName);
return buildVendorDLL();
}
else {
// first check if the md5 hashes match
const dependenciesHash = fs_1.readFileSync(vendorDLLHashFilePath, 'utf8');
const dependenciesChanged = dependenciesHash !== currentDependenciesHash;
if (dependenciesChanged) {
logging.regenerating(bundleName);
return buildVendorDLL();
}
else {
logging.unchanged(bundleName);
return Promise.resolve();
}
}
}
exports.createVendorDLL = createVendorDLL;
//# sourceMappingURL=create.js.map