mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
Profiling shows that using cache-loader saves ~6-7 seconds of time take by webpack-dev-server on subsequent runs. The overhaul this adds when nothing is cached (when running first time) is around 1-2 seconds. We don't use cache loader for ts-loader since webpack docs says it will slow it down and file-loader since it just copies files over and caching it would just was disk space. This is the second merge of this commit. It fixes the issue with the previous one by placingn cache-loader after mini-css-loader because it just extracts css and caching that will make file-loader not run which in turn makes developement enviorment break.
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import { basename, resolve } from 'path';
|
|
import { RuleSetRule } from 'webpack';
|
|
|
|
export const cacheLoader: RuleSetRule = {
|
|
loader: 'cache-loader',
|
|
options: {
|
|
cacheDirectory: resolve(__dirname, '../var/webpack-cache'),
|
|
},
|
|
};
|
|
|
|
/* Return imports-loader format to the config
|
|
For example:
|
|
[
|
|
// Adds 'imports-loader?this=>widndow'
|
|
{path: './foler/my_module.js', args: '?this=>window'},
|
|
]
|
|
*/
|
|
interface ImportLoaderOptions {
|
|
path: string;
|
|
args: string;
|
|
}
|
|
function getImportLoaders(optionsArr: ImportLoaderOptions[]): RuleSetRule[] {
|
|
const importsLoaders = [];
|
|
for (var loaderEntry of optionsArr) {
|
|
importsLoaders.push({
|
|
test: require.resolve(loaderEntry.path),
|
|
use: [cacheLoader, "imports-loader?" + loaderEntry.args],
|
|
});
|
|
}
|
|
return importsLoaders;
|
|
}
|
|
/* Return expose-loader format to the config
|
|
For example
|
|
[
|
|
// Exposes 'my_module' as the name
|
|
{path: './folder/my_module.js'},
|
|
|
|
// Exposes 'my_custom_name'
|
|
{path: './folder/my_module.js', name: 'my_custom_name'},
|
|
|
|
// Exposes 'name1' and 'name2'
|
|
{path: './folder/my_module.js', name: ['name1', 'name2']}
|
|
]
|
|
*/
|
|
interface ExportLoaderOptions {
|
|
path: string;
|
|
name?: string | string[];
|
|
}
|
|
function getExposeLoaders(optionsArr: ExportLoaderOptions[]): RuleSetRule[] {
|
|
const exposeLoaders = [];
|
|
for (var loaderEntry of optionsArr) {
|
|
const path = loaderEntry.path;
|
|
let name = "";
|
|
const useArr = [cacheLoader];
|
|
// If no name is provided, infer it
|
|
if (!loaderEntry.name) {
|
|
name = basename(path, '.js');
|
|
useArr.push({loader: 'expose-loader', options: name});
|
|
} else {
|
|
// If name is an array
|
|
if (Array.isArray(loaderEntry.name)) {
|
|
for (var exposeName of loaderEntry.name) {
|
|
useArr.push({loader: 'expose-loader', options: exposeName});
|
|
}
|
|
// If name is a string
|
|
} else {
|
|
useArr.push({loader: 'expose-loader', options: loaderEntry.name});
|
|
}
|
|
}
|
|
exposeLoaders.push({
|
|
test: require.resolve(path),
|
|
use: useArr,
|
|
});
|
|
}
|
|
return exposeLoaders;
|
|
}
|
|
export {
|
|
getExposeLoaders,
|
|
getImportLoaders,
|
|
};
|