使用Webpack从包中排除ts/js文件 [英] Exclude ts/js file from bundle using webpack
问题描述
我是webpack的新手.目前,我正在使用webpack开发angular2应用程序.作为要求的一部分,我们希望有一个不应捆绑的设置文件,以便在捆绑后也可以更改settings.js文件中的URL.
I am new to webpack. Currently I am working on angular2 application with webpack. As part of requirement we want to have a settings file which should not be bundled so that one can change URL in settings.js file after bundle also.
下面是两个文件的代码.
Below is the code for two files.
settings.ts
settings.ts
const foo = {
url:'localhost'
};
export { foo };
script.ts
script.ts
import { foo } from 'settings';
两个ts文件都将在捆绑之前被编译为js文件.现在,我想从捆绑中排除settings.ts文件,并要复制dist文件夹中的settings.ts文件.
Both ts files will be compiled to js file before bundle. And now i want to exclude settings.ts file from bundle and want to copy setting.ts file separate in dist folder.
下面是webpack.config文件
Below is the webpack.config file
loaders: [
{
test: /\.ts$/,
loaders: ['awesome-typescript-loader',
'angular2-template-loader'],
exclude: [/\settings.ts$/]
},
]
我发现没有运气.
我能够使用CopyWebpackPlugin复制dist文件夹中的settings.js文件.但是无法从绑定中排除settings.ts文件.
I am able to copy settings.js file in dist folder by using CopyWebpackPlugin. But not able to exclude settings.ts file from bundling.
下面是文件夹结构的屏幕截图. Webpack.config与src文件夹位于同一级别
Below is the screenshot of folder structure. Webpack.config is on same level as src folder
推荐答案
现在我正在使用angular 7,并将文件路径放在angular.json的资产"列表中对我有用.
Now i am using angular 7 and putting file path in "assets" list in angular.json works for me.
"assets": [
"src/assets",
"src/config/settings.json",
],
这篇关于使用Webpack从包中排除ts/js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!