How to use the loader-utils.getCurrentRequest function in loader-utils

To help you get started, we’ve selected a few loader-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sx1989827 / DOClever / Desktop / node_modules / css-loader / lib / loader.js View on Github external
if (map.sources) {
				map.sources = map.sources.map(function (source) {
					return source.replace(/\\/g, '/');
				});
				map.sourceRoot = '';
			}
		}
	} else {
		// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
		map = null;
	}

	processCss(content, map, {
		mode: moduleMode ? "local" : "global",
		from: loaderUtils.getRemainingRequest(this).split("!").pop(),
		to: loaderUtils.getCurrentRequest(this).split("!").pop(),
		query: query,
		resolve: resolve,
		minimize: this.minimize,
		loaderContext: this,
		sourceMap: sourceMap
	}, function(err, result) {
		if(err) return callback(err);

		var cssAsString = JSON.stringify(result.source);

		// for importing CSS
		var importUrlPrefix = getImportPrefix(this, query);

		var alreadyImported = {};
		var importJs = result.importItems.filter(function(imp) {
			if(!imp.mediaQuery) {
github fossasia / susper.com / node_modules / css-loader / lib / loader.js View on Github external
if (map.sources) {
				map.sources = map.sources.map(function (source) {
					return source.replace(/\\/g, '/');
				});
				map.sourceRoot = '';
			}
		}
	} else {
		// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
		map = null;
	}

	processCss(content, map, {
		mode: moduleMode ? "local" : "global",
		from: loaderUtils.getRemainingRequest(this).split("!").pop(),
		to: loaderUtils.getCurrentRequest(this).split("!").pop(),
		query: query,
		resolve: resolve,
		minimize: this.minimize,
		loaderContext: this,
		sourceMap: sourceMap
	}, function(err, result) {
		if(err) return callback(err);

		var cssAsString = JSON.stringify(result.source);

		// for importing CSS
		var importUrlPrefix = getImportPrefix(this, query);

		var alreadyImported = {};
		var importJs = result.importItems.filter(function(imp) {
			if(!imp.mediaQuery) {
github sx1989827 / DOClever / Client / node_modules / css-loader / lib / loader.js View on Github external
if (map.sources) {
				map.sources = map.sources.map(function (source) {
					return source.replace(/\\/g, '/');
				});
				map.sourceRoot = '';
			}
		}
	} else {
		// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
		map = null;
	}

	processCss(content, map, {
		mode: moduleMode ? "local" : "global",
		from: loaderUtils.getRemainingRequest(this).split("!").pop(),
		to: loaderUtils.getCurrentRequest(this).split("!").pop(),
		query: query,
		resolve: resolve,
		minimize: this.minimize,
		loaderContext: this,
		sourceMap: sourceMap
	}, function(err, result) {
		if(err) return callback(err);

		var cssAsString = JSON.stringify(result.source);

		// for importing CSS
		var importUrlPrefix = getImportPrefix(this, query);

		var alreadyImported = {};
		var importJs = result.importItems.filter(function(imp) {
			if(!imp.mediaQuery) {
github JonDum / polymer-loader / index.js View on Github external
"\tvar styles = require('./"+elementName+"."+styleExtension+"');",
                "\tvar el = document.createElement('div');",
                "\tel.setAttribute('name', '"+elementName+"')",
                "\tel.setAttribute('hidden','')",
                "\tel.innerHTML = '<dom-module id=\""+elementName+"\"><template><style>'+styles+'</style></template></dom-module>';",
                "\tdocument.body.appendChild(el);",
            "})(document);"
        ].join('\n');
    }

    // support existing SourceMap
    // https://github.com/mozilla/source-map#sourcenode
    // https://github.com/webpack/imports-loader/blob/master/index.js#L34-L44
    // https://webpack.github.io/docs/loaders.html#writing-a-loader
    if (sourceMap) {
        var currentRequest = loaderUtils.getCurrentRequest(this);
        var SourceNode = SourceMap.SourceNode;
        var SourceMapConsumer = SourceMap.SourceMapConsumer;
        var sourceMapConsumer = new SourceMapConsumer(sourceMap);
        var node = SourceNode.fromStringWithSourceMap(source, sourceMapConsumer);

        node.prepend(inject);

        var result = node.toStringWithSourceMap({
            file: currentRequest
        });

        this.callback(null, result.code, result.map.toJSON());

        return;
    }
github niieani / webpack-dependency-suite / utils / inject.ts View on Github external
export function appendCodeAndCallback(loader: Webpack.Core.LoaderContext, source: string, inject: string, sourceMap?: SourceMap.RawSourceMap, synchronousIfPossible = false) {
  inject += (!source.trim().endsWith(';')) ? ';\n' : '\n'

  // support existing SourceMap
  // https://github.com/mozilla/source-map#sourcenode
  // https://github.com/webpack/imports-loader/blob/master/index.js#L34-L44
  // https://webpack.github.io/docs/loaders.html#writing-a-loader
  if (sourceMap) {
    const currentRequest = loaderUtils.getCurrentRequest(loader)
    const SourceNode = SourceMap.SourceNode
    const SourceMapConsumer = SourceMap.SourceMapConsumer
    const sourceMapConsumer = new SourceMapConsumer(sourceMap)
    const node = SourceNode.fromStringWithSourceMap(source, sourceMapConsumer)

    node.add(inject)

    const result = node.toStringWithSourceMap({
      file: currentRequest
    })

    loader.callback(null, result.code, result.map.toJSON())
  } else {
    if (synchronousIfPossible) {
      return inject ? source + inject : source
    } else {
github Khan / perseus / node / jsx-loader.js View on Github external
module.exports = function(source) {
    this.cacheable && this.cacheable();

    const sourceFilename = loaderUtils.getRemainingRequest(this);
    const current = loaderUtils.getCurrentRequest(this);

    const query = loaderUtils.parseQuery(this.query);
    // For some reason, Babel doesn't use .babelrc here; maybe because we're
    // in a subfolder.
    const options = _.extend({}, babelrc, {
        sourceMaps: query.sourceMap,
    });
    const transform = babel.transform(source, options);

    // NOTE(charlie): If we remove these additional transforms, we can use the
    // standard babel-loader.
    if (includeEditor) {
        // TODO(emily): Do this with a real AST transform.
        transform.code = transform.code.replace(/__EDITOR__/g, "true");
    } else {
        // When removing editors, we use uglifyjs to strip out dead code from
github yuhaoju / webpack-config-handbook / 03-loaders / how-to-write-a-loader / force-strict-loader / index.js View on Github external
module.exports = function(content, sourceMap) {
  var useStrictPrefix = '\'use strict\';\n\n';
  if (this.cacheable) {
    this.cacheable();
  }
  // source-map
  var options = loaderUtils.getOptions(this) || {};
  if (options.sourceMap && sourceMap) {
    var currentRequest = loaderUtils.getCurrentRequest(this);
    var node = SourceNode.fromStringWithSourceMap(
      content,
      new SourceMapConsumer(sourceMap)
    );
    node.prepend(useStrictPrefix);
    var result = node.toStringWithSourceMap({ file: currentRequest });
    this.callback(null, result.code, result.map.toJSON());
    return;
  }
  return useStrictPrefix + content;
};
github zinserjan / shim-loader / src / shimLoader.js View on Github external
module.exports = function shimLoader(code, sourcemap) {
  // cacheable loader
  this.cacheable();

  const shim = getShim(this);

  if (typeof shim !== 'undefined') {
    const [requestedModule, shimConfig] = shim;
		const file = getCurrentRequest(this);
    const result = transform(requestedModule, code, sourcemap, file, shimConfig);

		this.callback(null, result.source, result.map);
		return;
  }

	if (sourcemap) {
		this.callback(null, code, sourcemap);
		return;
	}

  return code;
};
github retyui / group-css-media-queries-loader / src / index.js View on Github external
function GroupCssMediaQueriesLoader(inputSource, prevMap, meta) {
  const options = getOptions(this) || {};
  const sourceMap =
    options.sourceMap === undefined
      ? this.sourceMap
      : Boolean(options.sourceMap);
  const callback = this.async();

  pipeline
    .process(inputSource, {
      from: getRemainingRequest(this)
        .split("!")
        .pop(),
      to: getCurrentRequest(this)
        .split("!")
        .pop(),
      map: sourceMap
        ? {
            prev: prevMap,
            sourcesContent: true,
            inline: false,
            annotation: false
          }
        : null
    })
    .then(result => {
      callback(null, result.css, result.map && result.map.toJSON(), meta);
    })
    .catch(callback);
}
github jlongster / sweetjs-loader / index.js View on Github external
module.exports = function(source) {
  var loader = this;
  loader.async();
  var config = loaderUtils.parseQuery(loader.query);
  var loaderRequest = loaderUtils.getCurrentRequest(this);
  var fileRequest = loaderUtils.getRemainingRequest(this);
  config.modules = config.modules || [];

  RSVP.all(config.modules.map(function(mod) {
    if(moduleCache[mod]) {
      return moduleCache[mod];
    }
    return resolve(loader, mod).then(function(res) {
      moduleCache[mod] = sweet.loadNodeModule(process.cwd(), res);
      return moduleCache[mod];
    });
  })).then(function(modules) {
    if(config.readers) {
      return RSVP.all(config.readers.map(function(mod) {
        return resolve(loader, mod).then(function(res) {
          sweet.setReadtable(res);