通过构建一个使用ng build --prod的角度应用程序来编译的网站文件不包含任何版权信息,特别是那些我添加到源代码中的版权标题。
例如:
/*********
* Copyright the current year Acme, Co.
*********/如何以自动方式将版权头信息添加到ng CLI产品生成输出的文件中?
例如,目标是修改dist/apps/myapp/main.js文件的版权文件头,使其看起来与上面相同。
我尝试在构建结束时将一个文件与只有版权头和来自构建过程输出的文件连接在一起,但这意味着我必须向发布过程ng build --prod; cat ...添加步骤,但我不想这样做,因为它增加了构建的复杂性。
发布于 2019-09-24 17:03:47
如果你想以“角度”的方式去做,这取决于版本。对于最新版本,使用角质建造者的自定义webpack。
我认为将注释添加到输出文件顶部的最简单的webpack插件/特性是BannerPlugin,您可以在这里更多地了解https://webpack.js.org/plugins/banner-plugin/
要做到这一点(还没有经过测试),您可以创建一个像webpack.extra.js这样的新文件,并添加以下内容:
const webpack = require('webpack'); module.exports = { plugins: [ new webpack.BannerPlugin('—– Some test comment for top of file —–') ] }用于ngx,然后在下一个构建中传递这个额外的文件:
ng build –extra-webpack-config webpack.extra.js
并且“一些测试评论”应该出现在每个块的顶部。
For @angular的自定义webpack构建器,编辑您的angular.json文件,并将路径添加到您额外的webpack配置中,作为customWebpackConfig 根据文件 (很好的示例)。然后,当您ng build时,将包含额外的webpack配置,您将在生成的每个块的顶部找到注释。
发布于 2019-09-24 16:50:35
如果要在编译后添加变量,请遵循本教程https://www.jvandemo.com/how-to-use-environment-variables-to-configure-your-angular-application-without-a-rebuild/
文件: env.js
(function (window) {
window.__env = window.__env || {};
window.__env.apiUrl = 'http://localhost:8080';
window.__env.copyright= 'Copyright the current year Acme, Co.';
window.__env.enableDebug = true;
}(this));发布于 2021-06-15 05:02:58
我已经创建了一个节点js任务,它将在npm、构建和更新所有带有版权文本的js和css文件之后运行。
在同一目录结构中创建copyright.txt文件。
const fs = require('fs');
const path = require('path');
const Copyright = {
_fs: null,
_path: null,
_config: {
directory: './dist/'
},
_copyrightTextBuffer: null,
_readCopyrightTextFromFile: function () {
console.log(`Reading copyright text from ${__dirname}\\copyright.txt`);
this._copyrightTextBuffer = fs.readFileSync(`${__dirname}\\copyright.txt`);
console.info(`Copyright text is: \n ${this._copyrightTextBuffer}`);
},
_readFileRecusivly: function () {
const self = this;
const directoryPath = this._path.join(__dirname, this._config.directory);
console.log(`Updaing js file inside ${directoryPath} directory.`);
this._fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.error('Unable to scan directory: ' + err);
}
const jsFileRegex = new RegExp('\\.js$|\\.css$');
files.forEach((file) => {
// if file is only js type
if (jsFileRegex.test(file)) {
self._updateFileWithCopyRight(file)
}
});
});
},
_updateFileWithCopyRight: function (filename) {
const file = this._path.join(__dirname, `${this._config.directory}\\${filename}`);
var data = this._fs.readFileSync(file); //read existing contents into data
var fd = this._fs.openSync(file, 'w+');
fs.writeSync(fd, this._copyrightTextBuffer, 0, this._copyrightTextBuffer.length, 0); //write new data
fs.writeSync(fd, data, 0, data.length, this._copyrightTextBuffer.length); //append old data
// close the file descriptor
fs.close(fd, function (err) {
if (err) {
console.error(`Failed to close the file: ${file}`);
}
console.log(`Udated the copyright text for '${file}' file`);
});
},
init: function (fs, path, directoryPath) {
this._fs = fs;
this._path = path;
this._config.directory = directoryPath ? directoryPath : this._config.directory;
this._readCopyrightTextFromFile();
this._readFileRecusivly();
}
}
if (process.argv && process.argv.length === 3) {
Copyright.init(fs, path, process.argv[2]);
} else {
Copyright.init(fs, path);
}
https://stackoverflow.com/questions/58084305
复制相似问题