Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from css-modules/move-deps-into-the-lib
Copy css-modules-loader-core sources into the repo
- Loading branch information
Showing
11 changed files
with
2,239 additions
and
1,731 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,5 @@ sudo: false | |
language: node_js | ||
node_js: | ||
- node | ||
- 11 | ||
- 12 | ||
- 10 | ||
- 8 |
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 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copied from https://github.com/css-modules/css-modules-loader-core | ||
|
||
import postcss from "postcss"; | ||
import localByDefault from "postcss-modules-local-by-default"; | ||
import extractImports from "postcss-modules-extract-imports"; | ||
import scope from "postcss-modules-scope"; | ||
import values from "postcss-modules-values"; | ||
|
||
import Parser from "./parser"; | ||
|
||
export default class Core { | ||
constructor(plugins) { | ||
this.plugins = plugins || Core.defaultPlugins; | ||
} | ||
|
||
load(sourceString, sourcePath, trace, pathFetcher) { | ||
let parser = new Parser(pathFetcher, trace); | ||
|
||
return postcss(this.plugins.concat([parser.plugin])) | ||
.process(sourceString, { from: "/" + sourcePath }) | ||
.then((result) => { | ||
return { | ||
injectableSource: result.css, | ||
exportTokens: parser.exportTokens, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
// These four plugins are aliased under this package for simplicity. | ||
Core.values = values; | ||
Core.localByDefault = localByDefault; | ||
Core.extractImports = extractImports; | ||
Core.scope = scope; | ||
|
||
Core.defaultPlugins = [values, localByDefault, extractImports, scope]; |
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,91 @@ | ||
// Copied from https://github.com/css-modules/css-modules-loader-core | ||
|
||
import Core from "./index.js"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
// Sorts dependencies in the following way: | ||
// AAA comes before AA and A | ||
// AB comes after AA and before A | ||
// All Bs come after all As | ||
// This ensures that the files are always returned in the following order: | ||
// - In the order they were required, except | ||
// - After all their dependencies | ||
const traceKeySorter = (a, b) => { | ||
if (a.length < b.length) { | ||
return a < b.substring(0, a.length) ? -1 : 1; | ||
} else if (a.length > b.length) { | ||
return a.substring(0, b.length) <= b ? -1 : 1; | ||
} else { | ||
return a < b ? -1 : 1; | ||
} | ||
}; | ||
|
||
export default class FileSystemLoader { | ||
constructor(root, plugins) { | ||
this.root = root; | ||
this.sources = {}; | ||
this.traces = {}; | ||
this.importNr = 0; | ||
this.core = new Core(plugins); | ||
this.tokensByFile = {}; | ||
} | ||
|
||
fetch(_newPath, relativeTo, _trace) { | ||
let newPath = _newPath.replace(/^["']|["']$/g, ""), | ||
trace = _trace || String.fromCharCode(this.importNr++); | ||
return new Promise((resolve, reject) => { | ||
let relativeDir = path.dirname(relativeTo), | ||
rootRelativePath = path.resolve(relativeDir, newPath), | ||
fileRelativePath = path.resolve( | ||
path.join(this.root, relativeDir), | ||
newPath | ||
); | ||
|
||
// if the path is not relative or absolute, try to resolve it in node_modules | ||
if (newPath[0] !== "." && newPath[0] !== "/") { | ||
try { | ||
fileRelativePath = require.resolve(newPath); | ||
} catch (e) { | ||
// noop | ||
} | ||
} | ||
|
||
const tokens = this.tokensByFile[fileRelativePath]; | ||
if (tokens) { | ||
return resolve(tokens); | ||
} | ||
|
||
fs.readFile(fileRelativePath, "utf-8", (err, source) => { | ||
if (err) reject(err); | ||
this.core | ||
.load(source, rootRelativePath, trace, this.fetch.bind(this)) | ||
.then(({ injectableSource, exportTokens }) => { | ||
this.sources[fileRelativePath] = injectableSource; | ||
this.traces[trace] = fileRelativePath; | ||
this.tokensByFile[fileRelativePath] = exportTokens; | ||
resolve(exportTokens); | ||
}, reject); | ||
}); | ||
}); | ||
} | ||
|
||
get finalSource() { | ||
const traces = this.traces; | ||
const sources = this.sources; | ||
let written = new Set(); | ||
|
||
return Object.keys(traces) | ||
.sort(traceKeySorter) | ||
.map((key) => { | ||
const filename = traces[key]; | ||
if (written.has(filename)) { | ||
return null; | ||
} | ||
written.add(filename); | ||
|
||
return sources[filename]; | ||
}) | ||
.join(""); | ||
} | ||
} |
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,74 @@ | ||
// Copied from https://github.com/css-modules/css-modules-loader-core | ||
|
||
const importRegexp = /^:import\((.+)\)$/; | ||
import replaceSymbols from "icss-replace-symbols"; | ||
|
||
export default class Parser { | ||
constructor(pathFetcher, trace) { | ||
this.pathFetcher = pathFetcher; | ||
this.plugin = this.plugin.bind(this); | ||
this.exportTokens = {}; | ||
this.translations = {}; | ||
this.trace = trace; | ||
} | ||
|
||
plugin(css) { | ||
return Promise.all(this.fetchAllImports(css)) | ||
.then(() => this.linkImportedSymbols(css)) | ||
.then(() => this.extractExports(css)); | ||
} | ||
|
||
fetchAllImports(css) { | ||
let imports = []; | ||
css.each((node) => { | ||
if (node.type == "rule" && node.selector.match(importRegexp)) { | ||
imports.push( | ||
this.fetchImport(node, css.source.input.from, imports.length) | ||
); | ||
} | ||
}); | ||
return imports; | ||
} | ||
|
||
linkImportedSymbols(css) { | ||
replaceSymbols(css, this.translations); | ||
} | ||
|
||
extractExports(css) { | ||
css.each((node) => { | ||
if (node.type == "rule" && node.selector == ":export") | ||
this.handleExport(node); | ||
}); | ||
} | ||
|
||
handleExport(exportNode) { | ||
exportNode.each((decl) => { | ||
if (decl.type == "decl") { | ||
Object.keys(this.translations).forEach((translation) => { | ||
decl.value = decl.value.replace( | ||
translation, | ||
this.translations[translation] | ||
); | ||
}); | ||
this.exportTokens[decl.prop] = decl.value; | ||
} | ||
}); | ||
exportNode.remove(); | ||
} | ||
|
||
fetchImport(importNode, relativeTo, depNr) { | ||
let file = importNode.selector.match(importRegexp)[1], | ||
depTrace = this.trace + String.fromCharCode(depNr); | ||
return this.pathFetcher(file, relativeTo, depTrace).then( | ||
(exports) => { | ||
importNode.each((decl) => { | ||
if (decl.type == "decl") { | ||
this.translations[decl.prop] = exports[decl.value]; | ||
} | ||
}); | ||
importNode.remove(); | ||
}, | ||
(err) => console.log(err) | ||
); | ||
} | ||
} |
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 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
Oops, something went wrong.