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

auto generate entrypoint for block.json files #132

Closed
wants to merge 11 commits into from
6,327 changes: 3,543 additions & 2,784 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/toolkit/config/filenames.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
js: 'js/[name].js',
jsChunk: 'js/[name].[contenthash].chunk.js',
css: 'css/[name].css',
block: 'blocks/[name]/editor.js',
block: '[name].js',
blockCSS: 'blocks/[name]/editor.css',
};
7 changes: 6 additions & 1 deletion packages/toolkit/config/webpack/entry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { getWebpackEntryPoints } = require('../../utils');

const removeDistFolder = (file) => {
return file.replace(/(^\.\/dist\/)|^dist\//, '');
};
Expand Down Expand Up @@ -31,5 +33,8 @@ module.exports = ({
return config;
}

return buildFiles;
return {
...buildFiles,
...getWebpackEntryPoints(),
};
};
1 change: 1 addition & 0 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"eslint-plugin-react": "^7.27.0",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-webpack-plugin": "^3.1.1",
"fast-glob": "^3.2.11",
"html-webpack-plugin": "^5.5.0",
"ignore-emit-webpack-plugin": "2.0.6",
"image-minimizer-webpack-plugin": "^3.2.3",
Expand Down
46 changes: 46 additions & 0 deletions packages/toolkit/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { existsSync: fileExists } = require('fs');
const path = require('path');
const camelcase = require('camelcase');
const { sync: glob } = require('fast-glob');
const { hasArgInCLI, getArgFromCLI } = require('./cli');
const { fromConfigRoot, fromProjectRoot, hasProjectFile } = require('./file');
const { hasPackageProp, getPackage } = require('./package');
Expand Down Expand Up @@ -253,6 +254,50 @@ const getBuildFiles = () => {
return entries;
};

/**
* Detects the list of entry points to use with webpack. There are three ways to do this:
* Scan `block.json` files for scripts.
*
* @see https://webpack.js.org/concepts/entry-points/
*
* @returns {object<string, string>} The list of entry points.
*/
function getWebpackEntryPoints() {
const blockMetadataFiles = glob('includes/blocks/**/block.json', {
absolute: true,
});
if (blockMetadataFiles.length > 0) {
return blockMetadataFiles.reduce((accumulator, blockMetadataFile) => {
const { editorScript, script, viewScript } = require(blockMetadataFile);
[editorScript, script, viewScript]
.flat()
.filter((value) => value && value.startsWith('file:'))
.forEach((value) => {
// Removes the `file:` prefix.
const filepath = path.join(
path.dirname(blockMetadataFile),
value.replace('file:', ''),
);

// Takes the path without the file extension, and relative to the `src` directory.
const [, entryName] = filepath.split('.')[0].split('dist/');
if (!entryName) {
return;
}

// Detects the proper file extension used in the `src` directory.
const [entryFilepath] = glob(`includes/${entryName}.[jt]s?(x)`, {
absolute: true,
});

accumulator[entryName.replace(/\//g, '--')] = entryFilepath;
});
return accumulator;
}, {});
}
return {};
}

module.exports = {
hasBabelConfig,
getJestOverrideConfigFile,
Expand All @@ -268,4 +313,5 @@ module.exports = {
getTenUpScriptsPackageBuildConfig,
hasWebpackConfig,
hasTsConfig,
getWebpackEntryPoints,
};
2 changes: 2 additions & 0 deletions packages/toolkit/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
getTenUpScriptsPackageBuildConfig,
hasWebpackConfig,
hasTsConfig,
getWebpackEntryPoints,
} = require('./config');
const { fromProjectRoot, fromConfigRoot, hasProjectFile } = require('./file');

Expand Down Expand Up @@ -61,4 +62,5 @@ module.exports = {
getTenUpScriptsPackageBuildConfig,
hasWebpackConfig,
displayWebpackStats,
getWebpackEntryPoints,
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"padding": false
}
},
"editorScript": "file:../../../dist/blocks/example-block/editor.js",
"editorScript": "file:../../../dist/blocks/example-block/edit.js",
"editorStyle": "file:../../../dist/blocks/example-block/editor.css"
}
2 changes: 0 additions & 2 deletions projects/10up-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"devURL": "https://my-project.test",
"entry": {
"admin": "./assets/js/admin/admin.js",
"blocks": "./includes/blocks/blocks.js",
"frontend": "./assets/js/frontend/frontend.ts",
"shared": "./assets/js/shared/shared.js",
"styleguide": "./assets/js/styleguide/styleguide.js",
Expand All @@ -35,7 +34,6 @@
"shared-style": "./assets/css/shared/shared-style.css",
"styleguide-style": "./assets/css/styleguide/styleguide.css",
"core-block-overrides": "./includes/core-block-overrides.js",
"example-block": "./includes/blocks/example-block/index.js",
"test-style": "./assets/css/frontend/testing.scss"
}
}
Expand Down