Skip to content

Commit 72c957c

Browse files
committed
Adding way to set postcss syntax and including trim plugin
1 parent 159c30f commit 72c957c

15 files changed

+13974
-2
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ npm-debug.log*
44
.eslintcache
55

66
/coverage
7-
/dist
87
/local
98
/reports
109
/node_modules
@@ -16,4 +15,3 @@ Thumbs.db
1615
.vscode
1716
*.sublime-project
1817
*.sublime-workspace
19-
package-lock.json

dist/CssSyntaxError.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = void 0;
7+
8+
class CssSyntaxError extends Error {
9+
constructor(error) {
10+
super(error);
11+
const {
12+
reason,
13+
line,
14+
column
15+
} = error;
16+
this.name = 'CssSyntaxError'; // Based on https://github.com/postcss/postcss/blob/master/lib/css-syntax-error.es6#L132
17+
// We don't need `plugin` and `file` properties.
18+
19+
this.message = `${this.name}\n\n`;
20+
21+
if (typeof line !== 'undefined') {
22+
this.message += `(${line}:${column}) `;
23+
}
24+
25+
this.message += `${reason}`;
26+
const code = error.showSourceCode();
27+
28+
if (code) {
29+
this.message += `\n\n${code}\n`;
30+
} // We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
31+
32+
33+
this.stack = false;
34+
}
35+
36+
}
37+
38+
exports.default = CssSyntaxError;

dist/Warning.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = void 0;
7+
8+
class Warning extends Error {
9+
constructor(warning) {
10+
super(warning);
11+
const {
12+
text,
13+
line,
14+
column
15+
} = warning;
16+
this.name = 'Warning'; // Based on https://github.com/postcss/postcss/blob/master/lib/warning.es6#L74
17+
// We don't need `plugin` properties.
18+
19+
this.message = `${this.name}\n\n`;
20+
21+
if (typeof line !== 'undefined') {
22+
this.message += `(${line}:${column}) `;
23+
}
24+
25+
this.message += `${text}`; // We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
26+
27+
this.stack = false;
28+
}
29+
30+
}
31+
32+
exports.default = Warning;

dist/cjs.js

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

dist/index.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = loader;
7+
8+
var _schemaUtils = _interopRequireDefault(require("schema-utils"));
9+
10+
var _postcss = _interopRequireDefault(require("postcss"));
11+
12+
var _package = _interopRequireDefault(require("postcss/package.json"));
13+
14+
var _loaderUtils = require("loader-utils");
15+
16+
var _options = _interopRequireDefault(require("./options.json"));
17+
18+
var _plugins = require("./plugins");
19+
20+
var _utils = require("./utils");
21+
22+
var _Warning = _interopRequireDefault(require("./Warning"));
23+
24+
var _CssSyntaxError = _interopRequireDefault(require("./CssSyntaxError"));
25+
26+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27+
28+
/*
29+
MIT License http://www.opensource.org/licenses/mit-license.php
30+
Author Tobias Koppers @sokra
31+
*/
32+
function loader(content, map, meta) {
33+
const options = (0, _loaderUtils.getOptions)(this) || {};
34+
(0, _schemaUtils.default)(_options.default, options, {
35+
name: 'CSS Loader',
36+
baseDataPath: 'options'
37+
});
38+
const callback = this.async();
39+
const sourceMap = options.sourceMap || false; // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
40+
// eslint-disable-next-line no-param-reassign
41+
42+
map = sourceMap && map ? (0, _utils.normalizeSourceMap)(map) : null; // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
43+
44+
if (meta) {
45+
const {
46+
ast
47+
} = meta;
48+
49+
if (ast && ast.type === 'postcss' && ast.version === _package.default.version) {
50+
// eslint-disable-next-line no-param-reassign
51+
content = ast.root;
52+
}
53+
}
54+
55+
const plugins = [];
56+
57+
if (options.modules) {
58+
plugins.push(...(0, _utils.getModulesPlugins)(options, this));
59+
} // Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
60+
61+
62+
const importPrefix = (0, _utils.getImportPrefix)(this, options.importLoaders);
63+
plugins.push((0, _plugins.icssParser)({
64+
loaderContext: this,
65+
importPrefix,
66+
localsConvention: options.localsConvention
67+
}));
68+
69+
if (options.import !== false) {
70+
plugins.push((0, _plugins.importParser)({
71+
loaderContext: this,
72+
importPrefix,
73+
filter: (0, _utils.getFilter)(options.import, this.resourcePath)
74+
}));
75+
}
76+
77+
if (options.url !== false) {
78+
plugins.push((0, _plugins.urlParser)({
79+
loaderContext: this,
80+
filter: (0, _utils.getFilter)(options.url, this.resourcePath, value => (0, _loaderUtils.isUrlRequest)(value))
81+
}));
82+
}
83+
84+
plugins.push((0, _plugins.trimPlugin)());
85+
const postCssOptions = {
86+
from: (0, _loaderUtils.getRemainingRequest)(this).split('!').pop(),
87+
to: (0, _loaderUtils.getCurrentRequest)(this).split('!').pop(),
88+
map: options.sourceMap ? {
89+
prev: map,
90+
inline: false,
91+
annotation: false
92+
} : null
93+
};
94+
95+
if (options.postCssSyntax) {
96+
postCssOptions.syntax = options.postCssSyntax;
97+
}
98+
99+
(0, _postcss.default)(plugins).process(content, postCssOptions).then(result => {
100+
result.warnings().forEach(warning => this.emitWarning(new _Warning.default(warning)));
101+
102+
if (!result.messages) {
103+
// eslint-disable-next-line no-param-reassign
104+
result.messages = [];
105+
}
106+
107+
const {
108+
onlyLocals
109+
} = options;
110+
const importItems = result.messages.filter(message => message.type === 'import' ? message : false).reduce((accumulator, currentValue) => {
111+
accumulator.push(currentValue.import);
112+
return accumulator;
113+
}, []);
114+
const exportItems = result.messages.filter(message => message.type === 'export' ? message : false).reduce((accumulator, currentValue) => {
115+
accumulator.push(currentValue.export);
116+
return accumulator;
117+
}, []);
118+
const importCode = (0, _utils.getImportCode)(importItems, onlyLocals);
119+
const moduleCode = (0, _utils.getModuleCode)(result, sourceMap, onlyLocals);
120+
const exportCode = (0, _utils.getExportCode)(exportItems, onlyLocals);
121+
const apiCode = (0, _utils.getApiCode)(this, sourceMap, onlyLocals);
122+
return callback(null, (0, _utils.prepareCode)({
123+
apiCode,
124+
importCode,
125+
moduleCode,
126+
exportCode
127+
}, result.messages, this, importPrefix, onlyLocals));
128+
}).catch(error => {
129+
callback(error.name === 'CssSyntaxError' ? new _CssSyntaxError.default(error) : error);
130+
});
131+
}

dist/options.json

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"additionalProperties": false,
3+
"properties": {
4+
"url": {
5+
"description": "Enables/Disables 'url'/'image-set' functions handling (https://github.com/webpack-contrib/css-loader#url).",
6+
"anyOf": [
7+
{
8+
"type": "boolean"
9+
},
10+
{
11+
"instanceof": "Function"
12+
}
13+
]
14+
},
15+
"import": {
16+
"description": "Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).",
17+
"anyOf": [
18+
{
19+
"type": "boolean"
20+
},
21+
{
22+
"instanceof": "Function"
23+
}
24+
]
25+
},
26+
"modules": {
27+
"description": "Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).",
28+
"anyOf": [
29+
{
30+
"type": "boolean"
31+
},
32+
{
33+
"enum": ["local", "global"]
34+
},
35+
{
36+
"type": "object",
37+
"additionalProperties": false,
38+
"properties": {
39+
"mode": {
40+
"enum": ["local", "global"]
41+
},
42+
"localIdentName": {
43+
"type": "string"
44+
},
45+
"localIdentRegExp": {
46+
"anyOf": [
47+
{
48+
"type": "string"
49+
},
50+
{
51+
"instanceof": "RegExp"
52+
}
53+
]
54+
},
55+
"context": {
56+
"type": "string"
57+
},
58+
"hashPrefix": {
59+
"type": "string"
60+
},
61+
"getLocalIdent": {
62+
"anyOf": [
63+
{
64+
"type": "boolean"
65+
},
66+
{
67+
"instanceof": "Function"
68+
}
69+
]
70+
}
71+
}
72+
}
73+
]
74+
},
75+
"sourceMap": {
76+
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
77+
"type": "boolean"
78+
},
79+
"importLoaders": {
80+
"description": "Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).",
81+
"anyOf": [
82+
{
83+
"type": "boolean"
84+
},
85+
{
86+
"type": "number"
87+
}
88+
]
89+
},
90+
"localsConvention": {
91+
"description": "Style of exported classnames (https://github.com/webpack-contrib/css-loader#localsconvention).",
92+
"enum": ["asIs", "camelCase", "camelCaseOnly", "dashes", "dashesOnly"]
93+
},
94+
"onlyLocals": {
95+
"description": "Export only locals (https://github.com/webpack-contrib/css-loader#onlylocals).",
96+
"type": "boolean"
97+
},
98+
"postCssSyntax": {
99+
"description": "Allows to choose with syntax of postcss to use when processing files",
100+
"type": "object"
101+
}
102+
},
103+
"type": "object"
104+
}

dist/plugins/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
Object.defineProperty(exports, "importParser", {
7+
enumerable: true,
8+
get: function () {
9+
return _postcssImportParser.default;
10+
}
11+
});
12+
Object.defineProperty(exports, "icssParser", {
13+
enumerable: true,
14+
get: function () {
15+
return _postcssIcssParser.default;
16+
}
17+
});
18+
Object.defineProperty(exports, "urlParser", {
19+
enumerable: true,
20+
get: function () {
21+
return _postcssUrlParser.default;
22+
}
23+
});
24+
Object.defineProperty(exports, "trimPlugin", {
25+
enumerable: true,
26+
get: function () {
27+
return _postcssTrimPlugin.default;
28+
}
29+
});
30+
31+
var _postcssImportParser = _interopRequireDefault(require("./postcss-import-parser"));
32+
33+
var _postcssIcssParser = _interopRequireDefault(require("./postcss-icss-parser"));
34+
35+
var _postcssUrlParser = _interopRequireDefault(require("./postcss-url-parser"));
36+
37+
var _postcssTrimPlugin = _interopRequireDefault(require("./postcss-trim-plugin"));
38+
39+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

0 commit comments

Comments
 (0)