简体   繁体   English

angular-cli:CSS取决于环境

[英]angular-cli: CSS dependent on environment

I am using Angular 2 with angular-cli . 我正在使用Angular 2和angular-cli For customer A the CSS file style.A.css is relevant, for customer B it's style.B.css . 对于客户A,CSS文件style.A.css是相关的,对于客户B,它是style.B.css Is it possible make the css files defined in apps[0].styles dependent on the active environment? 是否有可能使apps[0].styles定义的css文件取决于活动环境? If not, any elegant solution for this? 如果没有,对此有什么优雅的解决方案?

The CSS's scope is the whole app, like a totally different appearance. CSS的范围是整个应用程序,就像一个完全不同的外观。 So component scope will not be sufficient. 因此,组件范围将不够。

This is dependent on how your project is setup. 这取决于项目的设置方式。 And whether you are ejected from the CLI. 以及是否从CLI中退出。 I use an ejected angular-cli with angular 4 and webpack. 我将弹出的angular-cli与angular 4和webpack一起使用。 You can customize environment files if you are ejected or not ejected, but the css build process is earlier during the build than the environment files. 您可以自定义环境文件(如果已弹出或未弹出),但是在构建过程中,css的构建过程比环境文件更早。 I would recommend customizing the webpack config to copy a file based on your npm script depending on your build target. 我建议根据您的构建目标,自定义webpack配置,以根据您的npm脚本复制文件。 You can use the GlobCopyWebpackPlugin to copy files to the output folder. 您可以使用GlobCopyWebpackPlugin将文件复制到输出文件夹。 Then you would static import that css file. 然后,您将静态导入该css文件。

For example in webpack.config.js I am copying files during the build to the output folder, you could copy a css the same way, then in your main index.html just bring in that css that is supplied by the environment you are building for. 例如,在webpack.config.js中,我在构建过程中将文件复制到输出文件夹,您可以以相同的方式复制css,然后在主index.html中仅引入由您所构建的环境提供的css对于。

new GlobCopyWebpackPlugin({
  "patterns": [
    "assets",
    "favicon.ico",
    "manifest.json",
    "sw.js"
  ],
  "globOptions": {
    "cwd": process.cwd() + "/src",
    "dot": true,
    "ignore": "**/.gitkeep"
  }
})

My package.json provides the environment flag during the script build process 我的package.json在脚本构建过程中提供了环境标志

  "scripts": {
    "ng": "ng",
    "start": "webpack-dev-server --env.target=local --port=4200 --history-api-fallback",

You could use the environment flag to select the appropriate css file for the GlobCopyWebpackPlugin 您可以使用环境标志来为GlobCopyWebpackPlugin选择适当的CSS文件

// export a function that returns a promise that returns the config object
module.exports = function(env) {

const isLocal = env.target === 'local';
if(isLocal) { /* set the css file to a variable mycssfile*/ }

/* code removed for brevity */
  "plugins": [

    new NoEmitOnErrorsPlugin(),

    new GlobCopyWebpackPlugin({
          "patterns": [
            `${mycssfile}`,
            "assets",
            "favicon.ico",
            "manifest.json",
            "sw.js"
          ],
          "globOptions": {
            "cwd": process.cwd() + "/src",
            "dot": true,
            "ignore": "**/.gitkeep"
          }
        })`

You can explore Material palette. 您可以浏览“材质”面板。 Changing one client color palette to another. 将一个客户端调色板更改为另一个。

https://www.materialpalette.com/ https://www.materialpalette.com/

We used different app-names to solve that problem. 我们使用了不同的应用程序名称来解决该问题。 We defined different customers in our angular-cli.json and defined there which style.css to use for which app name. 我们在angular-cli.json中定义了不同的客户,并在那里定义了哪个style.css用于哪个应用程序名称。 There we have a default and then all the customers we have and for each we can say which assets we want to use and which styles and even which enviornment is sufficient. 在这里,我们有一个默认值,然后我们拥有所有客户,对于每个客户,我们都可以说我们要使用哪些资产,哪种样式甚至哪种环境就足够了。

And to start and build the application you then only have to call 'ng serve app-name' or 'ng serve' for default 而要启动和构建应用程序,则只需默认调用“ ng serve app-name”或“ ng serve”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM