Skip to content

Commit

Permalink
feat: load plugins from project dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann authored and ewanharris committed Apr 29, 2020
1 parent dab4ffc commit d0ed15e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/plugin-api/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import readPkg from 'read-pkg';

import { loadModule } from './loader';
import PluginApi from './plugin-api';
import { isPlugin } from './utils';

/**
* The plugin context.
Expand Down Expand Up @@ -128,8 +129,21 @@ export default class PluginContext extends EventEmitter {
this.watchers.set(pkgPath, pkgWatcher);
}
const pkg = readPkg.sync({ cwd });
// TODO: Plugins from pkg.devDependencies and pkg.dependencies

// project plugins installed as dependencies
const projectPlugins = Object.keys(pkg.devDependencies || {})
.concat(Object.keys(pkg.dependencies || {}))
.filter(isPlugin)
.map(id => {
return {
id,
// eslint-disable-next-line security/detect-non-literal-require
apply: loadModule(id, cwd, true)
};
});
plugins = plugins.concat(projectPlugins);

// project local plugins configured in package.json
if (pkg.appcdWebpackPlugins) {
const files = pkg.appcdWebpackPlugins;
if (!Array.isArray(files)) {
Expand Down
4 changes: 3 additions & 1 deletion src/plugin-api/plugin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import hash from 'hash-sum';
import path from 'path';
import Config from 'webpack-chain';
import { matchesPluginId } from './utils';

/** @typedef {import("./context").default} PluginContext */

Expand Down Expand Up @@ -91,7 +92,8 @@ export default class PluginApi {
* @return {boolean}
*/
hasPlugin(id) {
return this.context.plugins.has(id);
return Array.from(this.context.plugins.keys())
.some(pid => matchesPluginId(pid, id));
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/plugin-api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ export function createPluginOptions(baseOptions) {

return options;
}

const pluginPattern = /^(@appcd\/|@titanium-sdk\/|@[\w]+\/appcd-|appcd-)webpack-plugin-/;

export function isPlugin(id) {
return pluginPattern.test(id);
}

export function matchesPluginId(fullPluginId, id) {
const shortPluginId = fullPluginId.replace(pluginPattern, '');
return fullPluginId === id || shortPluginId === id;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import generateOptions from './fixtures/options';
import config from '../config/config';
import generateOptions from '../../fixtures/options';
import config from '../../../config/config';

import BuildJob from '../dist/job/build-job';
import BuildJob from '../../../dist/job/build-job';

describe('BuildJob', () => {
it('should have initial stopped status', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import generateOptions from './fixtures/options';
import config from '../config/config';
import BuildJob from '../dist/job/build-job';
import JobManager from '../dist/job/manager';
import generateOptions from '../../fixtures/options';
import config from '../../../config/config';
import BuildJob from '../../../dist/job/build-job';
import JobManager from '../../../dist/job/manager';

let options = generateOptions('classic');
let identifier = options.identifier;
Expand Down
57 changes: 57 additions & 0 deletions test/specs/plugin-api/test-utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { matchesPluginId } from '../../../dist/plugin-api/utils';

describe('Plugin API - utils', () => {
describe('matchesPluginId', () => {
it('should match full plugin id', () => {
const match = matchesPluginId(
'appcd-webpack-plugin-babel',
'appcd-webpack-plugin-babel'
);
expect(match).to.be.true;
});

it('should match short plugin id', () => {
const match = matchesPluginId(
'appcd-webpack-plugin-babel',
'babel'
);
expect(match).to.be.true;
});

it('should match full plugin id with scope', () => {
const match = matchesPluginId(
'@someorg/appcd-webpack-plugin-typescript',
'@someorg/appcd-webpack-plugin-typescript'
);
expect(match).to.be.true;
});

it('should match short plugin id with scope', () => {
const match = matchesPluginId(
'@someorg/appcd-webpack-plugin-typescript',
'typescript'
);
expect(match).to.be.true;
});

it('should match full plugin id with official scopes', () => {
[ '@appcd', '@titanium-sdk' ].forEach(scope => {
const match = matchesPluginId(
`${scope}/webpack-plugin-babel`,
`${scope}/webpack-plugin-babel`
);
expect(match).to.be.true;
});
});

it('should match short plugin id with official scopes', () => {
[ '@appcd', '@titanium-sdk' ].forEach(scope => {
const match = matchesPluginId(
`${scope}/webpack-plugin-babel`,
'babel'
);
expect(match).to.be.true;
});
});
});
});

0 comments on commit d0ed15e

Please sign in to comment.