From 4e0fc92365bb3a122655f8d219bdd264c1f803d7 Mon Sep 17 00:00:00 2001 From: Shawn Allen Date: Tue, 12 Feb 2019 10:30:54 -0800 Subject: [PATCH] chore: clean up dist/meta.json import lists --- script/dist | 110 +++++++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/script/dist b/script/dist index 2fadec8d..508966e2 100755 --- a/script/dist +++ b/script/dist @@ -12,62 +12,63 @@ const outDir = 'dist' const statsDir = join(outDir, 'stats') const encoding = 'utf8' +// Bundle paths are normalized in getPathName() using dirname() and then +// replacing any slashes with hyphens, but some bundles need to be +// special-cased. Keys in this object are the path minus the "src/" prefix, +// and values are the bundle file base name. ("primer" produces +// "dist/primer.css", etc.) +const bundleNames = { + 'index.scss': 'primer' +} + remove(outDir) .then(() => mkdirp(statsDir)) .then(() => globby([`${inDir}/**/index.scss`])) .then(files => { - return loadConfig().then(({plugins, options}) => { - const processor = postcss(plugins) + return loadConfig() + .then(({plugins, options}) => { + const processor = postcss(plugins) + const bundles = {} - const inPattern = new RegExp(`^${inDir}/`) - const names = { - 'index.scss': 'primer' - } + const inPattern = new RegExp(`^${inDir}/`) + const tasks = files.map(from => { + const path = from.replace(inPattern, '') + const name = bundleNames[path] || getPathName(dirname(path)) - const bundles = {} + const to = join(outDir, `${name}.css`) + const meta = { + name, + source: from, + sass: `@primer/css/${path}`, + css: to, + map: `${to}.map`, + js: join(outDir, `${name}.js`), + stats: join(statsDir, `${name}.json`), + legacy: `primer-${name}/index.scss` + } - const tasks = files.map(from => { - const path = from.replace(inPattern, '') - const name = names[path] || dirname(path).replace(/\//g, '-') + return readFile(from, encoding) + .then(scss => { + meta.imports = getExternalImports(scss, path).map(getPathName) + return processor.process(scss, Object.assign({from, to}, options)) + }) + .then(result => + Promise.all([ + writeFile(to, result.css, encoding), + writeFile(meta.stats, JSON.stringify(cssstats(result.css)), encoding), + writeFile(meta.js, `module.exports = {cssstats: require('./stats/${name}.json')}`, encoding), + result.map ? writeFile(meta.map, result.map, encoding) : null + ]) + ) + .then(() => (bundles[name] = meta)) + }) - const to = join(outDir, `${name}.css`) - const info = { - name, - source: from, - sass: `@primer/css/${path}`, - css: to, - map: `${to}.map`, - js: join(outDir, `${name}.js`), - stats: join(statsDir, `${name}.json`), - legacy: `primer-${name}/index.scss` - } - - return readFile(from, encoding) - .then(scss => { - info.imports = getExternalImports(scss, path) - .map(dirname) - .filter(ext => ext !== 'support') - return processor.process(scss, Object.assign({from, to}, options)) - }) - .then(result => Promise.all([ - writeFile(to, result.css, encoding), - writeFile(info.stats, JSON.stringify(cssstats(result.css)), encoding), - writeFile(info.js, `module.exports = {cssstats: require('./stats/${name}.json')}`, encoding), - result.map ? writeFile(info.map, result.map, encoding) : null - ])) - .then(() => { - bundles[name] = info - return info - }) + return Promise.all(tasks).then(() => bundles) + }) + .then(bundles => { + const meta = {bundles} + return writeFile(join(outDir, 'meta.json'), JSON.stringify(meta, null, 2), encoding) }) - - return Promise.all(tasks) - .then(() => bundles) - }) - .then(bundles => { - const meta = {bundles} - return writeFile(join(outDir, 'meta.json'), JSON.stringify(meta, null, 2), encoding) - }) }) .catch(error => { console.error(error) @@ -76,8 +77,19 @@ remove(outDir) function getExternalImports(scss, relativeTo) { const imports = [] - scss.replace(/@import "(..\/[^"]+)";/g, (_, dep) => { - imports.push(join(dirname(relativeTo), dep)) + const dir = dirname(relativeTo) + // XXX: this might *seem* fragile, but since we enforce double quotes via + // stylelint, I think it's kosher. + scss.replace(/@import "(.+)\/index\.scss";/g, (_, dep) => { + imports.push(join(dir, dep)) }) return imports } + +function getPathName(path) { + return path.replace(/\//g, '-') +} + +function unique(d, i, list) { + return list.indexOf(d) === i +}