1 | "use strict"
|
2 |
|
3 | const path = require("path")
|
4 |
|
5 |
|
6 | const applyConditions = require("./lib/apply-conditions")
|
7 | const applyRaws = require("./lib/apply-raws")
|
8 | const applyStyles = require("./lib/apply-styles")
|
9 | const loadContent = require("./lib/load-content")
|
10 | const parseStyles = require("./lib/parse-styles")
|
11 | const resolveId = require("./lib/resolve-id")
|
12 |
|
13 | function 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 |
|
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 |
|
68 | AtImport.postcss = true
|
69 |
|
70 | module.exports = AtImport
|