gpt4 book ai didi

javascript - Webpack - 排除 node_modules 也保持独立的浏览器和服务器管理

转载 作者:太空狗 更新时间:2023-10-29 19:26:19 26 4
gpt4 key购买 nike

(webpack.config.js文件内容如下)

我正在尝试对 Node 模块进行 webpack 排除。

我发现使用 webpack-node-externals 是可行的,但在我的常用配置中使用它会导致其他错误: Require is not defined on reflect-metadata - __webpack_require__ issue

所以...我想知道如何在浏览器端排除 webpack 捆绑而不会出现任何问题。

我的webpack版本:3.11.0


webpack-config.js

const path = require('path');    
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

var nodeExternals = require('webpack-node-externals');

module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {

//externals: [nodeExternals()], // in order to ignore all modules in node_modules folder

stats: { modules: false },
context: __dirname,
resolve: { extensions: [ '.js', '.ts' ] },
output: {
filename: '[name].js',
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.ts$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] : '@ngtools/webpack' },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: [ 'to-string-loader', 'style-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
]
},
plugins: [new CheckerPlugin()]
};

// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './ClientApp/boot.browser.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [
// Plugins that apply in production builds only
new webpack.optimize.UglifyJsPlugin(),
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.browser.module#AppModule'),
exclude: ['./**/*.server.ts']
})
])
});

// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './ClientApp/boot.server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
].concat(isDevBuild ? [] : [
// Plugins that apply in production builds only
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.server.module#AppModule'),
exclude: ['./**/*.browser.ts']
})
]),
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},

target: 'node',
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder,
devtool: 'inline-source-map'
});

return [clientBundleConfig, serverBundleConfig];
};

最佳答案

明白了!

在发布我的解决方案之前,我要感谢 Aluan Haddad 在我的上述问题中提供的有用评论。

正如 Aluan 所建议的,事实上,问题与需要使用模块加载器有关,而不是模块打包器。

所以,我遵循的步骤是:

  • 安装requireJS ==> http://requirejs.org/docs/node.html
  • Removing externals: [nodeExternals()],//以便从我常用的 webpack 配置中忽略 node_modules 文件夹中的所有模块 并将其添加到我的服务器配置(在我的问题之前完成,但这是非常重要的一步)[请参阅问题中的 webpack.config.js 内容]
  • 添加 target: 'node', 在我上面的外部点之前,在我的服务器端部分下(在我的问题之前完成,但这是一个非常重要的步骤)[见问题中的webpack.config.js内容]
    这确保浏览器端保留 target:'web'(默认目标),并且 target 成为仅用于服务器的 Node 。
  • 从 powershell 手动启动 webpack 配置 vendor 命令 webpack --config webpack.config.vendor.js
  • 从 powershell 手动启动 webpack 配置命令 webpack --config webpack.config.js

这对我有用!希望它也适用于阅读此问题并遇到此问题的任何其他人!

关于javascript - Webpack - 排除 node_modules 也保持独立的浏览器和服务器管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49729841/

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