简体   繁体   English

Webpack2:对所有入口点都不强制外部

[英]Webpack2: make external not mandatory for all entry points

Given the following existing webpack.config.babel.js that's working fine for this application, I would like to add another entry ( widget ), but if I do so, it requires all external items to be loaded in my HTML page even when I don't need it with my new feature ( google , leaflet ...) on this part of the application. 鉴于以下现有的webpack.config.babel.js适用于此应用程序,我想添加另一个entrywidget ),但如果我这样做,它需要在我的HTML页面中加载所有external项目,即使我在应用程序的这一部分上不需要我的新功能( googleleaflet ...)。

widget.js:10488 Uncaught ReferenceError: google is not defined

The plugin & resolve & output existing sections are applying to the new entry js I want to add, so it's good. pluginresolveoutput现有部分正在应用于我想要添加的新entry js ,所以它很好。 Only the external is bothering me. 只有external困扰着我。

What's the best way to resolve this ? 解决这个问题的最佳方法是什么? I have very little knowledge of webpack. 我对webpack知之甚少。 Thanks. 谢谢。

 import path from 'path'; import webpack from 'webpack'; import eslintFormatter from 'eslint-friendly-formatter'; export default (env) => { const isProd = env ? !!env.release : false; const isVerbose = env ? !!env.verbose : true; process.env.NODE_ENV = isProd ? 'production' : 'development'; return { entry: { showcase: path.resolve(process.cwd(), 'src/AppBundle/Resources/private/js/showcase/index.js'), // widget: path.resolve(process.cwd(), 'src/AppBundle/Resources/private/js/widget/index.js'), }, output: { path: path.resolve(process.cwd(), 'web/dist/components'), filename: '[name].js', publicPath: '/', }, resolve: { extensions: ['.js', '.json', '.vue'], alias: { Translator: 'node_modules/bazinga-translator/js', }, }, externals: { vue: 'Vue', vuex: 'Vuex', google: 'google', leaflet: 'L', translator: 'Translator', markerclustererplus: 'MarkerClusterer', lodash: '_', routing: 'Routing', }, module: { rules: [ { test: /\\.(js|vue)$/, enforce: 'pre', include: path.resolve(process.cwd(), 'src/AppBundle/Resources/private/js'), use: { loader: 'eslint-loader', options: { formatter: eslintFormatter, }, }, }, { test: /\\.js$/, include: path.resolve(process.cwd(), 'src/AppBundle/Resources/private/js'), use: 'babel-loader', }, { test: /\\.vue$/, use: 'vue-loader', }, ], }, plugins: [ // Define environment variables new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify(process.env.NODE_ENV), }, }), // No compile changes on errors ...isProd ? [] : [new webpack.NoEmitOnErrorsPlugin()], // JavaScript code minimizing ...isProd ? [ // Minimize all JavaScript output of chunks // https://github.com/mishoo/UglifyJS2#compressor-options new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: isVerbose, }, }), ] : [], ], watchOptions: { aggregateTimeout: 300, poll: 1000, }, }; }; 

Externals is only configuration suppose modules will be exists but webpack does not call it itself. 外部只是配置假设模块将存在但webpack不会自己调用它。 It is possible alredy existed entry loaded togather with all externals and worked fine with no errors. 有可能alredy已存在的入口加载与所有外部和工作正常没有错误。 But new entry loaded without loading externals and new entry make (or some else) call not loaded externals. 但是加载的新条目没有加载外部和新条目使(或其他一些)调用未加载的外部。 Check you possible have dependencies requiers some externals or your newely added entry make call some of externals (which actually is not loaded in second case). 检查你可能有依赖项requiers一些外部或你的newely添加条目调用一些外部(实际上没有加载在第二种情况下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM