Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS style file is generated with a random string in its name - AOT mode #3853

Closed
fuitattila opened this issue Jan 4, 2017 · 13 comments
Closed
Labels
area: @angular-devkit/build-angular feature Issue that requests a new feature

Comments

@fuitattila
Copy link

Please provide us with the following information:

OS?

Windows 10.

Versions.

angular-cli: 1.0.0-beta.24
node: 5.11.0
os: win32 ia32
@angular/common: 2.1.1
@angular/compiler: 2.1.1
@angular/core: 2.1.1
@angular/forms: 2.1.1
@angular/http: 2.1.1
@angular/platform-browser: 2.1.1
@angular/platform-browser-dynamic: 2.1.1
@angular/router: 3.1.1

Question or bug


Generation command:
ng build --aot --prod --no-sourcemap --verbose

With the below standalone and lazy style generation config:
{ "input": "assets/scss/accessibility.style.scss", "output": "accessibility.style.css", "lazy": true }

... generates angular-cli in AOT mode the filename with a random string. Like this:
accessibility.style.**027b05e9f06e48912b48**.bundle.css

That is very frustrating, because I need to know what is the generated file name to injext - with lazy option.

Is there any way to turn off that behaviour? It works fine with ng build command.

Expected generated file name in AOT mode:
accessibility.style.bundle.css

Related thread: #3401

Thank you for your help!

@jasonkrot
Copy link

jasonkrot commented Jan 4, 2017

ohh, i have the same issue...

@fuitattila
Copy link
Author

@filipesilva do you have any idea?

@filipesilva
Copy link
Contributor

There is no way to disable the hash for prod build currently. But I agree that it should be disabled by default for the lazy loaded scripts/styles.

@filipesilva filipesilva self-assigned this Jan 5, 2017
@filipesilva filipesilva added command: build P5 The team acknowledges the request but does not plan to address it, it remains open for discussion labels Jan 5, 2017
@aegyed91
Copy link

aegyed91 commented Jan 5, 2017

A lot of if not all backend solutions provide cache busting mechanism. For example in symfony the asset method: <script type="text/javascript" src="{{ asset("inline.bundle.js") }}"></script>.

I had to fork angular-cli and implement it myself, i did it in a non-destructive way (with cli flag). Will do a PR today i think.

Here are the changes: 73ee829

@filipesilva

@clydin
Copy link
Member

clydin commented Jan 6, 2017

@filipesilva what about a separate webpack config for the lazy styles? (scripts too?) It could run in parallel and since they're lazy they shouldn't have any overlap with the rest of the app.

@fuitattila
Copy link
Author

Another problem is for lazy generated JS files is that angular-cli injects newly generated JS files, to the end of index.html, like that:

<!DOCTYPE html><html>
<head>
  <base href="">
<script type="text/javascript" src="lazy.bundle.js">
</head>
<body>
<app></app>
<script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="scripts.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>
</html>

And the webpack helper plugin contains inline.bundle.js. But I want to use some lazy loaded JS in <head></head> - @see: lazy.bundle.js - because of performance considerations.

This is very annoying, if we need just a plain JS on top of index.html.

@filipesilva
Copy link
Contributor

@clydin that would work I suppose. I'm not too familiar with the multiple config setup and it's limitations (if any). Can you tell me how it would be better than having the single config? Is it faster to have separate configs for instance?

If there are such benefits then I don't see why it shouldn't be done even regardless of the chunk hashes.

One thing to keep in mind is that it would be preferable to not have the command fail when there is no entry for that config (like #3867 tries to do).

@fuitattila lazy generated files are not auto-injected, or at least they shouldn't be. I suppose it would be useful for some cases to choose where stuff is injected but since order matters that's not very easy to orchestrate.

@aegyed91
Copy link

aegyed91 commented Jan 6, 2017

@filipesilva roger, will modify the PR after i get home from work

@jerousseau
Copy link

I have the same issue, with the version 1.4 the script with output name and lazy does not include the hash but the style always include the hash.

@riemerio
Copy link

For anyone who want to use global css scipts in .angular-cli.json lazy loaded without hash I wrote following script (e.g. patch-ng-cli.js)

const fs = require('fs');

const stylesFileToPatch = "node_modules/@angular/cli/models/webpack-configs/styles.js";

const regex = /extraPlugins\.push\(.*\}\)\)\;/;
const patchContent = `
        // PATCHED CONTENT START
        const globalStyles = utils_1.extraEntryParser(appConfig.styles, appRoot, 'styles');
        extraPlugins.push(new ExtractTextPlugin({ filename: getPath => {
            const generatedFileName = getPath(\`[name]\${hashFormat.extract}.bundle.css\`);
            const name = generatedFileName.split(".")[0];
            const globalAppStylesConfigEntry = globalStyles.find(path => path.output === name);
            if (globalAppStylesConfigEntry && globalAppStylesConfigEntry.lazy){
                console.log(\`\${name} will not be hashed due to lazy loading\`);
                return \`\${name}.bundle.css\`
            }
            console.log(generatedFileName);
            return generatedFileName;
        }}));
        // PATCHED CONTENT END
`;


fs.readFile(stylesFileToPatch, (err, data) => {
    if (err) { throw err; }

    const text = data.toString();

    const isAlreadyPatched = !!text.match("PATCHED CONTENT");

    if (isAlreadyPatched) return console.warn("-- already patched --", stylesFileToPatch);

    console.log('-- Patching ng-cli: ', stylesFileToPatch);
    const patchedContent = text.replace(regex, patchContent);

    const file = fs.openSync(stylesFileToPatch, 'r+');
    fs.writeFile(file, patchedContent, () => console.log("-- Patching -- OK"));
    fs.close(file);
});

Then run this script after npm install via npm scripts in package.json

"postinstall": "node ./patch-ng-cli.js",

@filipesilva filipesilva added feature Issue that requests a new feature and removed P5 The team acknowledges the request but does not plan to address it, it remains open for discussion labels Feb 1, 2018
@AndreiShostik
Copy link

nice.
instead of the process simplification you need to struggle with angular-cli

@clydin
Copy link
Member

clydin commented Aug 17, 2018

This has been corrected as of 6.1.0.

@clydin clydin closed this as completed Aug 17, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: @angular-devkit/build-angular feature Issue that requests a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants