从 requirejs 迁移到 webpack [英] Migrating from requirejs to webpack

查看:40
本文介绍了从 requirejs 迁移到 webpack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个基于 require.js 的项目迁移/移动到 webpack v3.由于我的所有模块都使用以下语法:

I'm migrating/moving a project based on require.js to webpack v3. Since all my modules are using the following syntax:

    define([modules,..], function(mod1,..) 

其中声明要使用哪些模块,并将模块分配给匿名函数中的变量.从 webpack v2 开始,这似乎已被弃用.我找不到任何相关信息(除了 webpack v1 的文档).

Which declares which modules to use, and assigns the modules to the variables in the anonymous function. This seems to be deprecated since v2 of webpack. I can't find any information about this (except for the documentation for web pack v1).

我应该将所有模块重写为 commonjs(包括依赖项)还是有任何聪明的方法来使用 AMD 模块?

Should I rewrite all my modules to the commonjs (including dependencies) or are there any smart way to use the AMD modules?

非常感谢帮助:-)问候

Help much appreciated :-) Regards

推荐答案

AMD 在 requirejs 之外从未发现太多用途,因此您可能需要转换.有一些工具可以提供帮助:

AMD never found much use outside of requirejs so likely you will need to convert. There are tools that will help:

  • https://github.com/anodynos/uRequire can convert code from AMD -> UMD / CommonJS

有来自 (https://github.com/anodynos/uRequire/wiki/nodejs-模板):

  • 将模型/PersonModel 等路径运行时转换为 ../../models/PersonModel,具体取决于从何处调用.您仍然会得到构建时转换的 bundleRelative 路径,到它们的 nodejs 文件相对等价物.

对于大多数项目来说,这不是问题.

For most projects this is not an issue.

  • 不能使用异步版本的 require(['dep'], function(dep){...})

您应该能够使用 require 的同步版本.如果使用 webpack2,你可以使用 System.importrequire.ensure

You should be able to use the synchronous version of require. If using webpack2 you can use System.import or require.ensure

  • 无法运行 requirejs 加载器插件,例如 text!... 或 json!...

你会找到所有这些插件的 webpack 版本

You will find webpack version of all of these plugins

  • 没有/的映射,即 webRootMap 等或使用 requirejs.config 的 {baseUrl:"...."} 或 {paths:"lib":"../../lib"}

这可以用 https://www.npmjs.com/复制包/babel-plugin-module-alias

这篇关于从 requirejs 迁移到 webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆