【发布时间】: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