|
1 | | -import './guard'; |
2 | 1 | import hook from './hook'; |
3 | | -import postcss from 'postcss'; |
4 | | -import { dirname, join, parse, relative, resolve, sep } from 'path'; |
5 | 2 | import { readFileSync } from 'fs'; |
6 | | -import isPlainObject from 'lodash.isplainobject'; |
| 3 | +import { dirname, sep, relative, resolve } from 'path'; |
| 4 | +import { identity, removeQuotes } from './fn'; |
| 5 | +import postcss from 'postcss'; |
7 | 6 |
|
8 | 7 | import ExtractImports from 'postcss-modules-extract-imports'; |
9 | 8 | import LocalByDefault from 'postcss-modules-local-by-default'; |
10 | 9 | import Scope from 'postcss-modules-scope'; |
11 | 10 | import Parser from './parser'; |
12 | 11 |
|
13 | | -let processCss; |
14 | | -let rootDir; |
15 | | -let plugins; |
| 12 | +// cache |
| 13 | +let importNr = 0; |
| 14 | +let tokensByFile = {}; |
| 15 | +// processing functions |
| 16 | +const preProcess = identity; |
| 17 | +let postProcess; |
| 18 | +// defaults |
| 19 | +let plugins = [LocalByDefault, ExtractImports, Scope]; |
| 20 | +let rootDir = process.cwd(); |
16 | 21 |
|
17 | 22 | /** |
18 | | - * @param {object} opts |
| 23 | + * @param {object} opts |
| 24 | + * @param {function} opts.createImportedName |
19 | 25 | * @param {function} opts.generateScopedName |
20 | | - * @param {function} opts.processCss|.p |
21 | | - * @param {string} opts.rootDir|.root|.d |
22 | | - * @param {array} opts.use|.u |
| 26 | + * @param {function} opts.processCss |
| 27 | + * @param {string} opts.rootDir |
| 28 | + * @param {array} opts.use |
23 | 29 | */ |
24 | | -export default function buildOptions(opts = {}) { |
25 | | - if (!isPlainObject(opts)) { |
26 | | - throw new Error('Use plain object'); |
| 30 | +export default function setup(opts = {}) { |
| 31 | + // clearing cache |
| 32 | + importNr = 0; |
| 33 | + tokensByFile = {}; |
| 34 | + |
| 35 | + if (opts.processCss && typeof opts.processCss !== 'function') { |
| 36 | + throw new Error('should specify function for processCss'); |
27 | 37 | } |
28 | 38 |
|
29 | | - processCss = get(opts, 'processCss|p'); |
30 | | - rootDir = get(opts, 'rootDir|root|d'); |
31 | | - rootDir = rootDir ? resolve(rootDir) : process.cwd(); |
| 39 | + postProcess = opts.processCss || null; |
32 | 40 |
|
33 | | - const customPlugins = get(opts, 'use|u'); |
34 | | - if (Array.isArray(customPlugins)) { |
35 | | - return void (plugins = customPlugins); |
| 41 | + if (opts.rootDir && typeof opts.rootDir !== 'string') { |
| 42 | + throw new Error('should specify string for rootDir'); |
36 | 43 | } |
37 | 44 |
|
38 | | - plugins = []; |
| 45 | + rootDir = opts.rootDir || process.cwd(); |
39 | 46 |
|
40 | | - plugins.push( |
41 | | - opts.mode |
42 | | - ? new LocalByDefault({mode: opts.mode}) |
43 | | - : LocalByDefault |
44 | | - ); |
45 | | - |
46 | | - plugins.push( |
47 | | - opts.createImportedName |
48 | | - ? new ExtractImports({createImportedName: opts.createImportedName}) |
49 | | - : ExtractImports |
50 | | - ); |
51 | | - |
52 | | - plugins.push( |
53 | | - opts.generateScopedName |
54 | | - ? new Scope({generateScopedName: opts.generateScopedName}) |
55 | | - : Scope |
56 | | - ); |
57 | | -} |
| 47 | + if (opts.use) { |
| 48 | + if (!Array.isArray(opts.use)) { |
| 49 | + throw new Error('should specify array for use'); |
| 50 | + } |
58 | 51 |
|
59 | | -const escapedSeparator = sep.replace(/(.)/g, '\\$1'); |
60 | | -const relativePathPattern = new RegExp(`^.{1,2}$|^.{1,2}${escapedSeparator}`); |
61 | | -const tokensByFile = {}; |
62 | | -let importNr = 0; |
| 52 | + return void (plugins = opts.use); |
| 53 | + } |
63 | 54 |
|
64 | | -/** |
65 | | - * @param {object} object |
66 | | - * @param {string} keys 'a|b|c' |
67 | | - * @return {*} |
68 | | - */ |
69 | | -function get(object, keys) { |
70 | | - let key; |
| 55 | + plugins = []; |
71 | 56 |
|
72 | | - keys.split('|').some(k => { |
73 | | - if (!object[k]) { |
74 | | - return false; |
| 57 | + if (opts.mode) { |
| 58 | + if (typeof opts.mode !== 'string') { |
| 59 | + throw new Error('should specify string for mode'); |
75 | 60 | } |
76 | 61 |
|
77 | | - key = k; |
78 | | - return true; |
79 | | - }); |
| 62 | + plugins.push(new LocalByDefault({mode: opts.mode})); |
| 63 | + } else { |
| 64 | + plugins.push(LocalByDefault); |
| 65 | + } |
80 | 66 |
|
81 | | - return key ? object[key] : null; |
82 | | -} |
| 67 | + if (opts.createImportedName) { |
| 68 | + if (typeof opts.createImportedName !== 'function') { |
| 69 | + throw new Error('should specify function for createImportedName'); |
| 70 | + } |
83 | 71 |
|
84 | | -/** |
85 | | - * @param {string} pathname |
86 | | - * @return {boolean} |
87 | | - */ |
88 | | -function isModule(pathname) { |
89 | | - const parsed = parse(pathname); |
90 | | - return !parsed.root && !relativePathPattern.test(parsed.dir); |
91 | | -} |
| 72 | + plugins.push(new ExtractImports({createImportedName: opts.createImportedName})); |
| 73 | + } else { |
| 74 | + plugins.push(ExtractImports); |
| 75 | + } |
92 | 76 |
|
93 | | -/** |
94 | | - * @param {string} sourceString The file content |
95 | | - * @param {string} sourcePath |
96 | | - * @param {string} trace |
97 | | - * @param {function} pathFetcher |
98 | | - * @return {object} |
99 | | - */ |
100 | | -function load(sourceString, sourcePath, trace, pathFetcher) { |
101 | | - const lazyResult = postcss(plugins.concat(new Parser({ pathFetcher, trace }))) |
102 | | - .process(sourceString, {from: sourcePath}); |
| 77 | + if (opts.generateScopedName) { |
| 78 | + if (typeof opts.generateScopedName !== 'function') { |
| 79 | + throw new Error('should specify function for generateScopedName'); |
| 80 | + } |
103 | 81 |
|
104 | | - return { injectableSource: lazyResult.css, exportTokens: lazyResult.root.tokens }; |
| 82 | + plugins.push(new Scope({generateScopedName: opts.generateScopedName})); |
| 83 | + } else { |
| 84 | + plugins.push(Scope); |
| 85 | + } |
105 | 86 | } |
106 | 87 |
|
107 | 88 | /** |
108 | | - * @param {string} _newPath |
109 | | - * @param {string} _relativeTo |
| 89 | + * @param {string} _newPath Absolute or relative path. Also can be path to the Node.JS module. |
| 90 | + * @param {string} _sourcePath Absolute path (relative to root). |
110 | 91 | * @param {string} _trace |
111 | 92 | * @return {object} |
112 | 93 | */ |
113 | | -function fetch(_newPath, _relativeTo, _trace) { |
114 | | - const newPath = _newPath.replace(/^["']|["']$/g, ''); |
| 94 | +function fetch(_newPath, _sourcePath, _trace) { |
115 | 95 | const trace = _trace || String.fromCharCode(importNr++); |
116 | | - |
117 | | - const relativeDir = dirname(_relativeTo); |
118 | | - const rootRelativePath = resolve(relativeDir, newPath); |
119 | | - let fileRelativePath = resolve(join(rootDir, relativeDir), newPath); |
120 | | - |
121 | | - if (isModule(newPath)) { |
122 | | - fileRelativePath = require.resolve(newPath); |
123 | | - } |
124 | | - |
125 | | - const tokens = tokensByFile[fileRelativePath]; |
| 96 | + const newPath = removeQuotes(_newPath); |
| 97 | + // getting absolute path to the processing file |
| 98 | + const filename = /\w/.test(newPath[0]) |
| 99 | + ? require.resolve(newPath) |
| 100 | + : resolve(rootDir + dirname(_sourcePath), newPath); |
| 101 | + |
| 102 | + // checking cache |
| 103 | + let tokens = tokensByFile[filename]; |
126 | 104 | if (tokens) { |
127 | 105 | return tokens; |
128 | 106 | } |
129 | 107 |
|
130 | | - const source = readFileSync(fileRelativePath, 'utf-8'); |
131 | | - const { exportTokens, injectableSource } = load(source, rootRelativePath, trace, fetch); |
| 108 | + const rootRelativePath = sep + relative(rootDir, filename); |
| 109 | + const CSSSource = preProcess(readFileSync(filename, 'utf8')); |
| 110 | + |
| 111 | + const result = postcss(plugins.concat(new Parser({ fetch, trace }))) |
| 112 | + .process(CSSSource, {from: rootRelativePath}) |
| 113 | + .root; |
132 | 114 |
|
133 | | - tokensByFile[fileRelativePath] = exportTokens; |
| 115 | + tokens = result.tokens; |
| 116 | + tokensByFile[filename] = tokens; |
134 | 117 |
|
135 | | - if (typeof processCss === 'function') { |
136 | | - processCss(injectableSource); |
| 118 | + if (postProcess) { |
| 119 | + postProcess(result.toResult().css); |
137 | 120 | } |
138 | 121 |
|
139 | | - return exportTokens; |
| 122 | + return tokens; |
140 | 123 | } |
141 | 124 |
|
142 | | -// setting defaults |
143 | | -buildOptions(); |
144 | | - |
145 | | -hook(filename => fetch(`.${sep}${relative(rootDir, filename)}`, '/')); |
| 125 | +hook(filename => fetch(filename, sep + relative(rootDir, filename))); |
0 commit comments