draft: refactor project so it sits on top of a standalone app
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "importParser", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _postcssImportParser.default;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "icssParser", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _postcssIcssParser.default;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "urlParser", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _postcssUrlParser.default;
|
||||
}
|
||||
});
|
||||
|
||||
var _postcssImportParser = _interopRequireDefault(require("./postcss-import-parser"));
|
||||
|
||||
var _postcssIcssParser = _interopRequireDefault(require("./postcss-icss-parser"));
|
||||
|
||||
var _postcssUrlParser = _interopRequireDefault(require("./postcss-url-parser"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _postcss = _interopRequireDefault(require("postcss"));
|
||||
|
||||
var _icssUtils = require("icss-utils");
|
||||
|
||||
var _loaderUtils = require("loader-utils");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
const pluginName = 'postcss-icss-parser';
|
||||
|
||||
function normalizeIcssImports(icssImports) {
|
||||
return Object.keys(icssImports).reduce((accumulator, url) => {
|
||||
const tokensMap = icssImports[url];
|
||||
const tokens = Object.keys(tokensMap);
|
||||
|
||||
if (tokens.length === 0) {
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
const normalizedUrl = (0, _loaderUtils.urlToRequest)(url);
|
||||
|
||||
if (!accumulator[normalizedUrl]) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
accumulator[normalizedUrl] = tokensMap;
|
||||
} else {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
accumulator[normalizedUrl] = { ...accumulator[normalizedUrl],
|
||||
...tokensMap
|
||||
};
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
}, {});
|
||||
}
|
||||
|
||||
var _default = _postcss.default.plugin(pluginName, () => function process(css, result) {
|
||||
const importReplacements = Object.create(null);
|
||||
const {
|
||||
icssImports,
|
||||
icssExports
|
||||
} = (0, _icssUtils.extractICSS)(css);
|
||||
const normalizedIcssImports = normalizeIcssImports(icssImports);
|
||||
Object.keys(normalizedIcssImports).forEach((url, importIndex) => {
|
||||
const importName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}___`;
|
||||
result.messages.push({
|
||||
pluginName,
|
||||
type: 'import',
|
||||
value: {
|
||||
type: 'icss-import',
|
||||
importName,
|
||||
url
|
||||
}
|
||||
});
|
||||
const tokenMap = normalizedIcssImports[url];
|
||||
const tokens = Object.keys(tokenMap);
|
||||
tokens.forEach((token, replacementIndex) => {
|
||||
const replacementName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}_REPLACEMENT_${replacementIndex}___`;
|
||||
const localName = tokenMap[token];
|
||||
importReplacements[token] = replacementName;
|
||||
result.messages.push({
|
||||
pluginName,
|
||||
type: 'replacer',
|
||||
value: {
|
||||
type: 'icss-import',
|
||||
importName,
|
||||
replacementName,
|
||||
localName
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if (Object.keys(importReplacements).length > 0) {
|
||||
(0, _icssUtils.replaceSymbols)(css, importReplacements);
|
||||
}
|
||||
|
||||
Object.keys(icssExports).forEach(name => {
|
||||
const value = (0, _icssUtils.replaceValueSymbols)(icssExports[name], importReplacements);
|
||||
result.messages.push({
|
||||
pluginName,
|
||||
type: 'export',
|
||||
value: {
|
||||
name,
|
||||
value
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
exports.default = _default;
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _postcss = _interopRequireDefault(require("postcss"));
|
||||
|
||||
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
||||
|
||||
var _loaderUtils = require("loader-utils");
|
||||
|
||||
var _utils = require("../utils");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
const pluginName = 'postcss-import-parser';
|
||||
|
||||
function getParsedValue(node) {
|
||||
if (node.type === 'function' && node.value.toLowerCase() === 'url') {
|
||||
const {
|
||||
nodes
|
||||
} = node;
|
||||
const isStringValue = nodes.length !== 0 && nodes[0].type === 'string';
|
||||
const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
|
||||
return {
|
||||
url,
|
||||
isStringValue
|
||||
};
|
||||
}
|
||||
|
||||
if (node.type === 'string') {
|
||||
const url = node.value;
|
||||
return {
|
||||
url,
|
||||
isStringValue: true
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseImport(params) {
|
||||
const {
|
||||
nodes
|
||||
} = (0, _postcssValueParser.default)(params);
|
||||
|
||||
if (nodes.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const value = getParsedValue(nodes[0]);
|
||||
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let {
|
||||
url
|
||||
} = value;
|
||||
|
||||
if (url.trim().length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((0, _loaderUtils.isUrlRequest)(url)) {
|
||||
const {
|
||||
isStringValue
|
||||
} = value;
|
||||
url = (0, _utils.normalizeUrl)(url, isStringValue);
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
media: _postcssValueParser.default.stringify(nodes.slice(1)).trim().toLowerCase()
|
||||
};
|
||||
}
|
||||
|
||||
function walkAtRules(css, result, filter) {
|
||||
const items = [];
|
||||
css.walkAtRules(/^import$/i, atRule => {
|
||||
// Convert only top-level @import
|
||||
if (atRule.parent.type !== 'root') {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const parsed = parseImport(atRule.params);
|
||||
|
||||
if (!parsed) {
|
||||
// eslint-disable-next-line consistent-return
|
||||
return result.warn(`Unable to find uri in '${atRule.toString()}'`, {
|
||||
node: atRule
|
||||
});
|
||||
}
|
||||
|
||||
if (filter && !filter(parsed)) {
|
||||
return;
|
||||
}
|
||||
|
||||
atRule.remove();
|
||||
items.push(parsed);
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
var _default = _postcss.default.plugin(pluginName, options => function process(css, result) {
|
||||
const items = walkAtRules(css, result, options.filter);
|
||||
items.forEach(item => {
|
||||
const {
|
||||
url,
|
||||
media
|
||||
} = item;
|
||||
result.messages.push({
|
||||
pluginName,
|
||||
type: 'import',
|
||||
value: {
|
||||
type: '@import',
|
||||
url,
|
||||
media
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
exports.default = _default;
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _postcss = _interopRequireDefault(require("postcss"));
|
||||
|
||||
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
||||
|
||||
var _utils = require("../utils");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
const pluginName = 'postcss-url-parser';
|
||||
const isUrlFunc = /url/i;
|
||||
const isImageSetFunc = /^(?:-webkit-)?image-set$/i;
|
||||
const needParseDecl = /(?:url|(?:-webkit-)?image-set)\(/i;
|
||||
|
||||
function getNodeFromUrlFunc(node) {
|
||||
return node.nodes && node.nodes[0];
|
||||
}
|
||||
|
||||
function walkUrls(parsed, callback) {
|
||||
parsed.walk(node => {
|
||||
if (node.type !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isUrlFunc.test(node.value)) {
|
||||
const {
|
||||
nodes
|
||||
} = node;
|
||||
const isStringValue = nodes.length !== 0 && nodes[0].type === 'string';
|
||||
const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
|
||||
callback(getNodeFromUrlFunc(node), url, false, isStringValue); // Do not traverse inside `url`
|
||||
// eslint-disable-next-line consistent-return
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isImageSetFunc.test(node.value)) {
|
||||
node.nodes.forEach(nNode => {
|
||||
const {
|
||||
type,
|
||||
value
|
||||
} = nNode;
|
||||
|
||||
if (type === 'function' && isUrlFunc.test(value)) {
|
||||
const {
|
||||
nodes
|
||||
} = nNode;
|
||||
const isStringValue = nodes.length !== 0 && nodes[0].type === 'string';
|
||||
const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
|
||||
callback(getNodeFromUrlFunc(nNode), url, false, isStringValue);
|
||||
}
|
||||
|
||||
if (type === 'string') {
|
||||
callback(nNode, value, true, true);
|
||||
}
|
||||
}); // Do not traverse inside `image-set`
|
||||
// eslint-disable-next-line consistent-return
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getUrlsFromValue(value, result, filter, decl) {
|
||||
if (!needParseDecl.test(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parsed = (0, _postcssValueParser.default)(value);
|
||||
const urls = [];
|
||||
walkUrls(parsed, (node, url, needQuotes, isStringValue) => {
|
||||
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
|
||||
result.warn(`Unable to find uri in '${decl ? decl.toString() : value}'`, {
|
||||
node: decl
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (filter && !filter(url)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const splittedUrl = url.split(/(\?)?#/);
|
||||
const [urlWithoutHash, singleQuery, hashValue] = splittedUrl;
|
||||
const hash = singleQuery || hashValue ? `${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}` : '';
|
||||
const normalizedUrl = (0, _utils.normalizeUrl)(urlWithoutHash, isStringValue);
|
||||
urls.push({
|
||||
node,
|
||||
url: normalizedUrl,
|
||||
hash,
|
||||
needQuotes
|
||||
});
|
||||
}); // eslint-disable-next-line consistent-return
|
||||
|
||||
return {
|
||||
parsed,
|
||||
urls
|
||||
};
|
||||
}
|
||||
|
||||
function walkDecls(css, result, filter) {
|
||||
const items = [];
|
||||
css.walkDecls(decl => {
|
||||
const item = getUrlsFromValue(decl.value, result, filter, decl);
|
||||
|
||||
if (!item || item.urls.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
items.push({
|
||||
decl,
|
||||
parsed: item.parsed,
|
||||
urls: item.urls
|
||||
});
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
function flatten(array) {
|
||||
return array.reduce((a, b) => a.concat(b), []);
|
||||
}
|
||||
|
||||
function collectUniqueUrlsWithNodes(array) {
|
||||
return array.reduce((accumulator, currentValue) => {
|
||||
const {
|
||||
url,
|
||||
needQuotes,
|
||||
hash,
|
||||
node
|
||||
} = currentValue;
|
||||
const found = accumulator.find(item => url === item.url && needQuotes === item.needQuotes && hash === item.hash);
|
||||
|
||||
if (!found) {
|
||||
accumulator.push({
|
||||
url,
|
||||
hash,
|
||||
needQuotes,
|
||||
nodes: [node]
|
||||
});
|
||||
} else {
|
||||
found.nodes.push(node);
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
}, []);
|
||||
}
|
||||
|
||||
var _default = _postcss.default.plugin(pluginName, options => function process(css, result) {
|
||||
const traversed = walkDecls(css, result, options.filter);
|
||||
const flattenTraversed = flatten(traversed.map(item => item.urls));
|
||||
const urlsWithNodes = collectUniqueUrlsWithNodes(flattenTraversed);
|
||||
const replacers = new Map();
|
||||
urlsWithNodes.forEach((urlWithNodes, index) => {
|
||||
const {
|
||||
url,
|
||||
hash,
|
||||
needQuotes,
|
||||
nodes
|
||||
} = urlWithNodes;
|
||||
const replacementName = `___CSS_LOADER_URL_REPLACEMENT_${index}___`;
|
||||
result.messages.push({
|
||||
pluginName,
|
||||
type: 'import',
|
||||
value: {
|
||||
type: 'url',
|
||||
replacementName,
|
||||
url,
|
||||
needQuotes,
|
||||
hash
|
||||
}
|
||||
}, {
|
||||
pluginName,
|
||||
type: 'replacer',
|
||||
value: {
|
||||
type: 'url',
|
||||
replacementName
|
||||
}
|
||||
});
|
||||
nodes.forEach(node => {
|
||||
replacers.set(node, replacementName);
|
||||
});
|
||||
});
|
||||
traversed.forEach(item => {
|
||||
walkUrls(item.parsed, node => {
|
||||
const replacementName = replacers.get(node);
|
||||
|
||||
if (!replacementName) {
|
||||
return;
|
||||
} // eslint-disable-next-line no-param-reassign
|
||||
|
||||
|
||||
node.type = 'word'; // eslint-disable-next-line no-param-reassign
|
||||
|
||||
node.value = replacementName;
|
||||
}); // eslint-disable-next-line no-param-reassign
|
||||
|
||||
item.decl.value = item.parsed.toString();
|
||||
});
|
||||
});
|
||||
|
||||
exports.default = _default;
|
||||
Reference in New Issue
Block a user