-
Notifications
You must be signed in to change notification settings - Fork 86
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
WIP: Refactor loader dependencies #119
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
export default (imported, importee, projectRoot) => { | ||
return new Promise((resolve, reject) => { | ||
let importeeDir = path.dirname(importee), | ||
absolutePath = path.resolve( | ||
path.join(projectRoot, importeeDir), | ||
imported | ||
); | ||
export function resolveRelativeImport(importee, importer, projectRoot) { | ||
const importerDir = path.dirname(importer); | ||
let pathFromProjectRoot = path.resolve(projectRoot, importerDir, importee); | ||
|
||
// if the path is not relative or absolute, try to resolve it in node_modules | ||
if (imported[0] !== "." && imported[0] !== "/") { | ||
try { | ||
absolutePath = require.resolve(imported); | ||
} catch (e) { | ||
// noop | ||
} | ||
// if the path is not relative or absolute, try to resolve it in node_modules | ||
if (importee[0] !== "." && importee[0] !== "/") { | ||
try { | ||
pathFromProjectRoot = require.resolve(importee); | ||
} catch (e) { | ||
// noop | ||
} | ||
} | ||
|
||
fs.readFile(absolutePath, "utf-8", (err, source) => { | ||
if (err) reject(err); | ||
else resolve(source); | ||
return pathFromProjectRoot; | ||
} | ||
|
||
export default { | ||
resolveId: (importee, importer, projectRoot) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice idea to use |
||
return resolveRelativeImport(importee, importer, projectRoot) | ||
}, | ||
resolve: (importee) => { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(importee, "utf-8", (err, source) => { | ||
if (err) reject(err); | ||
else resolve(source); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
// Copied from https://github.com/css-modules/css-modules-loader-core | ||
|
||
const importRegexp = /^:import\((.+)\)$/; | ||
import replaceSymbols from "icss-replace-symbols"; | ||
import postcss from 'postcss'; | ||
import path from "path"; | ||
|
||
const importRegexp = /^:import\((.+)\)$/; | ||
|
||
export default class Parser { | ||
constructor(root, plugins, pathFetcher, trace, ctx = {}) { | ||
constructor(root, plugins, loader, trace, ctx = {}) { | ||
this.root = root; | ||
this.plugins = plugins; | ||
this.pathFetcher = pathFetcher; | ||
this.loader = loader; | ||
this.plugin = this.plugin.bind(this); | ||
this.exportTokens = {}; | ||
this.translations = {}; | ||
this.trace = trace; | ||
this.trace = trace || ''; | ||
|
||
this.sources = ctx.sources || {}; | ||
this.traces = ctx.traces || {}; | ||
|
@@ -34,10 +33,11 @@ export default class Parser { | |
|
||
fetchAllImports(css) { | ||
let imports = []; | ||
let trace = 0; | ||
css.each((node) => { | ||
if (node.type == "rule" && node.selector.match(importRegexp)) { | ||
imports.push( | ||
this.fetchImport(node, css.source.input.from, imports.length) | ||
this.fetchImport(node, css.source.input.from, trace++) | ||
); | ||
} | ||
}); | ||
|
@@ -72,36 +72,30 @@ export default class Parser { | |
|
||
async fetchImport(importNode, relativeTo, depNr) { | ||
const file = importNode.selector.match(importRegexp)[1].replace(/^["']|["']$/g, ""), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's split the declarations into separate statements everywhere for the readability sake |
||
depTrace = this.trace + String.fromCharCode(depNr), | ||
relativeDir = path.dirname(relativeTo), | ||
rootRelativePath = path.resolve(relativeDir, file), | ||
fileRelativePath = path.resolve( | ||
path.join(this.root, relativeDir), | ||
file | ||
), | ||
subParser = new Parser(this.root, this.plugins, this.pathFetcher, depTrace, { | ||
depTrace = this.trace + String.fromCharCode(65 + depNr), // 65 = A, making the trace more readable | ||
id = this.loader.resolveId(file, relativeTo, this.root), | ||
subParser = new Parser(this.root, this.plugins, this.loader, depTrace, { | ||
sources: this.sources, | ||
traces: this.traces, | ||
tokensByFile: this.tokensByFile | ||
}), | ||
tokens = this.tokensByFile[fileRelativePath]; | ||
}); | ||
let tokens = this.tokensByFile[id]; | ||
|
||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be cleaner now 👍 |
||
let exports = tokens; | ||
if (!exports) { | ||
const content = await this.pathFetcher(file, relativeTo, this.root); | ||
if (!tokens) { | ||
const content = await this.loader.resolve(id); | ||
const { css } = await postcss(this.plugins.concat([subParser.plugin()])) | ||
.process(content, { from: "/" + rootRelativePath }); | ||
exports = subParser.exportTokens; | ||
.process(content, { from: id }); | ||
tokens = subParser.exportTokens; | ||
|
||
this.sources[fileRelativePath] = css; | ||
this.traces[depTrace] = fileRelativePath; | ||
this.tokensByFile[fileRelativePath] = exports; | ||
this.sources[id] = css; | ||
this.traces[depTrace] = id; | ||
this.tokensByFile[id] = tokens; | ||
} | ||
|
||
importNode.each((decl) => { | ||
if (decl.type == "decl") { | ||
this.translations[decl.prop] = exports[decl.value]; | ||
this.translations[decl.prop] = tokens[decl.value]; | ||
} | ||
}); | ||
importNode.remove(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be an internal function. Let's not export it then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, of course. good catch :)