Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
feat: add options validation (schema-utils) (#78)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: loader-utils upgrade to > 1.0 is not backwards
compatible with previous versions
  • Loading branch information
michael-ciniawsky authored and joshwiens committed Sep 25, 2017
1 parent edcda35 commit 5e2f5e6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
'use strict';

const path = require('path');
const loaderUtils = require('loader-utils');
const validateOptions = require('schema-utils');
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const loaderUtils = require('loader-utils');
const schema = require('./options.json');

const getWorker = (file, content, query) => {
const getWorker = (file, content, options) => {
const workerPublicPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;
if (query.inline) {
if (options.inline) {
const createInlineWorkerPath = JSON.stringify(`!!${path.join(__dirname, 'createInlineWorker.js')}`);
const fallbackWorkerPath = query.fallback === false ? 'null' : workerPublicPath;
const fallbackWorkerPath = options.fallback === false ? 'null' : workerPublicPath;
return `require(${createInlineWorkerPath})(${JSON.stringify(content)}, ${fallbackWorkerPath})`;
}
return `new Worker(${workerPublicPath})`;
Expand All @@ -22,10 +24,13 @@ module.exports.pitch = function pitch(request) {
if (!this.webpack) throw new Error('Only usable with webpack');
this.cacheable(false);
const callback = this.async();
const query = loaderUtils.getOptions(this) || {};
const filename = loaderUtils.interpolateName(this, query.name || '[hash].worker.js', {
context: query.context || this.options.context,
regExp: query.regExp,
const options = loaderUtils.getOptions(this) || {};

validateOptions(schema, options, 'Worker Loader');

const filename = loaderUtils.interpolateName(this, options.name || '[hash].worker.js', {
context: options.context || this.options.context,
regExp: options.regExp,
});
const outputOptions = {
filename,
Expand Down Expand Up @@ -59,8 +64,8 @@ module.exports.pitch = function pitch(request) {
if (err) return callback(err);
if (entries[0]) {
const workerFile = entries[0].files[0];
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), query);
if (query.fallback === false) {
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), options);
if (options.fallback === false) {
delete this._compilation.assets[workerFile];
}
return callback(null, `module.exports = function() {\n\treturn ${workerFactory};\n};`);
Expand Down
15 changes: 15 additions & 0 deletions options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"inline": {
"type": "boolean"
},
"fallback": {
"type": "boolean"
}
},
"additionalProperties": false
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"webpack": ">=0.9 <2 || ^2.1.0-beta || ^2.2.0"
},
"dependencies": {
"loader-utils": "^1.0.2"
"loader-utils": "^1.0.2",
"schema-utils": "^0.3.0"
},
"devDependencies": {
"del": "^2.2.2",
Expand Down
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ ajv@^4.7.0, ajv@^4.9.1:
co "^4.6.0"
json-stable-stringify "^1.0.1"

ajv@^5.0.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.2.0.tgz#c1735024c5da2ef75cc190713073d44f098bf486"
dependencies:
co "^4.6.0"
fast-deep-equal "^0.1.0"
json-schema-traverse "^0.3.0"
json-stable-stringify "^1.0.1"

align-text@^0.1.1, align-text@^0.1.3:
version "0.1.4"
resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117"
Expand Down Expand Up @@ -855,6 +864,10 @@ extsprintf@1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550"

fast-deep-equal@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-0.1.0.tgz#5c6f4599aba6b333ee3342e2ed978672f1001f8d"

fast-levenshtein@~2.0.4:
version "2.0.6"
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
Expand Down Expand Up @@ -1337,6 +1350,10 @@ json-loader@^0.5.4:
version "0.5.4"
resolved "https://registry.yarnpkg.com/json-loader/-/json-loader-0.5.4.tgz#8baa1365a632f58a3c46d20175fc6002c96e37de"

json-schema-traverse@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"

json-schema@0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
Expand Down Expand Up @@ -2064,6 +2081,12 @@ safe-buffer@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.0.1.tgz#d263ca54696cd8a306b5ca6551e92de57918fbe7"

schema-utils@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-0.3.0.tgz#f5877222ce3e931edae039f17eb3716e7137f8cf"
dependencies:
ajv "^5.0.0"

"semver@2 || 3 || 4 || 5", semver@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
Expand Down

0 comments on commit 5e2f5e6

Please sign in to comment.