I am using Visual Studio 2017 for my Typescript project. I tried using Webpack to create a bundle for the source files. The source map produced by Webpack contains the source file urls in this format: "webpack:///./Main/SomeFile.ts". This causes Chrome Dev Tools to display webpack as a separate domain in the "Sources" tab. And when expanded, I can see the source ts files and successfully set breakpoints. But the problem is that I cannot debug using VS 2017 since the breakpoints I set in the IDE do not work.
As a work-around, I manually replaced all these "webpack:///." parts in the source map file by "../" which now points to the correct paths of the source files relative to the bundle file. And now VS picks up the breakpoints and I can debug inside VS.
My questions are:
What is the meaning of "webpack:///" and why is it being produced instead of relative paths?
Why doesn't VS 2017 pick up these and work out-of-the-box? And what is the proper solution as opposed to the work-around I tried above?
If there is no other solution than the work-around I mentioned, how can I integrate it to the webpack processing pipeline so that I don't have to do it manually every time?
Here are my configs:
webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
devtool: "source-map",
entry: {
app: './Components/MainComponent/MainComponent.ts'
},
output: {
filename: '[name].js',
path: __dirname + '/dist'
},
module: {
rules: [
{ test: /.css$/, use: 'css-loader' },
{ test: /.ts$/, use: 'ts-loader' },
//{ test: /.ts$/, use: 'awesome-typescript-loader' },
//{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
resolve: {
extensions: ['.ts', '.js', '.json']
}
}
tsconfig.json
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": false,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"outDir": "./dist/",
"allowJs": true,
"module": "amd",
"alwaysStrict": true
},
"exclude": [
"node_modules",
"wwwroot"
],
"compileOnSave": true
}
Add this to your webpack.config.js, inside module.exports:
devtool: false,
plugins: [
new webpack.SourceMapDevToolPlugin({
filename: "[file].map",
fallbackModuleFilenameTemplate: '[absolute-resource-path]',
moduleFilenameTemplate: '[absolute-resource-path]'
})
]
This will make breakpoints work in Visual Studio 2017.
Related
Is it possible to compile all .scss files residing in a folder using web compiler in Visual Studio 2019?
I don't want to specify each and every file. I tried something like this.
[
{
"outputFile": "wwwroot/assets/css/pages/",
"inputFile": "wwwroot/assets/sass/pages/**/*.scss",
"minify": { "enabled": true },
"includeInProject": true,
"options": { "sourceMap": true }
}
]
I am building a site using Visual Studio and WebCompiler is enabled.
When I compile on build my path files are changing to:
../../img/icons-#2x.png
rather than what the expected outcome is:
../img/icons-#2x.png
This is my current compilerconfig.json file:
[
{
"outputFile": "dist/css/style.css",
"inputFile": "styles/style.scss",
"sourceMap": false,
"relativeUrls": false
}
]
I've racked my brains/googled for hours but cannot figure out what is causing this to happen?
Just to note I resolved this issue by including the following in my compilerconfig.json:
"options": {
"sourceMap": false,
"relativeUrls": false
}
enter code herenpm test;
var webpackConfig = require('./webpack.test');
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
{pattern: './karma-shim.js', watched: false}
],
exclude: [
],
preprocessors: {
'./karma-shim.js': ['webpack']
},
webpack: webpackConfig,
plugins:[
'karma-jasmine',
'karma-chrome-launcher',
require("karma-webpack")
],
proxies:{
"/app/": "http://localhost:3000/src/app"
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
module.exports = {
devtool: 'cheap-module-eval-source-map',
resolve: {
extensions: ['','.ts','.js']
},
module: {
loaders: [
//以.ts结尾的文件使用 TypeScript loader
{test: /.ts$/,loader: 'awesome-typescript-loader'},
{
test:/\.html$/,
loader: 'html'
},
{
test:/\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'null'
},
{
test:/\.css$/,
loader: 'null'
}
]
}
}
then throws a BUG.
karma start karma.conf.js
keywords if/then/else require v5 option
WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.entry should be one of these:
object { : string | [string] } | string | [string]
The entry point(s) of the compilation.
- configuration.resolve.extensions[0] should not be empty.
Can not load "webpack"!
First i don't see any entey point mentioned in config file which is required in order to webpack understand where to start from.
Second your resolve option in config file hase mentioned three types to resolve and first one is empty string which webpack don't like so by removing that empty string it should fix that problem
Hope this help you to fix the issue.
I had the same issue and resolved by updating the karma-webpack package to the latest version.
I'm starting a project the NG6-Kit-starter.
I'm using WebStorm.
I want to be able to debug my unit tests using WebStorm, so I followed this tutorial.
I can run unit test from WebStorm but I can't put breakpoints, it never stops at breakpoints and I have don't know why.
I suspect it has to do something with the fact that I'm using a preprocessor in my karma config file.
preprocessors: { 'spec.bundle.js': ['webpack', 'sourcemap'] },
See below my full karma.config.js
module.exports = function (config) {
config.set({
// base path used to resolve all patterns
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'sinon-chai'],
// list of files/patterns to load in the browser
files: [{ pattern: 'spec.bundle.js', watched: false }],
// files to exclude
exclude: [],
plugins: [
require("karma-sinon-chai"),
require("karma-chrome-launcher"),
require("karma-mocha"),
require("karma-mocha-reporter"),
require("karma-sourcemap-loader"),
require("karma-webpack")
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: { 'spec.bundle.js': ['webpack', 'sourcemap'] },
webpack: {
//devtool: 'inline-source-map',
devtool: 'source-map',
module: {
loaders: [
{ test: /\.js/, exclude: [/app\/lib/, /node_modules/], loader: 'babel' },
{ test: /\.html$/, loader: 'raw' },
{ test: /\.(scss|sass)$/, loader: 'style!css!sass' },
{ test: /\.css$/, loader: 'style!css' },
{ test: /\.svg/, loader: 'svg-url-loader' },
{ test: /\.json$/, loader: 'json-loader' }
]
}
},
webpackServer: {
noInfo: true // prevent console spamming when running in Karma!
},
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha'],
// web server port
port: 9876,
// enable colors in the output
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
// toggle whether to watch files and rerun tests upon incurring changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// if true, Karma runs tests once and exits
singleRun: true
});
};
And my spec.bundle.js file:
import angular from 'angular';
import mocks from 'angular-mocks';
let context = require.context('./client/app', true, /\.spec\.js/);
context.keys().forEach(context);
Does anyone know how to make this work with WebStorm in order to be able to put breakpoints in unit tests ?
Just tried 2017.1 EAP - karma debugging works out of the box:
right-click karma.config.js
debug - breakpoints in client/app/common/hero/hero.spec.js are hit.
In 2016.3.2 I have to refresh the browser page (the one that has JetBrains IDE Extension enabled) to get breakpoints hit.
Thanks for your reply, it helped me find what I was doing wrong. So I tested like you did and it is working exactly as you say (you have to refresh on Webstorm 2016 and it's working out of the box with the EAP version).
SO I went commit by commit (I did 4 commit) to find out what I was doing wrong:
I'm new with webpack and when I was experimenting some stuff I tried changing this setting in karma.conf.js:
Replacing:
webpack: {
devtool: 'inline-source-map',
By:
webpack: {
devtool: 'source-map',
Changing it back solved my issue. The unit tests now stops at breakpoints
I did a bit of research to understand better what this setting is, have a look at this question if you're interested: Why inline source maps?
I find Web Essentials autoprefixer not auto enough - I need to manually say it to add prefixes. Also it doesn't offer me prefixes when I'm writing .less or .scss.
Is there any extension or option to make it automatically add prefixes on css compilation from .less or .scss stage?
I've tried Web Compiler extension, but it doesn't support prefixing for sass, and says that it supports prefixing for less, but I've tried enabling autoprefix in compilerconfig.json while writing .less and it didn't add anything.
Is there something for visual studio? Or maybe I should dump it and use some editor + gulp?
I'm sure there will be an extension out there but it isn't too much work to create a Grunt/Gulp file to do your compiling for you. Task Runner Explorer will then manage the running of the file. Writing your own will give you the control and the flexibility that an extension will not.
Here is a sample using Grunt, taken from my post on the subject Getting started with Grunt, SASS and Task Runner Explorer
module.exports = function (grunt) {
'use strict';
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Sass
sass: {
options: {
sourceMap: true, // Create source map
outputStyle: 'compressed' // Minify output
},
dist: {
files: [
{
expand: true, // Recursive
cwd: "sass", // The startup directory
src: ["**/*.scss"], // Source files
dest: "stylesheets", // Destination
ext: ".css" // File extension
}
]
}
},
// Autoprefixer
autoprefixer: {
options: {
browsers: ['last 2 versions'],
map: true // Update source map (creates one if it can't find an existing map)
},
// Prefix all files
multiple_files: {
src: 'stylesheets/**/*.css'
},
},
// Watch
watch: {
css: {
files: ['sass/**/*.scss'],
tasks: ['sass', 'autoprefixer'],
options: {
spawn: false
}
}
}
});
grunt.registerTask('dev', ['watch']);
grunt.registerTask('prod', ['sass', 'autoprefixer']);
};