draft: refactor project so it sits on top of a standalone app
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
Copyright 2015 Glen Maddern
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
# CSS Modules: Extract Imports
|
||||
|
||||
[](https://travis-ci.org/css-modules/postcss-modules-extract-imports)
|
||||
|
||||
Transforms:
|
||||
|
||||
```css
|
||||
:local(.continueButton) {
|
||||
composes: button from "library/button.css";
|
||||
color: green;
|
||||
}
|
||||
```
|
||||
|
||||
into:
|
||||
|
||||
```css
|
||||
:import("library/button.css") {
|
||||
button: __tmp_487387465fczSDGHSABb;
|
||||
}
|
||||
:local(.continueButton) {
|
||||
composes: __tmp_487387465fczSDGHSABb;
|
||||
color: green;
|
||||
}
|
||||
```
|
||||
|
||||
## Specification
|
||||
|
||||
- Only a certain whitelist of properties are inspected. Currently, that whitelist is `['composes']` alone.
|
||||
- An extend-import has the following format:
|
||||
```
|
||||
composes: className [... className] from "path/to/file.css";
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
- `failOnWrongOrder` `bool` generates exception for unpredictable imports order.
|
||||
|
||||
```css
|
||||
.aa {
|
||||
composes: b from './b.css';
|
||||
composes: c from './c.css';
|
||||
}
|
||||
|
||||
.bb {
|
||||
/* "b.css" should be before "c.css" in this case */
|
||||
composes: c from './c.css';
|
||||
composes: b from './b.css';
|
||||
}
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
```
|
||||
npm install
|
||||
npm test
|
||||
```
|
||||
|
||||
[](https://travis-ci.org/css-modules/postcss-modules-extract-imports)
|
||||
|
||||
* Lines: [](https://coveralls.io/r/css-modules/postcss-modules-extract-imports?branch=master)
|
||||
* Statements: [](http://codecov.io/github/css-modules/postcss-modules-extract-imports?branch=master)
|
||||
|
||||
## Development
|
||||
|
||||
- `npm watch` will watch `src` for changes and rebuild
|
||||
- `npm autotest` will watch `src` and `test` for changes and retest
|
||||
|
||||
## License
|
||||
|
||||
ISC
|
||||
|
||||
## With thanks
|
||||
|
||||
- Mark Dalgleish
|
||||
- Tobias Koppers
|
||||
- Guy Bedford
|
||||
|
||||
---
|
||||
Glen Maddern, 2015.
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"_from": "postcss-modules-extract-imports@^2.0.0",
|
||||
"_id": "postcss-modules-extract-imports@2.0.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-LaYLDNS4SG8Q5WAWqIJgdHPJrDDr/Lv775rMBFUbgjTz6j34lUznACHcdRWroPvXANP2Vj7yNK57vp9eFqzLWQ==",
|
||||
"_location": "/postcss-modules-extract-imports",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "postcss-modules-extract-imports@^2.0.0",
|
||||
"name": "postcss-modules-extract-imports",
|
||||
"escapedName": "postcss-modules-extract-imports",
|
||||
"rawSpec": "^2.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^2.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/css-loader"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/postcss-modules-extract-imports/-/postcss-modules-extract-imports-2.0.0.tgz",
|
||||
"_shasum": "818719a1ae1da325f9832446b01136eeb493cd7e",
|
||||
"_spec": "postcss-modules-extract-imports@^2.0.0",
|
||||
"_where": "O:\\zero\\zero.Web\\node_modules\\css-loader",
|
||||
"author": {
|
||||
"name": "Glen Maddern"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/css-modules/postcss-modules-extract-imports/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"postcss": "^7.0.5"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "A CSS Modules transform to extract local aliases for inline imports",
|
||||
"devDependencies": {
|
||||
"codecov.io": "^0.1.2",
|
||||
"coveralls": "^2.11.2",
|
||||
"husky": "^0.13.3",
|
||||
"jest": "^20.0.3",
|
||||
"lint-staged": "^3.4.2",
|
||||
"prettier": "^1.3.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
},
|
||||
"files": [
|
||||
"src"
|
||||
],
|
||||
"homepage": "https://github.com/css-modules/postcss-modules-extract-imports",
|
||||
"keywords": [
|
||||
"css-modules",
|
||||
"postcss",
|
||||
"plugin"
|
||||
],
|
||||
"license": "ISC",
|
||||
"lint-staged": {
|
||||
"*.js": [
|
||||
"prettier --single-quote --no-semi --write",
|
||||
"git add"
|
||||
]
|
||||
},
|
||||
"main": "src/index.js",
|
||||
"name": "postcss-modules-extract-imports",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/css-modules/postcss-modules-extract-imports.git"
|
||||
},
|
||||
"scripts": {
|
||||
"precommit": "lint-staged",
|
||||
"prepublish": "yarn run test",
|
||||
"test": "jest --coverage"
|
||||
},
|
||||
"version": "2.0.0"
|
||||
}
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
const postcss = require('postcss')
|
||||
const topologicalSort = require('./topologicalSort')
|
||||
|
||||
const declWhitelist = ['composes']
|
||||
const declFilter = new RegExp(`^(${declWhitelist.join('|')})$`)
|
||||
const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)'|(global))$/
|
||||
const icssImport = /^:import\((?:"([^"]+)"|'([^']+)')\)/
|
||||
|
||||
const VISITED_MARKER = 1
|
||||
|
||||
function createParentName(rule, root) {
|
||||
return `__${root.index(rule.parent)}_${rule.selector}`
|
||||
}
|
||||
|
||||
function serializeImports(imports) {
|
||||
return imports.map(importPath => '`' + importPath + '`').join(', ')
|
||||
}
|
||||
|
||||
/**
|
||||
* :import('G') {}
|
||||
*
|
||||
* Rule
|
||||
* composes: ... from 'A'
|
||||
* composes: ... from 'B'
|
||||
|
||||
* Rule
|
||||
* composes: ... from 'A'
|
||||
* composes: ... from 'A'
|
||||
* composes: ... from 'C'
|
||||
*
|
||||
* Results in:
|
||||
*
|
||||
* graph: {
|
||||
* G: [],
|
||||
* A: [],
|
||||
* B: ['A'],
|
||||
* C: ['A'],
|
||||
* }
|
||||
*/
|
||||
function addImportToGraph(importId, parentId, graph, visited) {
|
||||
const siblingsId = parentId + '_' + 'siblings'
|
||||
const visitedId = parentId + '_' + importId
|
||||
|
||||
if (visited[visitedId] !== VISITED_MARKER) {
|
||||
if (!Array.isArray(visited[siblingsId])) visited[siblingsId] = []
|
||||
|
||||
const siblings = visited[siblingsId]
|
||||
|
||||
if (Array.isArray(graph[importId]))
|
||||
graph[importId] = graph[importId].concat(siblings)
|
||||
else graph[importId] = siblings.slice()
|
||||
|
||||
visited[visitedId] = VISITED_MARKER
|
||||
siblings.push(importId)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = postcss.plugin('modules-extract-imports', function(
|
||||
options = {}
|
||||
) {
|
||||
const failOnWrongOrder = options.failOnWrongOrder
|
||||
|
||||
return css => {
|
||||
const graph = {}
|
||||
const visited = {}
|
||||
|
||||
const existingImports = {}
|
||||
const importDecls = {}
|
||||
const imports = {}
|
||||
|
||||
let importIndex = 0
|
||||
|
||||
const createImportedName = typeof options.createImportedName !== 'function'
|
||||
? (importName /*, path*/) =>
|
||||
`i__imported_${importName.replace(/\W/g, '_')}_${importIndex++}`
|
||||
: options.createImportedName
|
||||
|
||||
// Check the existing imports order and save refs
|
||||
css.walkRules(rule => {
|
||||
const matches = icssImport.exec(rule.selector)
|
||||
|
||||
if (matches) {
|
||||
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches
|
||||
const importPath = doubleQuotePath || singleQuotePath
|
||||
|
||||
addImportToGraph(importPath, 'root', graph, visited)
|
||||
|
||||
existingImports[importPath] = rule
|
||||
}
|
||||
})
|
||||
|
||||
// Find any declaration that supports imports
|
||||
css.walkDecls(declFilter, decl => {
|
||||
let matches = decl.value.match(matchImports)
|
||||
let tmpSymbols
|
||||
|
||||
if (matches) {
|
||||
let [
|
||||
,
|
||||
/*match*/ symbols,
|
||||
doubleQuotePath,
|
||||
singleQuotePath,
|
||||
global
|
||||
] = matches
|
||||
|
||||
if (global) {
|
||||
// Composing globals simply means changing these classes to wrap them in global(name)
|
||||
tmpSymbols = symbols.split(/\s+/).map(s => `global(${s})`)
|
||||
} else {
|
||||
const importPath = doubleQuotePath || singleQuotePath
|
||||
const parentRule = createParentName(decl.parent, css)
|
||||
|
||||
addImportToGraph(importPath, parentRule, graph, visited)
|
||||
|
||||
importDecls[importPath] = decl
|
||||
imports[importPath] = imports[importPath] || {}
|
||||
|
||||
tmpSymbols = symbols.split(/\s+/).map(s => {
|
||||
if (!imports[importPath][s]) {
|
||||
imports[importPath][s] = createImportedName(s, importPath)
|
||||
}
|
||||
|
||||
return imports[importPath][s]
|
||||
})
|
||||
}
|
||||
|
||||
decl.value = tmpSymbols.join(' ')
|
||||
}
|
||||
})
|
||||
|
||||
const importsOrder = topologicalSort(graph, failOnWrongOrder)
|
||||
|
||||
if (importsOrder instanceof Error) {
|
||||
const importPath = importsOrder.nodes.find(importPath =>
|
||||
importDecls.hasOwnProperty(importPath)
|
||||
)
|
||||
const decl = importDecls[importPath]
|
||||
|
||||
const errMsg =
|
||||
'Failed to resolve order of composed modules ' +
|
||||
serializeImports(importsOrder.nodes) +
|
||||
'.'
|
||||
|
||||
throw decl.error(errMsg, {
|
||||
plugin: 'modules-extract-imports',
|
||||
word: 'composes'
|
||||
})
|
||||
}
|
||||
|
||||
let lastImportRule
|
||||
importsOrder.forEach(path => {
|
||||
const importedSymbols = imports[path]
|
||||
let rule = existingImports[path]
|
||||
|
||||
if (!rule && importedSymbols) {
|
||||
rule = postcss.rule({
|
||||
selector: `:import("${path}")`,
|
||||
raws: { after: '\n' }
|
||||
})
|
||||
|
||||
if (lastImportRule) css.insertAfter(lastImportRule, rule)
|
||||
else css.prepend(rule)
|
||||
}
|
||||
|
||||
lastImportRule = rule
|
||||
|
||||
if (!importedSymbols) return
|
||||
|
||||
Object.keys(importedSymbols).forEach(importedSymbol => {
|
||||
rule.append(
|
||||
postcss.decl({
|
||||
value: importedSymbol,
|
||||
prop: importedSymbols[importedSymbol],
|
||||
raws: { before: '\n ' }
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
const PERMANENT_MARKER = 2
|
||||
const TEMPORARY_MARKER = 1
|
||||
|
||||
function createError(node, graph) {
|
||||
const er = new Error("Nondeterministic import's order")
|
||||
|
||||
const related = graph[node]
|
||||
const relatedNode = related.find(
|
||||
relatedNode => graph[relatedNode].indexOf(node) > -1
|
||||
)
|
||||
|
||||
er.nodes = [node, relatedNode]
|
||||
|
||||
return er
|
||||
}
|
||||
|
||||
function walkGraph(node, graph, state, result, strict) {
|
||||
if (state[node] === PERMANENT_MARKER) return
|
||||
if (state[node] === TEMPORARY_MARKER) {
|
||||
if (strict) return createError(node, graph)
|
||||
return
|
||||
}
|
||||
|
||||
state[node] = TEMPORARY_MARKER
|
||||
|
||||
const children = graph[node]
|
||||
const length = children.length
|
||||
|
||||
for (let i = 0; i < length; ++i) {
|
||||
const er = walkGraph(children[i], graph, state, result, strict)
|
||||
if (er instanceof Error) return er
|
||||
}
|
||||
|
||||
state[node] = PERMANENT_MARKER
|
||||
|
||||
result.push(node)
|
||||
}
|
||||
|
||||
function topologicalSort(graph, strict) {
|
||||
const result = []
|
||||
const state = {}
|
||||
|
||||
const nodes = Object.keys(graph)
|
||||
const length = nodes.length
|
||||
|
||||
for (let i = 0; i < length; ++i) {
|
||||
const er = walkGraph(nodes[i], graph, state, result, strict)
|
||||
if (er instanceof Error) return er
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports = topologicalSort
|
||||
Reference in New Issue
Block a user