Skip to content

Commit

Permalink
bug #1247 bug: accept use with addPlugin only (gimler)
Browse files Browse the repository at this point in the history
This PR was merged into the main branch.

Discussion
----------

bug: accept use with addPlugin only

i have a multi config setup. In one of my configs i only call `addPlugin()`.

```js
const path = require('node:path');
const fileName = process.env.COPY_FILE_NAME || false;
let copyConfig = {};
if (fileName) {
  copyConfig = {
    from: `${fileName}`,
    to: `${path.dirname(path.relative(path.resolve(__dirname, 'picture'), fileName))}/[name].[contenthash:8][ext]`,
  }
} else {
  copyConfig = {
    from: './picture',
    to: '[path][name].[contenthash:8][ext]'
  }

  Encore.cleanupOutputBeforeBuild()
};

const CopyPlugin = require('copy-webpack-plugin');
Encore
...
  .addPlugin(
      new CopyPlugin({
        patterns: [copyConfig],
      })
    )
...
```

this config only copy files optional only  a specific ;)

Actually only use `addPlugin()` is not supported. I change the code and add test for the scenario.

releated #1140

Commits
-------

1bfd797 bug: accept use with addPlugin only
  • Loading branch information
weaverryan committed Jan 22, 2024
2 parents a3f6855 + 1bfd797 commit cc30762
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/config/validator.js
Expand Up @@ -43,8 +43,9 @@ class Validator {
if (this.webpackConfig.entries.size === 0
&& this.webpackConfig.styleEntries.size === 0
&& this.webpackConfig.copyFilesConfigs.length === 0
&& this.webpackConfig.plugins.length === 0
) {
throw new Error('No entries found! You must call addEntry() or addEntries() or addStyleEntry() or copyFiles() at least once - otherwise... there is nothing to webpack!');
throw new Error('No entries found! You must call addEntry() or addEntries() or addStyleEntry() or copyFiles() or addPlugin() at least once - otherwise... there is nothing to webpack!');
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/config/validator.js
Expand Up @@ -24,6 +24,8 @@ function createConfig() {
}

describe('The validator function', () => {
function CustomPlugin1() {}

it('throws an error if there are no entries', () => {
const config = createConfig();
config.publicPath = '/';
Expand All @@ -47,6 +49,17 @@ describe('The validator function', () => {
expect(Object.keys(config.copyFilesConfigs).length).to.equal(1);
});

it('should accept use with addPlugin() only', () => {
const config = createConfig();
config.setOutputPath('/tmp');
config.setPublicPath('/tmp');
config.addPlugin(new CustomPlugin1());

expect(() => {
validator(config);
}).not.throw();
});

it('throws an error if there is no output path', () => {
const config = createConfig();
config.publicPath = '/';
Expand Down

0 comments on commit cc30762

Please sign in to comment.