Skip to content

Use yarn and test with jest #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
lib/
coverage
lib
Empty file removed .npmignore
Empty file.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ node_js:
- "4"
- "6"
- "node"
script: npm run travis

before_install:
- '[ "${TRAVIS_NODE_VERSION}" != "0.10" ] || npm install -g npm'
51 changes: 31 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,32 @@
"version": "1.3.0",
"description": "PostCSS plugin for CSS Modules to pass arbitrary values between your module files",
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"lint": "standard src test",
"build": "babel --out-dir lib src",
"autotest": "chokidar src test -c 'npm test'",
"test": "mocha --compilers js:babel-core/register",
"posttest": "npm run lint && npm run build",
"travis": "npm run test",
"prepublish": "npm run build"
"test": "jest --coverage",
"precommit": "lint-staged",
"prepublish": "yarn run test && yarn run build"
},
"lint-staged": {
"*.js": [
"prettier --single-quote --no-semi --write",
"git add"
]
},
"babel": {
"presets": [
[
"env",
{
"targets": {
"node": 4
}
}
]
]
},
"repository": {
"type": "git",
Expand All @@ -29,23 +47,16 @@
"homepage": "https://github.com/css-modules/postcss-modules-values#readme",
"devDependencies": {
"babel-cli": "^6.5.2",
"babel-core": "^6.5.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.3.13",
"chokidar": "^1.2.0",
"mocha": "^3.0.2",
"standard": "^8.4.0"
"babel-jest": "^20.0.3",
"babel-preset-env": "^1.5.0",
"husky": "^0.13.3",
"jest": "^20.0.3",
"lint-staged": "^3.4.2",
"prettier": "^1.3.1",
"strip-indent": "^2.0.0"
},
"dependencies": {
"icss-replace-symbols": "^1.1.0",
"postcss": "^6.0.1"
},
"babel": {
"presets": [
"es2015"
],
"plugins": [
"add-module-exports"
]
}
}
63 changes: 37 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import postcss from 'postcss'
import replaceSymbols, {replaceAll} from 'icss-replace-symbols'
const postcss = require('postcss')
const { default: replaceSymbols, replaceAll } = require('icss-replace-symbols')

const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
let options = {}
let importIndex = 0
let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)
let createImportedName =
(options && options.createImportedName) ||
((importName /*, path*/) =>
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)

export default postcss.plugin('postcss-modules-values', () => (css, result) => {
module.exports = postcss.plugin('postcss-modules-values', () => (
css,
result
) => {
let importAliases = []
let definitions = {}

const addDefinition = atRule => {
let matches
while (matches = matchValueDefinition.exec(atRule.params)) {
let [/*match*/, key, value] = matches
while ((matches = matchValueDefinition.exec(atRule.params))) {
let [, key, value] = matches
// Add to the definitions, knowing that values can refer to each other
definitions[key] = replaceAll(definitions, value)
atRule.remove()
Expand All @@ -25,20 +31,23 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
const addImport = atRule => {
let matches = matchImports.exec(atRule.params)
if (matches) {
let [/*match*/, aliases, path] = matches
let [, aliases, path] = matches
// We can use constants for path names
if (definitions[path]) path = definitions[path]
let imports = aliases.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1').split(/\s*,\s*/).map(alias => {
let tokens = matchImport.exec(alias)
if (tokens) {
let [/*match*/, theirName, myName = theirName] = tokens
let importedName = createImportedName(myName)
definitions[myName] = importedName
return { theirName, importedName }
} else {
throw new Error(`@import statement "${alias}" is invalid!`)
}
})
let imports = aliases
.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1')
.split(/\s*,\s*/)
.map(alias => {
let tokens = matchImport.exec(alias)
if (tokens) {
let [, /*match*/ theirName, myName = theirName] = tokens
let importedName = createImportedName(myName)
definitions[myName] = importedName
return { theirName, importedName }
} else {
throw new Error(`@import statement "${alias}" is invalid!`)
}
})
importAliases.push({ path, imports })
atRule.remove()
}
Expand All @@ -59,11 +68,13 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {

/* We want to export anything defined by now, but don't add it to the CSS yet or
it well get picked up by the replacement stuff */
let exportDeclarations = Object.keys(definitions).map(key => postcss.decl({
value: definitions[key],
prop: key,
raws: { before: "\n " }
}))
let exportDeclarations = Object.keys(definitions).map(key =>
postcss.decl({
value: definitions[key],
prop: key,
raws: { before: '\n ' }
})
)

/* If we have no definitions, don't continue */
if (!Object.keys(definitions).length) return
Expand All @@ -75,7 +86,7 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
if (exportDeclarations.length > 0) {
let exportRule = postcss.rule({
selector: `:export`,
raws: { after: "\n" }
raws: { after: '\n' }
})
exportRule.append(exportDeclarations)
css.prepend(exportRule)
Expand All @@ -85,13 +96,13 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
importAliases.reverse().forEach(({ path, imports }) => {
let importRule = postcss.rule({
selector: `:import(${path})`,
raws: { after: "\n" }
raws: { after: '\n' }
})
imports.forEach(({ theirName, importedName }) => {
importRule.append({
value: theirName,
prop: importedName,
raws: { before: "\n " }
raws: { before: '\n ' }
})
})

Expand Down
158 changes: 0 additions & 158 deletions test/index.js

This file was deleted.

Loading