Skip to content

Commit

Permalink
🏗 Add exports for stylesheets to package.json (#36027)
Browse files Browse the repository at this point in the history
* Add styles.css export to package.json

* Conditionally add export

* Use `fast-glob`

* Add separate export entry for each stylesheet

* window support

* sort for lint

Co-authored-by: Jake Fried <samouri@users.noreply.github.com>
  • Loading branch information
swissspidy and samouri committed Sep 28, 2021
1 parent 2f8feaa commit cb5341d
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions build-system/npm-publish/write-package-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/

const [extension, ampVersion, extensionVersion] = process.argv.slice(2);
const fastGlob = require('fast-glob');
const path = require('path');
const {getSemver} = require('./utils');
const {log} = require('../common/logging');
const {stat, writeFile} = require('fs/promises');
Expand All @@ -23,6 +25,19 @@ async function shouldSkip() {
}
}

/**
* Returns relative paths to all the extension's CSS file
*
* @return {Promise<string[]>}
*/
async function getStylesheets() {
const extDir = `extensions/${extension}/${extensionVersion}/dist`
.split('/')
.join(path.sep);
const files = await fastGlob(path.join(extDir, '**', '*.css'));
return files.map((file) => path.relative(extDir, file));
}

/**
* Write package.json
* @return {Promise<void>}
Expand All @@ -42,6 +57,22 @@ async function writePackageJson() {
return;
}

const exports = {
'.': './preact',
'./preact': {
import: './dist/component-preact.module.js',
require: './dist/component-preact.js',
},
'./react': {
import: './dist/component-react.module.js',
require: './dist/component-react.js',
},
};

for (const stylesheet of await getStylesheets()) {
exports[`./${stylesheet}`] = `./dist/${stylesheet}`;
}

const json = {
name: `@ampproject/${extension}`,
version,
Expand All @@ -50,17 +81,7 @@ async function writePackageJson() {
license: 'Apache-2.0',
main: './dist/component-preact.js',
module: './dist/component-preact.module.js',
exports: {
'.': './preact',
'./preact': {
import: './dist/component-preact.module.js',
require: './dist/component-preact.js',
},
'./react': {
import: './dist/component-react.module.js',
require: './dist/component-react.js',
},
},
exports,
files: ['dist/*', 'react.js'],
repository: {
type: 'git',
Expand Down

0 comments on commit cb5341d

Please sign in to comment.