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

fix(scripts): allow using same lib inside app #3814

Merged
merged 2 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 5 additions & 10 deletions packages/angular-cli/models/webpack-build-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,11 @@ export function getWebpackCommonConfig(
if (appConfig.scripts.length > 0) {
const globalScripts = extraEntryParser(appConfig.scripts, appRoot, 'scripts');

// add script entry points
globalScripts.forEach(script =>
entryPoints[script.entry]
? entryPoints[script.entry].push(script.path)
: entryPoints[script.entry] = [script.path]
);

// load global scripts using script-loader
extraRules.push({
include: globalScripts.map((script) => script.path), test: /\.js$/, loader: 'script-loader'
// add entry points and lazy chunks
globalScripts.forEach(script => {
let scriptPath = `script-loader!${script.path}`;
if (script.lazy) { lazyChunks.push(script.entry); }
entryPoints[script.entry] = (entryPoints[script.entry] || []).concat(scriptPath);
});
}

Expand Down
8 changes: 6 additions & 2 deletions tests/e2e/tests/build/scripts-array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
writeMultipleFiles,
expectFileToMatch
expectFileToMatch,
appendToFile
} from '../../utils/fs';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
Expand All @@ -16,6 +17,7 @@ export default function () {
'src/common-entry-script.js': 'console.log(\'common-entry-script\');',
'src/common-entry-style.css': '.common-entry-style { color: red }',
})
.then(() => appendToFile('src/main.ts', 'import \'./string-script.js\';'))
.then(() => updateJsonFile('angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['scripts'] = [
Expand Down Expand Up @@ -48,5 +50,7 @@ export default function () {
<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>
`));
`))
// ensure scripts aren't using script-loader when imported from the app
.then(() => expectFileToMatch('dist/main.bundle.js', 'console.log(\'string-script\');'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So string-script will be BOTH in scripts.bundle.js and main.bundle.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but in different ways:

// in scripts.bundle.js
/***/ 600:
/***/ function(module, exports) {
module.exports = "console.log('string-script');\r\n"

// in main.bundle.js
/***/ 614:
/***/ function(module, exports) {
console.log('scriptJsContent');

Script loader modifies the content such that it is a string, which makes it useless for normal imports.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks to me that the real fix isn't this PR then. Can we get rid of script-loader?

I don't want people having 2 versions of, say, jQuery in their apps... we'll have tons of issues filed with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

script-loader also causes CSP issues since it uses eval so removing it would be good in general.

Could the scripts be processed (concat, etc.) beforehand and then added as a webpack asset. or maybe a more extensive plugin or loader?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding them as an asset is an interesting idea. Maybe we can make a plugin that does that.

}