at-import / node-sass-import-once

Eyeglass style Import Once, but for all the things!
Other
94 stars 23 forks source link

Not compatible with Yarn 2 (PnP) #37

Open bertho-zero opened 4 years ago

bertho-zero commented 4 years ago

My workaround:

const importOnce = require('node-sass-import-once');
let pnp;

try {
  pnp = require('pnpapi');
} catch (error) { // pnpapi is not defined
  pnp = null;
}

function importer(uri, prev, done) {
  let resolution;
  let pnpError;

  try {
    resolution = pnp.resolveToUnqualified(uri, prev, { considerBuiltins: false });
  } catch (error) {
    pnpError = error;
  }

  importOnce.call(this, resolution || uri, prev, result => {
    const keys = Object.keys(result);

    if (keys.length) {
      return done(result);
    }

    if (pnpError) {
      console.error(pnpError);
    }

    return importOnce.call(this, uri, prev, done);
  });
}

/***/

sass.render({
  file: filename,
  importer: pnp ? importer : importOnce,
  includePaths: pnp
    ? []
    : [
      path.resolve(__dirname, '..'),
      path.resolve(__dirname, '../node_modules'),
      path.resolve(__dirname, '../../../node_modules')
    ]
})