Skip to content

Commit

Permalink
feature allow defining module scripts through project config decouple…
Browse files Browse the repository at this point in the history
…d from blocks
  • Loading branch information
fabiankaegy committed Apr 11, 2024
1 parent 73446c7 commit a3e7575
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/toolkit/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
getTenUpScriptsConfig,
getTenUpScriptsPackageBuildConfig,
} = require('../utils');
const { getModuleBuildFiles } = require('../utils/config');

const {
getEntryPoints,
Expand All @@ -25,6 +26,7 @@ const projectConfig = getTenUpScriptsConfig();
const packageConfig = getTenUpScriptsPackageBuildConfig();
const { source, main } = packageConfig;
const buildFiles = getBuildFiles();
const moduleBuildFiles = getModuleBuildFiles();

// assume it's a package if there's source and main
const isPackage = typeof source !== 'undefined' && typeof main !== 'undefined';
Expand All @@ -44,6 +46,7 @@ const config = {
projectConfig,
packageConfig,
buildFiles,
moduleBuildFiles,
isPackage,
mode,
isProduction,
Expand Down Expand Up @@ -95,7 +98,12 @@ const moduleConfig = {
...baseConfig.output.library,
type: 'module',
},
filename: 'blocks/[name].js',
filename: (pathData) => {
const isBlockAsset =
moduleBuildFiles[pathData.chunk.name].match(/\/blocks?\//) ||
moduleBuildFiles[pathData.chunk.name].match(/\\blocks?\\/);
return isBlockAsset ? projectConfig.filenames.block : projectConfig.filenames.js;
},
},
};

Expand Down
4 changes: 3 additions & 1 deletion packages/toolkit/config/webpack/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = ({
projectConfig: { devServer, paths, useBlockAssets, filenames },
packageConfig: { packageType, source, main, umd, libraryName },
buildFiles,
moduleBuildFiles,
}) => {
let additionalEntrypoints = {};
if (useBlockAssets) {
Expand Down Expand Up @@ -105,7 +106,8 @@ module.exports = ({
}

if (buildType === 'module') {
return additionalEntrypoints;
Object.assign(moduleBuildFiles, additionalEntrypoints);
return moduleBuildFiles;
}

// merge the new entrypoints with the existing ones
Expand Down
18 changes: 18 additions & 0 deletions packages/toolkit/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const getDefaultConfig = () => {

return {
entry: require(buildFilesPath),
moduleEntry: {},
filenames: require(filenamesPath),
paths: require(pathsPath),
wordpress: wpMode !== 'false',
Expand Down Expand Up @@ -295,6 +296,22 @@ const getBuildFiles = () => {
return entries;
};

const getModuleBuildFiles = () => {
const { moduleEntry } = getTenUpScriptsConfig();

const entries = {};

Object.keys(moduleEntry).forEach((key) => {
const filePath = path.resolve(process.cwd(), moduleEntry[key]);

if (fileExists(filePath)) {
entries[key] = filePath;
}
});

return entries;
};

module.exports = {
hasBabelConfig,
getJestOverrideConfigFile,
Expand All @@ -303,6 +320,7 @@ module.exports = {
hasPostCSSConfig,
hasStylelintConfig,
getBuildFiles,
getModuleBuildFiles,
hasEslintignoreConfig,
hasEslintConfig,
getTenUpScriptsConfig,
Expand Down
12 changes: 12 additions & 0 deletions projects/10up-theme/assets/js/frontend/frontend-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { store } from '@wordpress/interactivity';

const { state } = store('frontend', {
state: {
isExpanded: false,
},
actions: {
toggleExpanded() {
state.isExpanded = !state.isExpanded;
},
},
});
5 changes: 4 additions & 1 deletion projects/10up-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"styleguide-style": "./assets/css/styleguide/styleguide.css",
"core-block-overrides": "./includes/core-block-overrides.js",
"test-style": "./assets/css/frontend/testing.scss"
}
},
"moduleEntry": {
"frontend-module": "./assets/js/frontend/frontend-module.js"
}
}
}

0 comments on commit a3e7575

Please sign in to comment.