• Florian Goße's avatar
    Use keyword `warn` in eslint config (#234) · e839dff8
    Florian Goße authored
    * Use keyword `warn` in eslint config
    
    We can use the keyword `warn` for rule configuration instead of a number.
    
    * Fix comment which where WARNING was replaced
    
    * Remove extra apostrophe
    e839dff8
index.js 1.76 KiB
'use strict';
const { extname } = require('path');
function namedAssetImportPlugin({ types: t }) {
  const visited = new WeakSet();
  return {
    visitor: {
      ImportDeclaration(
        path,
          opts: { loaderMap },
      ) {
        const sourcePath = path.node.source.value;
        const ext = extname(sourcePath).substr(1);
        if (visited.has(path.node) || sourcePath.indexOf('!') !== -1) {
          return;
        if (loaderMap[ext]) {
          path.replaceWithMultiple(
            path.node.specifiers.map(specifier => {
              if (t.isImportDefaultSpecifier(specifier)) {
                const newDefaultImport = t.importDeclaration(
                    t.importDefaultSpecifier(
                      t.identifier(specifier.local.name)
                  t.stringLiteral(sourcePath)
                visited.add(newDefaultImport);
                return newDefaultImport;
              const newImport = t.importDeclaration(
                  t.importSpecifier(
                    t.identifier(specifier.local.name),
                    t.identifier(specifier.imported.name)
                t.stringLiteral(
                  loaderMap[ext][specifier.imported.name]
                    ? loaderMap[ext][specifier.imported.name].replace(
                        /\[path\]/,
                        sourcePath
                    : sourcePath
              visited.add(newImport);
              return newImport;
module.exports = namedAssetImportPlugin;