Skip to content
Open
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
10 changes: 7 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ module.exports = (config) => {
}),
coverage && rollupPluginInstrumentTsCode(),
rollupPluginInjectCode({
'index.js': {
line: 3,
code: " if ('adoptedStyleSheets' in document) { return; }\n",
insertions: {
'index.js': [
{
line: 3,
code: " if ('adoptedStyleSheets' in document) { return; }\n",
},
],
},
}),
].filter(Boolean),
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
"version": "3.0.6",
"description": "Constructible style sheets/adopted style sheets polyfill",
"main": "dist/adoptedStyleSheets.js",
"module": "dist/adoptedStyleSheets.js",
"module": "dist/adoptedStyleSheets.mjs",
"types": "dist/adoptedStyleSheets.d.ts",
"files": [
"dist/adoptedStyleSheets.js",
"dist/adoptedStyleSheets.d.ts"
"dist/adoptedStyleSheets.d.ts",
"dist/adoptedStyleSheets.mjs"
],
"exports": {
".": {
"import": "dist/adoptedStyleSheets.mjs",
"require": "dist/adoptedStyleSheets.js",
"types": "dist/adoptedStyleSheets.d.ts"
}
},
"scripts": {
"build": "rollup -c rollup.config.js",
"test": "karma start",
Expand Down
29 changes: 21 additions & 8 deletions plugins/rollup-plugin-inject-code.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
module.exports = (options) => ({
module.exports = ({insertions, processLine = (line) => line}) => ({
name: 'inject-code',
async generateBundle(_, assets) {
for (const chunkName in options) {
Object.entries(insertions).forEach(([chunkName, chunkInsertions]) => {
const chunk = assets[chunkName];
const {line, code} = options[chunkName];

const lines = chunk.code.split('\n');
lines.splice(line, 0, code);
chunk.code = lines.join('\n');
}
}
const lines = chunk.code.split('\n').map(processLine);

for (const {line, code} of chunkInsertions) {
switch (line) {
case -Infinity:
lines.unshift(code);
break;
case Infinity:
lines.push(code);
break;
default:
lines.splice(line, 0, code);
break;
}

chunk.code = lines.join('\n');
}
});
},
});
54 changes: 42 additions & 12 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,48 @@ const extensions = ['.ts', '.js'];

module.exports = {
input: 'src/index.ts',
output: {
file: 'dist/adoptedStyleSheets.js',
format: 'iife',
name: 'adoptedStyleSheets',
},
output: [
{
file: 'dist/adoptedStyleSheets.js',
format: 'iife',
name: 'adoptedStyleSheets',
plugins: [
injectCode({
insertions: {
'adoptedStyleSheets.js': [
{
line: 3,
code: " if (typeof document === 'undefined' || 'adoptedStyleSheets' in document) { return; }\n",
},
],
},
}),
],
},
{
file: 'dist/adoptedStyleSheets.mjs',
format: 'esm',
plugins: [
injectCode({
insertions: {
'adoptedStyleSheets.mjs': [
{
line: -Infinity,
code: "if (typeof document === 'undefined' || 'adoptedStyleSheets' in document) {\n",
},
{
line: Infinity,
code: '}',
},
],
},
processLine(line) {
return ` ${line}`;
},
}),
],
},
],
plugins: [
nodeResolve({
extensions,
Expand All @@ -33,12 +70,5 @@ module.exports = {
},
],
}),
injectCode({
'adoptedStyleSheets.js': {
line: 3,
code:
" if (typeof document === 'undefined' || 'adoptedStyleSheets' in document) { return; }\n",
},
}),
],
};