tompascall/js-to-styles-var-loader

load js from node_modules

theinterned opened this issue · 4 comments

Hi,

Thanks for building this awesome project!

I am trying to load vars from an npm package (in /node_modules). This doesn't seem to be possible though.

Here are some things I tried:

  • require('@nulogy/tokens');;
  • require('@nulogy/tokens/index.js');;
  • require('~@nulogy/tokens'); and require('~@nulogy/tokens/index.js'); – using the ~ prefix from the less-loader webpack resolver syntax.
  • require('../../../node_modules/@nulogy/tokens/');
  • require('../../../node_modules/@nulogy/tokens/index.js');

I leven tried to set my node_modules folders (I am using yarn workspaces so I have two) in less_loader:

 { 
  loader: 'less-loader', 
    options: {
      paths: [
        path.resolve('node_modules'),
        path.resolve('../node_modules')
       ]
    } 
},

In all cases I got an error:

ERROR in ../node_modules/css-loader!../node_modules/less-loader/dist/cjs.js!../node_modules/js-to-styles-var-loader!./src/cssPreprocessorResearch/less.less
Module build failed: 


require('@nulogy/tokens');
^
Function 'require' is undefined
      in /Users/user/src/nulogy/design-system/sandbox/src/cssPreprocessorResearch/less.less (line 13, column 0)
 @ ./src/cssPreprocessorResearch/less.less 2:14-181 21:1-42:3 22:19-186
 @ ./src/cssPreprocessorResearch/less.story.js
 @ ./src \.story.js$
 @ ./.storybook/config.js
 @ multi ./node_modules/@storybook/react/dist/server/config/polyfills.js ./node_modules/@storybook/react/dist/server/config/globals.js ../node_modules/webpack-hot-middleware/client.js?reload=true ./.storybook/config.js

You can see the code on this branch: https://github.com/nulogy/design-system/tree/loading-js-vars-into-less-from-node-modules

My webpack config is here: https://github.com/nulogy/design-system/blob/loading-js-vars-into-less-from-node-modules/sandbox/.storybook/webpack.config.js

The less file is here in this folder: https://github.com/nulogy/design-system/tree/loading-js-vars-into-less-from-node-modules/sandbox/src/cssPreprocessorResearch

specifically: https://github.com/nulogy/design-system/blob/loading-js-vars-into-less-from-node-modules/sandbox/src/cssPreprocessorResearch/less.less

I am able to successfully load vars from a local JS file with a relative path. But not from an npm package

I tried to require vars from a node_module in my project, and I could with using relative path (without adding path to less loader). Your error is quite interesting, Function 'require' is undefined. I could not reproduce it (I tried to build your project, but haven't seen this error message). How can I reproduce it?

This issue is very old! Closing.