【问题标题】:How to avoid loading from chunk in webpack? [Angular] [inline js]如何避免从 webpack 中的块加载? [角度] [内联 js]
【发布时间】:2021-04-17 22:24:40
【问题描述】:

我正在尝试将 Angular 项目捆绑到单个文件(js、css 和 html)中。我已经设法让 html 和 css 工作,并且 js 现在也内联(使用 HtmlWebpackInlineSourcePlugin)。缩小后生成的js文件如下:

vendor、main 和 polyfill 在 index.html 中内联。但是我看到为 main 和 vendor js 生成的 js 正在尝试从块 3.js 加载(这具有为应用程序编写的实际 js)。

我希望这个块也内联,但似乎无法找到一种方法来做到这一点。即使我确实设法使其内联,我如何避免供应商/主 js 加载这个块。 webpack 配置如下。

'use strict';

const webpackMerge         = require('webpack-merge');
const ngw                  = require('@ngtools/webpack');
const HtmlWebpackPlugin    = require('html-webpack-plugin');
var HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const isDev                = process.env.NODE_ENV !== 'production';

//just a wrapper for path
const helpers              = require('./helpers');

module.exports = {
    mode: 'production',

    output: {
        path: helpers.root('dist'),
        filename: '[name].js',
    },

    resolveLoader: {
        modules: [helpers.root('node_modules')]
    },

    entry: {
        vendor: './src/vendor.ts',
        polyfills: './src/polyfills.ts',
        main: './src/main.ts'
    },

    resolve: {
        extensions: ['.ts', '.js', '.scss']
    },

    module: {
        rules: [
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                test: /\.(scss|sass)$/,
                use: [
                    { loader: 'style-loader', options: { sourceMap: isDev } },
                    { loader: 'css-loader', options: { sourceMap: isDev } },
                    { loader: 'sass-loader', options: { sourceMap: isDev } }
                ],
                include: helpers.root('src', 'assets')
            },
            {
                test: /\.(scss|sass)$/,
                use: [
                    'to-string-loader',
                    { loader: 'css-loader', options: { sourceMap: isDev } },
                    { loader: 'sass-loader', options: { sourceMap: isDev } }
                ],
                include: helpers.root('src', 'app')
            },
            {
                test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
                loader: '@ngtools/webpack'
            }
        ]
    },

    plugins: [
        new ngw.AngularCompilerPlugin({
            tsConfigPath: helpers.root('tsconfig.json'),
            entryModule: helpers.root('src', 'app', 'app.module#AppModule')
        }),
        new HtmlWebpackPlugin({
            template: 'src/index.html',
            inlineSource: '.(js|css)$',
        }),
        new HtmlWebpackInlineSourcePlugin(HtmlWebpackPlugin),
       // new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/chunk/])
    ]
};

仅供参考 - 我希望它是单个内联文件,因为我需要在 google 应用程序脚本中使用(编辑器插件)。

【问题讨论】:

    标签: javascript angular webpack inline html-webpack-plugin


    【解决方案1】:

    我最终解决了这个问题,使用 webpack 中的 libraryTarget 编译为 umd。

    output: {
        path: helpers.root('dist'),
        publicPath: '/',
        filename: '[name].js',
        libraryTarget: "umd",
        globalObject: 'this'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 2022-12-31
      • 2017-02-20
      • 1970-01-01
      • 2016-03-26
      • 2013-04-20
      相关资源
      最近更新 更多