web-dev-qa-db-ja.com

JSではなくSCSSソースマップを生成するWebpack

https://github.com/franklin626/custom_webpack_undebuggable で問題を再現しました。

標準のAngular 9 CLIアプリケーションから始めて、私のSCSSファイルがJSON構成をインポートできるように、webpackビルドをカスタマイズする必要がありました。つまり、angular.json、から移動

"serve": {
      "builder": "@angular-devkit/build-angular:dev-server"

"serve": {
      "builder": "@angular-builders/custom-webpack:dev-server",
      "options": {
        "customWebpackConfig": {
          "path": "webpack.config.js"
        }
       ...

webpack.config.js以下を含む:

const jsonImporter = require('node-sass-json-importer');

module.exports = {
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.scss$|\.sass$/,
        use: [
          {
            loader: require.resolve('sass-loader'),
            options: {
              implementation: require('node-sass'),
              sassOptions: {
                // bootstrap-sass requires a minimum precision of 8
                precision: 8,
                importer: jsonImporter(),
                outputStyle: 'expanded'
              }
            }
          }
        ]
      }
    ]
  }
};

Cssのソースマップのみを取得していますが、javascriptのソースマップは取得していません。そして、はい、私のChromeはJSおよびCSSマップが有効になっています。ここで何が行われているのかわかりませんか?

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-web-app": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./webpack.deploy.config.js",
              "replaceDuplicatePlugins": true
            },
            "outputPath": "dist/my-web-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/custom-webpack:dev-server",
          "options": {
            "customWebpackConfig": {
              "path": "webpack.config.js",
              "sourceMap": true
            },
            "browserTarget": "my-web-app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "my-web-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "my-web-app:build"
          }
        },
        "test": {
          "builder": "@angular-builders/custom-webpack:karma",
          "options": {
            "customWebpackConfig": {
              "path": "webpack.config.js"
            },
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": ["tsconfig.app.json", "tsconfig.spec.json", "e2e/tsconfig.json"],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-builders/custom-webpack:protractor",
          "options": {
            "customWebpackConfig": {
              "path": "webpack.config.js"
            },
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "my-web-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "my-web-app:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "my-web-app"
}

package.json

{
  "name": "my-web-app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "test-headless": "ng test --watch=false --browsers=ChromeHeadless",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "doc": "node src/scripts/runMarked.js",
    "prestart": "npm run doc",
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook"
  },
  "dependencies": {
    "@angular/animations": "^9.1.4",
    "@angular/cdk": "^9.0.0",
    "@angular/common": "^9.1.4",
    "@angular/compiler": "^9.1.4",
    "@angular/core": "^9.1.4",
    "@angular/forms": "^9.1.4",
    "@angular/material": "^9.0.0",
    "@angular/platform-browser": "^9.1.4",
    "@angular/platform-browser-dynamic": "^9.1.4",
    "@angular/router": "^9.1.4",
    "@Azure/msal-angular": "^1.0.0-beta.3",
    "@types/vega": "^3.2.0",
    "build": "^0.1.4",
    "d3": "^5.15.0",
    "karma-viewport": "^1.0.5",
    "marked": "^0.8.0",
    "msal": "^1.2.2-beta.0",
    "ng": "0.0.0",
    "ngx-spinner": "^9.0.2",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "vega": "^5.9.1",
    "vega-embed": "^6.2.2",
    "vega-lite": "^4.4.0",
    "vega-typings": "^0.12.4",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "^9.0.0",
    "@angular-devkit/build-angular": "^0.901.0",
    "@angular/cli": "^9.1.4",
    "@angular/compiler-cli": "^9.1.4",
    "@angular/language-service": "^9.1.4",
    "@babel/core": "^7.9.0",
    "@storybook/addon-actions": "^5.3.18",
    "@storybook/addon-links": "^5.3.18",
    "@storybook/addon-notes": "^5.3.18",
    "@storybook/addons": "^5.3.18",
    "@storybook/angular": "^5.3.18",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "babel-loader": "^8.1.0",
    "codelyzer": "^5.1.2",
    "compression-webpack-plugin": "^3.1.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "karma-junit-reporter": "^2.0.1",
    "minimist": "^1.2.0",
    "node-sass": "^4.13.1",
    "node-sass-json-importer": "^4.1.2",
    "prettier": "1.19.1",
    "protractor": "~5.4.3",
    "terser-webpack-plugin": "^2.3.5",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "tslint-config-prettier": "^1.18.0",
    "TypeScript": "~3.7.5"
  }
}
4
BuZz

ここではwebpackangular.json構成:

angular.json

{
  ...
  "architect": {
    "build": {
      "builder": "@angular-builders/custom-webpack:browser", // <---
      "options": {
        "customWebpackConfig": {
          "path": "./webpack.config.js", // <---
        },
        ...
      }
    }
  },
  "serve": {
    "builder": "@angular-builders/custom-webpack:dev-server", // <---
    "options": {
      "browserTarget": "test:build" // <---
    },
    ...
  },
  ...
}

webpack.config.js

const jsonImporter = require("node-sass-json-importer");

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$|\.sass$/,
        use: [
          {
            loader: require.resolve("sass-loader"),
            options: {
              implementation: require("node-sass"),
              sassOptions: {
                // bootstrap-sass requires a minimum precision of 8
                precision: 8,
                importer: jsonImporter(),
                outputStyle: "expanded",
              },
            },
          },
        ],
      },
    ],
  }
};

ng serve/build/build --prod仕事。そして、私はこの結果を得ました(jsonファイルのように色を変更しました) enter image description here

一般的に、過去の答えは正しいようです。しかし、デフォルトとカスタムのWebpack構成のマージに問題があります。 @angular-builders/custom-webpackカスタムwebpack設定がオブジェクトを返す場合、設定自体をマージします。関数を返す場合、この関数はそれら自体をマージする必要があります。そして、おそらく、この合併が生み出し、発行します。

1
Dmitrii Makarov