Skip to content

Commit

Permalink
Adding support for publicPath (#330)
Browse files Browse the repository at this point in the history
Co-authored-by: Nícholas André <[email protected]>
  • Loading branch information
Antonio-Laguna and nicholasio authored Nov 6, 2023
1 parent 9b65473 commit 0c969ef
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-impalas-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"10up-toolkit": minor
---

Add support to configure Webpack's publicPath
37 changes: 27 additions & 10 deletions packages/toolkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,37 @@ module.exports = {
Alternatively you can specify the paths in `package.json`

```json
"10up-toolkit": {
"devURL": "https://my-project.test",
"entry": {
//...
},
"filenames": {
"block": "js/blocks/[name]/editor.js",
"blockCSS": "css/blocks/[name]/editor.css",
}
}
"10up-toolkit": {
"devURL": "https://my-project.test",
"entry": {
//...
},
"filenames": {
"block": "js/blocks/[name]/editor.js",
"blockCSS": "css/blocks/[name]/editor.css",
}
}
```

Note that when overriding via the `filenames.config.js` you must export the filenames for all file types.

### <a id="customize-public-path"></a> Customizing public path

When using toolkit in a React application, you might want to customize the public path so lazy loaded components know where the assets are.

This can be tweaked in the options within `package.json`:

```json
"10up-toolkit": {
"publicPath": "/my/custom/path"
}
```

Alternatively, you can set up `process.env.ASSET_PATH` to whatever path (or CDN) you want it to be.

> **Warning**
> Please note that using `process.env.ASSET_PATH` will override the `publicPath`
### WordPress Block Asset Handling

_NOTE: Since 10up-toolkit@6 this `useBlockAssets` is on by default_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ Object {
return isBlockAsset ? filenames.block : filenames.js;
},
"path": "/dist",
"publicPath": "/",
},
"performance": Object {
"hints": "warning",
Expand Down Expand Up @@ -1610,6 +1611,7 @@ Object {
return isBlockAsset ? filenames.block : filenames.js;
},
"path": "/dist",
"publicPath": "/",
},
"performance": Object {
"hints": "warning",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ Object {
return isBlockAsset ? filenames.block : filenames.js;
},
"path": "/dist",
"publicPath": "/",
},
"performance": Object {
"hints": "warning",
Expand Down Expand Up @@ -695,6 +696,7 @@ Object {
return isBlockAsset ? filenames.block : filenames.js;
},
"path": "/dist",
"publicPath": "/",
},
"performance": Object {
"hints": "warning",
Expand Down Expand Up @@ -909,6 +911,7 @@ Object {
return isBlockAsset ? filenames.block : filenames.js;
},
"path": "/dist",
"publicPath": "/",
},
"performance": Object {
"hints": "warning",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Object {
return isBlockAsset ? filenames.block : filenames.js;
},
"path": "/dist",
"publicPath": "/",
},
"performance": Object {
"hints": "warning",
Expand Down
3 changes: 2 additions & 1 deletion packages/toolkit/config/webpack/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
module.exports = ({
isPackage,
packageConfig: { packageType, main },
projectConfig: { filenames, hot },
projectConfig: { filenames, hot, publicPath },
buildFiles,
}) => {
if (isPackage) {
Expand All @@ -26,6 +26,7 @@ module.exports = ({
clean: !hot,
path: path.resolve(process.cwd(), 'dist'),
chunkFilename: filenames.jsChunk,
publicPath,
filename: (pathData) => {
if (pathData.chunk.name === 'runtime') {
return 'fast-refresh/hmr-runtime.js';
Expand Down
5 changes: 4 additions & 1 deletion packages/toolkit/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ const getDefaultConfig = () => {
analyze,
hot,
// true by default (if TENUP_NO_EXTERNALS is not set)
// if TENUP_NO_EXTERNALS is truthy then dependecyExternals is false
// if TENUP_NO_EXTERNALS is truthy then dependencyExternals is false
wpDependencyExternals:
typeof process.env.TENUP_NO_EXTERNALS === 'undefined' ||
!process.env.TENUP_NO_EXTERNALS,
publicPath: process.env.ASSET_PATH || '/',
useBlockAssets: true,
include,
};
Expand Down Expand Up @@ -150,12 +151,14 @@ const getTenUpScriptsConfig = () => {

const configInclude = config.include ?? [];
const include = defaultConfig.include.length === 0 ? configInclude : defaultConfig.include;
const publicPath = process.env.ASSET_PATH || config.publicPath || defaultConfig.publicPath;

return {
// override default configs with user-defined config
...defaultConfig,
...config,
include,
publicPath,
// these properties must be merged
filenames: {
...defaultConfig.filenames,
Expand Down

0 comments on commit 0c969ef

Please sign in to comment.