使用Vue构建打字稿代码时Webpack构建错误

时间:2019-05-05 07:06:29

标签: typescript vue.js webpack

我正在将基于vue.js的项目从Javascript转换为Typescript,当我尝试使用修改后的webpack配置和代码来构建项目时,我导入vue包失败。

我的index.js:

import Vue from "vue";
import store from './vuex/store';
import app from './app.vue';

let v = new Vue({
  el: '#eme',
  store,
  components: {app}
}
);

我的webpack配置:

'use strict'
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const appPkg = require('../app/package')

const postcss = [
  require('postcss-nested'),
  require('postcss-import')(),
  require('postcss-simple-vars'),
  require('postcss-mixins'),
  require('autoprefixer')({
    browsers: ['last 2 Chrome versions']
  })
]

module.exports = {
  entry: {
    app: ['./src/index.ts'],
    vendor: ['vue', 'vuex']
  },
  output: {
    path: process.cwd() + '/app/dist',
    filename: '[name].js'
  },
  resolve: {
    extensions: ['', '.js', '.vue', '.css', '.json', '.ts'],
    alias: {
      'vue$': 'vue/dist/vue.common.js',
      src: path.join(__dirname, '../src'),
      utils: path.join(__dirname, '../src/utils'),
      components: path.join(__dirname, '../src/components'),
      css: path.join(__dirname, '../src/css'),
      directives: path.join(__dirname, '../src/directives'),
      store: path.join(__dirname, '../src/vuex/store')
    }
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['babel'],
        exclude: [/node_modules/]
      },
      {
        test: /\.vue$/,
        loaders: ['vue']
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract(
          'style-loader',
          'css-loader!postcss-loader'
        )
      },
      {
        test: /\.json$/,
        loaders: ['json']
      },
      {
        test: /\.svg$/,
        loaders: ['svg-inline']
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      },
    ]
  },
  babel: {
    presets: ['es2015', 'stage-1'],
    plugins: ['transform-runtime']
  },
  vue: {
    autoprefixer: false,
    postcss,
    loaders: {
      css: ExtractTextPlugin.extract(
        'vue-style-loader',
        'css-loader?sourceMap'
      )
    }
  },
  postcss,
  target: 'electron-renderer',
  plugins: [
    new webpack.ExternalsPlugin('commonjs2', [
      './vendor/markdown-it-katex',
      '../package.json'
    ].concat(Object.keys(appPkg.dependencies))),
    new ExtractTextPlugin('[name].css'),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      filename: 'vendor.js'
    })
  ]
}

const webpack = require('webpack')
const config = require('./webpack.config')

config.plugins = config.plugins.concat([
  new webpack.optimize.OccurenceOrderPlugin(),
  new webpack.optimize.UglifyJsPlugin({
    sourceMap: true,
    compressor: {
      warnings: false
    },
    //comments: false
  }),
  new webpack.DefinePlugin({
    '__DEV__': false,
    'process.env.NODE_ENV': JSON.stringify('production')
  })
])

但是当我尝试构建该项目时,出现以下错误:

"app.js from UglifyJs\nSyntaxError: Unexpected token: name (Vue) [./src/index.ts:1,7]"

我不知道该怎么解决。有关我的环境的更多信息:

ts-loader: 3.5.1
vue: 2.6.10
webpack: 1.15.0

1 个答案:

答案 0 :(得分:0)

我已移至webpack 2,看来这是由于webpack 1中对ts-loader的支持不佳所致。我现在很好。 :D