Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit 1db3127

Browse files
committed
Added: cssnext now throw an error if used as a webpack loader to prevent unexpected usage with a recommendation for
Close #61
1 parent 18dcf8a commit 1db3127

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
- Added: cssnext now throw an error if used as a webpack loader to prevent
2+
unexpected usage with a recommendation for
3+
[cssnext-loader](https://github.com/cssnext/cssnext-loader)
4+
([#61](https://github.com/cssnext/cssnext/issues/61))
5+
16
# 1.8.0 - 2015-06-29
27

38
- Fixed: replacement of `postcss-log-warnings` (deprecated) by `postcss-reporter`
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const test = require("tape")
2+
3+
import {join as joinPath, dirname} from "path"
4+
5+
import webpack from "webpack"
6+
7+
test("cssnext-loader recommendation", function(t) {
8+
webpack(
9+
{
10+
entry: {
11+
"prevent-webpack-usage": [
12+
// we don't care about what file is being used
13+
// because this should throw an error before the input being used
14+
"./package.json",
15+
],
16+
},
17+
output: {
18+
// we don't care about the output...
19+
path: "./dist/__tests__/",
20+
filename: "prevent-webpack-usage.tmp-webpack-bundle.js",
21+
},
22+
module: {
23+
loaders: [
24+
{
25+
test: /\.json$/,
26+
// use directly cssnext index.js
27+
loader: joinPath(dirname(__filename), "..", "index.js"),
28+
},
29+
],
30+
},
31+
},
32+
(err, stats) => {
33+
if (err) {
34+
throw err
35+
}
36+
37+
if (!stats.hasErrors()) {
38+
t.fail(
39+
"doesn't throw an error if cssnext is used directly as webpack loader"
40+
)
41+
}
42+
else {
43+
t.ok(
44+
stats.compilation.errors[0].message.indexOf("cssnext-loader") > -1,
45+
"should recommand cssnext-loader"
46+
)
47+
}
48+
49+
t.end()
50+
})
51+
})

src/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ import optionMessages from "./option.messages"
1818
* @return {String} if string is given, or {Object} (postcss instance)
1919
*/
2020
function cssnext(string, options) {
21+
// prevent usage as a webpack loader
22+
// webpack run loader as function with an object as context
23+
// this object contains a "webpack" key set to true if used as a loader
24+
// https://github.com/cssnext/cssnext/issues/61
25+
if (typeof this === "object" && this.webpack === true) {
26+
throw new Error(
27+
"⚠︎ Don't use directly cssnext as a webpack loader. " +
28+
"Please use `cssnext-loader` instead: " +
29+
"https://github.com/cssnext/cssnext-loader"
30+
)
31+
}
32+
2133
if (arguments.length === 0) {
2234
options = {}
2335
}

0 commit comments

Comments
 (0)