-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 2.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const { getCleanedDeps } = require('./getDeps')
const { getCleanedSSUIDeps } = require('./getSSUIDeps')
const mapping = getCleanedDeps()
const mappingSSUI = getCleanedSSUIDeps()
function transform({ types: t }) {
return {
visitor: {
ImportDeclaration(path, state) {
// target regular SUI imports
const packageRegex = /^((.*!)?semantic-ui-react)([/\\].*)?$/
const match1 = packageRegex.exec(path.node.source.value)
const addImports = []
if (match1) {
const memberImports = path.node.specifiers.filter(specifier => specifier.type === 'ImportSpecifier')
// For each member import of a known component, add a separate import statement
memberImports.forEach((memberImport) => {
const deps = mapping[memberImport.imported.name]
deps && deps.forEach((dep) => {
addImports.push(t.importDeclaration([], t.stringLiteral(`semantic-ui-css/components/${dep.toLowerCase()}.min.css`)))
})
})
}
// target SSUI imports
const packageRegex2 = /^((.*!)?semantic-styled-ui)([/\\].*)?$/
const match2 = packageRegex2.exec(path.node.source.value)
if (match2) {
const memberImports = path.node.specifiers.filter(specifier => specifier.type === 'ImportSpecifier')
// For each member import of a known component, add a separate import statement
memberImports.forEach((memberImport) => {
const deps = mappingSSUI[memberImport.imported.name]
deps && deps.forEach((dep) => {
addImports.push(t.importDeclaration([], t.stringLiteral(`semantic-ui-css/components/${dep.toLowerCase()}.min.css`)))
})
})
}
// target base CSS import
const packageRegex3 = /^semantic-ui-css\/semantic.min.css$/
const match3 = packageRegex3.exec(path.node.source.value)
if (match3) {
// For each member import of a known component, add a separate import statement
addImports.push(t.importDeclaration([], t.stringLiteral(`semantic-ui-css/components/reset.min.css`)))
addImports.push(t.importDeclaration([], t.stringLiteral(`semantic-ui-css/components/site.min.css`)))
}
// FIXME: will input duplicate imports (webpack should eliminate)
if (match1 || match2 || match3) {
path.insertAfter(addImports)
}
if (match3) {
path.remove()
}
}
}
}
}
module.exports = transform