Exclude ts/js file from bundle using webpack

你说的曾经没有我的故事 提交于 2019-12-23 13:24:33

问题


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

const foo = {
    url:'localhost'
};
export { foo };

script.ts

import { foo }  from 'settings';

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.

Below is the webpack.config file

loaders: [

            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 
                          'angular2-template-loader'],
                exclude: [/\settings.ts$/]
            },
]

I found no luck with this.

I am able to copy settings.js file in dist folder by using CopyWebpackPlugin. But not able to exclude settings.ts file from bundling.

Below is the screenshot of folder structure. Webpack.config is on same level as src folder


回答1:


Try it like this:

exclude: [
  path.resolve(__dirname, '/settings.ts'),
]

As suggested on this GitHub issue.




回答2:


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",

           ],


来源:https://stackoverflow.com/questions/41348836/exclude-ts-js-file-from-bundle-using-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!