Exclude ts/js file from bundle using webpack
Asked Answered
F

2

6

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

enter image description here

Forecastle answered 27/12, 2016 at 16:37 Comment(5)
Instead on settings.ts file i am ready to use json file which will have a url property.Forecastle
Please add a screenshot of your folder structure.Despoil
Probably you need something like exclude: /app/settings/settings.ts Without the angle brackets and the escape slash.Despoil
Your Webpack configuration file is on the same level as src, right?Despoil
Yes you are rightForecastle
F
1

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

           ],
Forecastle answered 13/2, 2019 at 11:34 Comment(0)
D
0

Try it like this:

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

As suggested on this GitHub issue.

Despoil answered 27/12, 2016 at 23:35 Comment(1)
Tried exclude:[path.resolve(__dirname,'src/app/config/settings.ts')]. It gives me error "you may need appropriate loader to handle this file type"Forecastle

© 2022 - 2024 — McMap. All rights reserved.