-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to prepare the plugin to work in browsers
- Loading branch information
Showing
9 changed files
with
111 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
let fileSystem = { | ||
readFile: () => { | ||
throw Error("readFile not implemented"); | ||
}, | ||
|
||
writeFile: () => { | ||
throw Error("writeFile not implemented"); | ||
}, | ||
}; | ||
|
||
export function setFileSystem(fs) { | ||
fileSystem.readFile = fs.readFile | ||
fileSystem.writeFile = fs.writeFile | ||
} | ||
|
||
export function getFileSystem() { | ||
return fileSystem; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { writeFile } from "fs"; | ||
import { getFileSystem } from "./fs"; | ||
|
||
export default function saveJSON(cssFile, json) { | ||
return new Promise((resolve, reject) => { | ||
const { writeFile } = getFileSystem(); | ||
writeFile(`${cssFile}.json`, JSON.stringify(json), (e) => (e ? reject(e) : resolve(json))); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import extractImports from "postcss-modules-extract-imports"; | ||
import genericNames from "generic-names"; | ||
import localByDefault from "postcss-modules-local-by-default"; | ||
import modulesScope from "postcss-modules-scope"; | ||
import stringHash from "string-hash"; | ||
import values from "postcss-modules-values"; | ||
|
||
export const behaviours = { | ||
LOCAL: "local", | ||
GLOBAL: "global", | ||
}; | ||
|
||
export function getDefaultPlugins({ behaviour, generateScopedName, exportGlobals }) { | ||
const scope = modulesScope({ generateScopedName, exportGlobals }); | ||
|
||
const plugins = { | ||
[behaviours.LOCAL]: [values, localByDefault({ mode: "local" }), extractImports, scope], | ||
[behaviours.GLOBAL]: [values, localByDefault({ mode: "global" }), extractImports, scope], | ||
}; | ||
|
||
return plugins[behaviour]; | ||
} | ||
|
||
function isValidBehaviour(behaviour) { | ||
return ( | ||
Object.keys(behaviours) | ||
.map((key) => behaviours[key]) | ||
.indexOf(behaviour) > -1 | ||
); | ||
} | ||
|
||
export function getDefaultScopeBehaviour(scopeBehaviour) { | ||
return scopeBehaviour && isValidBehaviour(scopeBehaviour) ? scopeBehaviour : behaviours.LOCAL; | ||
} | ||
|
||
function generateScopedNameDefault(name, filename, css) { | ||
const i = css.indexOf(`.${name}`); | ||
const lineNumber = css.substr(0, i).split(/[\r\n]/).length; | ||
const hash = stringHash(css).toString(36).substr(0, 5); | ||
|
||
return `_${name}_${hash}_${lineNumber}`; | ||
} | ||
|
||
export function getScopedNameGenerator(generateScopedName, hashPrefix) { | ||
const scopedNameGenerator = generateScopedName || generateScopedNameDefault; | ||
|
||
if (typeof scopedNameGenerator === "function") { | ||
return scopedNameGenerator; | ||
} | ||
|
||
return genericNames(scopedNameGenerator, { | ||
context: process.cwd(), | ||
hashPrefix: hashPrefix, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters