使用插件将RequireJS / AMD迁移到Webpack

问题描述 投票:0回答:1

我正在努力将大型RequireJS应用程序迁移到Webpack。使用Webpack的基本构建似乎工作正常 - 我已将“路径”定义移动到“别名”,并且我为我的内容和填充程序设置了加载器,如jQuery。

但是,还有一个问题我不知道如何解决。基本上RequireJS应用程序使用“text-plugin”来包含HTML模板,而Webpack正在为HTML模板抛出“Module not found”错误。

我要捆绑的示例AMD模块看起来像这样:

带有文本插件的AMD模块

define([
   'security',
   'modals',
   'text!../templates/contact_info.html'
], function(security, modals, contactInfoTemplate) {
   return {
      foo: function() { return "bar"; }
   };
});

我以为我可以使用raw-loader加载模板文件。我将'text'别名为'raw-loader':

text: {
        test: /\.html$/,
        loader: "raw-loader"
      },

但是,我看到所有我需要的模板都出现以下错误:

Module not found: Error: Can't resolve 'text'
BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders. You need to specify 'text-loader' instead of 'text'.

我尝试用'text-loader!...'替换'text!...',然后我看到这个错误抱怨它无法加载/找到HTML模块!

Module not found: Error: Can't resolve '../templates/contact_info.html'

webpack.config.js,版本3.9.1

const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const path = require('path');
const webpack = require('webpack');

let basePath = path.join(__dirname, '/');

module.exports = {
  entry: {
    'main': basePath +  'js/main.js',
  },
  context: __dirname,
  output: {
    path: __dirname + '/build',
    filename: '[name].min.js',
    libraryTarget: 'amd',
    umdNamedDefine: true
  },
  module: {
    rules: [
      {
        test: /(\.js)$/,
        exclude: /(node_modules)/,
        use: {
          // babel-loader to convert ES6 code to ES5 + amdCleaning requirejs code into simple JS code, taking care of modules to load as desired
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: []
          }
        }
      },
      {
        test: /\.css$/,
        use: [
          { loader: "style-loader" },
          { loader: "css-loader" }
        ]
      },
      { test: /\.jpg$/, use: [ "file-loader" ] },
      { test: /\.png$/, use: [ "url-loader?mimetype=image/png" ] },
      {
        test: /\.(html)$/,
        use: {
          loader: 'raw-loader',
          options: {
            minimize: true
          }
        }
      }
    ]
  },
  resolve: {
    modules: [
      'js/**/*.js',
      'node_modules',
      path.resolve('./js')
    ],
    extensions: ['.js'], // File types,
    alias: {
      text: {
        test: /\.html$/,
        loader: "raw-loader"
      },
      bridge: 'libs/bridge',
      cache: 'libs/cache',
      cards: 'libs/cards',
      moment: 'libs/moment',
      underscore: 'libs/underscore',
    }
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: '../index.html'
    }),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    })
  ]
};

任何人都知道如何让Webpack与RequireJS Text插件很好地配合使用?

谢谢!

requirejs migrate webpack-3 requirejs-text
1个回答
1
投票

也许尝试安装text-loader

为了让像'text!../templates/contact_info.html'这样的东西正确“加载”,因为它不是JS,你需要安装text-loader来获取webpack来理解语法text!

npm install text-loader --save-dev

嗯...我刚刚安装了text-loaded,似乎我们也必须将text!改为text-loader!

© www.soinside.com 2019 - 2024. All rights reserved.