Skip to content

prod build using absolute paths to files and sub-directories for JS and CSS possible? #10171

@gitdisrupt

Description

@gitdisrupt

Apologies if this has already been addressed, but i could not find a clear answer anywhere.

Really this is a desired feature and not a bug, but my hope is that this issue can become a good reference for finding the best approach to achieve this.

I am asking specific to a real world use case as it relates to a production environment.
See below for more.

Versions

Angular CLI: 1.7.3
Node: 8.9.4
OS: darwin x64
Angular: 5.2.9
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.7.3
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.4.2
webpack: 3.11.0

Repro steps

In an attempt to organize where the angular-cli outputs JS and CSS files within the /dist directory during a build --prod, before deploying files to a remote server, i tried the following albeit knowing this would likely fail:

I created 2 new directories in my apps working root directory, ./js amd ./css
Then, i moved main.ts and polyfills.ts into the JS directory and styles.scss into the CSS dir.
Then i modified the locations of those files main.ts, polyfills.ts and styles.scss in .angular-cli.json as follows:

"main": "js/main.ts",
"polyfills": "js/polyfills.ts",
"styles": ["css/styles.scss"]

and then attempted ng build --prod

Observed behavior

The build obviously fails.

Desired behavior

The hope was that the build would ultimately output main.bundle.js, polyfills.bundle.js along with inline.bundle.js and any chunk.bundle.js files or vendor.bundle.js file into ./dist/js and syles.bundle.css into ./dist/css

Obviously, this does not work.

Is it possible to set Angular to use custom directories, or more importantly, absolute paths to CSS and JS files?

If not, what is the best suggestion from the community?
Using a custom build script to do so?

If the eject option and a custom webpack build script is the only way to achieve this, does anyone have any example webpack files they can reference?

From what i have found in experimentation, without using the eject option and writing a custom webpack build script, attempting to use a custom bash script to first run the standard angular-cli build command and then generate the desired directories and move said files into them after to achieve this works, however, in addition to having to modify the compiled index.html file to load js and css files from the new sub-directories, which is simple enough, what it also required was to actually modify the method in the compiled angular inline.bundle.js file to load chunk.bundle.js files from the js sub-directory like so, changing this:
a.src=o
to this:
a.src="js/"+o

Now i get it, this is a terrible idea and is NOT recommended, but it would be great if what i am trying to achieve is possible.

It would be excellent if somehow it were possible to use the environment files to set paths for local dev in environment.ts and production in the environment.prod.ts to achieve this.

Why? In a production environment, what happens when an admin wants our index.html to live on one server, and all other assets, JS, CSS, images and the like to live elsewhere on a CDN for example?

so, in my case, in addition to updating the paths to JS and CSS in the index.html file, i changed this a.src=o to this a.src="http://mycdn.domain.com/"+o in the compiled inline.bundle.js file.

In my particular case, my server admin is wanting my index.html to be rendered dynamically via PHP on a traditional apache server, and have all of my JS, CSS and image dependancies live elsewhere on a CDN which ultimately requires setting absolute paths to all my JS and CSS dependencies.

Any help at all greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions