-
Notifications
You must be signed in to change notification settings - Fork 62
/
stylable-parser.ts
85 lines (80 loc) · 3.21 KB
/
stylable-parser.ts
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
import { Imported, isAsset, makeAbsolute, Stylable } from '@stylable/core';
import path from 'path';
import webpack from 'webpack';
import { isLoadedByLoaders } from './is-loaded-by-loaders';
import {
StylableAssetDependency,
StylableExportsDependency,
StylableImportDependency
} from './stylable-dependencies';
const stylableExtension = /\.st\.css$/;
export class StylableParser {
constructor(
private stylable: Stylable,
private compilation: webpack.compilation.Compilation,
private useWeakDeps: boolean
) {}
public parse(_source: string, state: any) {
if (
isLoadedByLoaders(state.module, () => {
this.compilation.warnings.push(
`Loading a Stylable stylesheet via webpack loaders is not supported` +
` and may cause runtime errors.\n"${state.module.rawRequest}" in "${state.module.issuer.resource}"`
);
})
) {
return state;
}
const meta = this.stylable.process(state.module.resource);
state.module.buildInfo.stylableMeta = meta;
// state.module.buildMeta.exportsType = 'namespace';
meta.urls
.filter(url => isAsset(url))
.forEach(asset => {
const absPath = makeAbsolute(
asset,
(this.compilation as any).options.context,
path.dirname(state.module.resource)
);
state.module.buildInfo.fileDependencies.add(absPath);
state.module.addDependency(new StylableAssetDependency(absPath));
});
state.module.addDependency(new StylableExportsDependency(['default']));
meta.imports.forEach(stylableImport => {
state.module.buildInfo.fileDependencies.add(stylableImport.from);
if (stylableImport.fromRelative.match(stylableExtension)) {
const importRef = {
defaultImport: stylableImport.defaultExport,
names: []
};
const dep = this.useWeakDeps
? StylableImportDependency.createWeak(
stylableImport.fromRelative,
state.module,
importRef
)
: new StylableImportDependency(stylableImport.fromRelative, importRef);
state.module.addDependency(dep);
this.addChildDeps(stylableImport);
}
// TODO: handle js dependencies?
});
return state;
}
public addChildDeps(stylableImport: Imported) {
try {
this.stylable.process(stylableImport.from);
// .imports.forEach(childImport => {
// const fileDependencies = state.module.buildInfo.fileDependencies;
// if (childImport.fromRelative.match(stylableExtension)) {
// if (!fileDependencies.has(childImport.from)) {
// fileDependencies.add(childImport.from);
// this.addChildDeps(childImport/*, this.stylable*/);
// }
// }
// });
} catch {
/* */
}
}
}