-
Notifications
You must be signed in to change notification settings - Fork 115
Refactor path resolve #116
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,20 +5,18 @@ var fs = require("fs") | |
| var path = require("path") | ||
|
|
||
| var assign = require("object-assign") | ||
| var resolve = require("resolve") | ||
| var postcss = require("postcss") | ||
| var helpers = require("postcss-message-helpers") | ||
| var glob = require("glob") | ||
| var parseImports = require("./lib/parse-imports") | ||
| var resolveMedia = require("./lib/resolve-media") | ||
| var resolveId = require("./lib/resolve-id") | ||
|
|
||
| /** | ||
| * Constants | ||
| */ | ||
| var moduleDirectories = [ | ||
| "web_modules", | ||
| "node_modules", | ||
| "bower_components", | ||
| ] | ||
|
|
||
|
Contributor
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 should be added in the CHANGELOG since it's a breaking changes (and we should add how to add bower support like it was). |
||
| /** | ||
|
|
@@ -67,7 +65,6 @@ function AtImport(options) { | |
|
|
||
| var state = { | ||
| importedFiles: {}, | ||
| ignoredAtRules: [], | ||
| hashFiles: {}, | ||
| } | ||
| if (opts.from) { | ||
|
|
@@ -85,8 +82,8 @@ function AtImport(options) { | |
| createProcessor(result, options.plugins) | ||
| ) | ||
|
|
||
| function onParseEnd() { | ||
| addIgnoredAtRulesOnTop(styles, state.ignoredAtRules) | ||
| function onParseEnd(ignored) { | ||
| addIgnoredAtRulesOnTop(styles, ignored) | ||
|
|
||
| if ( | ||
| typeof opts.addDependencyTo === "object" && | ||
|
|
@@ -141,20 +138,32 @@ function parseStyles( | |
| }) | ||
|
|
||
| var importResults = imports.map(function(instance) { | ||
| return helpers.try(function transformAtImport() { | ||
| return readAtImport( | ||
| result, | ||
| instance.node, | ||
| instance, | ||
| options, | ||
| state, | ||
| media, | ||
| processor | ||
| ) | ||
| }, instance.node.source) | ||
| return readAtImport( | ||
| result, | ||
| instance.node, | ||
| instance, | ||
| options, | ||
| state, | ||
| media, | ||
| processor | ||
| ) | ||
| }) | ||
|
|
||
| return Promise.all(importResults) | ||
| return Promise.all(importResults).then(function(result) { | ||
| // Flatten ignored instances | ||
| return result.reduce(function(ignored, item) { | ||
| if (Array.isArray(item)) { | ||
| item = item.filter(function(instance) { | ||
| return instance | ||
| }) | ||
| ignored = ignored.concat(item) | ||
| } | ||
| else if (item) { | ||
| ignored.push(item) | ||
| } | ||
| return ignored | ||
| }, []) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -212,15 +221,14 @@ function parseGlob(imports, instance, options) { | |
| * @param {Array} state | ||
| */ | ||
| function addIgnoredAtRulesOnTop(styles, ignoredAtRules) { | ||
| var i = ignoredAtRules.length | ||
| if (i) { | ||
| while (i--) { | ||
| var ignored = ignoredAtRules[i][1] | ||
| ignored.node.params = ignored.fullUri + | ||
| (ignored.media.length ? " " + ignored.media.join(", ") : "") | ||
|
|
||
| styles.prepend(ignored.node) | ||
| } | ||
| var i = ignoredAtRules.length - 1 | ||
| while (i !== -1) { | ||
| var ignored = ignoredAtRules[i] | ||
| ignored.node.params = ignored.fullUri + | ||
| (ignored.media.length ? " " + ignored.media.join(", ") : "") | ||
|
|
||
| styles.prepend(ignored.node) | ||
| i -= 1 | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -247,51 +255,54 @@ function readAtImport( | |
| if (parsedAtImport.uri.match(/^(?:[a-z]+:)?\/\//i)) { | ||
| parsedAtImport.media = media | ||
|
|
||
| // save | ||
| state.ignoredAtRules.push([ atRule, parsedAtImport ]) | ||
|
|
||
| // detach | ||
| atRule.remove() | ||
|
|
||
| return Promise.resolve() | ||
| return Promise.resolve(parsedAtImport) | ||
| } | ||
|
|
||
| var dir = atRule.source && atRule.source.input && atRule.source.input.file | ||
| ? path.dirname(path.resolve(options.root, atRule.source.input.file)) | ||
| : options.root | ||
|
|
||
| addInputToPath(options) | ||
| var resolvedFilename = resolveFilename( | ||
| parsedAtImport.uri, | ||
| options.root, | ||
| options.path, | ||
| atRule.source, | ||
| options.resolve | ||
| ) | ||
|
|
||
| if (options.skipDuplicates) { | ||
| // skip files already imported at the same scope | ||
| if ( | ||
| state.importedFiles[resolvedFilename] && | ||
| state.importedFiles[resolvedFilename][media] | ||
| ) { | ||
| atRule.remove() | ||
| return Promise.resolve() | ||
| return Promise.resolve().then(function() { | ||
| if (options.resolve) { | ||
| return options.resolve(parsedAtImport.uri, dir, options) | ||
| } | ||
| return resolveId(parsedAtImport.uri, dir, options.path) | ||
| }).then(function(resolvedFilename) { | ||
| if (options.skipDuplicates) { | ||
| // skip files already imported at the same scope | ||
| if ( | ||
| state.importedFiles[resolvedFilename] && | ||
| state.importedFiles[resolvedFilename][media] | ||
| ) { | ||
| atRule.remove() | ||
| return Promise.resolve() | ||
| } | ||
|
|
||
| // save imported files to skip them next time | ||
| if (!state.importedFiles[resolvedFilename]) { | ||
| state.importedFiles[resolvedFilename] = {} | ||
| // save imported files to skip them next time | ||
| if (!state.importedFiles[resolvedFilename]) { | ||
| state.importedFiles[resolvedFilename] = {} | ||
| } | ||
| state.importedFiles[resolvedFilename][media] = true | ||
| } | ||
| state.importedFiles[resolvedFilename][media] = true | ||
| } | ||
|
|
||
| return readImportedContent( | ||
| result, | ||
| atRule, | ||
| parsedAtImport, | ||
| assign({}, options), | ||
| resolvedFilename, | ||
| state, | ||
| media, | ||
| processor | ||
| ) | ||
| return readImportedContent( | ||
| result, | ||
| atRule, | ||
| parsedAtImport, | ||
| assign({}, options), | ||
| resolvedFilename, | ||
| state, | ||
| media, | ||
| processor | ||
| ) | ||
| }).catch(function(err) { | ||
| result.warn(err.message, { node: atRule }) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -368,14 +379,18 @@ function readImportedContent( | |
| processor | ||
| ) | ||
|
|
||
| return parsedResult.then(function() { | ||
| var instances | ||
|
|
||
| return parsedResult.then(function(result) { | ||
| instances = result | ||
| return processor.process(newStyles) | ||
| }) | ||
| .then(function(newResult) { | ||
| result.messages = result.messages.concat(newResult.messages) | ||
| }) | ||
| .then(function() { | ||
| insertRules(atRule, parsedAtImport, newStyles) | ||
| return instances | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -414,63 +429,6 @@ function insertRules(atRule, parsedAtImport, newStyles) { | |
| atRule.replaceWith(newNodes) | ||
| } | ||
|
|
||
| /** | ||
| * Check if a file exists | ||
| * | ||
| * @param {String} name | ||
| */ | ||
| function resolveFilename(name, root, paths, source, resolver) { | ||
| var dir = source && source.input && source.input.file | ||
| ? path.dirname(path.resolve(root, source.input.file)) | ||
| : root | ||
|
|
||
| try { | ||
| var resolveOpts = { | ||
| basedir: dir, | ||
| moduleDirectory: moduleDirectories.concat(paths), | ||
| paths: paths, | ||
| extensions: [ ".css" ], | ||
| packageFilter: function processPackage(pkg) { | ||
| pkg.main = pkg.style || "index.css" | ||
| return pkg | ||
| }, | ||
| } | ||
| var file | ||
| resolver = resolver || resolve.sync | ||
| try { | ||
| file = resolver(name, resolveOpts) | ||
| } | ||
| catch (e) { | ||
| // fix to try relative files on windows with "./" | ||
| // if it's look like it doesn't start with a relative path already | ||
| // if (name.match(/^\.\.?/)) {throw e} | ||
| try { | ||
| file = resolver("./" + name, resolveOpts) | ||
| } | ||
| catch (err) { | ||
| // LAST HOPE | ||
| if (!paths.some(function(dir2) { | ||
| file = path.join(dir2, name) | ||
| return fs.existsSync(file) | ||
| })) { | ||
| throw err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return path.normalize(file) | ||
| } | ||
| catch (e) { | ||
| throw new Error( | ||
| "Failed to find '" + name + "' from " + root + | ||
| "\n in [ " + | ||
| "\n " + paths.join(",\n ") + | ||
| "\n ]", | ||
| source | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Read the contents of a file | ||
| * | ||
|
|
||
This file contains hidden or 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,70 @@ | ||
| var fs = require("fs") | ||
| var path = require("path") | ||
| var resolve = require("resolve") | ||
|
|
||
| var moduleDirectories = [ | ||
| "web_modules", | ||
| "node_modules", | ||
| ] | ||
|
|
||
| function isFilePromise(path) { | ||
| return new Promise(function(resolve, reject) { | ||
| fs.stat(path, function(err, stat) { | ||
| if (err) { | ||
| if (err.code === "ENOENT") { | ||
| return resolve(false) | ||
| } | ||
| return reject(err) | ||
| } | ||
| resolve(stat.isFile() || stat.isFIFO()) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| function resolvePromise(id, opts) { | ||
| return new Promise(function(res, rej) { | ||
| resolve(id, opts, function(err, path) { | ||
| if (err) { | ||
| return rej(err) | ||
| } | ||
| res(path) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| module.exports = function(id, base, paths) { | ||
| var resolveOpts = { | ||
| basedir: base, | ||
| moduleDirectory: moduleDirectories, | ||
| paths: paths, | ||
| extensions: [ ".css" ], | ||
| packageFilter: function processPackage(pkg) { | ||
| pkg.main = pkg.style || "index.css" | ||
| return pkg | ||
| }, | ||
| } | ||
| return resolvePromise(id, resolveOpts).catch(function() { | ||
| // fix to try relative files on windows with "./" | ||
| // if it's look like it doesn't start with a relative path already | ||
| // if (id.match(/^\.\.?/)) {throw e} | ||
| return resolvePromise("./" + id, resolveOpts) | ||
| }).catch(function() { | ||
| // LAST HOPE | ||
| return Promise.all(paths.map(function(p) { | ||
| return isFilePromise(path.resolve(p, id)) | ||
| })).then(function(results) { | ||
| for (var i = 0; i < results.length; i += 1) { | ||
| if (results[i]) { | ||
| return path.resolve(paths[i], id) | ||
| } | ||
| } | ||
|
|
||
| throw new Error([ | ||
| "Failed to find '" + id + "'", | ||
| "in [ ", | ||
| " " + paths.join(",\n "), | ||
| "]", | ||
| ].join("\n ")) | ||
| }) | ||
| }) | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
fyi, you can just add bower support by it to the
pathoption.So I would use
"bower_components" not supported by default anymore, use "path" option to add it back.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.
bower support is also bower.json support, which is not simple with main field as array.
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.
I know that, but bower is more a downloader, so I guess lots of people just point to the right css file when they import it (eg:
@import "jquery-slider-fx-2016/slider.css") so we don't really care about it. The idea is just to inform people that are currently using the feature we are breaking how to upgrade easily.