I am transforming my project(based on vue.js) from Javascript to Typescript and when I try to build my project with modified webpack configuration and code, I just failed on importing vue package.

my 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}
}
);

My webpack config:

'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')
  })
])

but when I try to build this project, I have the following error:

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

I don't know how to solve it. More information of my environment:

ts-loader: 3.5.1
vue: 2.6.10
webpack: 1.15.0

1 Answers

0
Aaron On

I have moved to webpack 2 and it seems that it is caused by bad ts-loader support in webpack 1. I am fine now. :D