Skip to content

Commit 794b717

Browse files
author
Derek Kent
committed
Dist
1 parent 2202572 commit 794b717

File tree

9 files changed

+586
-761
lines changed

9 files changed

+586
-761
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ npm-debug.log*
44
yarn-debug.log*
55
.eslintcache
66
/coverage
7-
/dist
87
/local
98
/reports
109
/node_modules

dist/Error.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
class SyntaxError extends Error {
7+
constructor(err) {
8+
super(err);
9+
10+
this.name = 'Syntax Error';
11+
this.message = '';
12+
13+
if (err.line) {
14+
this.message += `\n\n[${err.line}:${err.column}] ${err.reason}`;
15+
}
16+
17+
if (err.input.source) {
18+
this.message += `\n\n${err.showSourceCode()}\n`;
19+
}
20+
21+
Error.captureStackTrace(this, this.constructor);
22+
}
23+
}
24+
25+
exports.default = SyntaxError;

dist/cjs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('./index').default;

dist/index.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = loader;
7+
8+
var _options = require('./options.json');
9+
10+
var _options2 = _interopRequireDefault(_options);
11+
12+
var _loaderUtils = require('loader-utils');
13+
14+
var _schemaUtils = require('schema-utils');
15+
16+
var _schemaUtils2 = _interopRequireDefault(_schemaUtils);
17+
18+
var _postcss = require('postcss');
19+
20+
var _postcss2 = _interopRequireDefault(_postcss);
21+
22+
var _url = require('./plugins/url');
23+
24+
var _url2 = _interopRequireDefault(_url);
25+
26+
var _import = require('./plugins/import');
27+
28+
var _import2 = _interopRequireDefault(_import);
29+
30+
var _Error = require('./Error');
31+
32+
var _Error2 = _interopRequireDefault(_Error);
33+
34+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35+
36+
// Loader Defaults
37+
const DEFAULTS = {
38+
url: true,
39+
import: true,
40+
sourceMap: false
41+
};
42+
43+
// import runtime from './runtime';
44+
45+
// TODO(michael-ciniawsky)
46+
// replace with postcss-icss-{url, import}
47+
/* eslint-disable
48+
import/first,
49+
import/order,
50+
no-shadow,
51+
no-param-reassign
52+
*/
53+
function loader(css, map, meta) {
54+
// Loader Mode (Async)
55+
const cb = this.async();
56+
const file = this.resourcePath;
57+
58+
// Loader Options
59+
const options = Object.assign({}, DEFAULTS, (0, _loaderUtils.getOptions)(this));
60+
61+
(0, _schemaUtils2.default)(_options2.default, options, 'CSS Loader');
62+
63+
if (options.sourceMap) {
64+
if (map && typeof map !== 'string') {
65+
map = JSON.stringify(map);
66+
}
67+
} else {
68+
map = false;
69+
}
70+
71+
const plugins = [];
72+
73+
// URL Plugin
74+
if (options.url) {
75+
plugins.push((0, _url2.default)(options));
76+
}
77+
78+
// Import Plugin
79+
if (options.import) {
80+
plugins.push((0, _import2.default)(options));
81+
}
82+
83+
if (meta) {
84+
const { ast } = meta;
85+
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader')
86+
// to avoid reparsing the CSS
87+
if (ast && ast.type === 'postcss') {
88+
css = ast.root;
89+
}
90+
}
91+
92+
map = options.sourceMap ? {
93+
prev: map || false,
94+
inline: false,
95+
annotation: false,
96+
sourcesContent: true
97+
} : false;
98+
99+
return (0, _postcss2.default)(plugins).process(css, {
100+
from: `/css-loader!${file}`,
101+
map,
102+
to: file
103+
}).then(({ css, map, messages }) => {
104+
if (meta && meta.messages) {
105+
messages = messages.concat(meta.messages);
106+
}
107+
108+
// CSS Imports
109+
let imports = messages.filter(msg => msg.type === 'import' ? msg : false).reduce((imports, msg) => {
110+
try {
111+
msg = typeof msg.import === 'function' ? msg.import() : msg.import;
112+
113+
imports += msg;
114+
} catch (err) {
115+
// TODO(michael-ciniawsky)
116+
// revisit (CSSImportsError)
117+
this.emitError(err);
118+
}
119+
120+
return imports;
121+
}, '');
122+
123+
// CSS Exports
124+
let exports = messages.filter(msg => msg.type === 'export' ? msg : false).reduce((exports, msg) => {
125+
try {
126+
msg = typeof msg.export === 'function' ? msg.export() : msg.export;
127+
128+
exports += msg;
129+
} catch (err) {
130+
// TODO(michael-ciniawsky)
131+
// revisit (CSSExportsError)
132+
this.emitError(err);
133+
}
134+
135+
return exports;
136+
}, '');
137+
138+
imports = imports ? `// CSS Imports\n${imports}\n` : false;
139+
exports = exports ? `// CSS Exports\n${exports}\n` : false;
140+
css = `// CSS\nexport default \`${css}\``;
141+
142+
// TODO(michael-ciniawsky)
143+
// triage if and add CSS runtime back
144+
const result = [imports, exports, css].filter(Boolean).join('\n');
145+
146+
cb(null, result, map ? map.toJSON() : null);
147+
148+
return null;
149+
}).catch(err => {
150+
err = err.name === 'CssSyntaxError' ? new _Error2.default(err) : err;
151+
152+
cb(err);
153+
154+
return null;
155+
});
156+
}

dist/options.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"url": {
5+
"anyOf": [
6+
{ "type": "string" },
7+
{ "type": "boolean" },
8+
{ "instanceof": "RegExp" },
9+
{ "instanceof": "Function" }
10+
]
11+
},
12+
"import": {
13+
"anyOf": [
14+
{ "type": "string" },
15+
{ "type": "boolean" },
16+
{ "instanceof": "RegExp" },
17+
{ "instanceof": "Function" }
18+
]
19+
},
20+
"minimize": {
21+
"type": "boolean"
22+
},
23+
"sourceMap": {
24+
"type": "boolean"
25+
}
26+
},
27+
"additionalProperties": false
28+
}

dist/plugins/import.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _postcss = require('postcss');
8+
9+
var _postcss2 = _interopRequireDefault(_postcss);
10+
11+
var _postcssValueParser = require('postcss-value-parser');
12+
13+
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
14+
15+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16+
17+
/* eslint-disable */
18+
const plugin = 'postcss-icss-import';
19+
20+
const getArg = nodes => nodes.length !== 0 && nodes[0].type === 'string' ? nodes[0].value : _postcssValueParser2.default.stringify(nodes);
21+
22+
const getUrl = node => {
23+
if (node.type === 'function' && node.value === 'url') {
24+
return getArg(node.nodes);
25+
}
26+
if (node.type === 'string') {
27+
return node.value;
28+
}
29+
return '';
30+
};
31+
32+
const parseImport = params => {
33+
const { nodes } = (0, _postcssValueParser2.default)(params);
34+
35+
if (nodes.length === 0) {
36+
return null;
37+
}
38+
39+
const url = getUrl(nodes[0]);
40+
41+
if (url.trim().length === 0) {
42+
return null;
43+
}
44+
45+
return {
46+
url,
47+
media: _postcssValueParser2.default.stringify(nodes.slice(1)).trim()
48+
};
49+
};
50+
51+
const URL = /^\w+:\/\//;
52+
53+
const filter = (url, options) => {
54+
if (URL.test(url)) {
55+
return true;
56+
}
57+
58+
if (url.startsWith('//')) {
59+
return true;
60+
}
61+
62+
if (options.import instanceof RegExp) {
63+
return options.import.test(url);
64+
}
65+
66+
if (typeof options.import === 'function') {
67+
return options.import(url);
68+
}
69+
70+
return false;
71+
};
72+
73+
const walkImports = (css, cb) => {
74+
css.each(node => {
75+
if (node.type === 'atrule' && node.name.toLowerCase() === 'import') {
76+
cb(node);
77+
}
78+
});
79+
};
80+
81+
exports.default = _postcss2.default.plugin(plugin, options => (css, result) => {
82+
let idx = 0;
83+
84+
walkImports(css, atrule => {
85+
if (atrule.nodes) {
86+
return result.warn('It looks like you didn\'t end your @import statement correctly.\nChild nodes are attached to it.', { node: atrule });
87+
}
88+
89+
const parsed = parseImport(atrule.params);
90+
91+
if (parsed === null) {
92+
return result.warn(`Unable to find URI in '${atrule.toString()}'`, {
93+
node: atrule
94+
});
95+
}
96+
97+
let idx = 0;
98+
const url = parsed.url;
99+
100+
if (!filter(url, options)) {
101+
atrule.remove();
102+
103+
result.messages.push({
104+
type: 'import',
105+
plugin: 'postcss-icss-import',
106+
import: `import CSS__IMPORT__${idx} from '${url}';\n`
107+
});
108+
109+
idx++;
110+
}
111+
});
112+
});

0 commit comments

Comments
 (0)