Skip to content

Commit

Permalink
issue-132 - dedupe insertStyle in output files
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed Jun 27, 2024
1 parent d832124 commit c581ccc
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"scripts": {
"prepare": "npm run build && npm test",
"build": "npm run build-downlevel-dts && tsc --project tsconfig.json",
"build": "npm run build-downlevel-dts && tsc --project tsconfig.build.plugin.json && tsc --project tsconfig.build.insertStyle.json",
"build-downlevel-dts": "node scripts/clean-and-run-downlevel-dts.js",
"downlevel-dts": "downlevel-dts . ts3.5 [--to=3.5]",
"test": "nyc --reporter=html --reporter=text ava ./test/*.test.ts -s && npm run test:rollup.config.spec.ts",
Expand Down
15 changes: 7 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as sass from 'sass';
import {dirname} from 'path';
import * as fs from 'fs';
import {createFilter} from '@rollup/pluginutils';
import {insertStyle} from './style';
import {
SassImporterResult,
RollupAssetInfo,
Expand Down Expand Up @@ -107,14 +106,20 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
const out = JSON.stringify(resolvedCss);

let defaultExport = `""`;
let imports = '';

if (rollupOptions.insert) {
/**
* @see insertStyle.ts for additional information
* Let rollup handle import by processing insertStyle as a module
*/
imports = `import ${insertFnName} from '${__dirname}/insertStyle.js';\n`;
defaultExport = `${insertFnName}(${out});`;
} else if (!rollupOptions.output) {
defaultExport = out;
}

return `export default ${defaultExport};\n${restExports}`;
return `${imports}export default ${defaultExport};\n${restExports}`;
}); // @note do not `catch` here - let error propagate to rollup level
},

Expand Down Expand Up @@ -152,12 +157,6 @@ export = function plugin(options = {} as RollupPluginSassOptions): RollupPlugin
return {
name: 'rollup-plugin-sass',

intro() {
if (pluginOptions.insert) {
return insertStyle.toString().replace(/insertStyle/, insertFnName);
}
},

transform(code: string, filePath: string): Promise<any> {
if (!filter(filePath)) {
return Promise.resolve();
Expand Down
23 changes: 23 additions & 0 deletions src/insertStyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Create a style tag and append to head tag
*
* @warning this file is not included directly in the source code!
* If user specifies inject option to true, an import to this file will be injected in rollup output.
* Due to this reason this file is compiled into a ESM module separated from other plugin source files.
* That is the reason of why there are two tsconfig.build files.
*
* @return css style
*/
export default function insertStyle(css: string | undefined): string | undefined {
if (!css || typeof window === 'undefined') {
return;
}

const style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.innerHTML = css;

document.head.appendChild(style);

return css;
}
21 changes: 0 additions & 21 deletions src/style.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/style.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import {readFileSync} from 'fs';
import {insertStyle} from '../src/style';
import insertStyle from '../src/insertStyle';
import jsdom from 'jsdom';

const expectA = readFileSync('test/assets/expect_a.css').toString();
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.build.insertStyle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* @see insertStyle.ts for additional information */
{
"extends": "./tsconfig.json",
"include": ["./src/insertStyle.ts"],
"compilerOptions": {
"module": "ES6"
}
}
6 changes: 6 additions & 0 deletions tsconfig.build.plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* @see insertStyle.ts for additional information */
{
"extends": "./tsconfig.json",
"include": ["./src/*"],
"exclude": ["./src/insertStyle.ts"]
}

0 comments on commit c581ccc

Please sign in to comment.