Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: css-modules/postcss-modules-extract-imports
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7f4028cdd764aaa7a1b458567fe1febe555cdcd7
Choose a base ref
...
head repository: css-modules/postcss-modules-extract-imports
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 797d678bcc6cc7e07dbbc8323b3e9eec8e68b805
Choose a head ref
  • 5 commits
  • 17 files changed
  • 5 contributors

Commits on May 24, 2017

  1. Add jest and prettier (#145)

    TrySound authored and mightyaleksey committed May 24, 2017

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e017f1c View commit details

Commits on Oct 30, 2018

  1. Added LICENSE file (#149)

    warmans authored and mightyaleksey committed Oct 30, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    6d55f5c View commit details
  2. up version

    mightyaleksey committed Oct 30, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e286ada View commit details
  3. update postcss to version 7 (#150)

    EECOLOR authored and mightyaleksey committed Oct 30, 2018
    Copy the full SHA
    1ab8eb1 View commit details
  4. remove node@4 support (#151)

    * remove node@4 support
    
    * remove babel
    
    * add engine field
    
    * up lock file
    
    * add options description
    mightyaleksey authored Oct 30, 2018
    Copy the full SHA
    797d678 View commit details
Showing with 2,997 additions and 284 deletions.
  1. +0 −23 .eslintrc
  2. +1 −3 .gitignore
  3. +0 −2 .travis.yml
  4. +5 −0 LICENSE
  5. +17 −1 README.md
  6. +22 −23 package.json
  7. +87 −76 src/index.js
  8. +29 −25 src/topologicalSort.js
  9. +0 −32 test/check-import-order.js
  10. +23 −0 test/check-import-order.test.js
  11. +0 −19 test/custom-import-name.js
  12. +19 −0 test/custom-import-name.test.js
  13. +0 −28 test/test-cases.js
  14. +25 −0 test/test.js
  15. +0 −52 test/topologicalSort.js
  16. +64 −0 test/topologicalSort.test.js
  17. +2,705 −0 yarn.lock
23 changes: 0 additions & 23 deletions .eslintrc

This file was deleted.

4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.DS_Store
node_modules
coverage
lib
node_modules
yarn.lock
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
language: node_js
node_js:
- "4"
- "6"
- "node"
script: npm run travis

after_success:
- cat ./coverage/lcov.info | node_modules/.bin/coveralls --verbose
5 changes: 5 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -31,11 +31,27 @@ into:
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 build
npm test
```

45 changes: 22 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
{
"name": "postcss-modules-extract-imports",
"version": "1.2.0",
"version": "2.0.0",
"description": "A CSS Modules transform to extract local aliases for inline imports",
"main": "lib/index.js",
"main": "src/index.js",
"engines": {
"node": ">= 6"
},
"files": [
"src"
],
"scripts": {
"lint": "eslint src",
"build": "babel --out-dir lib src",
"watch": "chokidar src -c 'npm run build'",
"posttest": "npm run lint && npm run build",
"test": "mocha --compilers js:babel/register",
"autotest": "chokidar src test -c 'npm test'",
"precover": "npm run lint && npm run build",
"cover": "babel-istanbul cover node_modules/.bin/_mocha",
"travis": "npm run cover -- --report lcovonly",
"prepublish": "npm run build"
"test": "jest --coverage",
"precommit": "lint-staged",
"prepublish": "yarn run test"
},
"lint-staged": {
"*.js": [
"prettier --single-quote --no-semi --write",
"git add"
]
},
"repository": {
"type": "git",
@@ -24,27 +29,21 @@
"postcss",
"plugin"
],
"files": [
"lib"
],
"author": "Glen Maddern",
"license": "ISC",
"bugs": {
"url": "https://github.com/css-modules/postcss-modules-extract-imports/issues"
},
"homepage": "https://github.com/css-modules/postcss-modules-extract-imports",
"dependencies": {
"postcss": "^6.0.1"
"postcss": "^7.0.5"
},
"devDependencies": {
"babel": "^5.4.7",
"babel-eslint": "^7.2.2",
"babel-istanbul": "^0.4.0",
"babelify": "^7.3.0",
"chokidar-cli": "^1.0.1",
"codecov.io": "^0.1.2",
"coveralls": "^2.11.2",
"eslint": "^1.5.0",
"mocha": "^3.1.2"
"husky": "^0.13.3",
"jest": "^20.0.3",
"lint-staged": "^3.4.2",
"prettier": "^1.3.1"
}
}
163 changes: 87 additions & 76 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import postcss from 'postcss';
import topologicalSort from './topologicalSort';
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 declWhitelist = ['composes']
const declFilter = new RegExp(`^(${declWhitelist.join('|')})$`)
const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)'|(global))$/
const icssImport = /^:import\((?:"([^"]+)"|'([^']+)')\)/

const VISITED_MARKER = 1;
const VISITED_MARKER = 1

function createParentName(rule, root) {
return `__${root.index(rule.parent)}_${rule.selector}`;
return `__${root.index(rule.parent)}_${rule.selector}`
}

function serializeImports(imports) {
return imports.map(importPath => '`' + importPath + '`').join(', ');
return imports.map(importPath => '`' + importPath + '`').join(', ')
}

/**
@@ -38,132 +38,143 @@ function serializeImports(imports) {
* }
*/
function addImportToGraph(importId, parentId, graph, visited) {
const siblingsId = parentId + '_' + 'siblings';
const visitedId = parentId + '_' + importId;
const siblingsId = parentId + '_' + 'siblings'
const visitedId = parentId + '_' + importId

if (visited[visitedId] !== VISITED_MARKER) {
if (!Array.isArray(visited[siblingsId])) visited[siblingsId] = [];
if (!Array.isArray(visited[siblingsId])) visited[siblingsId] = []

const siblings = visited[siblingsId];
const siblings = visited[siblingsId]

if (Array.isArray(graph[importId]))
graph[importId] = graph[importId].concat(siblings);
else
graph[importId] = siblings.slice();
graph[importId] = graph[importId].concat(siblings)
else graph[importId] = siblings.slice()

visited[visitedId] = VISITED_MARKER;
siblings.push(importId);
visited[visitedId] = VISITED_MARKER
siblings.push(importId)
}
}

const processor = postcss.plugin('modules-extract-imports', function (options = {}) {
const failOnWrongOrder = options.failOnWrongOrder;
module.exports = postcss.plugin('modules-extract-imports', function(
options = {}
) {
const failOnWrongOrder = options.failOnWrongOrder

return css => {
const graph = {};
const visited = {};
const graph = {}
const visited = {}

const existingImports = {};
const importDecls = {};
const imports = {};
const existingImports = {}
const importDecls = {}
const imports = {}

let importIndex = 0;
let importIndex = 0

const createImportedName = typeof options.createImportedName !== 'function'
? (importName/*, path*/) => `i__imported_${importName.replace(/\W/g, '_')}_${importIndex++}`
: options.createImportedName;
? (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);
const matches = icssImport.exec(rule.selector)

if (matches) {
const [/*match*/, doubleQuotePath, singleQuotePath] = matches;
const importPath = doubleQuotePath || singleQuotePath;
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches
const importPath = doubleQuotePath || singleQuotePath

addImportToGraph(importPath, 'root', graph, visited);
addImportToGraph(importPath, 'root', graph, visited)

existingImports[importPath] = rule;
existingImports[importPath] = rule
}
});
})

// Find any declaration that supports imports
css.walkDecls(declFilter, decl => {
let matches = decl.value.match(matchImports);
let tmpSymbols;
let matches = decl.value.match(matchImports)
let tmpSymbols

if (matches) {
let [/*match*/, symbols, doubleQuotePath, singleQuotePath, global] = 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})`);
tmpSymbols = symbols.split(/\s+/).map(s => `global(${s})`)
} else {
const importPath = doubleQuotePath || singleQuotePath;
const parentRule = createParentName(decl.parent, css);
const importPath = doubleQuotePath || singleQuotePath
const parentRule = createParentName(decl.parent, css)

addImportToGraph(importPath, parentRule, graph, visited);
addImportToGraph(importPath, parentRule, graph, visited)

importDecls[importPath] = decl;
imports[importPath] = imports[importPath] || {};
importDecls[importPath] = decl
imports[importPath] = imports[importPath] || {}

tmpSymbols = symbols.split(/\s+/).map(s => {
if (!imports[importPath][s]) {
imports[importPath][s] = createImportedName(s, importPath);
imports[importPath][s] = createImportedName(s, importPath)
}

return imports[importPath][s];
});
return imports[importPath][s]
})
}

decl.value = tmpSymbols.join(' ');
decl.value = tmpSymbols.join(' ')
}
});
})

const importsOrder = topologicalSort(graph, failOnWrongOrder);
const importsOrder = topologicalSort(graph, failOnWrongOrder)

if (importsOrder instanceof Error) {
const importPath = importsOrder.nodes.find(importPath => importDecls.hasOwnProperty(importPath));
const decl = importDecls[importPath];
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) + '.';
const errMsg =
'Failed to resolve order of composed modules ' +
serializeImports(importsOrder.nodes) +
'.'

throw decl.error(errMsg, {
plugin: 'modules-extract-imports',
word: 'composes',
});
word: 'composes'
})
}

let lastImportRule;
let lastImportRule
importsOrder.forEach(path => {
const importedSymbols = imports[path];
let rule = existingImports[path];
const importedSymbols = imports[path]
let rule = existingImports[path]

if (!rule && importedSymbols) {
rule = postcss.rule({
selector: `:import("${path}")`,
raws: {after: '\n'},
});
raws: { after: '\n' }
})

if (lastImportRule)
css.insertAfter(lastImportRule, rule);
else
css.prepend(rule);
if (lastImportRule) css.insertAfter(lastImportRule, rule)
else css.prepend(rule)
}

lastImportRule = rule;
lastImportRule = rule

if (!importedSymbols) return;
if (!importedSymbols) return

Object.keys(importedSymbols).forEach(importedSymbol => {
rule.append(postcss.decl({
value: importedSymbol,
prop: importedSymbols[importedSymbol],
raws: {before: '\n '},
}));
});
});
};
});

export default processor;
rule.append(
postcss.decl({
value: importedSymbol,
prop: importedSymbols[importedSymbol],
raws: { before: '\n ' }
})
)
})
})
}
})
Loading