Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const processor: PostProcessor = (codeChunks) => {
let jsCode
let cssCode
let htmlCode
if (codeChunks[FileType.HTML]) {
htmlCode = StringUtils.removeLastEmptyLine(codeChunks[FileType.HTML])
} else {
throw new Error('No code chunk of type HTML found, vue file concatenation aborded')
}
if (codeChunks[FileType.JS]) {
jsCode = StringUtils.removeLastEmptyLine(codeChunks[FileType.JS])
} else {
throw new Error('No code chunk of type JS found, vue file concatenation aborded')
}
// if no CSS, skip the <style></style>
if (codeChunks[FileType.CSS]) {
cssCode = StringUtils.removeLastEmptyLine(codeChunks[FileType.CSS])
}
const formattedHTMLCode = StringUtils.addSpacesToEachLine(' '.repeat(2), htmlCode)
const vueCode = buildVueFile(formattedHTMLCode, jsCode, cssCode)
return {
[FileType.VUE]: vueCode,
}
nodesLookup: {
container: {
openingElement: {
name: {
name: '',
},
attributes: [],
},
},
},
dynamicRefPrefix: {
prop: 'props.',
},
},
type: ChunkType.AST,
fileType: FileType.JS,
linkAfter: ['import-local'],
content: {},
}
it('generates no chunk if no styles exist', async () => {
const uidlSample = component('JSS', elementNode('container'))
const structure: ComponentStructure = {
uidl: uidlSample,
options: {},
chunks: [componentChunk],
dependencies: {},
}
const { chunks } = await plugin(structure)
expect(chunks.length).toBe(1)
nodesLookup: {
container: {
openingElement: {
name: {
name: '',
},
attributes: [],
},
},
},
dynamicRefPrefix: {
prop: 'props.',
},
},
type: ChunkType.AST,
fileType: FileType.JS,
linkAfter: ['import-local'],
content: {},
}
it('adds nothing to the AST if not style is defined', async () => {
const uidlSample = component('InlineStyles', elementNode('container'))
const structure: ComponentStructure = {
uidl: uidlSample,
options: {},
chunks: [componentChunk],
dependencies: {},
}
const oldStructure = JSON.stringify(structure)
await plugin(structure)
const newStructure = JSON.stringify(structure)
describe('plugin-jsx-proptypes', () => {
const plugin = createPropTypesPlugin()
const reactChunk: ChunkDefinition = {
type: ChunkType.AST,
fileType: FileType.JS,
name: 'jsx-component',
content: {},
linkAfter: [],
}
const exportChunk = {
type: ChunkType.AST,
fileType: FileType.JS,
name: 'export',
content: {},
linkAfter: ['jsx-component'],
}
it('Should throw error when the chunk is supplied', async () => {
const uidlSample = component('SimpleComponent', elementNode('container'))
const structure: ComponentStructure = {
uidl: uidlSample,
describe('plugin-jsx-proptypes', () => {
const plugin = createPropTypesPlugin()
const reactChunk: ChunkDefinition = {
type: ChunkType.AST,
fileType: FileType.JS,
name: 'jsx-component',
content: {},
linkAfter: [],
}
const exportChunk = {
type: ChunkType.AST,
fileType: FileType.JS,
name: 'export',
content: {},
linkAfter: ['jsx-component'],
}
it('Should throw error when the chunk is supplied', async () => {
const uidlSample = component('SimpleComponent', elementNode('container'))
const structure: ComponentStructure = {
uidl: uidlSample,
options: {},
chunks: [],
dependencies: {},
}
try {
await plugin(structure)
} catch (e) {
nodesLookup: {
[elementKey]: {
openingElement: {
name: {
name: '',
},
attributes: [],
},
},
},
dynamicRefPrefix: {
prop: 'props.',
},
},
type: ChunkType.AST,
fileType: FileType.JS,
linkAfter: ['import-local'],
content: {},
}
return componentChunk
}
}
const jsxTagStructure = createJSXSyntax(uidl.node, jsxParams, jsxOptions)
if (!usePureComponent) {
dependencies.Component = PREACT_COMPONENT_DEPENDENCY
}
const componentName = UIDLUtils.getComponentClassName(uidl)
const preactComponent = usePureComponent
? createPureComponent(componentName, propDefinitions, jsxTagStructure)
: createClassComponent(componentName, propDefinitions, stateDefinitions, jsxTagStructure)
structure.chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: componentChunkName,
meta: {
nodesLookup,
dynamicRefPrefix: jsxOptions.dynamicReferencePrefixMap,
},
content: preactComponent,
linkAfter: [importChunkName],
})
structure.chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: exportChunkName,
content: ASTBuilders.createDefaultExport(componentName),
linkAfter: [componentChunkName],
})
const pureComponent = ASTBuilders.createFunctionalComponent(uidl.name, rootRouterTag)
structure.chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: componentChunkName,
content: pureComponent,
linkAfter: [importChunkName],
})
if (flavor === 'preact') {
const exportJSXApp = ASTBuilders.createDefaultExport('App')
structure.chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: domRenderChunkName,
content: exportJSXApp,
linkAfter: [componentChunkName],
})
} else {
const reactDomBind = ASTBuilders.createFunctionCall('ReactDOM.render', [
ASTBuilders.createSelfClosingJSXTag(uidl.name),
ASTBuilders.createFunctionCall('document.getElementById', ['app']),
])
structure.chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: domRenderChunkName,
content: reactDomBind,
linkAfter: [componentChunkName],
)
})
let exportSpecifiers = []
exportSpecifiers = Object.keys(components).map((component) =>
t.exportSpecifier(
t.identifier(StringUtils.capitalize(component)),
t.identifier(StringUtils.capitalize(component))
)
)
const exportAST = t.exportNamedDeclaration(null, exportSpecifiers)
chunks.push({
name: importChunkName,
type: ChunkType.AST,
fileType: FileType.JS,
content: importsAST,
linkAfter: [],
})
chunks.push({
name: exportChunkName,
type: ChunkType.AST,
fileType: FileType.JS,
content: exportAST,
linkAfter: [importChunkName],
})
return structure
}
export const createImportPlugin: ComponentPluginFactory = (config) => {
const {
importLibsChunkName = 'import-lib',
importPackagesChunkName = 'import-pack',
importLocalsChunkName = 'import-local',
fileType = FileType.JS,
} = config || {}
const importPlugin: ComponentPlugin = async (structure) => {
const { dependencies } = structure
const libraryDependencies = groupDependenciesByPackage(dependencies, 'library')
const packageDependencies = groupDependenciesByPackage(dependencies, 'package')
const localDependencies = groupDependenciesByPackage(dependencies, 'local')
addImportChunk(structure.chunks, libraryDependencies, importLibsChunkName, fileType)
addImportChunk(structure.chunks, packageDependencies, importPackagesChunkName, fileType)
addImportChunk(structure.chunks, localDependencies, importLocalsChunkName, fileType)
return structure
}
return importPlugin
}