-
-
Notifications
You must be signed in to change notification settings - Fork 606
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
fix: respected style field from package.json #1099
Merged
+1,492
−1,148
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b8e7732
fix: respected style field from package.json
cap-Bernardito e2205af
fix: respected style field from package.json
cap-Bernardito 4ae4e5c
fix: respected style field from package.json
cap-Bernardito 671b8d5
refactor: code
cap-Bernardito 00a90e1
refactor: code
cap-Bernardito 4d67b76
fix: resolve modules
cap-Bernardito ed56563
fix: resolve modules
cap-Bernardito 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 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 |
---|---|---|
|
@@ -16,3 +16,4 @@ Thumbs.db | |
.vscode | ||
*.sublime-project | ||
*.sublime-workspace | ||
/test/fixtures/import/import-absolute.css |
Large diffs are not rendered by default.
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 |
---|---|---|
|
@@ -47,9 +47,20 @@ export default function loader(content, map, meta) { | |
plugins.push(icssParser({ urlHandler })); | ||
|
||
if (options.import !== false && exportType === 'full') { | ||
const resolver = this.getResolve({ | ||
mainFields: ['css', 'style', 'main', '...'], | ||
mainFiles: ['index', '...'], | ||
extensions: ['.css'], | ||
restrictions: [/\.css$/i], | ||
conditionNames: ['style'], | ||
}); | ||
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. conditionNames: ['style'], |
||
|
||
plugins.push( | ||
importParser({ | ||
context: this.context, | ||
rootContext: this.rootContext, | ||
filter: getFilter(options.import, this.resourcePath), | ||
resolver, | ||
urlHandler, | ||
}) | ||
); | ||
|
@@ -125,6 +136,24 @@ export default function loader(content, map, meta) { | |
} | ||
} | ||
|
||
apiImports.sort((a, b) => a.index - b.index); | ||
|
||
/* | ||
* Order | ||
* CSS_LOADER_ICSS_IMPORT: [], | ||
* CSS_LOADER_AT_RULE_IMPORT: [], | ||
* CSS_LOADER_GET_URL_IMPORT: [], | ||
* CSS_LOADER_URL_IMPORT: [], | ||
* CSS_LOADER_URL_REPLACEMENT: [], | ||
* */ | ||
|
||
imports.sort((a, b) => { | ||
return ( | ||
(b.order < a.order) - (a.order < b.order) || | ||
(b.index < a.index) - (a.index < b.index) | ||
); | ||
}); | ||
|
||
const { localsConvention } = options; | ||
const esModule = | ||
typeof options.esModule !== 'undefined' ? options.esModule : false; | ||
|
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,132 +1,175 @@ | ||
import postcss from 'postcss'; | ||
import valueParser from 'postcss-value-parser'; | ||
import { isUrlRequest } from 'loader-utils'; | ||
|
||
import { normalizeUrl } from '../utils'; | ||
import { normalizeUrl, resolveRequests, isUrlRequestable } from '../utils'; | ||
|
||
const pluginName = 'postcss-import-parser'; | ||
|
||
export default postcss.plugin(pluginName, (options) => (css, result) => { | ||
const importsMap = new Map(); | ||
|
||
css.walkAtRules(/^import$/i, (atRule) => { | ||
// Convert only top-level @import | ||
if (atRule.parent.type !== 'root') { | ||
return; | ||
} | ||
|
||
// Nodes do not exists - `@import url('http://') :root {}` | ||
if (atRule.nodes) { | ||
result.warn( | ||
"It looks like you didn't end your @import statement correctly. Child nodes are attached to it.", | ||
{ node: atRule } | ||
); | ||
return new Promise((resolve, reject) => { | ||
const importsMap = new Map(); | ||
const tasks = []; | ||
|
||
return; | ||
} | ||
|
||
const { nodes } = valueParser(atRule.params); | ||
|
||
// No nodes - `@import ;` | ||
// Invalid type - `@import foo-bar;` | ||
if ( | ||
nodes.length === 0 || | ||
(nodes[0].type !== 'string' && nodes[0].type !== 'function') | ||
) { | ||
result.warn(`Unable to find uri in "${atRule.toString()}"`, { | ||
node: atRule, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
let isStringValue; | ||
let url; | ||
|
||
if (nodes[0].type === 'string') { | ||
isStringValue = true; | ||
url = nodes[0].value; | ||
} else if (nodes[0].type === 'function') { | ||
// Invalid function - `@import nourl(test.css);` | ||
if (nodes[0].value.toLowerCase() !== 'url') { | ||
result.warn(`Unable to find uri in "${atRule.toString()}"`, { | ||
node: atRule, | ||
}); | ||
// A counter is used instead of an index in callback css.walkAtRules because we mutate AST (atRule.remove()) | ||
let index = 0; | ||
|
||
css.walkAtRules(/^import$/i, (atRule) => { | ||
// Convert only top-level @import | ||
if (atRule.parent.type !== 'root') { | ||
return; | ||
} | ||
|
||
isStringValue = | ||
nodes[0].nodes.length !== 0 && nodes[0].nodes[0].type === 'string'; | ||
url = isStringValue | ||
? nodes[0].nodes[0].value | ||
: valueParser.stringify(nodes[0].nodes); | ||
} | ||
// Nodes do not exists - `@import url('http://') :root {}` | ||
if (atRule.nodes) { | ||
result.warn( | ||
"It looks like you didn't end your @import statement correctly. Child nodes are attached to it.", | ||
{ node: atRule } | ||
); | ||
|
||
// Empty url - `@import "";` or `@import url();` | ||
if (url.trim().length === 0) { | ||
result.warn(`Unable to find uri in "${atRule.toString()}"`, { | ||
node: atRule, | ||
}); | ||
return; | ||
} | ||
|
||
return; | ||
} | ||
const { nodes } = valueParser(atRule.params); | ||
|
||
const isRequestable = isUrlRequest(url); | ||
// No nodes - `@import ;` | ||
// Invalid type - `@import foo-bar;` | ||
if ( | ||
nodes.length === 0 || | ||
(nodes[0].type !== 'string' && nodes[0].type !== 'function') | ||
) { | ||
result.warn(`Unable to find uri in "${atRule.toString()}"`, { | ||
node: atRule, | ||
}); | ||
|
||
if (isRequestable) { | ||
url = normalizeUrl(url, isStringValue); | ||
return; | ||
} | ||
|
||
let isStringValue; | ||
let url; | ||
|
||
if (nodes[0].type === 'string') { | ||
isStringValue = true; | ||
url = nodes[0].value; | ||
} else if (nodes[0].type === 'function') { | ||
// Invalid function - `@import nourl(test.css);` | ||
if (nodes[0].value.toLowerCase() !== 'url') { | ||
result.warn(`Unable to find uri in "${atRule.toString()}"`, { | ||
node: atRule, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
isStringValue = | ||
nodes[0].nodes.length !== 0 && nodes[0].nodes[0].type === 'string'; | ||
url = isStringValue | ||
? nodes[0].nodes[0].value | ||
: valueParser.stringify(nodes[0].nodes); | ||
} | ||
|
||
// Empty url after normalize - `@import '\ | ||
// \ | ||
// \ | ||
// '; | ||
// Empty url - `@import "";` or `@import url();` | ||
if (url.trim().length === 0) { | ||
result.warn(`Unable to find uri in "${atRule.toString()}"`, { | ||
node: atRule, | ||
}); | ||
|
||
return; | ||
} | ||
} | ||
|
||
const media = valueParser.stringify(nodes.slice(1)).trim().toLowerCase(); | ||
let normalizedUrl; | ||
|
||
if (options.filter && !options.filter({ url, media })) { | ||
return; | ||
} | ||
const isRequestable = isUrlRequestable(url); | ||
|
||
atRule.remove(); | ||
if (isRequestable) { | ||
normalizedUrl = normalizeUrl(url, isStringValue, options.rootContext); | ||
|
||
if (isRequestable) { | ||
const importKey = url; | ||
let importName = importsMap.get(importKey); | ||
// Empty url after normalize - `@import '\ | ||
// \ | ||
// \ | ||
// '; | ||
if (normalizedUrl.trim().length === 0) { | ||
result.warn(`Unable to find uri in "${atRule.toString()}"`, { | ||
node: atRule, | ||
}); | ||
|
||
if (!importName) { | ||
importName = `___CSS_LOADER_AT_RULE_IMPORT_${importsMap.size}___`; | ||
importsMap.set(importKey, importName); | ||
|
||
result.messages.push({ | ||
type: 'import', | ||
value: { | ||
importName, | ||
url: options.urlHandler ? options.urlHandler(url) : url, | ||
}, | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
result.messages.push({ | ||
type: 'api-import', | ||
value: { type: 'internal', importName, media }, | ||
}); | ||
const media = valueParser.stringify(nodes.slice(1)).trim().toLowerCase(); | ||
|
||
return; | ||
} | ||
if ( | ||
options.filter && | ||
!options.filter({ url: normalizedUrl || url, media }) | ||
) { | ||
return; | ||
} | ||
|
||
result.messages.push({ | ||
pluginName, | ||
type: 'api-import', | ||
value: { type: 'external', url, media }, | ||
atRule.remove(); | ||
|
||
index += 1; | ||
|
||
tasks.push( | ||
Promise.resolve(index).then(async (currentIndex) => { | ||
if (isRequestable) { | ||
const importKey = normalizedUrl; | ||
let importName = importsMap.get(importKey); | ||
|
||
if (!importName) { | ||
importName = `___CSS_LOADER_AT_RULE_IMPORT_${importsMap.size}___`; | ||
importsMap.set(importKey, importName); | ||
|
||
const { resolver, context } = options; | ||
|
||
let resolvedUrl; | ||
|
||
try { | ||
resolvedUrl = await resolveRequests(resolver, context, [ | ||
...new Set([normalizedUrl, url]), | ||
]); | ||
} catch (error) { | ||
throw error; | ||
} | ||
|
||
result.messages.push({ | ||
type: 'import', | ||
value: { | ||
// 'CSS_LOADER_AT_RULE_IMPORT' | ||
order: 1, | ||
importName, | ||
url: options.urlHandler | ||
? options.urlHandler(resolvedUrl) | ||
: resolvedUrl, | ||
index: currentIndex, | ||
}, | ||
}); | ||
} | ||
|
||
result.messages.push({ | ||
type: 'api-import', | ||
value: { | ||
type: 'internal', | ||
importName, | ||
media, | ||
index: currentIndex, | ||
}, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
result.messages.push({ | ||
pluginName, | ||
type: 'api-import', | ||
value: { type: 'external', url, media, index: currentIndex }, | ||
}); | ||
}) | ||
); | ||
}); | ||
|
||
Promise.all(tasks).then( | ||
() => resolve(), | ||
(error) => reject(error) | ||
); | ||
}); | ||
}); |
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.
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.
This restriction broke
@import
for files with an extension other than.css
(#1164). Can we remove it (#1165)?