代码之家  ›  专栏  ›  技术社区  ›  RoboBear

将带有插件的RequireJS/AMD迁移到Webpack

  •  0
  • RoboBear  · 技术社区  · 6 年前

    我正在将一个大型的RequireJS应用程序迁移到Webpack。Webpack的基本构建似乎很好——我已经将“路径”定义移到了“别名”,并且为我的内容和垫片设置了加载程序,比如jQuery。

    我想捆绑的AMD模块示例如下:

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

    思想 我可以使用原始加载程序来加载模板文件。我把“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'.
    

    我试着替换“文本”使用“文本加载器…”,然后我看到这个错误抱怨它不能加载/找到HTML模块!

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

    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文本插件配合使用吗?

    谢谢!

    1 回复  |  直到 6 年前
        1
  •  2
  •   thehme    6 年前

    或许可以尝试安装 text-loader ?

    'text!../templates/contact_info.html' 要正确“加载”,因为它不是JS,所以需要安装 让webpack理解语法 text!

    npm install text-loader --save-dev

    嗯…我刚安装了 text-loaded 看来我们也要改变了 短信! text-loader!

    推荐文章