| 7 | const inlineLoader = '!css2json-loader?useForImports!'; |
| 8 | |
| 9 | export default function loader(content: string, map: any) { |
| 10 | const options = this.getOptions() || {}; |
| 11 | const inline = !!options.useForImports; |
| 12 | const requirePrefix = inline ? inlineLoader : ''; |
| 13 | |
| 14 | const ast = parse(content); |
| 15 | |
| 16 | // todo: revise if this is necessary |
| 17 | // todo: perhaps use postCSS and just build imports into a single file? |
| 18 | let dependencies = []; |
| 19 | getAndRemoveImportRules(ast) |
| 20 | .map(extractUrlFromRule) |
| 21 | .map(createRequireUri) |
| 22 | .forEach(({ uri, requireURI }) => { |
| 23 | dependencies.push(`require("${requirePrefix}${requireURI}")`); |
| 24 | }); |
| 25 | |
| 26 | const str = JSON.stringify(ast, (k, v) => (k === 'position' ? undefined : v)); |
| 27 | |
| 28 | // map.mappings = map.mappings.replace(/;{2,}/, '') |
| 29 | |
| 30 | const code = dedent` |
| 31 | /* CSS2JSON */ |
| 32 | ${dependencies.join('\n')} |
| 33 | const ___CSS2JSON_LOADER_EXPORT___ = ${str} |
| 34 | export default ___CSS2JSON_LOADER_EXPORT___ |
| 35 | `; |
| 36 | this.callback( |
| 37 | null, |
| 38 | code, //`${dependencies.join('\n')}module.exports = ${str};`, |
| 39 | map |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | function getImportRules(ast: Stylesheet): Import[] { |
| 44 | if (!ast || (<any>ast).type !== 'stylesheet' || !ast.stylesheet) { |