Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local require for @babel/core and postcss #6264

Merged
merged 9 commits into from
May 13, 2021
Merged
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
1 change: 0 additions & 1 deletion packages/core/integration-tests/test/transpilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ describe('transpilation', function() {
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
console.log(file);
assert(file.includes('React.createElement("div"'));
assert(file.includes('...a'));
assert(!file.includes('@swc/helpers'));
Expand Down
16 changes: 15 additions & 1 deletion packages/core/package-manager/src/JSONParseStream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow strict-local

import type {JSONObject} from '@parcel/types';

import logger from '@parcel/logger';
import {Transform} from 'stream';

// Transforms chunks of json strings to parsed objects.
Expand All @@ -17,7 +19,19 @@ export default class JSONParseStream extends Transform {
callback: (err: ?Error, parsed: ?JSONObject) => mixed,
) {
try {
callback(null, JSON.parse(chunk.toString()));
let parsed;
try {
parsed = JSON.parse(chunk.toString());
} catch (e) {
// Be permissive and ignoreJSON parse errors in case there was
// a non-JSON line in the package manager's stdout.
logger.verbose({
message: 'Ignored invalid JSON message: ' + chunk.toString(),
origin: '@parcel/package-manager',
});
return;
mischnic marked this conversation as resolved.
Show resolved Hide resolved
}
callback(null, parsed);
} catch (err) {
callback(err);
}
Expand Down
9 changes: 2 additions & 7 deletions packages/transformers/babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
"parcel": "^2.0.0-beta.1"
},
"dependencies": {
"@babel/core": "^7.12.0",
"@babel/generator": "^7.9.0",
"@babel/helper-compilation-targets": "^7.8.4",
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/plugin-transform-typescript": "^7.4.5",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/traverse": "^7.0.0",
"@parcel/babel-ast-utils": "2.0.0-beta.2",
"@parcel/babel-preset-env": "2.0.0-beta.2",
"@parcel/plugin": "2.0.0-beta.2",
"@parcel/source-map": "2.0.0-alpha.4.21",
"@parcel/utils": "2.0.0-beta.2",
Expand All @@ -39,9 +36,7 @@
},
"devDependencies": {
"@babel/core": "^7.12.0",
"@babel/preset-env": "^7.0.0",
"@parcel/types": "2.0.0-beta.2"
},
"peerDependencies": {
"@babel/core": "^7.12.0"
}
}
14 changes: 11 additions & 3 deletions packages/transformers/babel/src/babel7.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type {MutableAsset, AST, PluginOptions} from '@parcel/types';

import invariant from 'assert';
import * as babel from '@babel/core';
import * as internalBabelCore from '@babel/core';
import {relativeUrl} from '@parcel/utils';

import packageJson from '../package.json';
Expand All @@ -17,6 +17,14 @@ export default async function babel7(
babelOptions: any,
additionalPlugins: Array<any> = [],
): Promise<?AST> {
const babelCore = babelOptions.internal
? internalBabelCore
: await options.packageManager.require('@babel/core', asset.filePath, {
range: '^7.12.0',
saveDev: true,
shouldAutoInstall: options.shouldAutoInstall,
});

let config = {
...babelOptions.config,
plugins: additionalPlugins.concat(babelOptions.config.plugins),
Expand Down Expand Up @@ -53,13 +61,13 @@ export default async function babel7(
let ast = await asset.getAST();
let res;
if (ast) {
res = await babel.transformFromAstAsync(
res = await babelCore.transformFromAstAsync(
ast.program,
asset.isASTDirty() ? undefined : await asset.getCode(),
config,
);
} else {
res = await babel.transformAsync(await asset.getCode(), config);
res = await babelCore.transformAsync(await asset.getCode(), config);
}

if (res.ast) {
Expand Down
21 changes: 18 additions & 3 deletions packages/transformers/babel/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import type {Config, PluginOptions} from '@parcel/types';
import type {PluginLogger} from '@parcel/logger';
import typeof * as BabelCore from '@babel/core';

import path from 'path';
import * as babelCore from '@babel/core';
import * as internalBabelCore from '@babel/core';
import {md5FromObject, relativePath, resolveConfig} from '@parcel/utils';

import isJSX from './jsx';
Expand Down Expand Up @@ -59,6 +60,20 @@ export async function load(
return;
}

const babelCore: BabelCore = await options.packageManager.require(
devongovett marked this conversation as resolved.
Show resolved Hide resolved
'@babel/core',
config.searchPath,
{
range: '^7.12.0',
saveDev: true,
shouldAutoInstall: options.shouldAutoInstall,
},
);
config.addDevDependency({
moduleSpecifier: '@babel/core',
resolveFrom: config.searchPath,
});

let babelOptions = {
filename: config.searchPath,
cwd: options.projectRoot,
Expand Down Expand Up @@ -197,13 +212,13 @@ async function buildDefaultBabelConfig(options: PluginOptions, config: Config) {
}

babelOptions.presets = (babelOptions.presets || []).map(preset =>
babelCore.createConfigItem(preset, {
internalBabelCore.createConfigItem(preset, {
type: 'preset',
dirname: BABEL_TRANSFORMER_DIR,
}),
);
babelOptions.plugins = (babelOptions.plugins || []).map(plugin =>
babelCore.createConfigItem(plugin, {
internalBabelCore.createConfigItem(plugin, {
type: 'plugin',
dirname: BABEL_TRANSFORMER_DIR,
}),
Expand Down
69 changes: 0 additions & 69 deletions packages/transformers/babel/src/env.js

This file was deleted.

25 changes: 0 additions & 25 deletions packages/transformers/babel/src/typescript.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/transformers/postcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@
},
"devDependencies": {
"postcss": "^8.2.1"
},
"peerDependencies": {
"postcss": "^8.2.1"
}
}
22 changes: 18 additions & 4 deletions packages/transformers/postcss/src/PostCSSTransformer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import type {FilePath, MutableAsset} from '@parcel/types';
import type {FilePath, MutableAsset, PluginOptions} from '@parcel/types';

import {md5FromString} from '@parcel/utils';
import {Transformer} from '@parcel/plugin';
Expand All @@ -9,8 +9,8 @@ import nullthrows from 'nullthrows';
import path from 'path';
import semver from 'semver';
import valueParser from 'postcss-value-parser';
import postcss from 'postcss';
import postcssModules from 'postcss-modules';
import typeof * as Postcss from 'postcss';

import {load} from './loadConfig';

Expand All @@ -26,11 +26,13 @@ export default (new Transformer({
return ast.type === 'postcss' && semver.satisfies(ast.version, '^8.2.1');
},

async parse({asset, config}) {
async parse({asset, config, options}) {
if (!config) {
return;
}

const postcss = await loadPostcss(options, asset.filePath);

return {
type: 'postcss',
version: '8.2.1',
Expand All @@ -48,6 +50,8 @@ export default (new Transformer({
return [asset];
}

const postcss: Postcss = await loadPostcss(options, asset.filePath);

let plugins = [...config.hydrated.plugins];
let cssModules: ?{|[string]: string|} = null;
if (config.hydrated.modules) {
Expand Down Expand Up @@ -149,7 +153,9 @@ export default (new Transformer({
return assets;
},

generate({ast}) {
async generate({asset, ast, options}) {
const postcss: Postcss = await loadPostcss(options, asset.filePath);

let code = '';
postcss.stringify(postcss.fromJSON(ast.program), c => {
code += c;
Expand Down Expand Up @@ -192,3 +198,11 @@ function createLoader(
}
};
}

function loadPostcss(options: PluginOptions, from: FilePath): Promise<Postcss> {
return options.packageManager.require('postcss', from, {
devongovett marked this conversation as resolved.
Show resolved Hide resolved
range: '^8.2.1',
saveDev: true,
shouldAutoInstall: options.shouldAutoInstall,
});
}
5 changes: 5 additions & 0 deletions packages/transformers/postcss/src/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export async function load({

let contents = null;
if (configFile) {
config.addDevDependency({
moduleSpecifier: 'postcss',
resolveFrom: config.searchPath,
});

contents = configFile.contents;
let isDynamic = configFile && path.extname(configFile.filePath) === '.js';
if (isDynamic) {
Expand Down
16 changes: 0 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,6 @@
dependencies:
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-syntax-typescript@^7.12.13":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.12.13.tgz#9dff111ca64154cef0f4dc52cf843d9f12ce4474"
integrity sha512-cHP3u1JiUiG2LFDKbXnwVad81GvfyIOmCD6HIEId6ojrY0Drfy2q1jw7BwN7dE84+kTnBjLkXoL3IEy/3JPu2w==
dependencies:
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-transform-arrow-functions@^7.13.0":
version "7.13.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.13.0.tgz#10a59bebad52d637a027afa692e8d5ceff5e3dae"
Expand Down Expand Up @@ -838,15 +831,6 @@
dependencies:
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-transform-typescript@^7.4.5":
version "7.13.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.13.0.tgz#4a498e1f3600342d2a9e61f60131018f55774853"
integrity sha512-elQEwluzaU8R8dbVuW2Q2Y8Nznf7hnjM7+DSCd14Lo5fF63C9qNLbwZYbmZrtV9/ySpSUpkRpQXvJb6xyu4hCQ==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.13.0"
"@babel/helper-plugin-utils" "^7.13.0"
"@babel/plugin-syntax-typescript" "^7.12.13"

"@babel/plugin-transform-unicode-escapes@^7.12.13":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-escapes/-/plugin-transform-unicode-escapes-7.12.13.tgz#840ced3b816d3b5127dd1d12dcedc5dead1a5e74"
Expand Down