bootstrap-loader icon indicating copy to clipboard operation
bootstrap-loader copied to clipboard

make `extract-text-webpack-plugin` resolve path configurable

Open vjpr opened this issue 8 years ago • 3 comments
trafficstars

When bootstrap-loader is npm linked, because it has extract-text-webpack-plugin in its devDependencies, it will use its local copy instead of the user's project's copy. Only one module instance should be used in the whole project.

Having a configuration option to pass in the absolute path would solve this problem.

The change needs to be made to buildExtractStylesLoader.js.

vjpr avatar Feb 13 '17 21:02 vjpr

Hi @vjpr!

  1. Would this apply to those trying to contribute this project only?
  2. Could we make this configurable? Would you like to make a PR with this?

Thanks for your help!

justin808 avatar Feb 13 '17 21:02 justin808

  1. Would this apply to those trying to contribute this project only?

Yep, purely a development feature. When it is installed by npm/pnpm/yarn the peerDependency should work correctly.

  1. Could we make this configurable?

Just need to pass a config here: https://github.com/vjpr/bootstrap-loader/blob/f889aa7673877115d425c3dc162102c66089635d/src/bootstrap.loader.js#L174-L174

Would you like to make a PR with this?

Yep I'll put one together when I have some time.


Workaround

At present I am using the following require hack which ensures that same version of extract-text-webpack-plugin for those who might want a workaround.

// Require hack to ensure same version of ExtractTextPlugin is used everywhere.

var builtinModules = require('builtin-modules');

function isBuiltinModule(str) {
    if (typeof str !== 'string') {
      throw new TypeError('Expected a string');
    }
    return builtinModules.indexOf(str) !== -1;
}

let modPath = null

const Module = require('module')
const fs = require('fs')
const {join} = require('path')
const oldResolveFilename = Module._resolveFilename
Module._resolveFilename = function(request, parent, isMain) {
  if (!request.startsWith('.') && !isBuiltinModule(request)) {
    const name = 'extract-text-webpack-plugin'
    if (request.includes(name)) {
      // TODO(vjpr): I don't think this is robust! Or maybe it is.
      const posAfterPath = request.lastIndexOf(name) + name.length
      const _modPath = request.substring(0, posAfterPath)
      const fileRequest = request.substring(posAfterPath)

      if (!modPath) {
        const filename = oldResolveFilename.call(this, request, parent, isMain)
        const posAfterPath = filename.lastIndexOf(name) + name.length
        const _modPath = filename.substring(0, posAfterPath)
        const fileRequest = filename.substring(posAfterPath)
        // cache first request
        modPath = _modPath
        console.log('WARNING')
        console.log(`All requests for '${name}' will be mapped to '${modPath}'`)
        console.log('This is to ensure that only one instance of the module is used.')
        console.log('WARNING')
      }
      const out = join(modPath, fileRequest)
      //return fs.realpathSync(filename)
      request = out

    }
  }
  return oldResolveFilename.call(this, request, parent, isMain)

}

vjpr avatar Feb 13 '17 22:02 vjpr

2.0.0-beta.21 pushed to npm! this might help!

justin808 avatar Feb 17 '17 06:02 justin808