Skip to content

Commit fdb6b66

Browse files
authored
Merge pull request #408 from smalruby/fix/github-pages-public-path
fix: resolve GitHub Pages subdirectory deployment publicPath issues
2 parents b3e4ac6 + 2b714ad commit fdb6b66

File tree

7 files changed

+136
-14
lines changed

7 files changed

+136
-14
lines changed

.github/workflows/ci-cd.yml

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,11 @@ jobs:
106106
if [[ ${{ contains(github.ref, 'hotfix') }} == 'true' ]]; then
107107
sed -e "s|hotfix/REPLACE|${{ github.ref_name }}|" --in-place release.config.js
108108
fi
109-
- name: Deploy playground to GitHub Pages
110-
uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 # v3
109+
- name: Prepare deployment files
111110
if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
112-
with:
113-
github_token: ${{ secrets.GITHUB_TOKEN }}
114-
publish_dir: ./build
115-
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
111+
run: |
112+
touch build/.nojekyll
113+
echo "smalruby.app" > build/CNAME
116114
- name: Deploy playground to Smalruby.app GitHub Pages
117115
uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 # v3
118116
if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
@@ -122,9 +120,52 @@ jobs:
122120
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
123121
cname: smalruby.app
124122
external_repository: smalruby/smalruby.app
123+
- name: Rebuild for smalruby3-gui GitHub Pages
124+
if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
125+
env:
126+
NODE_OPTIONS: --max-old-space-size=4000
127+
NODE_ENV: production
128+
PUBLIC_PATH: /smalruby3-gui/
129+
run: npm run build
130+
- name: Prepare deployment files for smalruby3-gui GitHub Pages
131+
if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
132+
run: touch build/.nojekyll
133+
- name: Deploy playground to GitHub Pages
134+
uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 # v3
135+
if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
136+
with:
137+
github_token: ${{ secrets.GITHUB_TOKEN }}
138+
publish_dir: ./build
139+
full_commit_message: "Build for ${{ github.sha }} ${{ github.event.head_commit.message }}"
125140
- name: Set branch name
126141
id: branch
127142
run: echo "BRANCH_NAME=${{ github.head_ref || github.ref_name }}" >> $GITHUB_OUTPUT
143+
- name: Rebuild for branch GitHub Pages
144+
if: |
145+
(!(
146+
github.ref == 'refs/heads/develop' ||
147+
github.ref == 'refs/heads/master' ||
148+
github.ref == 'refs/heads/main' ||
149+
startsWith(github.ref, 'refs/heads/hotfix/') ||
150+
startsWith(github.ref, 'refs/heads/dependabot/') ||
151+
startsWith(github.ref, 'refs/heads/renovate/')
152+
))
153+
env:
154+
NODE_OPTIONS: --max-old-space-size=4000
155+
NODE_ENV: production
156+
PUBLIC_PATH: /smalruby3-gui/${{ steps.branch.outputs.BRANCH_NAME }}/
157+
run: npm run build
158+
- name: Prepare deployment files for branch GitHub Pages
159+
if: |
160+
(!(
161+
github.ref == 'refs/heads/develop' ||
162+
github.ref == 'refs/heads/master' ||
163+
github.ref == 'refs/heads/main' ||
164+
startsWith(github.ref, 'refs/heads/hotfix/') ||
165+
startsWith(github.ref, 'refs/heads/dependabot/') ||
166+
startsWith(github.ref, 'refs/heads/renovate/')
167+
))
168+
run: touch build/.nojekyll
128169
- name: Deploy playground to GitHub Pages for branch
129170
if: |
130171
(!(

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"scripts": {
1414
"setup-opal": "node ./scripts/make-setup-opal.js",
1515
"setup-scratch-vm": "cd node_modules/scratch-vm && npm install && webpack",
16-
"build": "npm run clean && node ./scripts/makePWAAssetsManifest.js && webpack",
16+
"build": "npm run clean && node ./scripts/makePWAAssetsManifest.js && webpack && node ./scripts/postbuild.mjs",
1717
"clean": "rimraf ./build && mkdirp build && rimraf ./dist && mkdirp dist",
1818
"deploy": "touch build/.nojekyll && gh-pages -t -d build -m \"[skip ci] Build for $(git log --pretty=format:%H -n1)\"",
1919
"deploy:smalruby.app": "rimraf node_modules/gh-pages/.cache && echo \"smalruby.app\" > build/CNAME && touch build/.nojekyll && gh-pages -t -d build -m \"build: build for $(git log --pretty=format:%H -n1) [skip ci] \"",

scripts/postbuild.mjs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Post-build script to fix fetch-worker paths for GitHub Pages subdirectory deployments
2+
// This script runs after webpack build and modifies the generated JavaScript files
3+
// to ensure correct worker loading when PUBLIC_PATH is set for subdirectory deployments
4+
5+
import fs from 'fs';
6+
import path from 'path';
7+
import {fileURLToPath} from 'url';
8+
9+
// these aren't set in ESM mode
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = path.dirname(__filename);
12+
13+
// base/root path for the project
14+
const basePath = path.join(__dirname, '..');
15+
const buildPath = path.join(basePath, 'build');
16+
17+
/**
18+
* Fix fetch-worker paths in JavaScript files for subdirectory deployments
19+
* @param {string} filePath - Path to the JavaScript file
20+
* @param {string} publicPath - The public path (e.g., '/smalruby3-gui/')
21+
*/
22+
const fixFetchWorkerPaths = (filePath, publicPath) => {
23+
if (!fs.existsSync(filePath)) {
24+
console.warn(`File not found: ${filePath}`);
25+
return;
26+
}
27+
28+
const content = fs.readFileSync(filePath, 'utf8');
29+
30+
// Replace relative "chunks/" paths with absolute paths including publicPath
31+
const fixedContent = content.replace(
32+
/return "chunks\/" \+ "fetch-worker"/g,
33+
`return "${publicPath}chunks/" + "fetch-worker"`
34+
).replace(
35+
/[=]>"chunks\/fetch-worker\./g,
36+
`=>"${publicPath}chunks/fetch-worker.`
37+
);
38+
39+
if (content === fixedContent) {
40+
console.info(`No fetch-worker paths found in ${path.relative(basePath, filePath)}`);
41+
} else {
42+
fs.writeFileSync(filePath, fixedContent);
43+
console.info(`Fixed fetch-worker paths in ${path.relative(basePath, filePath)}`);
44+
}
45+
};
46+
47+
const postbuild = () => {
48+
const publicPath = process.env.PUBLIC_PATH;
49+
50+
if (!publicPath) {
51+
console.info('PUBLIC_PATH not set - skipping fetch-worker path fixes');
52+
return;
53+
}
54+
55+
console.info(`PUBLIC_PATH is set to: ${publicPath}`);
56+
console.info('Fixing fetch-worker paths for subdirectory deployment...');
57+
58+
// Files that need fetch-worker path fixes
59+
const filesToFix = [
60+
path.join(buildPath, 'gui.js'),
61+
path.join(buildPath, 'player.js')
62+
];
63+
64+
for (const filePath of filesToFix) {
65+
fixFetchWorkerPaths(filePath, publicPath);
66+
}
67+
};
68+
69+
postbuild();
70+
console.info('Post-build script complete');
71+
process.exit(0);

src/lib/ruby-to-blocks-converter/constants.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ const KeyOptions = [
4343
'9'
4444
];
4545

46+
/**
47+
* scratch-vm/src/engine/variable.jsからコピー
48+
* @const
49+
*/
50+
const Variable = {
51+
SCALAR_TYPE: '',
52+
LIST_TYPE: 'list',
53+
BROADCAST_MESSAGE_TYPE: 'broadcast_msg'
54+
};
55+
4656
export {
47-
KeyOptions
57+
KeyOptions,
58+
Variable
4859
};

src/lib/ruby-to-blocks-converter/event.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// eslint-disable-next-line import/no-unresolved
2-
import Variable from 'scratch-vm/src/engine/variable';
3-
import {KeyOptions} from './constants';
2+
import {KeyOptions, Variable} from './constants';
43

54
const GreaterThanMenu = [
65
'LOUDNESS',

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import log from '../log';
55
import Blockly from 'scratch-blocks';
66
import RubyParser from '../ruby-parser';
77
// eslint-disable-next-line import/no-unresolved
8-
import Variable from 'scratch-vm/src/engine/variable';
8+
import {Variable} from './constants';
99

1010
import Primitive from './primitive';
1111
import {RubyToBlocksConverterError} from './errors';
@@ -588,7 +588,7 @@ class RubyToBlocksConverter {
588588
SoundConverter,
589589
SensingConverter
590590
];
591-
591+
592592
for (let i = 0; i < legacyConverters.length; i++) {
593593
const converter = legacyConverters[i];
594594
if (Object.prototype.hasOwnProperty.call(converter, handlerName)) {

webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ const distConfig = baseConfig.clone()
101101
},
102102
output: {
103103
path: path.resolve(__dirname, 'dist')
104-
}
104+
},
105+
externals: ['react', 'react-dom']
105106
})
106-
.addExternals(['react', 'react-dom'])
107107
.addPlugin(
108108
new CopyWebpackPlugin({
109109
patterns: [

0 commit comments

Comments
 (0)