首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack -不包括node_modules,同时保持浏览器和服务器的分离管理。

Webpack -不包括node_modules,同时保持浏览器和服务器的分离管理。
EN

Stack Overflow用户
提问于 2018-04-09 09:37:19
回答 1查看 1.4K关注 0票数 3

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

我试图在节点模块上排除webpack。

我发现,使用webpack节点外部操作是可行的,但是在我的公共配置中使用它会导致另一个错误: issue

所以..。我想知道如何才能不把webpack捆绑在浏览器端而不引起任何问题。

我的webpack版本: 3.11.0

webpack-config.js

代码语言:javascript
复制
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];
};
EN

回答 1

Stack Overflow用户

发布于 2018-04-09 10:38:10

找到了!

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

正如Aluan所建议的,事实上,问题与需要使用一个模块加载程序有关,而不仅仅是一个模块绑定器。

所以,我遵循的步骤是:

  • 安装requireJS ==> http://requirejs.org/docs/node.html
  • 从普通webpack配置中删除 externals: [nodeExternals()], // in order to ignore all modules in node_modules folder并将其添加到服务器配置(在我的问题之前完成,但这是一个非常重要的步骤)参见问题中的webpack.config.js内容
  • 在我的外部点前面添加 target: 'node',,在服务器端部分下面(在我的问题之前完成,但这是一个非常重要的步骤)--参见问题中的webpack.config.js内容 --这确保浏览器端保留目标:‘web’(默认目标),并且目标成为服务器的节点。
  • 从powershell webpack --config webpack.config.vendor.js手动启动了webpack配置供应商命令。
  • 从powershell webpack --config webpack.config.js手动启动webpack配置命令。

对我起作用了!希望它也能适用于其他阅读这个问题和遇到这个问题的人!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49729841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档