webpack#ProvidePlugin TypeScript Examples
The following examples show how to use
webpack#ProvidePlugin.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: config.ts From backstage with Apache License 2.0 | 4 votes |
export async function createConfig(
paths: BundlingPaths,
options: BundlingOptions,
): Promise<webpack.Configuration> {
const { checksEnabled, isDev, frontendConfig } = options;
const { plugins, loaders } = transforms(options);
// Any package that is part of the monorepo but outside the monorepo root dir need
// separate resolution logic.
const { packages } = await getPackages(cliPaths.targetDir);
const externalPkgs = packages.filter(p => !isChildPath(paths.root, p.dir));
const baseUrl = frontendConfig.getString('app.baseUrl');
const validBaseUrl = new URL(baseUrl);
const publicPath = validBaseUrl.pathname.replace(/\/$/, '');
if (checksEnabled) {
plugins.push(
new ForkTsCheckerWebpackPlugin({
typescript: { configFile: paths.targetTsConfig, memoryLimit: 4096 },
}),
new ESLintPlugin({
context: paths.targetPath,
files: ['**', '!**/__tests__/**', '!**/?(*.)(spec|test).*'],
}),
);
}
// TODO(blam): process is no longer auto polyfilled by webpack in v5.
// we use the provide plugin to provide this polyfill, but lets look
// to remove this eventually!
plugins.push(
new ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
);
plugins.push(
new webpack.EnvironmentPlugin({
APP_CONFIG: options.frontendAppConfigs,
}),
);
plugins.push(
new HtmlWebpackPlugin({
template: paths.targetHtml,
templateParameters: {
publicPath,
config: frontendConfig,
},
}),
);
const buildInfo = await readBuildInfo();
plugins.push(
new webpack.DefinePlugin({
'process.env.BUILD_INFO': JSON.stringify(buildInfo),
}),
);
// Detect and use the appropriate react-dom hot-loader patch based on what
// version of React is used within the target repo.
const resolveAliases: Record<string, string> = {};
try {
// eslint-disable-next-line import/no-extraneous-dependencies
const { version: reactDomVersion } = require('react-dom/package.json');
if (reactDomVersion.startsWith('16.')) {
resolveAliases['react-dom'] = '@hot-loader/react-dom-v16';
} else {
resolveAliases['react-dom'] = '@hot-loader/react-dom-v17';
}
} catch (error) {
console.warn(`WARNING: Failed to read react-dom version, ${error}`);
}
return {
mode: isDev ? 'development' : 'production',
profile: false,
optimization: optimization(options),
bail: false,
performance: {
hints: false, // we check the gzip size instead
},
devtool: isDev ? 'eval-cheap-module-source-map' : 'source-map',
context: paths.targetPath,
entry: [require.resolve('react-hot-loader/patch'), paths.targetEntry],
resolve: {
extensions: ['.ts', '.tsx', '.mjs', '.js', '.jsx'],
mainFields: ['browser', 'module', 'main'],
fallback: {
...pickBy(require('node-libs-browser')),
module: false,
dgram: false,
dns: false,
fs: false,
http2: false,
net: false,
tls: false,
child_process: false,
/* new ignores */
path: false,
https: false,
http: false,
util: require.resolve('util/'),
},
plugins: [
new LinkedPackageResolvePlugin(paths.rootNodeModules, externalPkgs),
new ModuleScopePlugin(
[paths.targetSrc, paths.targetDev],
[paths.targetPackageJson],
),
],
alias: resolveAliases,
},
module: {
rules: loaders,
},
output: {
path: paths.targetDist,
publicPath: `${publicPath}/`,
filename: isDev ? '[name].js' : 'static/[name].[fullhash:8].js',
chunkFilename: isDev
? '[name].chunk.js'
: 'static/[name].[chunkhash:8].chunk.js',
...(isDev
? {
devtoolModuleFilenameTemplate: (info: any) =>
`file:///${resolvePath(info.absoluteResourcePath).replace(
/\\/g,
'/',
)}`,
}
: {}),
},
plugins,
};
}