0

在我的项目中,我需要利用ts-loader以合理的方式加载antd 。我收到以下错误:

✖ 「wds」: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.module.rules[0].use should be one of these:
   non-empty string | function | object { loader?, options?, ident?, query? } | function | [non-empty string | function | object { loader?, options?, ident?, query? }]
   -> Modifiers applied to the module when rule is matched
   Details:
    * configuration.module.rules[0].use should be a string.
    * configuration.module.rules[0].use should be an instance of function

这是有问题的代码:

const getTsLoaderRule = env => {
  const rules = [
    { loader: 'cache-loader' },
    {
        loader: 'thread-loader',
        options: {
            // there should be 1 cpu for the fork-ts-checker-webpack-plugin
            workers: require('os').cpus().length - 1
        }
    },
    {
      test: /\.(jsx|tsx|js|ts)$/,
      loader: 'ts-loader',
      options: {
        transpileOnly: true,
        getCustomTransformers: () => ({
          before: [ tsImportPluginFactory( {/*plugin to load antd library*/
            libraryName: 'antd',
            libraryDirectory: 'lib',
            style: true
          }) ]
          }),
          compilerOptions: {
            module: 'es2015'
          }
        },
        exclude: /node_modules/
    },
  ];
  if (env === 'development') {
    rules.unshift({
      loader: 'react-hot-loader/webpack'
    });
  }
  return rules;
};

如何使用 ts-loader 正确加载插件?编辑我需要包含 ts-loader 插件选项,如下所示,以允许动态加载库:选项:{

transpileOnly:真,

getCustomTransformers: () => ({
  before: [ tsImportPluginFactory( {/*plugin to load antd library*/
    libraryName: 'antd',
    libraryDirectory: 'lib',
    style: true
  }) ]
  }),
  compilerOptions: {
    module: 'es2015'
  }
}

更多细节在这里

4

1 回答 1

1

只需在此处使用基本配置文件或使用以下代码:

const path = require('path'); module.exports = {
    entry: './src/index.ts',
     module: {
             rules: [{
                 test: /\.tsx?$/,
                 use: 'ts-loader',
                 exclude: /node_modules/
             }
            ],
         },
    resolve: {
             extensions: ['.tsx', '.ts', '.js']
         },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'main.js'
    } }
于 2018-07-09T13:50:55.337 回答