UNPKG

1.66 kBJavaScriptView Raw
1"use strict"
2// builtin tooling
3const path = require("path")
4
5// internal tooling
6const applyConditions = require("./lib/apply-conditions")
7const applyRaws = require("./lib/apply-raws")
8const applyStyles = require("./lib/apply-styles")
9const loadContent = require("./lib/load-content")
10const parseStyles = require("./lib/parse-styles")
11const resolveId = require("./lib/resolve-id")
12
13function AtImport(options) {
14 options = {
15 root: process.cwd(),
16 path: [],
17 skipDuplicates: true,
18 resolve: resolveId,
19 load: loadContent,
20 plugins: [],
21 addModulesDirectories: [],
22 warnOnEmpty: true,
23 ...options,
24 }
25
26 options.root = path.resolve(options.root)
27
28 // convert string to an array of a single element
29 if (typeof options.path === "string") options.path = [options.path]
30
31 if (!Array.isArray(options.path)) options.path = []
32
33 options.path = options.path.map(p => path.resolve(options.root, p))
34
35 return {
36 postcssPlugin: "postcss-import",
37 async Once(styles, { result, atRule, postcss }) {
38 const state = {
39 importedFiles: {},
40 hashFiles: {},
41 }
42
43 if (styles.source?.input?.file) {
44 state.importedFiles[styles.source.input.file] = {}
45 }
46
47 if (options.plugins && !Array.isArray(options.plugins)) {
48 throw new Error("plugins option must be an array")
49 }
50
51 const bundle = await parseStyles(
52 result,
53 styles,
54 options,
55 state,
56 [],
57 [],
58 postcss,
59 )
60
61 applyRaws(bundle)
62 applyConditions(bundle, atRule)
63 applyStyles(bundle, styles)
64 },
65 }
66}
67
68AtImport.postcss = true
69
70module.exports = AtImport