Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 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 Answer

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',
    },
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...