How to use the getOptions function from loader-utils

Find comprehensive JavaScript loader-utils.getOptions code examples handpicked from public code repositorys.

290
291
292
293
294
295
296
297
298
299
```javascript
const loaderUtils = require('loader-utils');

module.exports = function(content) {
leanpub-start-insert
  const { name } = loaderUtils.getOptions(this);
leanpub-end-insert
  const url = loaderUtils.interpolateName(
leanpub-start-delete
    this, '[hash].[ext]', { content }
fork icon328
star icon3
watch icon7

+ 3 other calls in file

61
62
63
64
65
66
67
68
69

```js
module.exports = function(content) {

    // 获取 options 配置,上面已经讲过了就不在重复
  var options =  loaderUtils.getOptions(this) || {};
  // Options `dataUrlLimit` is backward compatibility with first loader versions
    // limit 参数,只有文件大小小于这个数值的时候我们才进行base64编码,否则将直接调用 file-loader
  var limit = options.limit || (this.options && this.options.url && this.options.url.dataUrlLimit);
fork icon90
star icon741
watch icon30

+ 23 other calls in file

15
16
17
18
19
20
21
22
23
24
async function stylexLoader(input, inputSourceMap) {
  const {
    inlineLoader = '',
    inject = true,
    ...options
  } = loaderUtils.getOptions(this) || {};

  const callback = this.async();

  const {code, map, metadata} = await babel.transformAsync(input, {
fork icon18
star icon196
watch icon0

103
104
105
106
107
108
109
110
111
```js
const loaderUtils = require('loader-utils');

module.exports = function (source) {
  // 获取到用户句给当前 Loader 传入的 options
  const options = loaderUtils.getOptions(this);
  return source;
};
```
fork icon19
star icon190
watch icon1

+ 11 other calls in file

216
217
218
219
220
221
222
223
224
225
// 拿到babel的参数 需要工具 loaderUtils
let loaderUtils =require('loader-utils')


function loader(source) {  // loader的参数就是源代码  这里的this就是loader的上下文
    let options = loaderUtils.getOptions(this)
    console.log(this.resourcePath, 444);   // [./src/index.js]
    let callback = this.async(); // babel的转换是异步的,同步的返回是不行的, 不能用return  同步就是直接掉用 异步会在async中
    babel.transform(source, {
        ...options,
fork icon88
star icon188
watch icon12

+ 47 other calls in file

141
142
143
144
145
146
147
148
149

```javascript
const loaderUtils = require('loader-utils');

module.exports = function(source) {
  const options =  loaderUtils.getOptions(this);
  return source.replace('darrell', options.name);
}
```
fork icon47
star icon191
watch icon6

+ 15 other calls in file

12
13
14
15
16
17
18
19
20

// make loader async
const cb = this.async();

// merge loader's options with plugin's options from store
const options = Object.assign(store.options, getOptions(this));

// basename gets used later to build the key for media query store
options.basename = interpolateName(this, '[name]', {});
fork icon27
star icon201
watch icon9

+ 13 other calls in file

13
14
15
16
17
18
19
20
21
22
  {
    name: '[hash].[ext]',
    context: this.rootContext,
    regExp: void 0,
  },
  utils.getOptions(this)
)
const publicPath = getPublicPath({}, this)
const interpolateName = (content = 'PLACEHOLDER') =>
  toSafeOutputPath(utils.interpolateName(this, options.name, {
fork icon20
star icon91
watch icon4

+ 15 other calls in file

2
3
4
5
6
7
8
9
10
11
const toPosix = require('./utils/to-posix')
const parseRequest = require('./utils/parse-request')
const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')

module.exports = function loader (content, prevOptions) {
  const options = prevOptions || loaderUtils.getOptions(this) || {}
  const context = options.context || this.rootContext

  let url = loaderUtils.interpolateName(this, options.name, {
    context,
fork icon350
star icon0
watch icon91

+ 3 other calls in file

216
217
218
219
220
221
222
223
224
225
```javascript
const loaderUtils = require('loader-utils')
module.exports = function(content){
    // content为文件内容
    // 获取webpack关于本loader的配置内容
    const options = loaderUtils.getOptions(this) || {}
    // 获取url路径
    const url = loaderUtils.interpolateName(this, options.name, {
        context: options.context || this.rootContext,
        content,
fork icon5
star icon73
watch icon10

20
21
22
23
24
25
26
27
28
29
30
31
32
module.exports = function (source) {


    this.cacheable();


    if (!hasRun) {
        var query = loaderUtils.getOptions(this);
        var envOpts = query.opts || {};
        if (query) {


            env = new nunjucks.Environment([], envOpts);
fork icon1
star icon2
watch icon0

7
8
9
10
11
12
13
14
15
16
17
18
var compileExports = require("./compile-exports");




module.exports = function(content, map) {
	var callback = this.async();
	var query = loaderUtils.getOptions(this) || {};
	var moduleMode = query.modules;
	var camelCaseKeys = query.camelCase;
	var sourceMap = query.sourceMap || false;

fork icon0
star icon1
watch icon0

11
12
13
14
15
16
17
18
19
20
21
22
function randomIdent() {
	return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
}


function getLoaderConfig(context) {
	var query = loaderUtils.getOptions(context) || {};
	var configKey = query.config || 'htmlLoader';
	var config = context.options && context.options.hasOwnProperty(configKey) ? context.options[configKey] : {};


	delete query.config;
fork icon1
star icon0
watch icon0

48
49
50
51
52
53
54
55
56
57
  rootContext,
  resourcePath,
  resourceQuery
} = loaderContext

const options = loaderUtils.getOptions(loaderContext) || {}

const filename = path.basename(resourcePath)
const context = rootContext || process.cwd()
const sourceRoot = path.dirname(path.relative(context, resourcePath))
fork icon0
star icon0
watch icon1

+ 4 other calls in file

36
37
38
39
40
41
42
43
44
45
46
47
 * - Execute any entrypoints specified in tui.json (entry and preEntry)
 */
module.exports = function(jsonSource, map) {
  const self = this;


  const options = getOptions(this);


  const config = JSON.parse(jsonSource);
  if (!config || !config.component) {
    throw new Error(
fork icon0
star icon0
watch icon0

+ 4 other calls in file

20
21
22
23
24
25
26
27
28
29
// webpack 1 compat
const resourceQuery = loaderContext.resourceQuery || '';
const compiler = loaderContext._compiler;
const isChildCompiler = compiler.isChild();
const parentCompiler = isChildCompiler ? compiler.parentCompilation.compiler : null;
const matchedRules = getOptions(loaderContext);

if (!content.includes('<svg')) {
  throw new Exceptions.InvalidSvg(content, matchedRules);
}
fork icon0
star icon0
watch icon1

+ 3 other calls in file

64
65
66
67
68
69
70
71
72
73
74
}


function _loader() {
  _loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
    const filename = this.resourcePath;
    let loaderOptions = loaderUtils.getOptions(this) || {};
    validateOptions(schema, loaderOptions, {
      name: "Babel loader"
    });

fork icon0
star icon0
watch icon1

+ 3 other calls in file

9
10
11
12
13
14
15
16
17
18
19
20
21
22
module.exports = function () {};


module.exports.pitch = function (request) {
	if (this.cacheable) this.cacheable();


	var options = loaderUtils.getOptions(this) || {};


	validateOptions(require('./options.json'), options, 'Style Loader (URL)');


	options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

50
51
52
53
54
55
56
57
58
59
this.cacheable()
const isServer = this.target === 'node'
const isProduction = this.minimize || process.env.NODE_ENV === 'production'

const loaderContext = this
const query = loaderUtils.getOptions(this) || {}
const options = Object.assign(
  {
    esModule: true
  },
fork icon0
star icon0
watch icon0

+ 2 other calls in file

18
19
20
21
22
23
24
25
26
27
28
module.exports = function svgReactLoader (source) {
    var context   = this;
    var callback  = context.async();
    var ctxOpts   = context.svgReactLoader;
    var filters   = ctxOpts && ctxOpts.filters || [];
    var query     = lutils.getOptions(context);
    var rsrcQuery = context.resourceQuery && lutils.parseQuery(context.resourceQuery);
    var params    = R.merge(query || {}, rsrcQuery || {});


    var titleCaseDelim = params.titleCaseDelim || /[._-]/;
fork icon0
star icon0
watch icon0