How to use the loader-utils.getOptions 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 xxxgitone / learningProcess / WebPack-Beginner / node_modules / babel-loader / lib / index.js View on Github external
module.exports = function (source, inputSourceMap) {
  var _this = this;

  // Handle filenames (#106)
  var webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
  var filename = webpackRemainingChain[webpackRemainingChain.length - 1];

  // Handle options
  var loaderOptions = loaderUtils.getOptions(this) || {};
  var fileSystem = this.fs ? this.fs : fs;
  var babelrcPath = null;
  if (loaderOptions.babelrc !== false) {
    babelrcPath = typeof loaderOptions.babelrc === "string" && exists(fileSystem, loaderOptions.babelrc) ? loaderOptions.babelrc : resolveRc(fileSystem, path.dirname(filename));
  }

  if (babelrcPath) {
    this.addDependency(babelrcPath);
  }

  var defaultOptions = {
    metadataSubscribers: [],
    inputSourceMap: inputSourceMap,
    sourceRoot: process.cwd(),
    filename: filename,
    cacheIdentifier: JSON.stringify({
github zeit / styled-jsx / src / webpack.js View on Github external
module.exports = function(content) {
  if (this.cacheable) this.cacheable()
  this.addDependency(this.resourcePath)
  const options = Object.assign({}, loaderUtils.getOptions(this))

  if (!options.type) {
    options.type = 'scoped'
  }

  // Calls type with the current file name.
  if (typeof options.type === 'function') {
    options.type = options.type(this.resourcePath, {
      query: loaderUtils.parseQuery(this.resourceQuery || '?') || {}
    })
  }

  if (!types.includes(options.type)) {
    return this.callback(
      'The given `type` option is invalid. \n\n' +
        `Expected:\n One of scoped|global|resolve \n\n` +
github symfony / webpack-encore / lib / webpack / copy-files-loader.js View on Github external
// https://github.com/webpack/webpack/issues/7057
    const requiredType = 'javascript/auto';
    if (this._module.type !== requiredType) {
        // Try to retrieve the factory used by the LoaderDependency type
        // which should be the NormalModuleFactory.
        const factory = this._compilation.dependencyFactories.get(LoaderDependency);
        if (factory === undefined) {
            throw new Error('Could not retrieve module factory for type LoaderDependency');
        }

        this._module.type = requiredType;
        this._module.generator = factory.getGenerator(requiredType);
        this._module.parser = factory.getParser(requiredType);
    }

    const options = loaderUtils.getOptions(this);

    // Retrieve the real path of the resource, relative
    // to the context used by copyFiles(...)
    const context = options.context;
    const resourcePath = this.resourcePath;
    const relativeResourcePath = path.relative(context, resourcePath);

    // Retrieve the pattern used in copyFiles(...)
    // The "source" part of the regexp is base64 encoded
    // in case it contains characters that don't work with
    // the "inline loader" syntax
    const pattern = new RegExp(
        Buffer.from(options.patternSource, 'base64').toString(),
        options.patternFlags
    );
github mpvue / mpvue-loader / lib / template-compiler / index.js View on Github external
module.exports = function (html) {
  this.async()
  this.cacheable()
  var isServer = this.target === 'node'
  var isProduction = this.minimize || process.env.NODE_ENV === 'production'
  var vueOptions = this.options.__vueOptions__ || {}
  var options = loaderUtils.getOptions(this) || {}

  var defaultModules = [transformRequire(options.transformToRequire, {
    outputPath: this.options.output.path,
    resourcePath: this.resourcePath,
    context: this.options.context
  })]

  var userModules = vueOptions.compilerModules || options.compilerModules
  // for HappyPack cross-process use cases
  if (typeof userModules === 'string') {
    userModules = require(userModules)
  }

  var compilerOptions = {
    preserveWhitespace: options.preserveWhitespace,
    modules: defaultModules.concat(userModules || []),
github perchlabs / po-loader / index.js View on Github external
module.exports = function(source) {
    this.cacheable();

    var options = utils.getOptions(this);
    if (options === null) {
        options = {};
    }

    // default option
    if (!('stringify' in options)) {
        options.stringify = true;
    }

    jsonData = po2json.parse(source, options);

    return jsonData;
}
github tinajs / mina-webpack / packages / mina-entry-webpack-plugin / src / loaders / virtual-mina-loader.ts View on Github external
* - https://github.com/webpack/webpack/issues/7057#issuecomment-381883220
   * - https://github.com/webpack/webpack/issues/6572#issuecomment-374987270
   */
  if (
    JavascriptGenerator &&
    JavascriptParser &&
    this._module.type !== 'javascript/auto'
  ) {
    this._module.type = 'javascript/auto'
    this._module.generator = new JavascriptGenerator()
    this._module.parser = new JavascriptParser()
  }

  const done = this.async()!

  const options = loaderUtils.getOptions(this) || {
    extensions: {},
  }

  this.addContextDependency(dirname(this.resourcePath))

  pProps(options.extensions, (extnames: Array) => {
    let findFileWithExtname = (extname: string) => {
      let filePath = replaceExt(this.resourcePath, `.${extname}`)
      // @ts-ignore
      return fs.exists(filePath).then(isExist => ({ isExist, filePath }))
    }
    return pAny(extnames.map(findFileWithExtname), {
      filter: ({ isExist }) => isExist,
    }).then(
      ({ filePath }) => {
        this.addDependency(filePath)
github shepherdwind / css-hot-loader / loader.js View on Github external
module.exports = function(content) {
  this.cacheable();
  const options = Object.assign(
    {},
    defaultOptions,
    loaderUtils.getOptions(this)
  );

  const accept = options.cssModule ? '' : 'module.hot.accept(undefined, cssReload);';
  return content + `
    if(module.hot) {
      // ${Date.now()}
      var cssReload = require(${loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'hotModuleReplacement.js'))})(module.id, ${JSON.stringify(options)});
      module.hot.dispose(cssReload);
      ${accept};
    }
  `;
};
github alibaba / rax / packages / jsx2mp-loader / src / page-loader.js View on Github external
module.exports = async function pageLoader(content) {
  const loaderOptions = getOptions(this);
  const { platform, entryPath, mode, disableCopyNpm, constantDir, turnOffSourceMap, pageConfig = {} } = loaderOptions;
  const rawContent = readFileSync(this.resourcePath, 'utf-8');
  const resourcePath = this.resourcePath;
  const rootContext = this.rootContext;
  const absoluteConstantDir = constantDir.map(dir => join(rootContext, dir));

  const outputPath = this._compiler.outputPath;
  const sourcePath = join(rootContext, dirname(entryPath));
  const relativeSourcePath = relative(sourcePath, this.resourcePath);
  const targetFilePath = join(outputPath, relativeSourcePath);

  const isFromConstantDir = cached(isFromTargetDirs(absoluteConstantDir));

  const compilerOptions = Object.assign({}, compiler.baseOptions, {
    resourcePath: this.resourcePath,
    outputPath,
github tinajs / mina-webpack / packages / mina-loader / src / loaders / mina.ts View on Github external
const mina: webpack.loader.Loader = function mina() {
  this.cacheable()

  const done = this.async()
  const webpackOptions = loaderUtils.getOptions(this) || {}
  const extensions: Partial> = {
    config: DEFAULT_EXTENSIONS.CONFIG,
    template: DEFAULT_EXTENSIONS.TEMPLATE,
    style: DEFAULT_EXTENSIONS.STYLE,
  }
  const options = merge(
    {},
    {
      loaders: {
        config: '',
        template: '',
        script: '',
        style: '',
      },
      languages: {},
      extensions,
github QwantResearch / erdapfel / local_modules / po-js-loader / index.js View on Github external
module.exports = function(source) {
  if(this.cacheable) {
    this.cacheable()
  }
  let options = utils.getOptions(this)
  options = options || {}
  return poToJs(source, options)
}