This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (124 loc) · 3.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const { extname, dirname, resolve } = require('path')
const mkdirp = require('mkdirp')
const { readFileSync, appendFileSync } = require('fs')
const deasync = require('deasync')
const postcss = require('postcss')
const atImport = require('postcss-import')
const simpleVars = require('postcss-simple-vars')
const cssNext = require('postcss-cssnext')
const cssModules = require('postcss-modules')
const extensions = ['.css']
// return {results, token}
const getStylesFromStylesheet = (stylesheetPath, file) => {
if (extensions.indexOf(extname(stylesheetPath)) !== -1) {
const requiringFile = file.opts.filename
const resolvedStylesheetPath = resolve(
process.env.PWD,
dirname(requiringFile),
stylesheetPath
)
const source = readFileSync(resolvedStylesheetPath, 'utf8')
let tokens = {}
// TODO: load post css configs from `postcss.config.js`
const plugins = [
atImport,
simpleVars,
cssNext,
cssModules({
getJSON: (cssFileName, json, outputFileName) => {
tokens = json
console.log({ json })
}
})
]
// read and process css with `postcss`
const results = sync(
postcss(plugins).process(source, {
from: resolvedStylesheetPath,
to: resolvedStylesheetPath
})
)
return { results, tokens }
}
}
const sync = promise => {
let success, error
promise.then(result => (success = { result }), err => (error = err))
deasync.loopWhile(() => !(success || error))
if (!success) {
throw error
}
return success.result
}
const writeCssFile = (targetFilePath, content) => {
mkdirp.sync(dirname(targetFilePath))
appendFileSync(targetFilePath, content, 'utf8')
}
module.exports = function(babel) {
const { types: t } = babel
return {
visitor: {
CallExpression(path, state) {
const { file, opts } = state
const { callee: { name: calleeName }, arguments: args } = path.node
const [{ value: stylesheetPath }] = args
if (
calleeName !== 'require' ||
!args.length ||
!t.isStringLiteral(args[0]) ||
// only care about `.css`
extensions.indexOf(extname(stylesheetPath)) === -1
) {
return
}
const { results, tokens } = getStylesFromStylesheet(
stylesheetPath,
file
)
const expression = path.findParent(
test => test.isVariableDeclaration() || test.isExpressionStatement()
)
path.replaceWith(
t.objectExpression(
Object.keys(tokens).map(token =>
t.objectProperty(
t.stringLiteral(token),
t.stringLiteral(tokens[token])
)
)
)
)
},
ImportDeclaration(path, state) {
const { file, opts } = state
const stylesheetPath = path.node.source.value
if (
path.node.specifiers.length !== 1 ||
extensions.indexOf(extname(stylesheetPath)) === -1
) {
return
}
const { results, tokens } = getStylesFromStylesheet(
stylesheetPath,
file
)
const distStylesheetPath = resolve(process.env.PWD, opts.extractCss)
// append all the compiled css into the dist css file
writeCssFile(distStylesheetPath, results.css)
const styles = t.objectExpression(
Object.keys(tokens).map(token =>
t.objectProperty(
t.stringLiteral(token),
t.stringLiteral(tokens[token])
)
)
)
const variableDeclaration = t.VariableDeclaration('var', [
t.VariableDeclarator(path.node.specifiers[0].local, styles)
])
// replace the import statement into `var styles = {}`
path.replaceWith(variableDeclaration)
}
}
}
}