我相信这是构建过程的责任。
看来你是对的。我们通常在构建过程中内联模板。
为此,您可以创建 js 文件,例如:
/utils/内联资源.js
const {dirname, join} = require('path');
const {readFileSync, writeFileSync} = require('fs');
const glob = require('glob');
/** Finds all JavaScript files in a directory and inlines all resources of Angular components. */
module.exports = function inlineResourcesForDirectory(folderPath) {
glob.sync(join(folderPath, '**/*.js')).forEach(filePath => inlineResources(filePath));
};
/** Inlines the external resources of Angular components of a file. */
function inlineResources(filePath) {
let fileContent = readFileSync(filePath, 'utf-8');
fileContent = inlineTemplate(fileContent, filePath);
fileContent = inlineStyles(fileContent, filePath);
writeFileSync(filePath, fileContent, 'utf-8');
}
/** Inlines the templates of Angular components for a specified source file. */
function inlineTemplate(fileContent, filePath) {
return fileContent.replace(/templateUrl:\s*'([^']+?\.html)'/g, (_match, templateUrl) => {
const templatePath = join(dirname(filePath), templateUrl);
const templateContent = loadResourceFile(templatePath);
return `template: "${templateContent}"`;
});
}
/** Inlines the external styles of Angular components for a specified source file. */
function inlineStyles(fileContent, filePath) {
return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, (_match, styleUrlsValue) => {
// The RegExp matches the array of external style files. This is a string right now and
// can to be parsed using the `eval` method. The value looks like "['AAA.css', 'BBB.css']"
const styleUrls = eval(styleUrlsValue);
const styleContents = styleUrls
.map(url => join(dirname(filePath), url))
.map(path => loadResourceFile(path));
return `styles: ["${styleContents.join(' ')}"]`;
});
}
/** Loads the specified resource file and drops line-breaks of the content. */
function loadResourceFile(filePath) {
return readFileSync(
filePath.replace('dist\\package\\esm5\\', '').replace('dist\\', ''), 'utf-8')
.replace(/([\n\r]\s*)+/gm, ' ')
.replace(/"/g, '\\"');
}
然后build.js
按如下方式更改您的文件:
构建.js
...
const ESM5_DIR = `${NPM_DIR}/esm5`;
const BUNDLES_DIR = `${NPM_DIR}/bundles`;
const OUT_DIR_ESM5 = `${NPM_DIR}/package/esm5`;
// 1) import function from created above file
const inlineResourcesForDirectory = require('./utils/inline-resources');
// 1) end
...
/* AoT compilation */
shell.echo(`Start AoT compilation`);
if (shell.exec(`ngc -p tsconfig-build.json`).code !== 0) {
shell.echo(chalk.red(`Error: AoT compilation failed`));
shell.exit(1);
}
shell.echo(chalk.green(`AoT compilation completed`));
// 2) Inline template after first ngc build
shell.echo(`Start inlining templates in ${NPM_DIR} folder`);
inlineResourcesForDirectory(NPM_DIR);
shell.echo(`Inlining templates in ${NPM_DIR} folder completed`);
// 2) end
...
shell.echo(`Produce ESM5 version`);
shell.exec(`ngc -p tsconfig-build.json --target es5 -d false --outDir ${OUT_DIR_ESM5} --importHelpers true --sourceMap`);
// 3) Inline template after second ngc build
shell.echo(`Start inlining templates in ${OUT_DIR_ESM5} folder`);
inlineResourcesForDirectory(OUT_DIR_ESM5);
shell.echo(`Inlining templates in ${OUT_DIR_ESM5} folder completed`);
// 3) end
if (shell.exec(`rollup -c rollup.es.config.js -i ${OUT_DIR_ESM5}/${PACKAGE}.js -o ${ESM5_DIR}/${PACKAGE}.js`).code !== 0) {
完成上述所有 3 项更改后,您可以检查例如您的ngx-ui-scroll.umd.js
捆绑包。它应该看起来像:

也可以看看