Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Fix/spa sourcemap 404 (#7696)

Alexander peczon5bd32762b558
diff --git a/client/angular.json b/client/angular.json
index ddc74e6fa9..62ebabf107 100644
--- a/client/angular.json
+++ b/client/angular.json
@@ -244,7 +244,10 @@
"production": {
"optimization": true,
"outputHashing": "all",
- "sourceMap": false,
+ "sourceMap": {
+ "scripts": true,
+ "styles": false
+ },
"namedChunks": false,
"extractLicenses": true,
"serviceWorker": "src/ngsw-config.json",
diff --git a/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/main.js b/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/main.js
new file mode 100644
index 0000000000..d76e106ffb
--- /dev/null
+++ b/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/main.js
@@ -0,0 +1,12 @@
+async function register () {
+ return
+}
+
+async function unregister () {
+ return
+}
+
+module.exports = {
+ register,
+ unregister
+}
diff --git a/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/package.json b/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/package.json
new file mode 100644
index 0000000000..4aa43636c3
--- /dev/null
+++ b/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/package.json
@@ -0,0 +1,22 @@
+{
+ "name": "peertube-plugin-test-css-sourcemap",
+ "version": "0.0.1",
+ "description": "Plugin CSS sourcemap test",
+ "engine": {
+ "peertube": ">=1.3.0"
+ },
+ "keywords": [
+ "peertube",
+ "plugin"
+ ],
+ "homepage": "https://github.com/Chocobozzz/PeerTube",
+ "author": "Chocobozzz",
+ "bugs": "https://github.com/Chocobozzz/PeerTube/issues",
+ "library": "./main.js",
+ "staticDirs": {},
+ "css": [
+ "./style.css"
+ ],
+ "clientScripts": [],
+ "translations": {}
+}
diff --git a/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/style.css b/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/style.css
new file mode 100644
index 0000000000..4b24992924
--- /dev/null
+++ b/packages/tests/fixtures/peertube-plugin-test-css-sourcemap/style.css
@@ -0,0 +1,5 @@
+.css-sourcemap-fixture {
+ color: red;
+}
+
+/*# sourceMappingURL=style.css.map */
diff --git a/packages/tests/src/plugins/html-injection.ts b/packages/tests/src/plugins/html-injection.ts
index e18b03f4ec..a9f19f8083 100644
--- a/packages/tests/src/plugins/html-injection.ts
+++ b/packages/tests/src/plugins/html-injection.ts
@@ -53,7 +53,17 @@ describe('Test plugins HTML injection', function () {
}
})
+ it('Should strip sourcemap comments from global css', async function () {
+ await command.install({ path: PluginsCommand.getPluginTestPath('-css-sourcemap') })
+
+ const text = await command.getCSS()
+ expect(text).to.contain('.css-sourcemap-fixture')
+ expect(text).to.not.contain('sourceMappingURL')
+ expect(text).to.not.contain('style.css.map')
+ })
+
it('Should have an empty global css on uninstall', async function () {
+ await command.uninstall({ npmName: 'peertube-plugin-test-css-sourcemap' })
await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
{
diff --git a/scripts/build/client.sh b/scripts/build/client.sh
index 74c9d7a75b..f307f2eade 100755
--- a/scripts/build/client.sh
+++ b/scripts/build/client.sh
@@ -59,8 +59,8 @@ cd client
# Don't build other languages if --light arg is provided
if [ -z ${1+x} ] || ([ "$1" != "--light" ] && [ "$1" != "--analyze-bundle" ]); then
additionalParams=""
- if [ ! -z ${1+x} ] && [ "$1" == "--source-map" ]; then
- additionalParams="--source-map=true"
+ if [ -z ${1+x} ] || [ "$1" != "--source-map" ]; then
+ additionalParams="--source-map=false"
fi
NODE_OPTIONS=--max_old_space_size=8192 node_modules/.bin/ng build --configuration production --output-path "dist/build" $additionalParams
diff --git a/server/core/lib/html/client-html.ts b/server/core/lib/html/client-html.ts
index b8695b9600..42d98026b1 100644
--- a/server/core/lib/html/client-html.ts
+++ b/server/core/lib/html/client-html.ts
@@ -2,10 +2,10 @@ import { HttpStatusCode } from '@peertube/peertube-models'
import express from 'express'
import { logger } from '../../helpers/logger.js'
import { ACCEPT_HEADERS } from '../../initializers/constants.js'
-import { VideoHtml } from './shared/video-html.js'
-import { PlaylistHtml } from './shared/playlist-html.js'
import { ActorHtml } from './shared/actor-html.js'
import { PageHtml } from './shared/page-html.js'
+import { PlaylistHtml } from './shared/playlist-html.js'
+import { VideoHtml } from './shared/video-html.js'
class ClientHtml {
static invalidateCache () {
diff --git a/server/core/lib/plugins/plugin-manager.ts b/server/core/lib/plugins/plugin-manager.ts
index 176ee0f381..e8f039c9e7 100644
--- a/server/core/lib/plugins/plugin-manager.ts
+++ b/server/core/lib/plugins/plugin-manager.ts
@@ -14,8 +14,8 @@ import { decachePlugin } from '@server/helpers/decache.js'
import { ApplicationModel } from '@server/models/application/application.js'
import { MOAuthTokenUser, MUser } from '@server/types/models/index.js'
import express from 'express'
-import { createReadStream, createWriteStream } from 'fs'
import { ensureDir, outputFile, readJSON } from 'fs-extra/esm'
+import { appendFile, readFile } from 'fs/promises'
import { Server } from 'http'
import { createRequire } from 'module'
import { basename, join } from 'path'
@@ -604,16 +604,10 @@ export class PluginManager implements ServerHook {
}
}
- private concatFiles (input: string, output: string) {
- return new Promise<void>((res, rej) => {
- const inputStream = createReadStream(input)
- const outputStream = createWriteStream(output, { flags: 'a' })
+ private async concatFiles (input: string, output: string) {
+ const css = await readFile(input, 'utf-8')
- inputStream.pipe(outputStream)
-
- inputStream.on('end', () => res())
- inputStream.on('error', err => rej(err))
- })
+ return appendFile(output, stripSourceMappingURLComments(css))
}
private async regeneratePluginGlobalCSS () {
@@ -715,3 +709,9 @@ export class PluginManager implements ServerHook {
return this.instance || (this.instance = new this())
}
}
+
+function stripSourceMappingURLComments (css: string) {
+ return css
+ .replace(/\/\*#\s*sourceMappingURL=[^*]*\*\//gs, '')
+ .replace(/\/\/#\s*sourceMappingURL=.*/g, '')
+}