首页 \ 问答 \ 带有ES6的Webpack Karma + Typescript(Webpack Karma + Typescript with ES6)

带有ES6的Webpack Karma + Typescript(Webpack Karma + Typescript with ES6)

我正在尝试为使用es2015编写的库运行karma测试,但是即使导入并为es2015配置了babel,仍然会获得Uncaught SyntaxError: Unexpected token import 。 (不幸的是,业力只会从v2.5开始支持ES6)

我需要更改配置以使其工作?

karma.conf.js:

var webpackConfig = require('./webpack.config.test');

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: [
            {
                pattern: './config/karma.tests.js',
                watched: false
            }
        ],
        preprocessors: {
            './config/karma.tests.js': ['babel', 'webpack', 'sourcemap']
        },
        plugins: [
            'karma-webpack',
            'karma-jasmine',
            'karma-sourcemap-loader',
            'karma-chrome-launcher',
            'karma-phantomjs-launcher',
            'karma-babel-preprocessor'
        ],
        babelPreprocessor: {
            options: {
                presets: ['es2015']
            }
        },
        webpack: webpackConfig,
        webpackMiddleware: {
            stats: 'errors-only'
        },
        webpackServer: {
            noInfo: true
        },
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        browsers: ['PhantomJS'],
        singleRun: true
    });
};

webpack.config.test.js

var webpack                         = require('webpack');
var helpers                         = require('./helpers');

var config = {
    devtool: 'inline-source-map',
    resolve: {
        root: helpers.root('src'),
        extensions: [ '', '.js', '.ts' ]
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                include: helpers.root('src'),
                exclude: helpers.root('node_modules')
            }
        ]
    },
}

module.exports = config;

tsconfig.json:

{
    "compileOnSave": false,
    "compilerOptions": {
        "declaration": true,
        "declarationDir": "declarations",
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "outDir": "dist",
        "preserveConstEnums": true,
        "removeComments": false,
        "sourceMap": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,        
        "target": "es6",
        "typeRoots": [
            "node_modules/@types"
        ]
    },
    "exclude": [
        "node_modules",
        "dist"
    ]
}

I am trying to run a karma test for a library written in typescript, but keep getting Uncaught SyntaxError: Unexpected token importeven though babel is imported and configured for es2015. (Unfortunately, karma will only support ES6 starting at v2.5)

What do I need change in the configuration to make things work?

karma.conf.js:

var webpackConfig = require('./webpack.config.test');

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: [
            {
                pattern: './config/karma.tests.js',
                watched: false
            }
        ],
        preprocessors: {
            './config/karma.tests.js': ['babel', 'webpack', 'sourcemap']
        },
        plugins: [
            'karma-webpack',
            'karma-jasmine',
            'karma-sourcemap-loader',
            'karma-chrome-launcher',
            'karma-phantomjs-launcher',
            'karma-babel-preprocessor'
        ],
        babelPreprocessor: {
            options: {
                presets: ['es2015']
            }
        },
        webpack: webpackConfig,
        webpackMiddleware: {
            stats: 'errors-only'
        },
        webpackServer: {
            noInfo: true
        },
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        browsers: ['PhantomJS'],
        singleRun: true
    });
};

webpack.config.test.js

var webpack                         = require('webpack');
var helpers                         = require('./helpers');

var config = {
    devtool: 'inline-source-map',
    resolve: {
        root: helpers.root('src'),
        extensions: [ '', '.js', '.ts' ]
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                include: helpers.root('src'),
                exclude: helpers.root('node_modules')
            }
        ]
    },
}

module.exports = config;

tsconfig.json:

{
    "compileOnSave": false,
    "compilerOptions": {
        "declaration": true,
        "declarationDir": "declarations",
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "outDir": "dist",
        "preserveConstEnums": true,
        "removeComments": false,
        "sourceMap": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,        
        "target": "es6",
        "typeRoots": [
            "node_modules/@types"
        ]
    },
    "exclude": [
        "node_modules",
        "dist"
    ]
}

原文:https://stackoverflow.com/questions/41911553
更新时间:2023-01-31 06:01

最满意答案

我真的不明白你的问题,但我会采取有根据的猜测......

您需要将逻辑包装在“confirm”IF语句中。 另外,你有一个额外的“});”。

$(document).ready(function(){

    $('.del_btn').click(function() {

        if (confirm("Are you sure you want to delete this Color?")) {

            var del_id = $(this).attr('rel');

            $.post('script/delete_color.php', {delete_id:del_id}, function(data) {

                if(data == 'true') {
                    $('#'+del_id).remove();alert('Color has been deleted!');
                } else {
                    alert('Color could not delete!');
                }

                document.messages.submit();
                return false; // This line added
            }
        }
    });
});

I didn't really understand your problem, but I'm going to take an educated guess...

You need to wrap your logic inside the "confirm" IF statement. Also, you had an extra "});".

$(document).ready(function(){

    $('.del_btn').click(function() {

        if (confirm("Are you sure you want to delete this Color?")) {

            var del_id = $(this).attr('rel');

            $.post('script/delete_color.php', {delete_id:del_id}, function(data) {

                if(data == 'true') {
                    $('#'+del_id).remove();alert('Color has been deleted!');
                } else {
                    alert('Color could not delete!');
                }

                document.messages.submit();
                return false; // This line added
            }
        }
    });
});

相关问答

更多
  • 不确定它会有所帮助,但首先要摆脱所有内联JS,以及相同事件的多个功能等。 将javascript放在头部,并删除内联处理程序: