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

Cleanup ngtools/webpack internal loaders #21295

Merged
merged 2 commits into from
Jul 7, 2021
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
13 changes: 13 additions & 0 deletions packages/ngtools/webpack/src/loaders/direct-resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export const DirectAngularResourceLoaderPath = __filename;

export default function (content: string) {
return `export default ${JSON.stringify(content)};`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import type { Compilation, LoaderContext } from 'webpack';

export const InlineAngularResourceLoaderPath = __filename;

export const InlineAngularResourceSymbol = Symbol('@ngtools/webpack[angular-resource]');

export interface CompilationWithInlineAngularResource extends Compilation {
Expand Down
7 changes: 4 additions & 3 deletions packages/ngtools/webpack/src/resource_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { createHash } from 'crypto';
import * as path from 'path';
import * as vm from 'vm';
import type { Asset, Compilation } from 'webpack';
import { normalizePath } from './ivy/paths';
import {
CompilationWithInlineAngularResource,
InlineAngularResourceLoaderPath,
InlineAngularResourceSymbol,
} from './inline-data-loader';
import { normalizePath } from './ivy/paths';
} from './loaders/inline-resource';

interface CompilationOutput {
content: string;
Expand All @@ -33,7 +34,7 @@ export class WebpackResourceLoader {
private modifiedResources = new Set<string>();
private outputPathCounter = 1;

private readonly inlineDataLoaderPath = require.resolve('./inline-data-loader');
private readonly inlineDataLoaderPath = InlineAngularResourceLoaderPath;

constructor(shouldCache: boolean) {
if (shouldCache) {
Expand Down
11 changes: 7 additions & 4 deletions packages/ngtools/webpack/src/transformers/replace_resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

import * as ts from 'typescript';

const inlineDataLoaderPath = require.resolve('../inline-data-loader');
import { DirectAngularResourceLoaderPath } from '../loaders/direct-resource';
import { InlineAngularResourceLoaderPath } from '../loaders/inline-resource';

export function replaceResources(
shouldTransform: (fileName: string) => boolean,
Expand Down Expand Up @@ -168,7 +168,10 @@ function visitComponentMetadata(
return undefined;

case 'templateUrl':
const url = getResourceUrl(node.initializer, directTemplateLoading ? '!raw-loader!' : '');
const url = getResourceUrl(
node.initializer,
directTemplateLoading ? `!${DirectAngularResourceLoaderPath}!` : '',
);
if (!url) {
return node;
}
Expand Down Expand Up @@ -208,7 +211,7 @@ function visitComponentMetadata(
} else if (inlineStyleFileExtension) {
const data = Buffer.from(node.text).toString('base64');
const containingFile = node.getSourceFile().fileName;
url = `${containingFile}.${inlineStyleFileExtension}!=!${inlineDataLoaderPath}?data=${encodeURIComponent(
url = `${containingFile}.${inlineStyleFileExtension}!=!${InlineAngularResourceLoaderPath}?data=${encodeURIComponent(
data,
)}!${containingFile}`;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { tags } from '@angular-devkit/core';
import * as ts from 'typescript';
import { DirectAngularResourceLoaderPath } from '../loaders/direct-resource';
import { replaceResources } from './replace_resources';
import { createTypescriptContext, transformTypescript } from './spec_helpers';

Expand Down Expand Up @@ -52,7 +53,7 @@ describe('@ngtools/webpack transformers', () => {
`;
const output = tags.stripIndent`
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.html";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.html";
import __NG_CLI_RESOURCE__1 from "./app.component.css";
import __NG_CLI_RESOURCE__2 from "./app.component.2.css";
import { Component } from '@angular/core';
Expand Down Expand Up @@ -102,7 +103,7 @@ describe('@ngtools/webpack transformers', () => {
AppComponent = tslib_1.__decorate([
core_1.Component({
selector: 'app-root',
template: require("!raw-loader!./app.component.html").default,
template: require("!${DirectAngularResourceLoaderPath}!./app.component.html").default,
styles: [require("./app.component.css").default, require("./app.component.2.css").default] }) ], AppComponent);
exports.AppComponent = AppComponent;
`;
Expand Down Expand Up @@ -166,7 +167,7 @@ describe('@ngtools/webpack transformers', () => {
`;
const output = tags.stripIndent`
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.svg";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.svg";
import { Component } from '@angular/core';
let AppComponent = class AppComponent {
constructor() {
Expand Down Expand Up @@ -202,7 +203,7 @@ describe('@ngtools/webpack transformers', () => {
`;
const output = tags.stripIndent`
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.html";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.html";
import __NG_CLI_RESOURCE__1 from "./app.component.css";
import { Component } from '@angular/core';

Expand Down Expand Up @@ -240,7 +241,7 @@ describe('@ngtools/webpack transformers', () => {
`;
const output = tags.stripIndent`
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.html";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.html";
import __NG_CLI_RESOURCE__1 from "data:text/css;charset=utf-8;base64,YSB7IGNvbG9yOiByZWQgfQ==";
import { Component } from '@angular/core';

Expand Down Expand Up @@ -284,7 +285,7 @@ describe('@ngtools/webpack transformers', () => {
`;
const output = `
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.html";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.html";
import __NG_CLI_RESOURCE__1 from "./app.component.css";
import __NG_CLI_RESOURCE__2 from "./app.component.2.css";

Expand Down Expand Up @@ -323,7 +324,7 @@ describe('@ngtools/webpack transformers', () => {
`;
const output = tags.stripIndent`
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.html";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.html";
import __NG_CLI_RESOURCE__1 from "./app.component.css";
import __NG_CLI_RESOURCE__2 from "./app.component.2.css";
import { Component as NgComponent } from '@angular/core';
Expand Down Expand Up @@ -366,7 +367,7 @@ describe('@ngtools/webpack transformers', () => {
`;
const output = tags.stripIndent`
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.html";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.html";
import __NG_CLI_RESOURCE__1 from "./app.component.css";
import __NG_CLI_RESOURCE__2 from "./app.component.2.css";

Expand Down Expand Up @@ -411,7 +412,7 @@ describe('@ngtools/webpack transformers', () => {

const output = tags.stripIndent`
import { __decorate } from "tslib";
import __NG_CLI_RESOURCE__0 from "!raw-loader!./app.component.html";
import __NG_CLI_RESOURCE__0 from "!${DirectAngularResourceLoaderPath}!./app.component.html";
import __NG_CLI_RESOURCE__1 from "./app.component.css";

import { Component } from '@angular/core';
Expand Down