Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

javascript - What's the recommended way to import libraries inside webpack.config.js?

I always used require call inside webpack.config.js to import libraries for my build workflow. However, recently I worked on a project where import is used.

Generally, it works fine but seems like some supporting tools that import webpack.config.js cannot import it when import is used inside of it with error:

Cannot use import statement outside a module.

As a tryout, I changed the config to run through babel and it worked, though for me looks like I am fine to use just good old require and official documentation still uses it.

Should I rollback import to avoid the caveats or I can use them without problem if I pass it through babel and renaming to webpack.config.babel.js?

question from:https://stackoverflow.com/questions/65891343/whats-the-recommended-way-to-import-libraries-inside-webpack-config-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After some research, I got to a solution - transpile webpack config before passing it to a library I used which failed to load it (eslint-import-resolver-webpack).

So, I don't load webpack.config.js directly, but via babel transpiler:

const webpackConfigTranspiled = babel.transformFileSync(
  path.resolve(__dirname, './webpack.config.js'),
).code;

Then pass the transpiled content to the library/tool:

settings: {
    'import/resolver': {
      webpack: webpackConfigTranspiled,
    },
    'css-modules': {
      basePath: 'src',
    },
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...