Skip to content

Commit 92503f6

Browse files
committed
webpack4 support
1 parent f75cad3 commit 92503f6

16 files changed

+2565
-450
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules/
22
/lib/
33
/es/
4+
/dist/
45
.DS_Store
56
.idea
67
npm-debug.log

_tests/automocking.spec.js

Whitespace-only changes.

_tests/scope.spec.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import {_clearPlugins} from '../src/plugins';
66
import nodePlugin from '../src/plugins/nodejs';
77

88
describe('scope ', () => {
9+
10+
const webpackDefault = exports => {
11+
return typeof __webpack_require__ !== 'undefined' ? exports.default: exports;
12+
};
13+
914
it('scope test: ', () => {
1015
addPlugin(nodePlugin);
1116
const unmockedBaz = require('./lib/a/test.js');
@@ -57,7 +62,7 @@ describe('scope ', () => {
5762
mock('./lib/a/../b/baz').with(() => 'cc');
5863
})
5964
.then((mockedBaz) => {
60-
expect(mockedBaz()).to.be.equal('aabbcc');
65+
expect(webpackDefault(mockedBaz)()).to.be.equal('aabbcc');
6166
});
6267
});
6368

@@ -75,7 +80,7 @@ describe('scope ', () => {
7580
mock('./lib/a/../b/baz').with(() => 'cc');
7681
}))
7782
.then((mockedBaz) => {
78-
expect(mockedBaz()).to.be.equal('aabbcc');
83+
expect(webpackDefault(mockedBaz)()).to.be.equal('aabbcc');
7984
});
8085
});
8186

@@ -87,7 +92,7 @@ describe('scope ', () => {
8792
() =>
8893
import('./lib/a/test.js')
8994
.then((mockedBaz) => {
90-
expect(mockedBaz()).to.be.equal('aabbcc');
95+
expect(webpackDefault(mockedBaz)()).to.be.equal('aabbcc');
9196
}),
9297
(mock) => {
9398
addPlugin(nodePlugin);
@@ -111,7 +116,7 @@ describe('scope ', () => {
111116
mock('./lib/a/../b/baz').with(() => 'cc');
112117
})
113118
.then((mockedBaz) => {
114-
expect(mockedBaz()).to.be.equal('aabarcc');
119+
expect(webpackDefault(mockedBaz)()).to.be.equal('aabarcc');
115120
});
116121
});
117122

@@ -121,7 +126,7 @@ describe('scope ', () => {
121126

122127
return rewiremock.around(() => import('./lib/a/test.js'))
123128
.then((mockedBaz) => {
124-
expect(mockedBaz()).to.be.equal('foobarbaz');
129+
expect(webpackDefault(mockedBaz)()).to.be.equal('foobarbaz');
125130
});
126131
});
127132

@@ -169,7 +174,7 @@ describe('scope ', () => {
169174
}));
170175

171176
return mockedBazLoad.then(mockedBaz => {
172-
expect(mockedBaz()).to.be.equal('aabarcc')
177+
expect(webpackDefault(mockedBaz)()).to.be.equal('aabarcc')
173178
})
174179
});
175180

_tests/simple-case.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('simples case ', () => {
1111

1212
rewiremock.enable();
1313
const mocked = require('./lib/use-node_module.js');
14+
1415
expect(mocked.test()).to.be.equal('mocked');
1516
rewiremock.disable();
1617
});

package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"build5": "BABEL_ENV=cjs babel src -d lib && cp src/index.js.flow lib/index.js.flow",
1818
"build6": "BABEL_ENV=ejs babel src -d es && cp src/index.js.flow es/index.js.flow",
1919
"prepublish": "npm run build",
20-
"lint": "eslint src tests",
20+
"lint": "eslint src",
2121
"lint:fix": "eslint src tests --fix"
2222
},
2323
"repository": {
@@ -50,7 +50,7 @@
5050
"mock",
5151
"mockery",
5252
"proxyquire"
53-
],view-source:https://upload.wikimedia.org/wikipedia/commons/0/05/Sierpinski_triangle_evolution.svgview-source:https://upload.wikimedia.org/wikipedia/commons/0/05/Sierpinski_triangle_evolution.svgff
53+
],
5454
"jest": {
5555
"testMatch": [
5656
"**/?(*.)(jest).js?(x)"
@@ -69,9 +69,10 @@
6969
"compare-module-exports": "^2.0.0",
7070
"lodash.some": "^4.6.0",
7171
"lodash.template": "^4.4.0",
72+
"node-libs-browser": "^2.1.0",
7273
"path-parse": "^1.0.5",
7374
"wipe-node-cache": "^1.1.0",
74-
"wipe-webpack-cache": "^1.0.0"
75+
"wipe-webpack-cache": "^1.0.2"
7576
},
7677
"devDependencies": {
7778
"babel-cli": "^6.18.0",
@@ -84,7 +85,7 @@
8485
"babel-preset-env": "1.6.1",
8586
"babel-register": "6.18.0",
8687
"chai": "^3.5.0",
87-
"eslint": "^3.19.0",
88+
"eslint": "^4.19.1",
8889
"eslint-plugin-mocha": "^4.9.0",
8990
"flow-bin": "^0.59.0",
9091
"jest-cli": "^21.2.1",
@@ -98,7 +99,8 @@
9899
"phantomjs-polyfill": "0.0.2",
99100
"phantomjs-polyfill-object-assign": "0.0.2",
100101
"sinon": "^4.4.2",
101-
"webpack": "^3.5.5",
102-
"webpack-dev-server": "^2.7.1"
102+
"webpack": "^4.2.0",
103+
"webpack-cli": "^2.0.13",
104+
"webpack-dev-server": "^3.1.1"
103105
}
104106
}

src/executor.js

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ function mockLoader(request, parent, isMain) {
145145
mockedModules[baseRequest] = true;
146146
}
147147

148+
//autoMock(baseRequest);
149+
148150
const mock = getMock(baseRequest) || getMock(request) || getMock(shortRequest);
149151

150152
if (mock) {

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const removePlugin = API.removePlugin;
3939
addPlugin(plugins.toBeUsed);
4040
addPlugin(plugins.directChild);
4141

42+
if(typeof __webpack_require__ !== "undefined"){
43+
addPlugin(plugins.nodeLibBrowser);
44+
}
45+
4246
export {
4347
addPlugin,
4448
removePlugin,

src/mockModule.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ const swapCache = () => {
258258
Object
259259
.keys(newCache)
260260
.filter(key => !oldCache[key])
261-
.filter(key => key.indexOf('\.node') < 0)
261+
.filter(key => key.indexOf('.node') < 0)
262262
.forEach(key => delete newCache[key]);
263263

264264
Object

src/plugins/common/aliases.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,14 @@ function processFile(filePath, {aliasConf, extensionsConf}) {
139139
if (aliasConf.hasOwnProperty(aliasFrom)) {
140140

141141
let aliasTo = aliasConf[aliasFrom];
142-
const regex = new RegExp(`^${aliasFrom}(\/|$)`);
142+
const regex = new RegExp(`^${aliasFrom}(/|$)`);
143143

144144
// If the regex matches, replace by the right config
145145
if (regex.test(filePath)) {
146146

147147
// notModuleRegExp from https://github.com/webpack/enhanced-resolve/blob/master/lib/Resolver.js
148+
149+
/* eslint-disable no-useless-escape */
148150
const notModuleRegExp = /^\.$|^\.[\\\/]|^\.\.$|^\.\.[\/\\]|^\/|^[A-Z]:[\\\/]/i;
149151
const isModule = !notModuleRegExp.test(aliasTo);
150152

src/plugins/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import mockThoughByDefault from './mockThoughByDefault';
1111
import usedByDefault from './usedByDefault';
1212
import alwaysMatchOrigin from './toMatchOrigin';
1313

14+
import nodeLibBrowser from './node-lib-browser';
15+
1416
const exports = {
1517
childOnly,
1618
nodejs,
@@ -23,7 +25,9 @@ const exports = {
2325
mockThoughByDefault,
2426
alwaysMatchOrigin,
2527
usedByDefault,
26-
directChild
28+
directChild,
29+
30+
nodeLibBrowser
2731
};
2832

2933
export default exports;

src/plugins/node-lib-browser.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import mapping from 'node-libs-browser';
2+
3+
import createPlugin from './_common';
4+
5+
const fileNameTransformer = (fileName/*, module*/) => mapping[fileName] || fileName;
6+
7+
const plugin = createPlugin({
8+
fileNameTransformer,
9+
//wipeCheck,
10+
//shouldMock,
11+
12+
name: 'node-libs-browser'
13+
});
14+
15+
export default plugin;

src/wipeCache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const primaryResolver = (stubs, moduleName) =>
1212

1313
const resolver = (stubs, moduleName) => {
1414
// never wipe .node(native) module
15-
if (moduleName.indexOf('\.node') > -1) {
15+
if (moduleName.indexOf('.node') > -1) {
1616
return false;
1717
}
1818
return shouldWipe(stubs, moduleName) || primaryResolver(stubs, moduleName) || relativeWipeCheck(stubs, moduleName);

webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const webpack = require('webpack');
22
const mockPlugin = require("./webpack/plugin");
33

44
module.exports = {
5+
mode: "development",
56
// webpack configuration
67
plugins: [
78
new webpack.NamedModulesPlugin(),

webpack/module.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Module = {
1212

1313
const asFile = keys.find(name => name.indexOf(targetFile) > 0);
1414
const asIndex = keys.find(name => name.indexOf(targetFileIndex) > 0);
15+
1516
if (asFile && asIndex && asFile.substr(targetFile.length+1).indexOf('/') >= 0) {
1617
return asIndex;
1718
}

webpack/plugin.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class RewiremockPlugin {
1111

1212
compilation.moduleTemplate.plugin('render', function (moduleSource) {
1313
const source = new ConcatSource();
14-
source.add(injectString);
14+
if (moduleSource.source().indexOf('require') > 0) {
15+
source.add(injectString);
16+
}
1517
source.add(moduleSource);
1618
return source;
1719
});

0 commit comments

Comments
 (0)