【发布时间】:2019-06-06 20:59:34
【问题描述】:
我已经开始使用 TypeScript 并使用 Browserify(使用 tsify),它运行良好,直到我切换到 Webpack 以在我的项目中使用 vue 和 babel。
这是我的项目结构(简化,它也使用 ASP.NET Core):
BestAppEver
---| [C# stuff]
---| scripts
---| pages
- Login.ts
- Dashboard.ts
---| components
- CoolThing.vue
- App.ts
- package.json
- webpack.config.js
- tsconfig.json
pages 中的文件通常包含如下 jquery 声明:
import * as $ from "jquery";
console.log("hi i got loaded!");
$("#deleteConfirmationInput").change(function() {
if (isInputValid()) {
$("#deleteConfirmationButton").removeAttr("disabled");
} else {
$("#deleteConfirmationButton").attr("disabled", "disabled");
}
});
这是我的webpack.config.js 文件
var path = require('path');
var webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
module.exports = {
entry: './scripts/App.ts',
mode: process.env.NODE_ENV || 'production',
output: {
path: path.resolve(__dirname, 'wwwroot/js'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader'
},
{
loader: 'ts-loader',
options: {
appendTsSuffixTo: [/\.vue$/]
}
}
]
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
plugins: [
new TsconfigPathsPlugin()
]
}
plugins: [
new VueLoaderPlugin()
],
devtool: 'source-map'
}
if (process.env.NODE_ENV === 'production') {
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
]);
}
使用 Browserify 和 tsify,所有文件都被包含并运行,所以"hi i got loaded!" 被打印出来。
使用 ts-loader,只包含并运行我 imported 和使用的文件。 不导入文件,"hi i got loaded!" 不会被打印出来。 .vue 文件不是这种情况。
有没有办法自动导入这些文件并让它们全部在最终包中运行?
谢谢:D
【问题讨论】:
标签: typescript webpack ts-loader