Skip to content

Commit

Permalink
special/webpack: Use tryRequire to load configuration file (#451)
Browse files Browse the repository at this point in the history
This will prevent the process to fail if loading a TypeScript configuration file when depcheck is not executed with ts-node.
  • Loading branch information
sveyret authored and rumpl committed Nov 1, 2019
1 parent 855ced0 commit 9ede22f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/special/webpack.js
@@ -1,5 +1,6 @@
import path from 'path';
import lodash from 'lodash';
import { tryRequire } from '../utils';

const webpackConfigRegex = /webpack(\..+)?\.conf(?:ig|)\.(babel\.)?[jt]s/;
const loaderTemplates = ['*-webpack-loader', '*-web-loader', '*-loader', '*'];
Expand Down Expand Up @@ -115,14 +116,16 @@ function parseEntries(entries, deps) {
export default function parseWebpack(content, filepath, deps) {
const filename = path.basename(filepath);
if (webpackConfigRegex.test(filename)) {
const wpConfig = require(filepath); // eslint-disable-line global-require
const module = wpConfig.module || {};
const entry = wpConfig.entry || [];

const webpack1Loaders = parseWebpack1(module, deps);
const webpack2Loaders = parseWebpack2(module, deps);
const webpackEntries = parseEntries(entry, deps);
return [...webpack1Loaders, ...webpack2Loaders, ...webpackEntries];
const wpConfig = tryRequire(filepath);
if (wpConfig) {
const module = wpConfig.module || {};
const entry = wpConfig.entry || [];

const webpack1Loaders = parseWebpack1(module, deps);
const webpack2Loaders = parseWebpack2(module, deps);
const webpackEntries = parseEntries(entry, deps);
return [...webpack1Loaders, ...webpack2Loaders, ...webpackEntries];
}
}

return [];
Expand Down

0 comments on commit 9ede22f

Please sign in to comment.