Skip to content

Commit

Permalink
Add support for Webpack 4 (in addition to 3 & prior)
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Jun 29, 2018
1 parent a24e504 commit eb51eb3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -27,6 +27,7 @@
"eslint": "^4.16.0",
"eslint-config-developit": "^1.1.1",
"microbundle": "^0.4.3",
"webpack": "^4.14.0",
"workerize-loader": "^1.0.1"
},
"dependencies": {
Expand All @@ -48,7 +49,9 @@
"karma-webpack": "2.0.7",
"puppeteer": "^1.0.0",
"sade": "^1.3.1",
"script-loader": "^0.7.2",
"webpack": "^3.10.0"
"script-loader": "^0.7.2"
},
"peerDependencies": {
"webpack": ">=4"
}
}
18 changes: 16 additions & 2 deletions src/configure.js
Expand Up @@ -5,6 +5,9 @@ import { moduleDir, tryRequire, dedupe, cleanStack, readFile, readDir } from './
import babelLoader from './lib/babel-loader';
import cssLoader from './lib/css-loader';

const WEBPACK_VERSION = String(require('webpack').version || '3.0.0');
const WEBPACK_MAJOR = WEBPACK_VERSION.split('.')[0]|0;

export default function configure(options) {
let cwd = process.cwd(),
res = file => path.resolve(cwd, file);
Expand Down Expand Up @@ -102,7 +105,7 @@ export default function configure(options) {
return Object.assign({}, configured || {}, value);
}

return {
let generatedConfig = {
basePath: cwd,
plugins: PLUGINS.map(require.resolve),
frameworks: ['jasmine'],
Expand Down Expand Up @@ -152,8 +155,10 @@ export default function configure(options) {

webpack: {
devtool: 'cheap-module-eval-source-map',
mode: webpackConfig.mode || 'development',
module: {
loaders: loaders.concat(
// @TODO check webpack version and use loaders VS rules as the key here appropriately:
rules: loaders.concat(
!getLoader( rule => `${rule.use},${rule.loader}`.match(/\bbabel-loader\b/) ) && babelLoader(options),
!getLoader('foo.css') && cssLoader(options)
).filter(Boolean)
Expand Down Expand Up @@ -199,4 +204,13 @@ export default function configure(options) {
}
}
};

if (WEBPACK_MAJOR < 4) {
delete generatedConfig.webpack.mode;
let { rules } = generatedConfig.webpack.module;
delete generatedConfig.webpack.module.rules;
generatedConfig.webpack.module.loaders = rules;
}

return generatedConfig;
}
2 changes: 1 addition & 1 deletion test/fixture.worker.js
@@ -1,3 +1,3 @@
export function foo() {
export async function foo() {
return 1;
}
7 changes: 4 additions & 3 deletions test/index.test.js
@@ -1,3 +1,5 @@
import worker from 'workerize-loader!./fixture.worker.js';

const sleep = ms => new Promise( r => setTimeout(r, ms) );

describe('demo', () => {
Expand All @@ -18,9 +20,8 @@ describe('demo', () => {
});

it('should do MAGIC', async () => {
let lib = await import('workerize-loader!./fixture.worker.js');
expect(lib).toEqual(jasmine.any(Function));
let mod = lib();
let mod = worker();
expect(mod.foo).toEqual(jasmine.any(Function));
expect(await mod.foo()).toEqual(1);
});
});

0 comments on commit eb51eb3

Please sign in to comment.