Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for declaring specific endpoints to be built #120

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,48 @@ describe('CLI Build Command', () => {
expect(process.stdout.write).toHaveBeenCalledWith(` * my-theme `);
});

it('runs specific projects and entrypoints mode when requested', () => {
mockFs({
...requiredRealDirs,
plugins: {
'my-plugin': {
'package.json': JSON.stringify({
name: 'my-plugin',
}),
src: {
entrypoints: {
'some-file.js': 'console.log("file content here");',
'frontend.js': 'console.log("file content here");',
},
},
},
},
themes: {
'my-theme': {
'package.json': JSON.stringify({
name: 'my-theme',
}),
src: {
entrypoints: {
'some-file.js': 'console.log("file content here");',
},
},
},
},
'client-mu-plugins': {},
});

runCommand('build', '--once', 'my-plugin@frontend,my-theme');

expect(mockWebpack).toHaveBeenCalled();
expect(process.stdout.write).toHaveBeenCalledWith(
`\x1b[1mCompiling \x1b[4mlist\x1b[0m\x1b[1m of projects in development mode.\x1b[0m\n`,
);
expect(process.stdout.write).toHaveBeenCalledWith('Processing the following projects:\n');
expect(process.stdout.write).toHaveBeenCalledWith(` * my-plugin `);
expect(process.stdout.write).toHaveBeenCalledWith(` * my-theme `);
});

it('runs specific projects mode if some requested projects are not found', () => {
mockFs({
...requiredRealDirs,
Expand Down
34 changes: 34 additions & 0 deletions example-site/yarn.lock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just spotted this, yarn isn't used in the project so can this be removed to avoid confusion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"js-tokens@^3.0.0 || ^4.0.0":
"integrity" "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="
"resolved" "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz"
"version" "4.0.0"

"loose-envify@^1.4.0":
"integrity" "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q=="
"resolved" "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz"
"version" "1.4.0"
dependencies:
"js-tokens" "^3.0.0 || ^4.0.0"

"object-assign@^4.1.1":
"integrity" "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg=="
"resolved" "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz"
"version" "4.1.1"

"prop-types@^15.8.1":
"integrity" "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg=="
"resolved" "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz"
"version" "15.8.1"
dependencies:
"loose-envify" "^1.4.0"
"object-assign" "^4.1.1"
"react-is" "^16.13.1"

"react-is@^16.13.1":
"integrity" "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="
"resolved" "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz"
"version" "16.13.1"
16 changes: 14 additions & 2 deletions src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const spinner = ora();

const { findAllProjectPaths, validateProject } = require('./../utils/projectpaths');
const { getPackage } = require('./../utils/get-package');
const { getFilteredEntryPoints } = require('./../utils/get-filtered-entrypoints');
const dirsExist = require('../utils/dirs-exist');
const getProjectConfig = require('../utils/get-project-config');

Expand Down Expand Up @@ -59,7 +60,7 @@ exports.handler = async ({

const mode = production ? 'production' : 'development';
// Use env variables if working on Webpack >=5.
const projectsList = projects.split(',').filter((item) => item.length > 0);
const projectsList = projects.split(',').filter((item) => item.length > 0).map((item) => item.split('@')[0]);
const hasTargetDirs = dirsExist(targetDirs);
const isAllProjects = (site || hasTargetDirs) && !projects;

Expand Down Expand Up @@ -115,7 +116,18 @@ exports.handler = async ({
spinner.start('Building webpack configs.\n');

const configMap = validProjects.map((packageObject) => {
const projectConfig = getProjectConfig(packageObject, mode);
// Empty array means all entrypoints.
let filteredEntrypoints = [];

if (projects.startsWith('@')) {
// Handle entrypoints when for standalone builds and all project builds.
filteredEntrypoints = projects.split('@')[1].split('+');
} else {
// Handle entrypoints for each specified project build.
filteredEntrypoints = getFilteredEntryPoints(projects)[packageObject.name];
}

const projectConfig = getProjectConfig(packageObject, mode, filteredEntrypoints);

return webpackConfig(projectConfig, mode);
});
Expand Down
2 changes: 1 addition & 1 deletion src/commands/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = (__PROJECT_CONFIG__, mode) => {

let webpackConfig = {
mode,
entry: entrypoints(__PROJECT_CONFIG__.paths.src),
entry: entrypoints(__PROJECT_CONFIG__.paths.src, __PROJECT_CONFIG__.filteredEntrypoints),

resolve: {
modules: [__PROJECT_CONFIG__.paths.node_modules, 'node_modules'],
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__tests__/entrypoints.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Entrypoints', () => {
},
});
const src = './';
const result = entrypoints(src);
const result = entrypoints(src, []);
const cwd = process.cwd();
expect(result).toEqual({
'empty-dir': `${cwd}/entrypoints/empty-dir`,
Expand Down
21 changes: 21 additions & 0 deletions src/utils/__tests__/get-filtered-entrypoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { getFilteredEntryPoints } = require('../get-filtered-entrypoints');

describe('Get filtered entrypoints', () => {
it('Return object with empty array if no filtered entry points', () => {
const projects = 'test-project';
const result = getFilteredEntryPoints(projects);
expect(result).toEqual({ 'test-project': [] });
});

it('Returns object with array of entrypoints', () => {
const projects = 'test-project@frontend+editor';
const result = getFilteredEntryPoints(projects);
expect(result).toEqual({ 'test-project': ['frontend', 'editor'] });
});

it('Return object with entrypoints where @ is present and empty array if non defined', () => {
const projects = 'test-project@editor,test-project-2';
const result = getFilteredEntryPoints(projects);
expect(result).toEqual({ 'test-project': ['editor'], 'test-project-2': []});
});
});
24 changes: 17 additions & 7 deletions src/utils/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const path = require('path');
* Create entry points object from src folder.
*
* @param {string} src Project src path
* @param {array} filteredEntrypoints entry point to build (ie frontend, editor, etc).
* @returns {object} webpack entrypoints
*/
module.exports = (src) => {
module.exports = (src, filteredEntrypoints) => {
const entrypoints = `${src}/entrypoints`;
const pathToEntryPoints = path.resolve(process.cwd(), entrypoints);

Expand All @@ -16,10 +17,19 @@ module.exports = (src) => {
}

return fs.readdirSync(pathToEntryPoints).reduce(
(accumulator, file) => ({
...accumulator,
[file.split('.')[0]]: path.resolve(pathToEntryPoints, file),
}),
{},
);
(accumulator, file) => {
// If types are provided, only watch/build those.
if (filteredEntrypoints.length > 0) {
const type = file.split('.')[0];
ampersarnie marked this conversation as resolved.
Show resolved Hide resolved

if (!filteredEntrypoints.includes(type)) {
return accumulator;
}
}

return {
...accumulator,
[file.split('.')[0]]: path.resolve(pathToEntryPoints, file),
};
}, {});
};
27 changes: 27 additions & 0 deletions src/utils/get-filtered-entrypoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const getFilteredEntryPoints = (projects) => {
ampersarnie marked this conversation as resolved.
Show resolved Hide resolved
const filteredProjectEntryPoints = {};
const projectNames = projects.split(',');

projectNames.forEach((project) => {
const projectParts = project.split('@');
const projectName = projectParts[0];
const entryPoints = projectParts[1] ? projectParts[1].split('+') : [];

if (!projectName) {
return filteredProjectEntryPoints;
}

if (entryPoints.length === 0) {
filteredProjectEntryPoints[projectName] = [];
return;
}

return filteredProjectEntryPoints[projectName] = entryPoints;
});

return filteredProjectEntryPoints;
};

module.exports = {
getFilteredEntryPoints,
};
4 changes: 3 additions & 1 deletion src/utils/get-project-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = (
},
path: './'
},
mode = 'development'
mode = 'development',
filteredEntrypoints = []
) => {
return {
name: packageObject?.name ?? '',
Expand All @@ -33,5 +34,6 @@ module.exports = (
clean: true,
copy: true,
mode,
filteredEntrypoints,
};
}
Loading