Skip to content

Commit 759f05a

Browse files
committed
fix(webpack|angular): styleUrls with platform suffixes
1 parent f861be0 commit 759f05a

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

packages/webpack5/__tests__/configuration/__snapshots__/angular.spec.ts.snap

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ exports[`angular configuration for android 1`] = `
112112
{
113113
test: /\\\\.css$/,
114114
exclude: [
115-
/\\\\.component\\\\.css$/
115+
/\\\\.component(\\\\.\\\\w+)?\\\\.css$/
116116
],
117117
use: [
118118
/* config.module.rule('css').use('apply-css-loader') */
@@ -140,7 +140,7 @@ exports[`angular configuration for android 1`] = `
140140
{
141141
test: /\\\\.scss$/,
142142
exclude: [
143-
/\\\\.component\\\\.scss$/
143+
/\\\\.component(\\\\.\\\\w+)?\\\\.scss$/
144144
],
145145
use: [
146146
/* config.module.rule('scss').use('apply-css-loader') */
@@ -197,7 +197,7 @@ exports[`angular configuration for android 1`] = `
197197
},
198198
/* config.module.rule('css|component') */
199199
{
200-
test: /\\\\.component\\\\.css$/,
200+
test: /\\\\.component(\\\\.\\\\w+)?\\\\.css$/,
201201
use: [
202202
/* config.module.rule('css|component').use('raw-loader') */
203203
{
@@ -207,7 +207,7 @@ exports[`angular configuration for android 1`] = `
207207
},
208208
/* config.module.rule('scss|component') */
209209
{
210-
test: /\\\\.component\\\\.scss$/,
210+
test: /\\\\.component(\\\\.\\\\w+)?\\\\.scss$/,
211211
use: [
212212
/* config.module.rule('scss|component').use('raw-loader') */
213213
{
@@ -331,6 +331,7 @@ exports[`angular configuration for android 1`] = `
331331
{
332332
tsConfigPath: '__jest__/tsconfig.json',
333333
mainPath: '__jest__/src/app.js',
334+
hostReplacementPaths: function () { /* omitted long function */ },
334335
platformTransformers: [
335336
function () { /* omitted long function */ }
336337
]
@@ -460,7 +461,7 @@ exports[`angular configuration for ios 1`] = `
460461
{
461462
test: /\\\\.css$/,
462463
exclude: [
463-
/\\\\.component\\\\.css$/
464+
/\\\\.component(\\\\.\\\\w+)?\\\\.css$/
464465
],
465466
use: [
466467
/* config.module.rule('css').use('apply-css-loader') */
@@ -488,7 +489,7 @@ exports[`angular configuration for ios 1`] = `
488489
{
489490
test: /\\\\.scss$/,
490491
exclude: [
491-
/\\\\.component\\\\.scss$/
492+
/\\\\.component(\\\\.\\\\w+)?\\\\.scss$/
492493
],
493494
use: [
494495
/* config.module.rule('scss').use('apply-css-loader') */
@@ -545,7 +546,7 @@ exports[`angular configuration for ios 1`] = `
545546
},
546547
/* config.module.rule('css|component') */
547548
{
548-
test: /\\\\.component\\\\.css$/,
549+
test: /\\\\.component(\\\\.\\\\w+)?\\\\.css$/,
549550
use: [
550551
/* config.module.rule('css|component').use('raw-loader') */
551552
{
@@ -555,7 +556,7 @@ exports[`angular configuration for ios 1`] = `
555556
},
556557
/* config.module.rule('scss|component') */
557558
{
558-
test: /\\\\.component\\\\.scss$/,
559+
test: /\\\\.component(\\\\.\\\\w+)?\\\\.scss$/,
559560
use: [
560561
/* config.module.rule('scss|component').use('raw-loader') */
561562
{
@@ -679,6 +680,7 @@ exports[`angular configuration for ios 1`] = `
679680
{
680681
tsConfigPath: '__jest__/tsconfig.json',
681682
mainPath: '__jest__/src/app.js',
683+
hostReplacementPaths: function () { /* omitted long function */ },
682684
platformTransformers: [
683685
function () { /* omitted long function */ }
684686
]

packages/webpack5/src/configuration/angular.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import Config from 'webpack-chain';
22
import { existsSync } from 'fs';
3+
import { extname } from 'path';
34

5+
import { getEntryPath, getPlatformName } from '../helpers/platform';
46
import { getProjectFilePath } from '../helpers/project';
57
import { env as _env, IWebpackEnv } from '../index';
6-
import { getEntryPath } from '../helpers/platform';
78
import base from './base';
89

910
export default function (config: Config, env: IWebpackEnv = _env): Config {
1011
base(config, env);
1112

13+
const platform = getPlatformName();
14+
1215
const tsConfigPath = [
1316
getProjectFilePath('tsconfig.app.json'),
1417
getProjectFilePath('tsconfig.json'),
@@ -43,12 +46,15 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
4346
.loader('raw-loader');
4447

4548
// exclude component css files from the normal css rule
46-
config.module.rule('css').exclude.add(/\.component\.css$/);
49+
config.module
50+
.rule('css')
51+
.exclude// exclude *.component.{platform}.css
52+
.add(/\.component(\.\w+)?\.css$/);
4753

4854
// and instead use raw-loader, since that's what angular expects
4955
config.module
5056
.rule('css|component')
51-
.test(/\.component\.css$/)
57+
.test(/\.component(\.\w+)?\.css$/)
5258
.use('raw-loader')
5359
.loader('raw-loader');
5460

@@ -59,12 +65,15 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
5965
.get('options');
6066

6167
// exclude component css files from the normal css rule
62-
config.module.rule('scss').exclude.add(/\.component\.scss$/);
68+
config.module
69+
.rule('scss')
70+
.exclude// exclude *.component.{platform}.scss
71+
.add(/\.component(\.\w+)?\.scss$/);
6372

6473
// and instead use raw-loader, since that's what angular expects
6574
config.module
6675
.rule('scss|component')
67-
.test(/\.component\.scss$/)
76+
.test(/\.component(\.\w+)?\.scss$/)
6877
.use('raw-loader')
6978
.loader('raw-loader')
7079
.end()
@@ -79,6 +88,25 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
7988
{
8089
tsConfigPath,
8190
mainPath: getEntryPath(),
91+
hostReplacementPaths(path: string) {
92+
const ext = extname(path);
93+
const platformExt = `.${platform}${ext}`;
94+
95+
// already includes a platform specific extension - ignore
96+
if (path.includes(platformExt)) {
97+
return path;
98+
}
99+
100+
const platformPath = path.replace(ext, platformExt);
101+
// check if the same file exists with a platform suffix and return if it does.
102+
if (existsSync(platformPath)) {
103+
// console.log(`[hostReplacementPaths] resolving "${path}" to "${platformPath}"`);
104+
return platformPath;
105+
}
106+
107+
// just return the original path otherwise
108+
return path;
109+
},
82110
platformTransformers: [require('../transformers/NativeClass').default],
83111
},
84112
]);
@@ -110,7 +138,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
110138
return config;
111139
}
112140

113-
function getAngularCompilerPlugin() {
141+
function getAngularCompilerPlugin(): any {
114142
const { AngularCompilerPlugin } = require('@ngtools/webpack');
115143
return AngularCompilerPlugin;
116144
}

0 commit comments

Comments
 (0)