Skip to content
This repository was archived by the owner on Sep 28, 2020. It is now read-only.

Commit 80ba5a4

Browse files
wonismMoOx
authored andcommitted
Webpack 4 compatibility (#212)
* fix #204 | get this.options.eslint without error * add missing files and update travis * remove 4 on node_js versioin in .travis.yml * do not support webpack 1 * change version of webpack * move webpack into devDep
1 parent 0d3d6dd commit 80ba5a4

File tree

9 files changed

+1502
-871
lines changed

9 files changed

+1502
-871
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ language: node_js
33
node_js:
44
- 8
55
- 6
6-
- 4
76

87
env:
8+
- WEBPACK_VERSION=4
99
- WEBPACK_VERSION=3
1010
- WEBPACK_VERSION=2
11-
- WEBPACK_VERSION=1
1211

1312
matrix:
1413
fast_finish: true

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ module.exports = function(input, map) {
145145

146146
var userOptions = assign(
147147
// user defaults
148-
webpack.options.eslint || {},
148+
(webpack.options && webpack.options.eslint) || webpack.query || {},
149149
// loader query string
150150
loaderUtils.getOptions(webpack)
151151
)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"eslint": "^4.0.0",
3131
"eslint-friendly-formatter": "^2.0.4",
3232
"npmpub": "^3.0.1",
33-
"webpack": "^3.3.0"
33+
"webpack": "^4.0.0"
3434
},
3535
"scripts": {
3636
"lint": "eslint .",

test/autofix.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ test.cb("loader doesn't throw error if file ok after auto-fixing",
1818
{
1919
entry: "./test/fixtures/fixable-clone.js",
2020
module: {
21-
loaders: [
21+
rules: [
2222
{
2323
test: /\.js$/,
24-
loader: "./index?fix=true",
24+
use: "./index?fix=true",
2525
exclude: /node_modules/,
2626
},
2727
],

test/cache.js

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var rimraf = require("rimraf")
66
var webpack = require("webpack")
77

88
var defaultCacheDir = path.join(
9-
__dirname,
9+
__dirname,
1010
"../node_modules/.cache/eslint-loader"
1111
)
1212
var cacheDir = path.join(__dirname, "output/cache/cachefiles")
@@ -16,10 +16,10 @@ var eslintLoader = path.join(__dirname, "../index")
1616
var globalConfig = {
1717
entry: path.join(__dirname, "fixtures/cache.js"),
1818
module: {
19-
loaders: [
19+
rules: [
2020
{
2121
test: /\.js$/,
22-
loader: eslintLoader,
22+
use: eslintLoader,
2323
exclude: /node_modules/,
2424
},
2525
],
@@ -53,14 +53,11 @@ test.cb("should output files to cache directory", (t) => {
5353
path: t.context.directory,
5454
},
5555
module: {
56-
loaders: [
56+
rules: [
5757
{
5858
test: /\.js$/,
59-
loader: eslintLoader,
59+
use: `${eslintLoader}?cache=${t.context.cache}`,
6060
exclude: /node_modules/,
61-
query: {
62-
cache: t.context.cache,
63-
},
6461
},
6562
],
6663
},
@@ -78,21 +75,18 @@ test.cb("should output files to cache directory", (t) => {
7875
})
7976
})
8077

81-
test.cb.serial("should output json.gz files to standard cache dir by default",
78+
test.cb.serial("should output json.gz files to standard cache dir by default",
8279
(t) => {
8380
var config = assign({}, globalConfig, {
8481
output: {
8582
path: t.context.directory,
8683
},
8784
module: {
88-
loaders: [
85+
rules: [
8986
{
9087
test: /\.jsx?/,
91-
loader: eslintLoader,
88+
use: `${eslintLoader}?cache=true`,
9289
exclude: /node_modules/,
93-
query: {
94-
cache: true,
95-
},
9690
},
9791
],
9892
},
@@ -112,17 +106,17 @@ test.cb.serial("should output json.gz files to standard cache dir by default",
112106
})
113107

114108
test.cb.serial(
115-
"should output files to standard cache dir if set to true in query",
109+
"should output files to standard cache dir if set to true in query",
116110
(t) => {
117111
var config = assign({}, globalConfig, {
118112
output: {
119113
path: t.context.directory,
120114
},
121115
module: {
122-
loaders: [
116+
rules: [
123117
{
124118
test: /\.jsx?/,
125-
loader: `${eslintLoader}?cache=true`,
119+
use: `${eslintLoader}?cache=true`,
126120
exclude: /node_modules/,
127121
},
128122
],
@@ -143,21 +137,18 @@ test.cb.serial(
143137
})
144138
})
145139

146-
test.cb.serial("should read from cache directory if cached file exists",
140+
test.cb.serial("should read from cache directory if cached file exists",
147141
(t) => {
148142
var config = assign({}, globalConfig, {
149143
output: {
150144
path: t.context.directory,
151145
},
152146
module: {
153-
loaders: [
147+
rules: [
154148
{
155149
test: /\.jsx?/,
156-
loader: eslintLoader,
150+
use: `${eslintLoader}?cache=${t.context.cache}`,
157151
exclude: /node_modules/,
158-
query: {
159-
cache: t.context.cache,
160-
},
161152
},
162153
],
163154
},
@@ -186,14 +177,11 @@ test.cb.serial("should have one file per module", (t) => {
186177
path: t.context.directory,
187178
},
188179
module: {
189-
loaders: [
180+
rules: [
190181
{
191182
test: /\.jsx?/,
192-
loader: eslintLoader,
183+
use: `${eslintLoader}?cache=${t.context.cache}`,
193184
exclude: /node_modules/,
194-
query: {
195-
cache: t.context.cache,
196-
},
197185
},
198186
],
199187
},
@@ -218,15 +206,11 @@ test.cb.serial("should generate a new file if the identifier changes", (t) => {
218206
path: t.context.directory,
219207
},
220208
module: {
221-
loaders: [
209+
rules: [
222210
{
223211
test: /\.jsx?/,
224-
loader: eslintLoader,
212+
use: `${eslintLoader}?cache=${t.context.cache}&cacheIdentifier=a`,
225213
exclude: /node_modules/,
226-
query: {
227-
cache: t.context.cache,
228-
cacheIdentifier: "a",
229-
},
230214
},
231215
],
232216
},
@@ -236,15 +220,11 @@ test.cb.serial("should generate a new file if the identifier changes", (t) => {
236220
path: t.context.directory,
237221
},
238222
module: {
239-
loaders: [
223+
rules: [
240224
{
241225
test: /\.jsx?/,
242-
loader: eslintLoader,
226+
use: `${eslintLoader}?cache=${t.context.cache}&cacheIdentifier=b`,
243227
exclude: /node_modules/,
244-
query: {
245-
cache: t.context.cache,
246-
cacheIdentifier: "b",
247-
},
248228
},
249229
],
250230
},
@@ -284,4 +264,4 @@ function createTestDirectory(baseDirectory, testTitle, cb) {
284264

285265
function escapeDirectory(directory) {
286266
return directory.replace(/[/?<>\\:*|"\s]/g, "_")
287-
}
267+
}

test/multiple-engines.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@ test.cb("eslint-loader will create an engine for each unique config", function(t
88
{
99
entry: "./test/fixtures/good.js",
1010
module: {
11-
loaders: [
11+
rules: [
1212
{
1313
test: /\.js$/,
14-
loader: "./index",
15-
query: {
16-
rules: {
17-
quotes: [1, "single"],
18-
},
19-
},
14+
use: "./index?{rules:{quotes:[1,'single']}}",
2015
exclude: /node_modules/,
2116
},
2217
{
2318
test: /\.js$/,
24-
loader: "./index",
25-
query: {
26-
rules: {
27-
semi: [1, "always"],
28-
},
29-
},
19+
use: "./index?{rules:{semi:[1,'always']}}",
3020
exclude: /node_modules/,
3121
},
3222
],

test/parameters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ test.cb("eslint-loader supports query strings parameters", function(t) {
88
{
99
entry: "./test/fixtures/good-semi.js",
1010
module: {
11-
loaders: [
11+
rules: [
1212
{
1313
test: /\.js$/,
14-
loader: "./index?{rules:{semi:0}}",
14+
use: "./index?{rules:{semi:0}}",
1515
exclude: /node_modules/,
1616
},
1717
],

test/utils/conf.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
var path = require("path")
2+
var assign = require("object-assign")
23

34
var webpack = require("webpack")
45
var webpackVersion = require("./version.js")
5-
var assign = require("object-assign")
66

77
var DEFAULT_CONFIG = {
88
output: {
99
path: path.join(__dirname, "..", "output") + path.sep,
1010
filename: "bundle.js",
1111
},
1212
module: {
13-
loaders: [
13+
rules: [
1414
{
1515
test: /\.js$/,
16-
loader: "./index",
16+
use: "./index",
1717
exclude: /node_modules/,
1818
},
1919
],
@@ -27,6 +27,7 @@ var DEFAULT_CONFIG = {
2727
* @returns {Object}
2828
*/
2929
module.exports = function conf(webpackConf, loaderConf) {
30+
var mode = webpackVersion < 4 ? {} : {mode: "development"}
3031

3132
loaderConf = {
3233
eslint: assign({
@@ -40,16 +41,17 @@ module.exports = function conf(webpackConf, loaderConf) {
4041
// webpack v1 allows loader option to be added directly to the root webpack
4142
// config object
4243
// webpack v2 requires them to be added via the LoaderOptionsPlugin
43-
return assign(DEFAULT_CONFIG, webpackConf,
44-
webpackVersion === "1" ?
45-
loaderConf : {
46-
plugins: [
47-
new webpack.LoaderOptionsPlugin({
48-
exclude: /node_modules/,
49-
options: loaderConf,
50-
}),
51-
],
52-
}
44+
// webpack v4 needs mode option
45+
return assign(DEFAULT_CONFIG,
46+
mode,
47+
webpackConf,
48+
{
49+
plugins: [
50+
new webpack.LoaderOptionsPlugin({
51+
exclude: /node_modules/,
52+
options: loaderConf,
53+
}),
54+
],
55+
}
5356
)
54-
5557
}

0 commit comments

Comments
 (0)