fix(client): ensure source is added to files (#56601)

This commit is contained in:
Oliver Eyton-Williams 2024-10-09 00:13:15 +02:00 committed by GitHub
parent 20a8c1d59b
commit 0a64f4bd53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 2 deletions

View File

@ -14,7 +14,8 @@ import {
transformContents,
transformHeadTailAndContents,
setExt,
compileHeadTail
compileHeadTail,
createSource
} from '../../../../../shared/utils/polyvinyl';
import { WorkerExecutor } from '../utils/worker-executor';
@ -275,6 +276,7 @@ const htmlTransformer = cond([
]);
export const getTransformers = loopProtectOptions => [
createSource,
replaceNBSP,
babelTransformer(loopProtectOptions),
partial(compileHeadTail, ''),
@ -282,6 +284,7 @@ export const getTransformers = loopProtectOptions => [
];
export const getPythonTransformers = () => [
createSource,
replaceNBSP,
partial(compileHeadTail, '')
];

View File

@ -20,7 +20,7 @@ const invariant = require('invariant');
// contents: String,
// history?: [...String],
// }) => PolyVinyl, throws
function createPoly({ name, ext, contents, history, ...rest } = {}) {
function createPoly({ name, ext, contents, history, ...rest }) {
invariant(typeof name === 'string', 'name must be a string but got %s', name);
invariant(typeof ext === 'string', 'ext must be a string, but was %s', ext);
@ -147,11 +147,20 @@ function transformHeadTailAndContents(wrap, poly) {
};
}
// createSource(poly: PolyVinyl) => PolyVinyl
function createSource(poly) {
return {
...poly,
source: poly.source || poly.contents
};
}
module.exports = {
createPoly,
isPoly,
setContent,
setExt,
createSource,
compileHeadTail,
regeneratePathAndHistory,
transformContents,

View File

@ -0,0 +1,29 @@
import polyvinyl from './polyvinyl';
const polyData = {
name: 'test',
ext: 'js',
contents: 'var hello = world;',
history: ['test.js']
};
describe('createSource', () => {
it('should return a vinyl object with a source matching the contents', () => {
const original = polyvinyl.createPoly(polyData);
const updated = polyvinyl.createSource(original);
expect(original).not.toHaveProperty('source');
expect(updated).toHaveProperty('source', 'var hello = world;');
expect(updated).toMatchObject(original);
});
it('should not update the source if it already exists', () => {
const original = polyvinyl.createPoly({
...polyData,
source: 'const hello = world;'
});
const updated = polyvinyl.createSource(original);
expect(updated).toStrictEqual(original);
});
});