Skip to content

Commit

Permalink
fix(import): check if the element is already imported & dont import u…
Browse files Browse the repository at this point in the history
…ndefined
  • Loading branch information
thorecaspersen committed Jun 10, 2019
1 parent f044558 commit a447e5b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion input/testmarkdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ prependJs:
---
# An h1 header

{{ <LetsDoThis /> }}
{{ <headline /> }}
Paragraphs are separated by a blank line.

2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists
Expand Down
20 changes: 16 additions & 4 deletions lib/templates/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const stringifyObject = require("stringify-object");

module.exports = data => {
let prepended = "";
// remember what impoter that is used, so we dont import same element twice
let usedElementNames = [];
if (data.prependJs !== undefined) {
data.prependJs.forEach(m => {
prepended += `${m}\n`;
Expand All @@ -17,10 +19,12 @@ module.exports = data => {
data.listOfReactComponentsUsed.forEach(value => {
// use special impoter, if it is defined in the impoter list

if (importer && value in importer) {
if (importer && typeof value != "undefined" && value in importer) {
prepended += `import ${value} from '${importer[value]}/${value}';\n`;
} else {
usedElementNames.push(value);
} else if (typeof value != "undefined") {
prepended += `import ${value} from '${data.defualtImport}/${value}';\n`;
usedElementNames.push(value);
}
}); // eksampel: import button from 'tequila-ui/button';
}
Expand All @@ -30,9 +34,17 @@ module.exports = data => {
// the array contains empty string, so we hva to check for that
if (value !== "") {
// use special impoter, if it is defined in the impoter list
if (importer && value in importer) {
if (
importer &&
typeof value != "undefined" &&
value in importer &&
!usedElementNames.includes(value)
) {
prepended += `import ${value} from '${importer[value]}/${value}';\n`;
} else {
} else if (
typeof value != "undefined" &&
!usedElementNames.includes(value)
) {
prepended += `import ${value} from '${
data.defualtImport
}/${value}';\n`;
Expand Down
4 changes: 2 additions & 2 deletions output/testmarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Everything is ok
import React from 'react';
const Timer = require('./timer');
import { Watcher } from './watcher';
import headline from 'semantic-ui-react/headline';
import LetsDoThis from 'tequila-ui/LetsDoThis';
import FancyReactComponent from 'antd/FancyReactComponent';
import headline from 'semantic-ui-react/headline';
import quotes from 'tequila-ui/quotes';

const frontMatter = {
Expand All @@ -20,7 +20,7 @@ export default class Testmarkdown extends React.PureComponent {
return (
<div>
<headline>An h1 header</headline>
<LetsDoThis />
<headline />
<p>Paragraphs are separated by a blank line. </p>
<p>
2nd paragraph. <em>Italic</em>, <strong>bold</strong>, and{' '}
Expand Down

0 comments on commit a447e5b

Please sign in to comment.