Skip to content

Commit

Permalink
fix: do not sanitize file names (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Nov 22, 2021
1 parent 0f7f618 commit 2ba0dff
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"babel-plugin-source-map-support": "^2.1.3",
"prop-types": "^15.7.2",
"react": "^17.0.1",
"rollup": "^2.34.0",
"rollup": "^2.60.1",
"source-map-support": "^0.5.19"
},
"devDependencies": {
Expand Down
37 changes: 35 additions & 2 deletions src/transpiler/__tests__/__snapshots__/transpiler.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Transpiler should keep names of files, even if special chars with a simple setup and import correctly 1`] = `
"'use strict';
var jsxRuntime = require('react/cjs/react-jsx-runtime.production.min');
require('source-map-support/register');
var path = require('path');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
// eslint-disable-next-line security/detect-non-literal-require, no-undef
const {
File
} = require(path__default[\\"default\\"].resolve(__dirname, '../../../../components'));
const greetings = name => \`hello \${name}\`; // eslint-disable-next-line react/display-name
function $$simple$$ () {
return /*#__PURE__*/jsxRuntime.jsx(File, {
children: greetings('Test')
});
}
module.exports = $$simple$$;
//# sourceMappingURL=$$simple$$.js.map
"
`;

exports[`Transpiler should keep names of files, even if special chars with a simple setup and import correctly 2`] = `"{\\"version\\":3,\\"file\\":\\"$$simple$$.js\\",\\"sources\\":[\\"../../testfiles/SpecialChars/$$simple$$.js\\"],\\"sourcesContent\\":[\\"import path from 'path';\\\\n// this weird import are only necessary because we test within the SDK itself.\\\\n// eslint-disable-next-line security/detect-non-literal-require, no-undef\\\\nconst {File} = require(path.resolve(__dirname, '../../../../components'));\\\\nconst greetings = name => \`hello \${name}\`;\\\\n// eslint-disable-next-line react/display-name\\\\nexport default function() {\\\\n return (\\\\n <File>\\\\n {greetings('Test')}\\\\n </File>\\\\n );\\\\n}\\"],\\"names\\":[\\"File\\",\\"require\\",\\"path\\",\\"resolve\\",\\"__dirname\\",\\"greetings\\",\\"name\\",\\"_jsx\\"],\\"mappings\\":\\";;;;;;;;;;AAEA;;AACA,MAAM;AAACA,EAAAA;AAAD,IAASC,OAAO,CAACC,wBAAI,CAACC,OAAL,CAAaC,SAAb,EAAwB,wBAAxB,CAAD,CAAtB;;AACA,MAAMC,SAAS,GAAGC,IAAI,IAAK,SAAQA,IAAK,EAAxC;;;AAEe,uBAAW;AACxB,sBACEC,eAAC,IAAD;AAAA,cACGF,SAAS,CAAC,MAAD;AADZ,IADF;AAKD;;;;\\"}"`;

exports[`Transpiler should transpile CommonJS files with a simple setup and import correctly 1`] = `
"'use strict';
Expand Down Expand Up @@ -46,7 +79,7 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
const {
File
} = require(path__default['default'].resolve(__dirname, '../../../../components'));
} = require(path__default[\\"default\\"].resolve(__dirname, '../../../../components'));
function greetings(name) {
return \`hello \${name}\`;
Expand Down Expand Up @@ -81,7 +114,7 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
const {
File
} = require(path__default['default'].resolve(__dirname, '../../../../components'));
} = require(path__default[\\"default\\"].resolve(__dirname, '../../../../components'));
const greetings = name => \`hello \${name}\`; // eslint-disable-next-line react/display-name
Expand Down
13 changes: 13 additions & 0 deletions src/transpiler/__tests__/testfiles/SpecialChars/$$simple$$.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from 'path';
// this weird import are only necessary because we test within the SDK itself.
// eslint-disable-next-line security/detect-non-literal-require, no-undef
const {File} = require(path.resolve(__dirname, '../../../../components'));
const greetings = name => `hello ${name}`;
// eslint-disable-next-line react/display-name
export default function() {
return (
<File>
{greetings('Test')}
</File>
);
}
30 changes: 30 additions & 0 deletions src/transpiler/__tests__/transpiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const readFile = promisify(fs.readFile);
describe('Transpiler', () => {
const testFiles = path.resolve(__dirname, './testfiles');
const outputFiles = path.resolve(__dirname, './__transpiled_testfiles');

beforeAll(async (done) => {
try {
await transpileFiles(testFiles, outputFiles, {
Expand All @@ -23,57 +24,86 @@ describe('Transpiler', () => {
done(e);
}
});

describe('should transpile CommonJS files', () => {
describe('with a simple setup', () => {
const commonjs_testFile = path.resolve(outputFiles, './CommonJS/simple.js');
const commonjs_testFileMap = path.resolve(outputFiles, './CommonJS/simple.js.map');

test('and import correctly', async () => {
const content = await readFile(commonjs_testFile, 'utf8');
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
const mapContent = await readFile(commonjs_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(await import(commonjs_testFile)).toBeDefined();
});

test('and render correctly', async () => {
const content = await renderTemplate(commonjs_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
});
});
});

describe('should transpile ES5 files', () => {
describe('with a simple setup', () => {
const es5_testFile = path.resolve(outputFiles, './ES5/simple.js');
const es5_testFileMap = path.resolve(outputFiles, './ES5/simple.js.map');

test('and import correctly', async () => {
const content = await readFile(es5_testFile, 'utf8')
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
const mapContent = await readFile(es5_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(await import(es5_testFile)).toBeDefined();
});

test('and render correctly', async () => {
const content = await renderTemplate(es5_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
});
});
});

describe('should transpile ES6 files', () => {
describe('with a simple setup', () => {
const es6_testFile = path.resolve(outputFiles, './ES6/simple.js');
const es6_testFileMap = path.resolve(outputFiles, './ES6/simple.js.map');

test('and import correctly', async () => {
const content = await readFile(es6_testFile, 'utf8')
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
const mapContent = await readFile(es6_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(await import(es6_testFile)).toBeDefined();
});

test('and render correctly', async () => {
const content = await renderTemplate(es6_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
});
});
});

describe('should keep names of files, even if special chars', () => {
describe('with a simple setup', () => {
const special_testFile = path.resolve(outputFiles, './SpecialChars/$$simple$$.js');
const special_testFileMap = path.resolve(outputFiles, './SpecialChars/$$simple$$.js.map');

test('and import correctly', async () => {
const content = await readFile(special_testFile, 'utf8');
expect(switchToUnixLinebreaks(content)).toMatchSnapshot();
const mapContent = await readFile(special_testFileMap, 'utf8');
expect(switchToUnixLinebreaks(mapContent)).toMatchSnapshot();
expect(await import(special_testFile)).toBeDefined();
});

test('and render correctly', async () => {
const content = await renderTemplate(special_testFile, { asyncapi: {} as AsyncAPIDocument, originalAsyncAPI: "", params: {} });
expect((content as TemplateRenderResult)?.content).toBe("hello Test");
});
});
});
});

/*
Expand Down
5 changes: 3 additions & 2 deletions src/transpiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function transpileFiles(directory: string, outputDir: string, optio
}],
],
})
]
],
})
await bundles.write({
format: "commonjs",
Expand All @@ -53,7 +53,8 @@ export async function transpileFiles(directory: string, outputDir: string, optio
exports: "auto",
paths: {
'react/jsx-runtime': 'react/cjs/react-jsx-runtime.production.min',
}
},
sanitizeFileName: false,
})
}

Expand Down

0 comments on commit 2ba0dff

Please sign in to comment.