Skip to content

Commit

Permalink
fix(css): fix css file paths when using --assets= style args
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
jbedard committed Nov 30, 2019
1 parent 0445f0d commit 6674db7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function parseArgs(cmdParams) {
function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestamp = Date.now) {
const {inputFile, outputFile, assets, rootDirs} = parseArgs(params);

const jsFiles = assets.filter(s => /\.m?js$/i.test(s));
const cssFiles = assets.filter(s => /\.css$/.test(s));

const document = parse5.parse(read(inputFile, {encoding: 'utf-8'}), {treeAdapter});

const body = findElementByName(document, 'body');
Expand Down Expand Up @@ -105,7 +108,6 @@ function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestam
return execPath;
}

const jsFiles = assets.filter(s => /\.m?js$/i.test(s));
for (const s of jsFiles) {
// Differential loading: for filenames like
// foo.mjs
Expand Down Expand Up @@ -143,7 +145,7 @@ function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestam
}
}

for (const s of params.filter(s => /\.css$/.test(s))) {
for (const s of cssFiles) {
const stylesheet = treeAdapter.createElement('link', undefined, [
{name: 'rel', value: 'stylesheet'},
{name: 'href', value: `/${relative(s)}?v=${timestamp()}`},
Expand Down
20 changes: 20 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe('HTML inserter', () => {
'<html><head></head><body><script src="/path/to/my.js?v=123"></script></body></html>');
});

it('should inject .js as script tag when --assets= is used', () => {
expect(main(["--out", outFile, "--html", inFile, '--assets=path/to/my.js'], read, write, () => 123)).toBe(0);
expect(output).toBe(
'<html><head></head><body><script src="/path/to/my.js?v=123"></script></body></html>');
});

it('should assume "module js" .mjs extension is type="module"', () => {
expect(main(["--out", outFile, "--html", inFile, '--assets', 'path/to/my.mjs'], read, write, () => 123))
.toBe(0);
Expand Down Expand Up @@ -69,6 +75,20 @@ describe('HTML inserter', () => {
'<html><head><link rel="stylesheet" href="/path/to/my.css?v=123"></head><body></body></html>');
});

it('should strip the longest matching prefix for .css files', () => {
expect(main(["--out", outFile, "--html", inFile,
"--roots", 'path', 'path/to',
'--assets', 'path/to/my.css'], read, write, () => 123)).toBe(0);
expect(output).toBe(
'<html><head><link rel="stylesheet" href="/my.css?v=123"></head><body></body></html>');
});

it('should inject .css files when --assets= is used', () => {
expect(main(["--out", outFile, "--html", inFile, '--assets=path/to/my.css'], read, write, () => 123)).toBe(0);
expect(output).toBe(
'<html><head><link rel="stylesheet" href="/path/to/my.css?v=123"></head><body></body></html>');
});

it('should create a pair of script tags for differential loading', () => {
expect(main(
["--out", outFile, "--html", inFile, '--assets', 'path/to/my.js', 'path/to/my.es2015.js'], read, write,
Expand Down

0 comments on commit 6674db7

Please sign in to comment.