简体   繁体   English

使用webpack目标节点从node_modules导入css

[英]import css from node_modules with webpack target node

I'm currently building a react app with server side rendering. 我目前正在构建一个带有服务器端渲染的反应应用程序。 I'm using some libraries that come with css files. 我正在使用一些带有css文件的库。 When I try to import them like this: 当我尝试像这样导入它们时:

import 'leaflet/dist/leaflet.css';

I get the following error in my server.js : 我在server.js收到以下错误:

/my/app/path/node_modules/leaflet/dist/leaflet.css:3
.leaflet-pane,
^

SyntaxError: Unexpected token .
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.__webpack_exports__.a (/my/app/path/server.js:725:18)

Importing with 导入

import '../../node_modules/leaflet/dist/leaflet.css';

works. 作品。

Is there some way to configure webpack that I can import those css files normally? 有没有办法配置webpack,我可以正常导入这些CSS文件?

Here's the webpack config for server.js : 这是server.js的webpack配置:

const nodeExternals = require('webpack-node-externals');
const postcssImport = require('postcss-import');
const cssnext = require('postcss-cssnext');
const cssnano = require('cssnano');

module.exports = {
  entry: './src/server.js',
  output: {
    filename: 'server.js',
    path: __dirname,
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          presets: [
            'react',
            ['env', {
              modules: false,
              targets: {
                node: process.versions.node,
              },
            }],
          ],
        },
      },
      {
        test: /\.css$/,
        use: [
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              plugins: [
                postcssImport,
                cssnext,
                cssnano({
                  safe: true,
                  autoprefixer: false,
                }),
              ],
            },
          },
        ],
      },
      {
        test: /\.png$/,
        use: ['url-loader?limit=100000'],
      },
    ],
  },
  target: 'node',
  externals: [nodeExternals()],
};

It is an issue of webpack-node-externals which prevents bundling all resources located under node_modules directory. 这是webpack-node-externals一个问题,它可以防止捆绑位于node_modules目录下的所有资源。

You can use the whitelist option to avoid this behavior. 您可以使用whitelist选项来避免此行为。

Here is an example from documentation of webpack-node-externals : 以下是webpack-node-externals 文档中的示例:

...
nodeExternals({
   // load non-javascript files with extensions, presumably via loaders
   whitelist: [/\.(?!(?:jsx?|json)$).{1,5}$/i],
}),
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM