-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add manifest path support to conditional reporter (#206)
- Loading branch information
Showing
9 changed files
with
313 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/dev/eslint-plugin/src/rules/no-relative-import.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Forked from https://github.com/azz/eslint-plugin-monorepo/blob/master/src/rules/no-relative-import.js | ||
|
||
/** | ||
* MIT License | ||
Copyright (c) 2017 Lucas Azzola | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
const moduleVisitor = require('eslint-module-utils/moduleVisitor'); | ||
|
||
const getPackages = require('get-monorepo-packages'); | ||
const isInside = require('path-is-inside'); | ||
const minimatch = require('minimatch'); | ||
const {join, relative, parse} = require('path'); | ||
const resolve = require('eslint-module-utils/resolve'); | ||
|
||
const getPackageDir = (filePath, packages) => { | ||
const match = packages.find((pkg) => | ||
minimatch(filePath, join(pkg.location, '**')), | ||
); | ||
|
||
if (match) { | ||
return match.location; | ||
} | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'Disallow usage of relative imports instead of package names in monorepo', | ||
}, | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
const { | ||
options: [moduleUtilOptions], | ||
} = context; | ||
const sourceFsPath = context.getFilename(); | ||
const packages = getPackages(process.cwd()); | ||
|
||
return moduleVisitor.default((node) => { | ||
const resolvedPath = resolve.default(node.value, context); | ||
if (!resolvedPath) { | ||
return; | ||
} | ||
const packageDir = getPackageDir(sourceFsPath, packages); | ||
|
||
if (!packageDir || isInside(resolvedPath, packageDir)) { | ||
return; | ||
} | ||
|
||
const pkg = packages.find((pkg) => isInside(resolvedPath, pkg.location)); | ||
if (!pkg) { | ||
return; | ||
} | ||
|
||
const subPackagePath = relative(pkg.location, resolvedPath); | ||
context.report({ | ||
node, | ||
message: `Import for monorepo package '${pkg.package.name}' should be absolute.`, | ||
fix: (fixer) => { | ||
let {dir, name} = parse( | ||
`${pkg.package.name}${ | ||
subPackagePath !== '.' ? `/${subPackagePath}` : '' | ||
}`, | ||
); | ||
|
||
if (name !== '.' && name !== 'index') { | ||
// Remove unneeded suffix | ||
return fixer.replaceText(node, `'${dir}/${name}'`); | ||
} else { | ||
dir = dir.replace(/\/(src)|(lib)/, ''); | ||
return fixer.replaceText(node, `'${dir}'`); | ||
} | ||
}, | ||
}); | ||
}, moduleUtilOptions); | ||
}, | ||
}; |
36 changes: 36 additions & 0 deletions
36
packages/dev/eslint-plugin/test/rules/no-relative-import.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
// @flow | ||
|
||
const {RuleTester} = require('eslint'); | ||
const rule = require('../../src/rules/no-relative-import'); | ||
|
||
const filename = __filename; | ||
|
||
new RuleTester({ | ||
parser: require.resolve('@babel/eslint-parser'), | ||
parserOptions: {ecmaVersion: 2018, sourceType: 'module'}, | ||
}).run('no-relative-import', rule, { | ||
valid: [{code: "import logger from '@atlaspack/logger';", filename}], | ||
invalid: [ | ||
{ | ||
code: `import Logger from '../../../../core/logger/src/Logger';`, | ||
errors: [ | ||
{ | ||
message: `Import for monorepo package '@atlaspack/logger' should be absolute.`, | ||
}, | ||
], | ||
filename, | ||
output: "import Logger from '@atlaspack/logger/src/Logger';", | ||
}, | ||
{ | ||
code: `import type { PluginOptions } from '../../../../core/types-internal/src';`, | ||
errors: [ | ||
{ | ||
message: `Import for monorepo package '@atlaspack/types-internal' should be absolute.`, | ||
}, | ||
], | ||
filename, | ||
output: "import type { PluginOptions } from '@atlaspack/types-internal';", | ||
}, | ||
], | ||
}); |
119 changes: 66 additions & 53 deletions
119
packages/reporters/conditional-manifest/src/ConditionalManifestReporter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,75 @@ | ||
// @flow strict-local | ||
import {relative, join} from 'path'; | ||
import {Reporter} from '@atlaspack/plugin'; | ||
import type { | ||
Async, | ||
PluginLogger, | ||
PluginOptions, | ||
PluginTracer, | ||
ReporterEvent, | ||
} from '@atlaspack/types-internal'; | ||
import {getConfig} from './Config'; | ||
|
||
export default (new Reporter({ | ||
async report({event, options, logger}) { | ||
if (event.type === 'buildSuccess') { | ||
const bundles = event.bundleGraph.getConditionalBundleMapping(); | ||
|
||
// Replace bundles with file paths | ||
const mapBundles = (bundles) => | ||
bundles.map((bundle) => | ||
relative(bundle.target.distDir, bundle.filePath), | ||
); | ||
|
||
const manifest = {}; | ||
for (const [bundle, conditions] of bundles.entries()) { | ||
const bundleInfo = {}; | ||
for (const [key, cond] of conditions) { | ||
bundleInfo[key] = { | ||
ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(), | ||
ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse(), | ||
}; | ||
} | ||
|
||
manifest[bundle.target.name] ??= {}; | ||
manifest[bundle.target.name][ | ||
relative(bundle.target.distDir, bundle.filePath) | ||
] = bundleInfo; | ||
async function report({ | ||
event, | ||
options, | ||
logger, | ||
}: {| | ||
event: ReporterEvent, | ||
options: PluginOptions, | ||
logger: PluginLogger, | ||
tracer: PluginTracer, | ||
|}): Async<void> { | ||
if (event.type === 'buildSuccess') { | ||
const bundles = event.bundleGraph.getConditionalBundleMapping(); | ||
|
||
// Replace bundles with file paths | ||
const mapBundles = (bundles) => | ||
bundles.map((bundle) => relative(bundle.target.distDir, bundle.filePath)); | ||
|
||
const manifest = {}; | ||
for (const [bundle, conditions] of bundles.entries()) { | ||
const bundleInfo = {}; | ||
for (const [key, cond] of conditions) { | ||
bundleInfo[key] = { | ||
// Reverse bundles so we load children bundles first | ||
ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(), | ||
ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse(), | ||
}; | ||
} | ||
|
||
// Error if there are multiple targets in the build | ||
const targets = new Set( | ||
event.bundleGraph.getBundles().map((bundle) => bundle.target), | ||
manifest[bundle.target.name] ??= {}; | ||
manifest[bundle.target.name][ | ||
relative(bundle.target.distDir, bundle.filePath) | ||
] = bundleInfo; | ||
} | ||
|
||
const targets = new Set( | ||
event.bundleGraph.getBundles().map((bundle) => bundle.target), | ||
); | ||
|
||
const {filename} = await getConfig(options); | ||
|
||
for (const target of targets) { | ||
const conditionalManifestFilename = join(target.distDir, filename); | ||
|
||
const conditionalManifest = JSON.stringify( | ||
manifest[target.name], | ||
null, | ||
2, | ||
); | ||
|
||
for (const target of targets) { | ||
const conditionalManifestFilename = join( | ||
target.distDir, | ||
'conditional-manifest.json', | ||
); | ||
|
||
const conditionalManifest = JSON.stringify( | ||
manifest[target.name], | ||
null, | ||
2, | ||
); | ||
|
||
await options.outputFS.writeFile( | ||
conditionalManifestFilename, | ||
conditionalManifest, | ||
{mode: 0o666}, | ||
); | ||
|
||
logger.info({ | ||
message: | ||
'Wrote conditional manifest to ' + conditionalManifestFilename, | ||
}); | ||
} | ||
await options.outputFS.writeFile( | ||
conditionalManifestFilename, | ||
conditionalManifest, | ||
{mode: 0o666}, | ||
); | ||
|
||
logger.info({ | ||
message: 'Wrote conditional manifest to ' + conditionalManifestFilename, | ||
}); | ||
} | ||
}, | ||
}): Reporter); | ||
} | ||
} | ||
|
||
export default (new Reporter({report}): Reporter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// @flow strict-local | ||
import {join} from 'path'; | ||
import type {PluginOptions} from '@atlaspack/types-internal'; | ||
|
||
type Config = {| | ||
filename: string, | ||
|}; | ||
|
||
export async function getConfig({ | ||
env, | ||
inputFS, | ||
projectRoot, | ||
}: PluginOptions): Promise<Config> { | ||
const packageJson = JSON.parse( | ||
await inputFS.readFile(join(projectRoot, 'package.json'), 'utf8'), | ||
); | ||
|
||
const config = packageJson['@atlaspack/reporter-conditional-manifest'] ?? {}; | ||
for (const [key, value] of Object.entries(config)) { | ||
// Replace values in the format of ${VARIABLE} with their corresponding env | ||
if (typeof value === 'string') { | ||
config[key] = value.replace(/\${([^}]+)}/g, (_, v) => env[v] ?? ''); | ||
} | ||
} | ||
|
||
const {filename} = config; | ||
|
||
return { | ||
filename: filename ?? 'conditional-manifest.json', | ||
}; | ||
} |
Oops, something went wrong.