Skip to content

Commit

Permalink
Defer resolve entry points in webpack plugin (resolves #949)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Feb 23, 2025
1 parent 1505557 commit e42b09d
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/knip/fixtures/plugins/webpack/webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = env =>
merge(common(), {
mode: env.production ? 'production' : 'development',
entry: {
main: './src/app.ts',
vendor: './src/vendor.ts',
main: './src/app',
vendor: './src/vendor',
},
module: {
rules: [
Expand Down
Empty file.
Empty file.
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions packages/knip/fixtures/plugins/webpack2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@fixtures/webpack2",
"scripts": {
"dev": "webpack --config webpack.dev.js",
"prod": "webpack --config webpack.prod.js"
},
"dependencies": {},
"devDependencies": {
"webpack": "*",
"webpack-cli": "*"
}
}
8 changes: 8 additions & 0 deletions packages/knip/fixtures/plugins/webpack2/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'webpack';

export default () => {
return {
mode: 'development',
entry: './entry.dev.js',
};
};
8 changes: 8 additions & 0 deletions packages/knip/fixtures/plugins/webpack2/webpack.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'webpack';

export default () => {
return {
mode: 'production',
entry: './entry.prod.js',
};
};
9 changes: 5 additions & 4 deletions packages/knip/src/plugins/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { compact } from '../../util/array.js';
import {
type Input,
toDeferResolve,
toDeferResolveEntry,
toDeferResolveProductionEntry,
toDependency,
toDevDependency,
toEntry,
toProductionEntry,
} from '../../util/input.js';
import { isAbsolute, isInternal, join, relative } from '../../util/path.js';
import { hasDependency } from '../../util/plugin.js';
Expand Down Expand Up @@ -99,8 +99,9 @@ export const findWebpackDependenciesFromConfig = async ({ config, cwd }: { confi
} else {
const absoluteEntry = isAbsolute(entry) ? entry : join(options.context ? options.context : cwd, entry);
const item = relative(cwd, absoluteEntry);
const value = options.mode === 'development' ? toEntry(item) : toProductionEntry(item);
inputs.add(value);
const input =
options.mode === 'development' ? toDeferResolveEntry(item) : toDeferResolveProductionEntry(item);
inputs.add(input);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions packages/knip/test/plugins/webpack2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../../src/index.js';
import { resolve } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.js';

const cwd = resolve('fixtures/plugins/webpack2');

test('Find dependencies with Webpack plugin (2)', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 4,
total: 4,
});
});

test('Find dependencies with Webpack plugin (2) (production)', async () => {
const { counters } = await main({
...baseArguments,
cwd,
isProduction: true,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 1,
total: 1,
});
});

0 comments on commit e42b09d

Please sign in to comment.