-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19375 from storybookjs/norbert/sb-735-bug-report-…
…addons-are-not-loading-in UI: Fix addon URL escaping in manager
- Loading branch information
Showing
3 changed files
with
42 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { sanitizePath } from './files'; | ||
|
||
test('sanitizePath', () => { | ||
const addonsDir = '/Users/username/Projects/projectname/storybook'; | ||
const text = 'demo text'; | ||
const file = { | ||
path: '/Users/username/Projects/projectname/storybook/node_modules/@storybook/addon-x+y/dist/manager.mjs', | ||
contents: Uint8Array.from(Array.from(text).map((letter) => letter.charCodeAt(0))), | ||
text, | ||
}; | ||
const { location, url } = sanitizePath(file, addonsDir); | ||
|
||
expect(location).toMatchInlineSnapshot( | ||
`"/Users/username/Projects/projectname/storybook/node_modules/@storybook/addon-x+y/dist/manager.mjs"` | ||
); | ||
expect(url).toMatchInlineSnapshot( | ||
`"./sb-addons/node_modules/%40storybook/addon-x%2By/dist/manager.mjs"` | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
import { OutputFile } from 'esbuild'; | ||
import { writeFile, ensureFile } from 'fs-extra'; | ||
import { compilation } from '../index'; | ||
import { join } from 'path'; | ||
import { Compilation } from '../types'; | ||
|
||
export async function readOrderedFiles(addonsDir: string) { | ||
export async function readOrderedFiles( | ||
addonsDir: string, | ||
outputFiles: Compilation['outputFiles'] | undefined | ||
) { | ||
const files = await Promise.all( | ||
compilation?.outputFiles?.map(async (file) => { | ||
await ensureFile(file.path).then(() => writeFile(file.path, file.contents)); | ||
return file.path.replace(addonsDir, './sb-addons'); | ||
outputFiles?.map(async (file) => { | ||
// convert deeply nested paths to a single level, also remove special characters | ||
const { location, url } = sanitizePath(file, addonsDir); | ||
|
||
await ensureFile(location).then(() => writeFile(location, file.contents)); | ||
return url; | ||
}) || [] | ||
); | ||
|
||
const jsFiles = files.filter((file) => file.endsWith('.mjs')); | ||
const cssFiles = files.filter((file) => file.endsWith('.css')); | ||
return { cssFiles, jsFiles }; | ||
} | ||
|
||
export function sanitizePath(file: OutputFile, addonsDir: string) { | ||
const filePath = file.path.replace(addonsDir, ''); | ||
const location = join(addonsDir, filePath); | ||
const url = `./sb-addons${filePath.split('/').map(encodeURIComponent).join('/')}`; | ||
return { location, url }; | ||
} |