From 8de6558e33487e7606e7cd7cb2adc2cccafef272 Mon Sep 17 00:00:00 2001 From: Samer Masterson Date: Thu, 14 Sep 2017 01:25:17 -0700 Subject: [PATCH] fix: get real path from `__filename` instead of `__dirname` (`NS`) Without this fix, this plugin doesn't work if your node_modules tree is made up of directories, but your files are symlinks. It's admittedly a weird environment, but that's the environment that the Bazel build system runs code in, which is the build system we use at Dropbox. This is the bug: - index.js is called from the runfiles directory, and it uses the realpath of its directory as the key for a function on the loaderContext object. - loader.js is called from the bazel-bin/npm directory outside of its runfiles (because Webpack escapes the runfiles when it calls loaders). It uses the realpath of its directory as a key on the loaderContext object, but because it's in a different directory from index.js, it can't find the function set on the loaderContext by index.js. The fix is to get the realpath of the filename instead of the directory so that both files point to the same place. --- src/index.js | 2 +- src/loader.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 69787f4b..7bf406ab 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ import { isFunction, } from './lib/helpers'; -const NS = fs.realpathSync(__dirname); +const NS = path.dirname(fs.realpathSync(__filename)); let nextId = 0; diff --git a/src/loader.js b/src/loader.js index 83ae03a8..f552c6eb 100644 --- a/src/loader.js +++ b/src/loader.js @@ -1,4 +1,5 @@ import fs from 'fs'; +import path from 'path'; import loaderUtils from 'loader-utils'; import NodeTemplatePlugin from 'webpack/lib/node/NodeTemplatePlugin'; import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin'; @@ -6,7 +7,7 @@ import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin'; import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'; import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin'; -const NS = fs.realpathSync(__dirname); +const NS = path.dirname(fs.realpathSync(__filename)); export default source => source;