Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions packages/webpack5/__tests__/loaders/app-css-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import ts from 'typescript';
import { SourceMapConsumer } from 'source-map';

import appCssLoader from '../../src/loaders/app-css-loader';

const SOURCE = [
"import { Application } from '@nativescript/core';",
'',
'declare const unused: number;',
'',
'function fail(): never {',
"\tthrow new Error('boom');",
'}',
'',
"Application.run({ moduleName: 'app-root' });",
].join('\n');

interface LoaderResult {
code: string;
map?: any;
}

function transpile() {
const output = ts.transpileModule(SOURCE, {
fileName: '/app/app.ts',
compilerOptions: {
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.ES2020,
sourceMap: true,
},
});
return { code: output.outputText, map: JSON.parse(output.sourceMapText) };
}

function runLoader(
content: string,
map: any,
cssFile: string | null,
): Promise<LoaderResult> {
return new Promise((resolve, reject) => {
const context = {
context: '/app',
resourcePath: '/app/app.ts',
getOptions: () => ({ platform: 'ios' }),
getResolve:
() =>
(_context: string, _request: string, done: (err, res?) => void) =>
cssFile ? done(null, cssFile) : done(new Error('not found')),
async: () => (err: Error | null, code: string, outputMap?: any) =>
err ? reject(err) : resolve({ code, map: outputMap }),
};
appCssLoader.call(context, content, map);
});
}

async function originalLine(result: LoaderResult, needle: string) {
const lines = result.code.split('\n');
const line = lines.findIndex((text) => text.includes(needle));
const consumer = await new SourceMapConsumer(result.map);
try {
return consumer.originalPositionFor({
line: line + 1,
column: lines[line].indexOf(needle),
}).line;
} finally {
consumer.destroy();
}
}

describe('app-css-loader', () => {
it('imports the app css and keeps source map positions', async () => {
const { code, map } = transpile();
const result = await runLoader(code, map, '/app/app.css');

expect(result.code).toContain('import "./app.css";');
expect(await originalLine(result, 'throw new Error')).toBe(6);
expect(await originalLine(result, 'Application.run')).toBe(9);
});

it('passes content and map through when there is no app css', async () => {
const { code, map } = transpile();
const result = await runLoader(code, map, null);

expect(result.code).toBe(code);
expect(result.map).toBe(map);
});

it('imports the app css without a map when none is given', async () => {
const { code } = transpile();
const result = await runLoader(code, undefined, '/app/app.scss');

expect(result.code).toContain('import "./app.scss";');
expect(result.code).toContain(code);
expect(result.map).toBeUndefined();
});
});
19 changes: 12 additions & 7 deletions packages/webpack5/src/loaders/app-css-loader/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dedent } from 'ts-dedent';
import { basename } from 'path';
import { sources } from 'webpack';

/**
* This loader tries to load an `app.scss` or and `app.css` relative to the main entry
*/
Expand All @@ -17,12 +18,16 @@ export default function loader(content: string, map: any) {
return callback(null, content, map);
}

const code = dedent`
// Added by app-css-loader
import "./${basename(res)}";
${content}
`;
const prefix = `// Added by app-css-loader\nimport "./${basename(res)}";\n`;
if (!map) {
return callback(null, prefix + content);
}

callback(null, code, map);
// The prefix shifts every line, so the incoming map must be offset too.
const { source, map: outputMap } = new sources.ConcatSource(
new sources.RawSource(prefix),
new sources.SourceMapSource(content, this.resourcePath, map),
).sourceAndMap();
callback(null, source, outputMap);
});
}
Loading