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

Add support for generating TypeScript declarations, and named pipelines #3613

Merged
merged 26 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e6ebdc8
WIP: Generate typescript declarations
devongovett Sep 30, 2019
9bf6979
Merge and tree shake typescript definitions
devongovett Oct 1, 2019
1ae545e
Initial pass at named pipelines
devongovett Oct 2, 2019
45e9cdb
Fix BundleGraph symbol resolution bug
devongovett Oct 2, 2019
554bb31
Add some tests
devongovett Oct 3, 2019
61bf3dd
Bug fixes and import hoisting
devongovett Oct 4, 2019
6eca253
Recrawl scope after adding imports 😰
devongovett Oct 5, 2019
4a1a070
Fix merge
devongovett Oct 7, 2019
1ae7d21
Bug fix
devongovett Oct 7, 2019
97ee24c
Remove pipeline from Target and just use name
devongovett Oct 7, 2019
2ee39f8
Lint and windows fix
devongovett Oct 7, 2019
961b7bb
Canonical file path
devongovett Oct 7, 2019
5807b83
Debugging
devongovett Oct 7, 2019
a647704
Normalize path
devongovett Oct 7, 2019
ee4f75e
Windows line endings?
devongovett Oct 7, 2019
cb049af
poidfjopdihjfpdoifhj
devongovett Oct 7, 2019
ec4b4d9
Add test for imports that conflict with exports
devongovett Oct 8, 2019
9acbadc
Add test for external imports in typescript
devongovett Oct 8, 2019
9d573ca
Ensure moduleResolution comes from enum
devongovett Oct 8, 2019
5e698aa
Merge branch 'ts-types' of github.com:parcel-bundler/parcel into ts-t…
devongovett Oct 8, 2019
8cc2c83
Mark used assets with exported symbols (fix sideEffects: false)
devongovett Oct 8, 2019
e07c566
lint
devongovett Oct 8, 2019
d0b8f06
Merge branch 'v2' into ts-types
mischnic Oct 8, 2019
540472a
Merge branch 'v2' into ts-types
Oct 9, 2019
c0e8964
Switch to md5FromObject for dependency id creation
devongovett Oct 10, 2019
f07fe3c
Merge branch 'ts-types' of github.com:parcel-bundler/parcel into ts-t…
devongovett Oct 10, 2019
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
2 changes: 2 additions & 0 deletions packages/configs/default/index.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"bundler": "@parcel/bundler-default",
"transforms": {
"types:*.{ts,tsx}": ["@parcel/transformer-typescript-types"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be the syntax for named pipelines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

"*.{js,mjs,jsm,jsx,es6,ts,tsx}": [
"@parcel/transformer-babel",
"@parcel/transformer-js"
Expand Down Expand Up @@ -34,6 +35,7 @@
"*.html": "@parcel/packager-html",
"*.css": "@parcel/packager-css",
"*.js": "@parcel/packager-js",
"*.ts": "@parcel/packager-ts",
"*": "@parcel/packager-raw"
},
"resolvers": ["@parcel/resolver-default"],
Expand Down
2 changes: 2 additions & 0 deletions packages/configs/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@parcel/packager-html": "^2.0.0-alpha.1.1",
"@parcel/packager-js": "^2.0.0-alpha.1.1",
"@parcel/packager-raw": "^2.0.0-alpha.1.1",
"@parcel/packager-ts": "^2.0.0-alpha.1.1",
"@parcel/reporter-cli": "^2.0.0-alpha.1.1",
"@parcel/reporter-dev-server": "^2.0.0-alpha.1.1",
"@parcel/reporter-hmr-server": "^2.0.0-alpha.1.1",
Expand All @@ -39,6 +40,7 @@
"@parcel/transformer-stylus": "^2.0.0-alpha.1.1",
"@parcel/transformer-sugarss": "^2.0.0-alpha.1.1",
"@parcel/transformer-toml": "^2.0.0-alpha.1.1",
"@parcel/transformer-typescript-types": "^2.0.0-alpha.1.1",
"@parcel/transformer-yaml": "^2.0.0-alpha.1.1"
}
}
1 change: 1 addition & 0 deletions packages/core/core/src/AssetGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default class AssetGraph extends Graph<AssetGraphNode> {
nodeFromDep(
createDependency({
moduleSpecifier: entryFile,
pipeline: target.name,
target: target,
env: target.env,
isEntry: true
Expand Down
21 changes: 18 additions & 3 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,19 @@ export default class BundleGraph {
);
let depSymbol = symbolLookup.get(identifier);
if (depSymbol != null) {
let resolved = nullthrows(this.getDependencyResolution(dep));
return this.resolveSymbol(resolved, depSymbol);
let resolvedAsset = nullthrows(this.getDependencyResolution(dep));
let {asset, symbol: resolvedSymbol, exportSymbol} = this.resolveSymbol(
resolvedAsset,
depSymbol
);

// If it didn't resolve to anything (likely CommonJS), pass through where we got to
if (resolvedSymbol == null) {
return {asset, symbol: resolvedSymbol, exportSymbol};
}

// Otherwise, keep the original symbol name along with the resolved symbol
return {asset, symbol: resolvedSymbol, exportSymbol: symbol};
}

// If this module exports wildcards, resolve the original module.
Expand All @@ -510,7 +521,11 @@ export default class BundleGraph {
let resolved = nullthrows(this.getDependencyResolution(dep));
let result = this.resolveSymbol(resolved, symbol);
if (result.symbol != null) {
return result;
return {
asset: result.asset,
symbol: result.symbol,
exportSymbol: symbol
};
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/ConfigLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class ConfigLoader {
}

async loadParcelConfig(configRequest: ConfigRequest) {
let {filePath, isSource, env} = configRequest;
let {filePath, isSource, env, pipeline} = configRequest;
let config = createConfig({
isSource,
searchPath: filePath,
Expand All @@ -55,7 +55,7 @@ export default class ConfigLoader {
let devDeps = [];
switch (configRequest.meta.actionType) {
case 'transformation':
devDeps = parcelConfig.getTransformerNames(filePath);
devDeps = parcelConfig.getTransformerNames(filePath, pipeline);
break;
case 'validation':
devDeps = parcelConfig.getValidatorNames(filePath);
Expand Down
6 changes: 4 additions & 2 deletions packages/core/core/src/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type DependencyOpts = {|
env: Environment,
meta?: Meta,
target?: Target,
symbols?: Map<Symbol, Symbol>
symbols?: Map<Symbol, Symbol>,
pipeline?: ?string
|};

export function createDependency(opts: DependencyOpts): Dependency {
Expand All @@ -31,7 +32,8 @@ export function createDependency(opts: DependencyOpts): Dependency {
md5FromString(
devongovett marked this conversation as resolved.
Show resolved Hide resolved
`${opts.sourceAssetId ?? ''}:${opts.moduleSpecifier}:${JSON.stringify(
opts.env
)}:${opts.target ? JSON.stringify(opts.target) : ''}`
)}:${opts.target ? JSON.stringify(opts.target) : ''}:${opts.pipeline ??
''}`
);

return {
Expand Down
7 changes: 1 addition & 6 deletions packages/core/core/src/InternalAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import SourceMap from '@parcel/source-map';
import {
bufferStream,
loadConfig,
md5FromFilePath,
md5FromString,
blobToStream,
TapStream
Expand Down Expand Up @@ -241,11 +240,7 @@ export default class InternalAsset {
return dep.id;
}

async addIncludedFile(file: File) {
if (file.hash == null) {
file.hash = await md5FromFilePath(this.options.inputFS, file.filePath);
}

addIncludedFile(file: File) {
this.value.includedFiles.set(file.filePath, file);
}

Expand Down
28 changes: 20 additions & 8 deletions packages/core/core/src/ParcelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ export default class ParcelConfig {
return validators;
}

getTransformerNames(filePath: FilePath): Array<string> {
getTransformerNames(filePath: FilePath, pipeline?: ?string): Array<string> {
let transformers: Pipeline | null = this.matchGlobMapPipelines(
filePath,
this.transforms
this.transforms,
pipeline
);
if (!transformers || transformers.length === 0) {
throw new Error(`No transformers found for "${filePath}".`);
Expand All @@ -137,8 +138,11 @@ export default class ParcelConfig {
return this.loadPlugins(this.getValidatorNames(filePath));
}

getTransformers(filePath: FilePath): Promise<Array<Transformer>> {
return this.loadPlugins(this.getTransformerNames(filePath));
getTransformers(
filePath: FilePath,
pipeline?: ?string
): Promise<Array<Transformer>> {
return this.loadPlugins(this.getTransformerNames(filePath, pipeline));
}

getBundler(): Promise<Bundler> {
Expand Down Expand Up @@ -206,8 +210,12 @@ export default class ParcelConfig {
return this.loadPlugins(this.reporters);
}

isGlobMatch(filePath: FilePath, pattern: Glob) {
return isMatch(filePath, pattern) || isMatch(basename(filePath), pattern);
isGlobMatch(filePath: FilePath, pattern: Glob, pipeline?: ?string) {
let prefix = pipeline ? `${pipeline}:` : '';
return (
isMatch(prefix + filePath, pattern) ||
isMatch(prefix + basename(filePath), pattern)
);
}

matchGlobMap(filePath: FilePath, globMap: {[Glob]: any, ...}) {
Expand All @@ -220,10 +228,14 @@ export default class ParcelConfig {
return null;
}

matchGlobMapPipelines(filePath: FilePath, globMap: {[Glob]: Pipeline, ...}) {
matchGlobMapPipelines(
filePath: FilePath,
globMap: {[Glob]: Pipeline, ...},
pipeline?: ?string
) {
let matches = [];
for (let pattern in globMap) {
if (this.isGlobMatch(filePath, pattern)) {
if (this.isGlobMatch(filePath, pattern, pipeline)) {
matches.push(globMap[pattern]);
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/RequestGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const nodeFromConfigRequest = (configRequest: ConfigRequest) => ({
id: md5FromObject({
filePath: configRequest.filePath,
plugin: configRequest.plugin,
env: configRequest.env
env: configRequest.env,
pipeline: configRequest.pipeline
}),
type: 'config_request',
value: configRequest
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/ResolverRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default class ResolverRunner {
filePath: result.filePath,
sideEffects: result.sideEffects,
code: result.code,
env: dependency.env
env: dependency.env,
pipeline: dependency.pipeline
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/TargetResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const DEFAULT_PRODUCTION_ENGINES = {
};

const DEFAULT_DIST_DIRNAME = 'dist';
const COMMON_TARGETS = ['main', 'module', 'browser'];
const COMMON_TARGETS = ['main', 'module', 'browser', 'types'];

export default class TargetResolver {
fs: FileSystem;
Expand Down
20 changes: 15 additions & 5 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export default class Transformation {
let asset = await this.loadAsset();
let pipeline = await this.loadPipeline(
this.request.filePath,
asset.value.isSource
asset.value.isSource,
this.request.pipeline
);
let results = await this.runPipeline(pipeline, asset);
let assets = results.map(a => a.value);
Expand Down Expand Up @@ -242,11 +243,16 @@ export default class Transformation {
});
}

async loadPipeline(filePath: FilePath, isSource: boolean): Promise<Pipeline> {
async loadPipeline(
filePath: FilePath,
isSource: boolean,
pipelineName?: ?string
): Promise<Pipeline> {
let configRequest = {
filePath,
env: this.request.env,
isSource,
pipeline: pipelineName,
meta: {
actionType: 'transformation'
}
Expand Down Expand Up @@ -291,8 +297,8 @@ export default class Transformation {
}

let pipeline = new Pipeline({
names: parcelConfig.getTransformerNames(filePath),
plugins: await parcelConfig.getTransformers(filePath),
names: parcelConfig.getTransformerNames(filePath, pipelineName),
plugins: await parcelConfig.getTransformers(filePath, pipelineName),
configs,
options: this.options,
workerApi: this.workerApi
Expand All @@ -314,7 +320,11 @@ export default class Transformation {
|}): Promise<?Pipeline> {
let nextFilePath =
filePath.slice(0, -path.extname(filePath).length) + '.' + nextType;
let nextPipeline = await this.loadPipeline(nextFilePath, isSource);
let nextPipeline = await this.loadPipeline(
nextFilePath,
isSource,
this.request.pipeline
);

if (nextPipeline.id === currentPipeline.id) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/public/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class MutableAsset extends BaseAsset implements IMutableAsset {
return this.#asset.addDependency(dep);
}

addIncludedFile(file: File): Promise<void> {
addIncludedFile(file: File) {
return this.#asset.addIncludedFile(file);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/core/core/src/public/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ export default class Dependency implements IDependency {
get symbols(): Map<Symbol, Symbol> {
return this.#dep.symbols;
}

get pipeline(): ?string {
return this.#dep.pipeline;
}
}
7 changes: 5 additions & 2 deletions packages/core/core/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export type Dependency = {|
target: ?Target,
sourceAssetId: ?string,
sourcePath: ?string,
symbols: Map<Symbol, Symbol>
symbols: Map<Symbol, Symbol>,
pipeline?: ?string
|};

export type Asset = {|
Expand Down Expand Up @@ -146,7 +147,8 @@ export type AssetRequest = {|
filePath: FilePath,
env: Environment,
sideEffects?: boolean,
code?: string
code?: string,
pipeline?: ?string
|};

// Asset group nodes are essentially used as placeholders for the results of an asset request
Expand Down Expand Up @@ -225,6 +227,7 @@ export type ConfigRequest = {|
filePath: FilePath,
env: Environment,
isSource: boolean,
pipeline?: ?string,
plugin?: PackageName,
//$FlowFixMe will lock this down more in a future commit
meta: any,
Expand Down
7 changes: 7 additions & 0 deletions packages/core/core/test/AssetGraph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('AssetGraph', () => {
graph.nodes.has(
createDependency({
moduleSpecifier: '/path/to/index1/src/main.js',
pipeline: 'test',
target: TARGETS[0],
env: DEFAULT_ENV
}).id
Expand All @@ -79,6 +80,7 @@ describe('AssetGraph', () => {
graph.nodes.has(
createDependency({
moduleSpecifier: '/path/to/index2/src/main.js',
pipeline: 'test',
target: TARGETS[0],
env: DEFAULT_ENV
}).id
Expand Down Expand Up @@ -109,6 +111,7 @@ describe('AssetGraph', () => {
from: 'entry_file:/path/to/index1/src/main.js',
to: createDependency({
moduleSpecifier: '/path/to/index1/src/main.js',
pipeline: 'test',
target: TARGETS[0],
env: DEFAULT_ENV
}).id,
Expand All @@ -118,6 +121,7 @@ describe('AssetGraph', () => {
from: 'entry_file:/path/to/index2/src/main.js',
to: createDependency({
moduleSpecifier: '/path/to/index2/src/main.js',
pipeline: 'test',
target: TARGETS[0],
env: DEFAULT_ENV
}).id,
Expand All @@ -138,6 +142,7 @@ describe('AssetGraph', () => {

let dep = createDependency({
moduleSpecifier: '/path/to/index/src/main.js',
pipeline: 'test',
target: TARGETS[0],
env: DEFAULT_ENV
});
Expand Down Expand Up @@ -171,6 +176,7 @@ describe('AssetGraph', () => {

let dep = createDependency({
moduleSpecifier: '/path/to/index/src/main.js',
pipeline: 'test',
target: TARGETS[0],
env: DEFAULT_ENV,
sourcePath: ''
Expand Down Expand Up @@ -304,6 +310,7 @@ describe('AssetGraph', () => {

let dep = createDependency({
moduleSpecifier: '/path/to/index/src/main.js',
pipeline: 'test',
env: DEFAULT_ENV,
target: TARGETS[0]
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {foo as bar} from 'foo';

export const foo = bar + 3;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "esm-conflict",
"private": true,
"module": "dist/index.js"
}
Loading