gpt4 book ai didi

angularjs - Webpack 正在加载模块两次

转载 作者:行者123 更新时间:2023-12-01 03:40:09 25 4
gpt4 key购买 nike

我正在使用 webpack 构建我的 angular.js 应用程序。
举一个简单的例子:

app.js

var angular = require('exports?window.angular!angular'),
angularMoment = require('angular-moment');

angular.module('myApp', [angularMoment]);

Angular 矩.js
define(['angular', 'moment'], function(angular, moment) {
// some library code
});

这里有两种 Angular 的导入方式,使用 loader 和仅按名称。它导致 Angular 将被注入(inject)到页面两次,我在控制台中看到以下警告:
WARNING: Tried to load angular more than once.

有没有办法让 webpack 将 angular 的两个导入视为同一个模块?

我的 webpack.conf.js
module.exports = {
entry: {
'app': './src/app/app.js'
},
plugins: [
new webpack.ResolverPlugin(
new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
),
new AngularPlugin()
]
};

最佳答案

一种选择是将 angular 设置为独立脚本并将其配置为外部依赖项:

module.exports = {
external: {
'angular' : 'angular'
},
entry: {
'app': './src/app/app.js'
},
plugins: [
new webpack.ResolverPlugin(
new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
),
new AngularPlugin()
]
};

关于angularjs - Webpack 正在加载模块两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31519503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com