Skip to content

Commit

Permalink
Support atlaspack engines field (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak authored Nov 18, 2024
1 parent 80c3a97 commit 66c8cd0
Show file tree
Hide file tree
Showing 101 changed files with 285 additions and 237 deletions.
4 changes: 2 additions & 2 deletions packages/bundlers/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"main": "lib/DefaultBundler.js",
"source": "src/DefaultBundler.js",
"engines": {
"node": ">= 16.0.0",
"parcel": "^2.12.0"
"atlaspack": "^2.12.0",
"node": ">= 16.0.0"
},
"dependencies": {
"@atlaspack/diagnostic": "2.12.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/bundlers/library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"main": "lib/LibraryBundler.js",
"source": "src/LibraryBundler.js",
"engines": {
"node": ">= 16.0.0",
"parcel": "^2.12.0"
"atlaspack": "^2.12.0",
"node": ">= 16.0.0"
},
"dependencies": {
"@atlaspack/plugin": "2.12.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/compressors/brotli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"main": "lib/BrotliCompressor.js",
"source": "src/BrotliCompressor.js",
"engines": {
"node": ">= 16.0.0",
"parcel": "^2.12.0"
"atlaspack": "^2.12.0",
"node": ">= 16.0.0"
},
"dependencies": {
"@atlaspack/plugin": "2.12.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/compressors/gzip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"main": "lib/GzipCompressor.js",
"source": "src/GzipCompressor.js",
"engines": {
"node": ">= 16.0.0",
"parcel": "^2.12.0"
"atlaspack": "^2.12.0",
"node": ">= 16.0.0"
},
"dependencies": {
"@atlaspack/plugin": "2.12.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/compressors/raw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"main": "lib/RawCompressor.js",
"source": "src/RawCompressor.js",
"engines": {
"node": ">= 16.0.0",
"parcel": "^2.12.0"
"atlaspack": "^2.12.0",
"node": ">= 16.0.0"
},
"dependencies": {
"@atlaspack/plugin": "2.12.0"
Expand Down
24 changes: 16 additions & 8 deletions packages/core/core/src/loadAtlaspackPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,26 @@ export default async function loadPlugin<T>(
// Remove plugin version compatiblility validation in canary builds as they don't use semver
if (!process.env.SKIP_PLUGIN_COMPATIBILITY_CHECK) {
if (!pluginName.startsWith('.')) {
// Validate the engines.parcel field in the plugin's package.json
let parcelVersionRange = pkg && pkg.engines && pkg.engines.parcel;
if (!parcelVersionRange) {
// Validate the plugin engines field
let key = 'atlaspack';
let atlaspackVersionRange;
if (pkg?.engines?.atlaspack) {
atlaspackVersionRange = pkg.engines.atlaspack;
} else if (pkg?.engines?.parcel) {
key = 'parcel';
atlaspackVersionRange = pkg.engines.parcel;
}

if (!atlaspackVersionRange) {
logger.warn({
origin: '@atlaspack/core',
message: `The plugin "${pluginName}" needs to specify a \`package.json#engines.parcel\` field with the supported Atlaspack version range.`,
message: `The plugin "${pluginName}" needs to specify a \`package.json#engines.atlaspack\` field with the supported Atlaspack version range.`,
});
}

if (
parcelVersionRange &&
!semver.satisfies(ATLASPACK_VERSION, parcelVersionRange)
atlaspackVersionRange &&
!semver.satisfies(ATLASPACK_VERSION, atlaspackVersionRange)
) {
let pkgFile = nullthrows(
await resolveConfig(
Expand All @@ -197,7 +205,7 @@ export default async function loadPlugin<T>(
let pkgContents = await options.inputFS.readFile(pkgFile, 'utf8');
throw new ThrowableDiagnostic({
diagnostic: {
message: md`The plugin "${pluginName}" is not compatible with the current version of Atlaspack. Requires "${parcelVersionRange}" but the current version is "${ATLASPACK_VERSION}".`,
message: md`The plugin "${pluginName}" is not compatible with the current version of Atlaspack. Requires "${atlaspackVersionRange}" but the current version is "${ATLASPACK_VERSION}".`,
origin: '@atlaspack/core',
codeFrames: [
{
Expand All @@ -206,7 +214,7 @@ export default async function loadPlugin<T>(
code: pkgContents,
codeHighlights: generateJSONCodeHighlights(pkgContents, [
{
key: '/engines/parcel',
key: `/engines/${key}`,
},
]),
},
Expand Down
101 changes: 77 additions & 24 deletions packages/core/core/test/AtlaspackConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,21 @@ describe('AtlaspackConfig', () => {
});

describe('loadPlugin', () => {
it('should warn if a plugin needs to specify an engines.parcel field in package.json', async () => {
it('should warn if a plugin needs to specify an engines.atlaspack field in package.json', async () => {
let projectRoot = path.join(__dirname, 'fixtures', 'plugins');
let configFilePath = toProjectPath(
projectRoot,
path.join(__dirname, 'fixtures', 'plugins', '.parcelrc'),
path.join(projectRoot, '.parcelrc'),
);

let config = new AtlaspackConfig(
{
filePath: configFilePath,
bundler: undefined,
transformers: {
'*.js': [
{
packageName: 'parcel-transformer-no-engines',
packageName: 'atlaspack-transformer-no-engines',
resolveFrom: configFilePath,
keyPath: '/transformers/*.js/0',
},
Expand All @@ -152,51 +153,103 @@ describe('AtlaspackConfig', () => {

let warnStub = sinon.stub(logger, 'warn');
let {plugin} = await config.loadPlugin({
packageName: 'parcel-transformer-no-engines',
packageName: 'atlaspack-transformer-no-engines',
resolveFrom: configFilePath,
keyPath: '/transformers/*.js/0',
});
assert(plugin);
assert.equal(typeof plugin.transform, 'function');

assert.equal(typeof plugin?.transform, 'function');
assert(warnStub.calledOnce);
assert.deepEqual(warnStub.getCall(0).args[0], {
origin: '@atlaspack/core',
message:
'The plugin "parcel-transformer-no-engines" needs to specify a `package.json#engines.parcel` field with the supported Atlaspack version range.',
'The plugin "atlaspack-transformer-no-engines" needs to specify a `package.json#engines.atlaspack` field with the supported Atlaspack version range.',
});
warnStub.restore();
});

it('should error if a plugin specifies an invalid engines.atlaspack field in package.json', async () => {
let projectRoot = path.join(__dirname, 'fixtures', 'plugins');
let configFilePath = toProjectPath(
projectRoot,
path.join(projectRoot, '.parcelrc'),
);

let config = new AtlaspackConfig(
{
filePath: configFilePath,
bundler: undefined,
transformers: {},
},
{...DEFAULT_OPTIONS, projectRoot},
);

// $FlowFixMe[untyped-import]
let atlaspackVersion = require('../package.json').version;
let pkgJSON = path.join(
projectRoot,
'node_modules',
'atlaspack-transformer-bad-engines',
'package.json',
);
let code = inputFS.readFileSync(pkgJSON, 'utf8');

// $FlowFixMe
await assert.rejects(
() =>
config.loadPlugin({
packageName: 'atlaspack-transformer-bad-engines',
resolveFrom: configFilePath,
keyPath: '/transformers/*.js/0',
}),
{
name: 'Error',
diagnostics: [
{
message: `The plugin "atlaspack-transformer-bad-engines" is not compatible with the current version of Atlaspack. Requires "1.x" but the current version is "${atlaspackVersion}".`,
origin: '@atlaspack/core',
codeFrames: [
{
filePath: pkgJSON,
language: 'json5',
code,
codeHighlights: [
{
start: {line: 5, column: 5},
end: {line: 5, column: 22},
message: undefined,
},
],
},
],
},
],
},
);
});

it('should error if a plugin specifies an invalid engines.parcel field in package.json', async () => {
let projectRoot = path.join(__dirname, 'fixtures', 'plugins');
let configFilePath = toProjectPath(
projectRoot,
path.join(__dirname, 'fixtures', 'plugins', '.parcelrc'),
path.join(projectRoot, '.parcelrc'),
);

let config = new AtlaspackConfig(
{
filePath: configFilePath,
bundler: undefined,
transformers: {
'*.js': [
{
packageName: 'parcel-transformer-not-found',
resolveFrom: configFilePath,
keyPath: '/transformers/*.js/0',
},
],
},
transformers: {},
},
{...DEFAULT_OPTIONS, projectRoot},
);

// $FlowFixMe[untyped-import]
let parcelVersion = require('../package.json').version;
let atlaspackVersion = require('../package.json').version;
let pkgJSON = path.join(
__dirname,
'fixtures',
'plugins',
projectRoot,
'node_modules',
'parcel-transformer-bad-engines',
'atlaspack-transformer-bad-parcel-engines',
'package.json',
);
let code = inputFS.readFileSync(pkgJSON, 'utf8');
Expand All @@ -205,15 +258,15 @@ describe('AtlaspackConfig', () => {
await assert.rejects(
() =>
config.loadPlugin({
packageName: 'parcel-transformer-bad-engines',
packageName: 'atlaspack-transformer-bad-parcel-engines',
resolveFrom: configFilePath,
keyPath: '/transformers/*.js/0',
}),
{
name: 'Error',
diagnostics: [
{
message: `The plugin "parcel-transformer-bad-engines" is not compatible with the current version of Atlaspack. Requires "5.x" but the current version is "${parcelVersion}".`,
message: `The plugin "atlaspack-transformer-bad-parcel-engines" is not compatible with the current version of Atlaspack. Requires "1.x" but the current version is "${atlaspackVersion}".`,
origin: '@atlaspack/core',
codeFrames: [
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 66c8cd0

Please sign in to comment.